Add json_to_logjson and logjson_to_json command line utilities
authorNick Downing <nick@ndcode.org>
Fri, 7 Jan 2022 06:41:05 +0000 (17:41 +1100)
committerNick Downing <nick@ndcode.org>
Fri, 7 Jan 2022 06:42:02 +0000 (17:42 +1100)
.gitignore
json_to_logjson.mjs [new file with mode: 0755]
logjson_to_json.mjs [new file with mode: 0755]
package.json

index a77e7b2..446fa49 100644 (file)
@@ -1,3 +1,6 @@
+/logjson-*.tgz
+/node_modules
+/package-lock.json
 /tests/a.json
 /tests/b.json
 /tests/c.json
diff --git a/json_to_logjson.mjs b/json_to_logjson.mjs
new file mode 100755 (executable)
index 0000000..8112857
--- /dev/null
@@ -0,0 +1,22 @@
+#!/usr/bin/env node
+
+import fsPromises from 'fs/promises'
+import logjson from './index.mjs'
+
+if (process.argv.length < 4) {
+  console.log(`usage: ${process.argv[0]} ${process.argv[1]} database.json database.logjson`)
+  process.exit(1)
+}
+
+let database = new logjson.Database()
+await database.open(process.argv[3])
+let transaction = database.Transaction()
+transaction.set(
+  transaction.json_to_logjson(
+    JSON.parse(
+      (await fsPromises.readFile(process.argv[2])).toString('utf-8')
+    )
+  )
+)
+transaction.commit()
+await database.close()
diff --git a/logjson_to_json.mjs b/logjson_to_json.mjs
new file mode 100755 (executable)
index 0000000..77511cd
--- /dev/null
@@ -0,0 +1,26 @@
+#!/usr/bin/env node
+
+import fsPromises from 'fs/promises'
+import logjson from './index.mjs'
+
+if (process.argv.length < 4) {
+  console.log(`usage: ${process.argv[0]} ${process.argv[1]} database.logjson database.json`)
+  process.exit(1)
+}
+
+let database = new logjson.Database()
+await database.open(process.argv[2])
+let transaction = database.Transaction()
+await fsPromises.writeFile(
+  process.argv[3],
+  Buffer.from(
+    JSON.stringify(
+      await logjson.logjson_to_json(await transaction.get()),
+      null,
+      2
+    ) + '\n',
+    'utf-8'
+  )
+)
+transaction.rollback()
+await database.close()
index 57585e3..c1c2f40 100644 (file)
   "bugs": {
     "email": "nick@ndcode.org"
   },
+  "bin": {
+    "json_to_logjson": "json_to_logjson.mjs",
+    "logjson_to_json": "logjson_to_json.mjs"
+  },
   "main": "index.mjs",
   "directories": {},
   "dependencies": {},