-
Notifications
You must be signed in to change notification settings - Fork 1
/
cli.js
132 lines (115 loc) · 3.67 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
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
#!/usr/bin/env node
/**
* minjson CLI tool
*
* The MIT License (MIT)
*
* Copyright (c) 2015 Zoltan Frombach
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
/*jshint browser:false, node:true*/
'use strict';
var fs = require('fs');
var path = require('path');
var cli = require('cli');
var concat = require('concat-stream');
var appName = require('./package.json').name;
var appVersion = require('./package.json').version;
var minify = require('./jsonminifier.js');
var input = null;
var output = null;
var beautifyOptions = {
compress: false,
output: {
beautify: true,
quote_keys: true
}
};
var minifyOptions = {
compress: false,
output: {
beautify: false,
quote_keys: true
}
};
cli.width = 100;
cli.option_width = 40;
cli.setApp(appName, appVersion);
var usage = appName + ' [OPTIONS] [FILE]\n\n If no input file specified then STDIN will be used for input.';
cli.setUsage(usage);
var cliOptions = {
version: ['v', 'Version information'],
output: ['o', 'Specify output file (if not specified STDOUT will be used for output)', 'file'],
beautify: ['b', 'Beautify']
};
cli.parse(cliOptions);
cli.main(function(args, options) {
function runMinify(original) {
var status = 0;
var minified = null;
try {
minified = minify(original, options.beautify ? beautifyOptions : minifyOptions);
} catch(e) {
status = 3;
process.stderr.write('Error: Minification error:', e);
}
if (minified !== null) {
// Write the output
try {
if (output !== null) {
fs.writeFileSync(path.resolve(output), minified);
} else {
process.stdout.write(minified);
}
} catch(e) {
status = 4;
process.stderr.write('Error: Cannot write to output');
}
}
cli.exit(status);
}
if (options.version) {
process.stderr.write(appName + ' v' + appVersion);
cli.exit(0);
}
if (args.length) {
input = args;
}
if (options.output) {
output = options.output;
}
if (input !== null) { // File(s) specified on the Command line ?
if (input.length > 1) {
process.stderr.write('Error: Too many command line arguments specified');
cli.exit(1);
}
var afile = input[0]; // We only minify the first file specified on the Command Line (because it doesn't make sense to concatenate multiple JSON files)
var original;
try {
original = fs.readFileSync(afile, 'utf8');
} catch(e) {
process.stderr.write('Error: Cannot read file ' + afile);
cli.exit(2);
}
runMinify(original);
} else { // Minifying input coming from STDIN
process.stdin.pipe(concat({encoding: 'string'}, runMinify));
}
});