-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgccs.js
executable file
·262 lines (222 loc) · 6.32 KB
/
gccs.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#!/usr/bin/env node
/**
* Compile JS files using Google's Closure-Compiler service
*
* @author Dumitru Uzun (https://DUzun.Me)
* @license MIT https://github.com/duzun/gccs/blob/master/LICENSE
* @version 1.3.2
*/
((utf8, dash, process) => {
'use strict';
const VERSION = '1.3.2';
const http = require('http');
const https = require('https');
const fs = require('fs');
const path = require('path');
const querystring = require('querystring');
compile.VERSION = VERSION;
// CLI
if ( !module.parent ) {
const opt = {};
const argv = process.argv;
let i = 2;
let a;
while( (a=argv[i]) && a.slice(0,2) == '--' ) {
++i;
if ( a === '--' ) break;
a = a.slice(2);
switch(a) {
case 'help':
usage(process.stdout);
process.exit(0);
break;
default: {
opt[a] = try_parse(argv[i++]);
}
}
}
let in_file = argv[i] || opt.in_file;
let out_file = argv[i+1] || opt.out_file;
if ( !in_file || in_file === dash ) {
in_file = dash;
if ( !out_file ) {
out_file = dash;
}
}
else if ( !out_file ) {
out_file = path.join(path.dirname(in_file), path.basename(in_file, '.js') + '.min.js');
}
opt.out_file = out_file;
delete opt.in_file;
compileFile(in_file, opt, (err) => {
if (err) {
console.error(err);
usage(process.stderr);
process.exit(2);
}
if ( out_file != dash ) {
console.log("\x1b[32m%s\x1b[0m", out_file);
}
});
}
// module
else {
compile.file = compileFile;
compile.cli_usage = usage;
module.exports = compile;
}
function compile(js_code, cb) {
let opt = {};
if ( isObject(js_code) && !isStream(js_code) ) {
opt = js_code;
js_code = opt.js_code;
}
if ( isStream(js_code) ) {
return stream2buffer(js_code, (err, js_code) => {
if ( err ) return cb(err);
opt.js_code = js_code;
compile(opt, cb);
});
}
if ( Buffer.isBuffer(js_code) ) {
js_code = js_code.toString(utf8);
}
let shebang = js_code.match(/^#!.{3,}/);
opt.js_code = js_code;
const options = Object.assign({
warning_level: 'QUIET'
, compilation_level: 'SIMPLE_OPTIMIZATIONS'
}, opt, {
output_info: 'compiled_code'
, output_format: 'text'
});
const { mod, port } = 'https' in options && !options.https
? { mod: http, port: 80 }
: { mod: https, port: 443 }
;
delete options.https;
const data = querystring.stringify(options);
const req = mod.request({
hostname: 'closure-compiler.appspot.com',
port: port,
path: '/compile',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(data),
},
}, (res) => {
// console.log('STATUS: ' + res.statusCode);
stream2buffer(res, (err, buf) => {
if ( buf ) buf = buf.toString(utf8);
if ( shebang ) buf = shebang[0] + '\n' + buf;
cb(err, buf, res);
// cb(err, (shebang ? shebang[0] + '\n' : '') + (buf ? buf.toString(utf8) : ''), res);
});
});
req.on('error', cb);
req.write(data);
req.end();
return req;
}
function compileFile(filename, opt, cb) {
if ( !cb && isFunction(opt) ) {
cb = opt;
opt = undefined;
}
let in_file = filename !== dash
? isStream(filename)
? filename
: fs.createReadStream(filename)
: process.stdin
;
let out_file;
if ( opt ) {
if ( isString(opt) || isStream(opt) ) {
out_file = opt;
opt = undefined;
}
else if ( opt.out_file ) {
out_file = opt.out_file;
delete opt.out_file;
}
if ( out_file ) {
if ( out_file === dash ) {
out_file = process.stdout;
}
}
}
return compile(Object.assign({}, opt, { js_code: in_file }), (err, code) => {
if ( err ) return cb(err);
if ( out_file ) {
if ( !isStream(out_file) ) {
// let dir = path.dirname(out_file);
// if ( !fs.existsSync(dir) ) {
// fs.mkdir(dir);
// }
out_file = fs.createWriteStream(out_file);
}
out_file.write(code, utf8, (err) => {
cb(err, code);
});
if ( !out_file._isStdio ) {
out_file.end();
}
}
else {
cb(err, code);
}
});
}
function usage(stream) {
const gccs = path.basename(process.argv[1], '.js');
const txt =
`Usage:
${gccs} --help
or
${gccs} [OPTIONS] [--] [ <in_file> [ <out_file> ] ]
If <out_file> is omitted, out_file = in_file.min.js
If <in_file> == "-", stdin is used (<out_file> defaults to "-").
If <out_file> == "-", stdout is used.
If <in_file> and <out_file> are both omitted, they both default to "-".
OPTIONS:
--https true | false
--compilation_level WHITESPACE_ONLY | SIMPLE_OPTIMIZATIONS | ADVANCED_OPTIMIZATIONS
--formatting pretty_print,print_input_delimiter
... (anything that Closure Compiler service accepts)
`;
if ( stream ) {
stream.write(txt);
}
else {
return txt;
}
}
// --- Helpers ---
function stream2buffer(stream, cb) {
let buf = [];
stream.on('data', (chunk) => { buf.push(chunk); });
stream.on('end', () => { cb(null, buf = Buffer.concat(buf), stream); });
stream.on('error', cb);
return stream;
}
function try_parse(str) {
if ( isString(str) ) try {
return JSON.parse(str);
}
catch(err) {}
return str;
}
function isString(val) {
return typeof val === 'string';
}
function isFunction(val) {
return typeof val === 'function';
}
function isObject(val) {
return val && typeof val === 'object';
}
function isStream(stream) {
return isObject(stream) && isFunction(stream.pipe);
}
})('utf8', '-', process);