-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
50 lines (42 loc) · 1.55 KB
/
Copy pathcli.js
File metadata and controls
50 lines (42 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env node
const fs = require("fs");
const program = require('commander');
let Carpeg;
try {
Carpeg = require('carpeg');
}catch (e) {
Carpeg = require("./bin/carpeg.js");
}
const path = require("path");
const { exec } = require('child_process');
program
.version('1.0.0')
.command('generate <parser> <output>')
.option("-t, --target [platform]", "Set the target platform(carbon, php, javascript)", "carbon")
.option("-d, --debug", "Saves the tmp carbon file")
.option("-n, --name [name]", "Parser class name", "CarpegParser")
.description('Generates a parser based on the parser cpeg file.')
.action(function (parser, output, cmd) {
let grammar = new Carpeg.grammar(fs.readFileSync(parser, "utf8"));
grammar.parserClass = cmd.name;
const carbonOutput = grammar.generate();
if (cmd.target == "carbon") {
fs.writeFileSync(output, carbonOutput);
}else{
let random = Math.floor(Math.random() * 100000);
fs.writeFileSync(`./cpegTmpFile${random}.carb`, carbonOutput);
// Call on carbonite to finish
exec(`carbonite compile ${cmd.target}.source.memory ./cpegTmpFile${random}.carb ${output}`, (err, stdout, stderr) => {
if (err) {
console.log(stdout + err + "\nMake sure you have carbonite-cli installed");
return;
}
if (!cmd.debug)
fs.unlinkSync(`./cpegTmpFile${random}.carb`);
if (cmd.target == "javascript") // Hack to export the parser
fs.writeFileSync(output, fs.readFileSync(output, "utf8") + `\nmodule.exports = ${cmd.name};`);
console.log(stdout);
});
}
});
program.parse(process.argv);