From c90a752253348b8a20b2ad377f7e4635e878c905 Mon Sep 17 00:00:00 2001 From: Nick Downing Date: Fri, 7 Jan 2022 17:41:05 +1100 Subject: [PATCH] Add json_to_logjson and logjson_to_json command line utilities --- .gitignore | 3 +++ json_to_logjson.mjs | 22 ++++++++++++++++++++++ logjson_to_json.mjs | 26 ++++++++++++++++++++++++++ package.json | 4 ++++ 4 files changed, 55 insertions(+) create mode 100755 json_to_logjson.mjs create mode 100755 logjson_to_json.mjs diff --git a/.gitignore b/.gitignore index a77e7b2..446fa49 100644 --- a/.gitignore +++ b/.gitignore @@ -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 index 0000000..8112857 --- /dev/null +++ b/json_to_logjson.mjs @@ -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 index 0000000..77511cd --- /dev/null +++ b/logjson_to_json.mjs @@ -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() diff --git a/package.json b/package.json index 57585e3..c1c2f40 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,10 @@ "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": {}, -- 2.34.1