~kiito/bare-js

8cbda5282d26b4232dfe1c61981eae6588eecbaf — Emma 4 years ago 65d16b9
Use argparse (npm) for command line utilities
6 files changed, 62 insertions(+), 30 deletions(-)

M converter/bare.js
M converter/dump.js
M converter/io.js
M converter/templates.js
M example/example.mjs
M package.json
M converter/bare.js => converter/bare.js +19 -11
@@ 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

M converter/dump.js => converter/dump.js +27 -11
@@ 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

M converter/io.js => converter/io.js +8 -3
@@ 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));
				}
			}
		});
	});

M converter/templates.js => converter/templates.js +1 -0
@@ 125,6 125,7 @@ function generateClasses(objectTree, libraryPath) {
	output += export_statement(typeNames);
	output += '\n';
	output += export_default_statement(typeNames);
	output += '\n';

	return output;
}

M example/example.mjs => example/example.mjs +4 -4
@@ 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

M package.json => package.json +3 -1
@@ 1,6 1,8 @@
{
  "name": "bare-js",
  "version": "0.1.0",
  "dependencies": {},
  "dependencies": {
    "argparse": ">=1.0.10"
  },
  "type": "module"
}