From 2d183d35e6790bbcdfc97054f1d91e15e951bbd9 Mon Sep 17 00:00:00 2001 From: Ajani Bilby <11359344+AjaniBilby@users.noreply.github.com> Date: Sat, 23 Sep 2023 10:09:39 +1000 Subject: [PATCH] +Added output dir cli arg option --- dist/bnf.d.ts | 2 +- docs/source/changelog.md | 5 +++++ docs/source/cli.md | 12 +++++++++++- package.json | 4 ++-- source/cli.ts | 29 ++++++++++++++++++++--------- 5 files changed, 39 insertions(+), 13 deletions(-) diff --git a/dist/bnf.d.ts b/dist/bnf.d.ts index 4b5d6b2..e821a3e 100644 --- a/dist/bnf.d.ts +++ b/dist/bnf.d.ts @@ -1,4 +1,4 @@ -import type _Shared from './shared.js'; +import type * as _Shared from './shared.js'; export type _Literal = { type: "literal", value: string, start: number, end: number, count: number, ref: _Shared.ReferenceRange }; export type Term_Program = { type: 'program', diff --git a/docs/source/changelog.md b/docs/source/changelog.md index edd942e..f81cbd8 100644 --- a/docs/source/changelog.md +++ b/docs/source/changelog.md @@ -1,5 +1,10 @@ # Changelog +## Version 4.1.0 + +### Added: + - [x] Added the optional extra argument for an output dir to the cli + ## Version 4.0.7 ### Fixes: diff --git a/docs/source/cli.md b/docs/source/cli.md index c484aba..b9afa38 100644 --- a/docs/source/cli.md +++ b/docs/source/cli.md @@ -14,4 +14,14 @@ npx bnf-compile ./bnf/ Once you have `bnf-parser` installed simply run `npx bnf-compile ./bnf/` where `./bnf/` is actually the folder where your bnfs are stored. After running this you will notice multiple artifacts will be generated which is what you will then `import`/`require` into your project. -Please note that by default this command will generate javascript modules using the `import`/`export` syntax, if you are running an environment which **does not support** `import`/`export`, the CLI will not currently be able export artifacts useable in your project. \ No newline at end of file +Please note that by default this command will generate javascript modules using the `import`/`export` syntax, if you are running an environment which **does not support** `import`/`export`, the CLI will not currently be able export artifacts useable in your project. + + +## Optional output dir + +```bash +npx bnf-compile ./bnf/ ./bin/bnf/ +``` + +You can also add a second argument for the output directory for the artifacts. +Note: If that directory does not currently exist, the cli will attempt to recursively make the dir \ No newline at end of file diff --git a/package.json b/package.json index d32cfda..4a9151b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "bnf-parser", - "version": "4.0.7", + "version": "4.1.0", "description": "Deterministic BNF compiler/parser", "homepage": "https://bnf-parser.ajanibilby.com", "main": "./bin/index.js", @@ -14,7 +14,7 @@ "build:ts": "tsc", "build:syntax": "node ./tools/build-syntax.js", "build:preload": "node ./tools/post-build.js", - "build:bnfs": "node ./bin/cli.js ./bnf/", + "build:bnfs": "node ./bin/cli.js ./bnf/ ./dist/", "build:bundle": "npx rollup -c" }, "repository": { diff --git a/source/cli.ts b/source/cli.ts index 2867f1b..4889dcb 100644 --- a/source/cli.ts +++ b/source/cli.ts @@ -3,15 +3,15 @@ import chalk from 'chalk'; -import { readdirSync, existsSync, readFileSync, writeFileSync, appendFileSync, statSync } from "fs"; +import { readdirSync, existsSync, readFileSync, writeFileSync, appendFileSync, statSync, mkdirSync } from "fs"; import { basename, extname, join, dirname } from "path"; import { legacy, wasm } from "./index.js"; -import { ParseError } from "./artifacts/shared.js"; import { CompileProgram } from "./compile.js"; import * as _Shared from "../dist/shared.js"; // things shared between multiple pre-compiled BNFs import * as bnf from "../dist/bnf.js"; // pre-compiled JS with WASM embedded +import binaryen from 'binaryen'; const script = join(process.argv[1], "../"); @@ -68,6 +68,7 @@ if (!existsSync(root)) { const isFile = statSync(root).isFile(); const root_dir = isFile ? dirname(root) : root.slice(0, -1); +const out_dir = process.argv[3] ? process.argv[3].slice(0, -1) : root_dir; if (!existsSync(root_dir)) { console.error(`Unknown path ${root}`); @@ -86,7 +87,16 @@ if (files.length === 0) { process.exit(1); } -console.log(`Found: ${files.join(', ')}\n`); +console.log(`Found: ${files.join(', ')}`); +if (out_dir !== root_dir) { + if (!existsSync(out_dir)) { + mkdirSync(out_dir, { recursive: true }); + console.log(` out: ${out_dir} (made dir)`); + } else { + console.log(` out: ${out_dir}`) + } +} +console.log(""); // blank spacer line let failure = false; for (const file of files) { @@ -134,13 +144,13 @@ for (const file of files) { // Generate type headers const types = wasm.CompileTypes(lang); - writeFileSync(`${root_dir}/${name}.d.ts`, types); + writeFileSync(`${out_dir}/${name}.d.ts`, types); // Generate web assembly try { const mod = wasm.GenerateWasm(lang); if (process.argv.includes("--emit-wat")) - writeFileSync(`${root_dir}/${name}.wat`, mod.emitText()); + writeFileSync(`${out_dir}/${name}.wat`, mod.emitText()); if (!mod.validate()) { console.error(` Compiling WASM...`); @@ -149,10 +159,11 @@ for (const file of files) { continue; } + binaryen.setOptimizeLevel(2); mod.optimize(); // Generate JS runner - writeFileSync(`${root_dir}/${name}.js`, + writeFileSync(`${out_dir}/${name}.js`, GenerateRunner(lang, mod.emitBinary()) ); } catch (e: any) { @@ -166,15 +177,15 @@ for (const file of files) { console.log(` - ${chalk.green("OK")}: ${file}`); } -writeFileSync(`${root_dir}/shared.js`, wasm.Runner.toString()); +writeFileSync(`${out_dir}/shared.js`, wasm.Runner.toString()); writeFileSync( - `${root_dir}/shared.d.ts`, + `${out_dir}/shared.d.ts`, readFileSync(`${script}/artifacts/shared.d.ts`, "utf8") .replace(/ /gm, "\t") .replace(/\r\n/g, "\n") ); appendFileSync( - `${root_dir}/shared.js`, + `${out_dir}/shared.js`, readFileSync(`${script}/artifacts/shared.js`, "utf8") .replace(/ /gm, "\t") .replace(/\r\n/g, "\n")