Skip to content

Commit 2d183d3

Browse files
committed
+Added output dir cli arg option
1 parent 3a17139 commit 2d183d3

5 files changed

Lines changed: 39 additions & 13 deletions

File tree

dist/bnf.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type _Shared from './shared.js';
1+
import type * as _Shared from './shared.js';
22
export type _Literal = { type: "literal", value: string, start: number, end: number, count: number, ref: _Shared.ReferenceRange };
33
export type Term_Program = {
44
type: 'program',

docs/source/changelog.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## Version 4.1.0
4+
5+
### Added:
6+
- [x] Added the optional extra argument for an output dir to the cli
7+
38
## Version 4.0.7
49

510
### Fixes:

docs/source/cli.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,14 @@ npx bnf-compile ./bnf/
1414
Once you have `bnf-parser` installed simply run `npx bnf-compile ./bnf/` where `./bnf/` is actually the folder where your bnfs are stored.
1515
After running this you will notice multiple artifacts will be generated which is what you will then `import`/`require` into your project.
1616

17-
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.
17+
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.
18+
19+
20+
## Optional output dir
21+
22+
```bash
23+
npx bnf-compile ./bnf/ ./bin/bnf/
24+
```
25+
26+
You can also add a second argument for the output directory for the artifacts.
27+
Note: If that directory does not currently exist, the cli will attempt to recursively make the dir

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "bnf-parser",
3-
"version": "4.0.7",
3+
"version": "4.1.0",
44
"description": "Deterministic BNF compiler/parser",
55
"homepage": "https://bnf-parser.ajanibilby.com",
66
"main": "./bin/index.js",
@@ -14,7 +14,7 @@
1414
"build:ts": "tsc",
1515
"build:syntax": "node ./tools/build-syntax.js",
1616
"build:preload": "node ./tools/post-build.js",
17-
"build:bnfs": "node ./bin/cli.js ./bnf/",
17+
"build:bnfs": "node ./bin/cli.js ./bnf/ ./dist/",
1818
"build:bundle": "npx rollup -c"
1919
},
2020
"repository": {

source/cli.ts

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33

44
import chalk from 'chalk';
55

6-
import { readdirSync, existsSync, readFileSync, writeFileSync, appendFileSync, statSync } from "fs";
6+
import { readdirSync, existsSync, readFileSync, writeFileSync, appendFileSync, statSync, mkdirSync } from "fs";
77
import { basename, extname, join, dirname } from "path";
88
import { legacy, wasm } from "./index.js";
99

10-
import { ParseError } from "./artifacts/shared.js";
1110
import { CompileProgram } from "./compile.js";
1211

1312
import * as _Shared from "../dist/shared.js"; // things shared between multiple pre-compiled BNFs
1413
import * as bnf from "../dist/bnf.js"; // pre-compiled JS with WASM embedded
14+
import binaryen from 'binaryen';
1515

1616

1717
const script = join(process.argv[1], "../");
@@ -68,6 +68,7 @@ if (!existsSync(root)) {
6868

6969
const isFile = statSync(root).isFile();
7070
const root_dir = isFile ? dirname(root) : root.slice(0, -1);
71+
const out_dir = process.argv[3] ? process.argv[3].slice(0, -1) : root_dir;
7172

7273
if (!existsSync(root_dir)) {
7374
console.error(`Unknown path ${root}`);
@@ -86,7 +87,16 @@ if (files.length === 0) {
8687
process.exit(1);
8788
}
8889

89-
console.log(`Found: ${files.join(', ')}\n`);
90+
console.log(`Found: ${files.join(', ')}`);
91+
if (out_dir !== root_dir) {
92+
if (!existsSync(out_dir)) {
93+
mkdirSync(out_dir, { recursive: true });
94+
console.log(` out: ${out_dir} (made dir)`);
95+
} else {
96+
console.log(` out: ${out_dir}`)
97+
}
98+
}
99+
console.log(""); // blank spacer line
90100

91101
let failure = false;
92102
for (const file of files) {
@@ -134,13 +144,13 @@ for (const file of files) {
134144

135145
// Generate type headers
136146
const types = wasm.CompileTypes(lang);
137-
writeFileSync(`${root_dir}/${name}.d.ts`, types);
147+
writeFileSync(`${out_dir}/${name}.d.ts`, types);
138148

139149
// Generate web assembly
140150
try {
141151
const mod = wasm.GenerateWasm(lang);
142152
if (process.argv.includes("--emit-wat"))
143-
writeFileSync(`${root_dir}/${name}.wat`, mod.emitText());
153+
writeFileSync(`${out_dir}/${name}.wat`, mod.emitText());
144154

145155
if (!mod.validate()) {
146156
console.error(` Compiling WASM...`);
@@ -149,10 +159,11 @@ for (const file of files) {
149159
continue;
150160
}
151161

162+
binaryen.setOptimizeLevel(2);
152163
mod.optimize();
153164

154165
// Generate JS runner
155-
writeFileSync(`${root_dir}/${name}.js`,
166+
writeFileSync(`${out_dir}/${name}.js`,
156167
GenerateRunner(lang, mod.emitBinary())
157168
);
158169
} catch (e: any) {
@@ -166,15 +177,15 @@ for (const file of files) {
166177
console.log(` - ${chalk.green("OK")}: ${file}`);
167178
}
168179

169-
writeFileSync(`${root_dir}/shared.js`, wasm.Runner.toString());
180+
writeFileSync(`${out_dir}/shared.js`, wasm.Runner.toString());
170181
writeFileSync(
171-
`${root_dir}/shared.d.ts`,
182+
`${out_dir}/shared.d.ts`,
172183
readFileSync(`${script}/artifacts/shared.d.ts`, "utf8")
173184
.replace(/ /gm, "\t")
174185
.replace(/\r\n/g, "\n")
175186
);
176187
appendFileSync(
177-
`${root_dir}/shared.js`,
188+
`${out_dir}/shared.js`,
178189
readFileSync(`${script}/artifacts/shared.js`, "utf8")
179190
.replace(/ /gm, "\t")
180191
.replace(/\r\n/g, "\n")

0 commit comments

Comments
 (0)