From 8cbda5282d26b4232dfe1c61981eae6588eecbaf Mon Sep 17 00:00:00 2001 From: Emma <-> Date: Thu, 9 Jul 2020 18:35:29 +0200 Subject: [PATCH] Use argparse (npm) for command line utilities --- converter/bare.js | 30 +++++++++++++++++++----------- converter/dump.js | 38 +++++++++++++++++++++++++++----------- converter/io.js | 11 ++++++++--- converter/templates.js | 1 + example/example.mjs | 8 ++++---- package.json | 4 +++- 6 files changed, 62 insertions(+), 30 deletions(-) diff --git a/converter/bare.js b/converter/bare.js index 395dbc9..30dbefa 100644 --- a/converter/bare.js +++ b/converter/bare.js @@ -13,27 +13,35 @@ import templates from './templates.js'; * as long as it supports promises and arrow syntax */ +import argparse from 'argparse'; + +let argParser = new argparse.ArgumentParser({ + /*version: '0.1.0',*/ + addHelp: true, + description: 'Convert a BARE schema to a JavaScript module', +}); +argParser.addArgument('schema', {nargs: '?', help: 'A BARE schema file, otherwise stdin'}); +argParser.addArgument('output', {nargs: '?', help: 'The filename of the JS module, otherwise stdout'}); +argParser.addArgument(['-l', '--js-lib'], {metavar: 'library', dest: 'library', defaultValue: './bare.mjs', help: 'The path to the bare-js library file'}); + async function processSchema() { - let [input, output, libraryPath] = process.argv.slice(2, 5); + let args = argParser.parseArgs(); let schema; - if (input) { - schema = await io.readFile(input, 'utf-8'); + if (args.schema) { + schema = await io.readFile(args.schema, 'utf-8'); } else { - schema = await io.readStdin(); - } - if (!libraryPath) { - libraryPath = './bare.mjs'; + schema = await io.readStdin('utf-8'); } let tokenList = tokenizer.tokenizeSchema(schema); let objectTree = parser.parseSchema(tokenList); - let jsModule = templates.generateClasses(objectTree, libraryPath); - if (output) { - await io.writeFile(output, jsModule); + let jsModule = templates.generateClasses(objectTree, args.library); + if (args.output) { + await io.writeFile(args.output, jsModule); } else { await io.writeStdout(jsModule); } } processSchema().catch((err) => { - console.error(err); + console.error(err.toString()); }); \ No newline at end of file diff --git a/converter/dump.js b/converter/dump.js index 9d6a0e3..6befc85 100644 --- a/converter/dump.js +++ b/converter/dump.js @@ -3,29 +3,45 @@ import tokenizer from './tokenizer.js'; import parser from './parser.js'; import templates from './templates.js'; +import argparse from 'argparse'; + +let argParser = new argparse.ArgumentParser({ + /*version: '0.1.0',*/ + addHelp: true, + description: 'Decode a BARE message given a schema and its type name', +}); +argParser.addArgument('schema', {help: 'A BARE schema file'}); +argParser.addArgument('type', {help: 'The name of the message type'}); +argParser.addArgument('message', {nargs: '?', help: 'The filename of the message, otherwise stdin'}); +argParser.addArgument(['-l', '--js-lib'], {metavar: 'library', dest: 'library', defaultValue: './bare.mjs', help: 'The path to the bare-js library file'}); + async function dumpMessage() { - let [schemaFile, type, binaryFile, libraryPath] = process.argv.slice(2, 2 + 4); - let schema = await io.readFile(schemaFile, 'utf-8'); - let binary = await io.readFile(binaryFile); + let args = argParser.parseArgs(); + + let schema = await io.readFile(args.schema, 'utf-8'); + let binary; + if (args.message) { + binary = await io.readFile(args.message); + } else { + binary = await io.readStdin(); + } + binary = Uint8Array.from(binary); - if (!libraryPath) { - libraryPath = './bare.mjs'; - } - const Bare = await import(libraryPath); + const Bare = await import(args.library); let tokenList = tokenizer.tokenizeSchema(schema); let objectTree = parser.parseSchema(tokenList); - let jsModule = templates.generateClasses(objectTree, libraryPath); + let jsModule = templates.generateClasses(objectTree, args.library); - let modulePath = schemaFile.replace('.bare', '.temp.mjs'); + let modulePath = args.schema.replace('.bare', '.temp.mjs'); await io.writeFile(modulePath, jsModule); let types = (await import(modulePath)).default; console.log('Available types:', Object.keys(types).join(', ')); - let [object, length] = types[type].unpack(binary); + let [object, length] = types[args.type].unpack(binary); await io.writeStdout(Bare.stringifyJSON(object, 2)); @@ -33,5 +49,5 @@ async function dumpMessage() { } dumpMessage().catch((err) => { - console.error(err); + console.error(err.toString()); }); \ No newline at end of file diff --git a/converter/io.js b/converter/io.js index 5b5dbdc..277c84c 100644 --- a/converter/io.js +++ b/converter/io.js @@ -16,13 +16,18 @@ function readFile(fileName, encoding) { }); } -function readStdin() { +function readStdin(encoding) { return new Promise((resolve, reject) => { - fs.read(/* stdin */0, function(err, bytesRead, buffer) { + let buf = Buffer.alloc(1024 * 32); + fs.read(/* stdin */0, buf, 0, buf.length, null, function(err, bytesRead, buffer) { if (err) { reject(err); } else { - resolve(buffer.toString("utf-8")); + if (encoding) { + resolve(buffer.slice(0, bytesRead).toString(encoding)); + } else { + resolve(buffer.slice(0, bytesRead)); + } } }); }); diff --git a/converter/templates.js b/converter/templates.js index b3d116f..d72b9ee 100644 --- a/converter/templates.js +++ b/converter/templates.js @@ -125,6 +125,7 @@ function generateClasses(objectTree, libraryPath) { output += export_statement(typeNames); output += '\n'; output += export_default_statement(typeNames); + output += '\n'; return output; } diff --git a/example/example.mjs b/example/example.mjs index f9ec7c4..5ee1925 100644 --- a/example/example.mjs +++ b/example/example.mjs @@ -131,16 +131,16 @@ let test4b = new Bare.UnionValue(Test4[1], {x: 3.6, y: Math.sqrt(2), z: 1/3}); console.log(addr); console.log("-------------------"); - console.log(test4a); + console.log(test4a.value); let test4a_bin = Test4.pack(test4a); console.log(test4a_bin); let [test4a_un, t4al] = Test4.unpack(test4a_bin); - console.log(test4a_un); + console.log(test4a_un.value); console.log("-------------------"); - console.log(test4b); + console.log(test4b.value); let test4b_bin = Test4.pack(test4b); console.log(test4b_bin); let [test4b_un, t4bl] = Test4.unpack(test4b_bin); - console.log(test4b_un); + console.log(test4b_un.value); })(); \ No newline at end of file diff --git a/package.json b/package.json index 93ede96..debd88a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,8 @@ { "name": "bare-js", "version": "0.1.0", - "dependencies": {}, + "dependencies": { + "argparse": ">=1.0.10" + }, "type": "module" } -- 2.45.2