forked from evanw/packer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
58 lines (54 loc) · 1.19 KB
/
cli.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
#!/usr/bin/env node
var fs = require('fs');
var packer = require('packer');
var args = require('nomnom').opts({
input_file: {
abbr: 'i',
help: 'Input file (default stdin)'
},
base62: {
abbr: 'b',
flag: true,
help: 'Base62 encode'
},
output_file: {
abbr: 'o',
help: 'Output file (default stdout)'
},
shrink: {
abbr: 's',
help: 'Shrink variables'
}
}).parseArgs();
var data = '';
function finish() {
var packed_data = packer.pack(data, args.base62, args.shrink) + '\n';
if (args.output_file) {
fs.writeFile(args.output_file, packed_data, function(error) {
if (error) {
console.log('could not write to "' + args.output_file + '": ' + error.message);
}
});
} else {
process.stdout.write(packed_data);
}
}
if (args.input_file) {
fs.readFile(args.input_file, function(error, input_data) {
if (error) {
console.log('could not read from "' + args.input_file + '": ' + error.message);
} else {
data = input_data;
finish();
}
});
} else {
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', function (chunk) {
data += chunk;
});
process.stdin.on('end', function () {
finish();
});
}