forked from onux/jspp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jspp
executable file
·122 lines (101 loc) · 2.38 KB
/
jspp
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/env node
var fs = require('fs'),
paramon = require('paramon'),
jspp = require('javascript-plusplus'),
compiler = jspp.compiler;
function jsparse () {
return jspp.narcissus.jsparse.apply(jspp.narcissus, arguments);
}
var args = paramon.readFormat(process.argv, {
name: "js++",
usage: "js++ file(s) [options]",
params: [
/*{
name: 'execute',
args: ['--execute', '-e'],
desc: 'Instead of printing output, execute. You can optionally specify the command to execute it with.',
maxParams: 1,
},*/
{
name: 'wrap',
args: ['--closure', '-c'],
desc: 'Wrap in a function (closure).',
maxParams: 0,
},
{
name: 'debug',
args: ['--debug', '-d'],
desc: 'Debug mode.',
maxParams: 0,
},
{
name: 'warnings',
args: ['--warnings', '-w'],
desc: 'Display warnings.',
maxParams: 0,
},
{
name: 'encoding',
args: ['--encoding', '-e'],
desc: 'The encoding to use for reading the files.',
maxParams: 1,
minParams: 1,
},
{
name: 'output',
args: ['--output', '-o'],
desc: 'The output target. Default=-',
maxParams: 1,
minParams: 1,
}
],
});
args.debug || process.on('uncaughtException', function (e) {
console.error(e);
process.exit(1);
});
if (!args['$!stray'].length) throw "No files specified.";
/* Heavy async soup */
function Dummy(){}
Dummy.prototype.toString = function () {
return this.r;
};
Dummy.prototype.r = '';
var asReady = 0;
function ready () {
if (++asReady !== files.length) return;
/* All files ready, let's get going */
var code = files.join('\n');
var t = +new Date;
var c = new compiler(jsparse(code), {
debug: args["debug"],
nowrap: !args["wrap"],
warnings: args["warnings"],
});
c.preprocess();
var output = c.compile();
console.error('Compiled in ' + (+new Date - t) + ' milliseconds.');
var filename = args["output"] || '-';
if (filename === '-') {
console.log(output);
} else {
fs.writeFileSync(filename, output, args["encoding"] || 'UTF-8');
}
}
var files = args['$!stray'].map(function(f){
var r = new Dummy;
if (f === '-') {
process.stdin.resume();
process.stdin.on('data', function (data) {
r.r += String(data);
});
process.stdin.on('end', ready);
} else {
fs.readFile(f, args["encoding"] || 'UTF-8', function(e, data) {
if (e) throw "Reading file '" + f + "' failed.";
r.r = String(data);
ready();
});
}
return r;
});