Skip to content

Commit

Permalink
+Added output dir cli arg option
Browse files Browse the repository at this point in the history
  • Loading branch information
AjaniBilby committed Sep 23, 2023
1 parent 3a17139 commit 2d183d3
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 13 deletions.
2 changes: 1 addition & 1 deletion dist/bnf.d.ts
Original file line number Diff line number Diff line change
@@ -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',
Expand Down
5 changes: 5 additions & 0 deletions docs/source/changelog.md
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
12 changes: 11 additions & 1 deletion docs/source/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
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
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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": {
Expand Down
29 changes: 20 additions & 9 deletions source/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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], "../");
Expand Down Expand Up @@ -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}`);
Expand All @@ -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) {
Expand Down Expand Up @@ -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...`);
Expand All @@ -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) {
Expand All @@ -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")
Expand Down

0 comments on commit 2d183d3

Please sign in to comment.