-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathcli.ts
76 lines (66 loc) · 1.97 KB
/
cli.ts
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/env node
import * as commander from 'commander';
import * as fs from 'fs';
import { parseSchema } from './index';
commander
.version(JSON.parse(fs.readFileSync(__dirname + '/package.json', 'utf8')).version)
.arguments('<schema_path>')
.option('--es5 <js_path>', 'Generate ES5 JavaScript code')
.option('--es6 <js_path>', 'Generate ES6 JavaScript code')
.option('--ts <ts_path>', 'Generate TypeScript code')
.option('--decode <msg_type>', 'Decode standard input to JSON')
.option('--encode <msg_type>', 'Encode standard input to JSON')
.parse(process.argv);
if (!process.argv.slice(2).length) {
commander.outputHelp();
process.exit(1);
}
const contents = fs.readFileSync(commander.args[0], 'utf8');
const schema = parseSchema(contents);
// Generate ES5 JavaScript code
if (commander.es5) {
const js = schema.toJavaScript({ es6: false });
fs.writeFileSync(commander.es5, js);
}
// Generate ES6 JavaScript code
if (commander.es6) {
const js = schema.toJavaScript({ es6: true });
fs.writeFileSync(commander.es6, js);
}
// Generate TypeScript code
if (commander.ts) {
const ts = schema.toTypeScript();
fs.writeFileSync(commander.ts, ts);
}
// Decode standard input to JSON
if (commander.decode) {
const chunks: Buffer[] = [];
process.stdin.on('data', chunk => {
chunks.push(chunk);
});
process.stdin.on('end', () => {
console.log(JSON.stringify(schema.compile()['decode' + commander.decode](Buffer.concat(chunks)), null, 2));
});
process.stdin.resume();
}
// Encode standard input to JSON
else if (commander.encode) {
const chunks: Buffer[] = [];
process.stdin.on('data', chunk => {
chunks.push(chunk);
});
process.stdin.on('end', () => {
process.stdout.write(schema.compile()['encode' + commander.encode](JSON.parse(chunks.join(''))));
});
process.stdin.resume();
}
if (
!commander.es5 &&
!commander.es6 &&
!commander.ts &&
!commander.decode &&
!commander.encode
) {
commander.outputHelp();
process.exit(1);
}