-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
61 lines (52 loc) · 1.33 KB
/
app.js
File metadata and controls
61 lines (52 loc) · 1.33 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
51
52
53
54
55
56
57
58
59
60
61
const program = require('commander');
const appVersion = require('./package.json').version
const costflow = require('costflow').default;
const fs = require("fs");
/*
* Parse command arguments
*/
program
.version(appVersion)
.option('-c, --config <file_path>', 'costflow config file', 'costflow.json')
.option('-j, --json', 'output result in json')
.argument('<command>', 'command to parse');
program.parse(process.argv);
const options = program.opts();
const jsonOutput = options.json || false;
const configPath = options.config;
const args = program.args;
if (args.length !== 1) {
console.error("too much commands to parse!");
process.exit(1);
}
const command = args[0];
/*
* Load config
*/
let config;
try {
const rawConfig = fs.readFileSync(configPath);
config = JSON.parse(rawConfig.toString());
} catch (err) {
console.error(`Cannot load config file. ${err.message}.`);
process.exit(1);
}
/*
* Parse command
*/
/**
* Escape UTF-8 in JSON
* @param s
* @returns {*}
*/
function jsonEscapeUTF(s) {
return s.replace(/[^\x20-\x7F]/g, x => "\\u" + ("000" + x.codePointAt(0).toString(16)).slice(-4))
}
(async () => {
let ret = await costflow.parse(command, config);
if (jsonOutput) {
console.log(jsonEscapeUTF(JSON.stringify(ret)));
} else {
console.log(ret.output);
}
})();