This repository has been archived by the owner on Mar 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
go.js
103 lines (78 loc) · 2.12 KB
/
go.js
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/env node
const v8 = require('v8');
const fs = require('fs');
const path = require('path');
const wrap = require('module').wrap;
const spawnSync = require('child_process').spawnSync;
const bytenode = require('./index.js');
let args = process.argv.slice(2);
if (args.includes('-c')) {
args[args.indexOf('-c')] = '--compile';
}
if (args.includes('-o')) {
args[args.indexOf('-o')] = '--out';
}
if (args.includes('-h')) {
args[args.indexOf('-h')] = '--help';
}
if (args.includes('-v')) {
args[args.indexOf('-v')] = '--version';
}
const program = {
dirname: __dirname,
filename: __filename,
nodeBin: process.argv[0],
flags: args.filter(arg => arg[0] === '-'),
files: args.filter(arg => arg[0] !== '-' && arg[1] !== '-'),
};
if (program.flags.includes('--compile')) {
program.files.forEach(function (filename) {
filename = path.resolve(filename);
if (fs.existsSync(filename) && fs.statSync(filename).isFile()) {
let compileAsModule = !program.flags.includes('--no-module');
try {
bytenode.compileFile({ filename, compileAsModule });
} catch (error) {
console.error(error);
}
} else {
console.error(`Error: Cannot find file '${filename}'.`);
}
});
if (program.files.length === 0) {
let script = '';
process.stdin.setEncoding('utf-8');
process.stdin.on('readable', () => {
let data = process.stdin.read();
if (data !== null) {
script += data;
}
});
process.stdin.on('end', () => {
try {
process.stdout.write(bytenode.compileCode(wrap(script)));
} catch (error) {
console.error(error);
}
});
}
}
else if (program.flags.includes('--help')) {
console.log(``);
}
else if (program.flags.includes('--version') && program.flags.length === 1 && program.files.length === 0) {
const package = require('./package.json');
console.log(package.name, package.version);
}
else {
try {
spawnSync(program.nodeBin, [
'-r',
path.resolve(__dirname, 'index.js')
].concat(args), {
stdio: 'inherit'
});
} catch (error) {
console.error(error);
}
}