This repository has been archived by the owner on Jun 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 64
/
cmd.js
executable file
·161 lines (145 loc) · 4.21 KB
/
cmd.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
#!/usr/bin/env node
/*
* Copyright 2016 The Closure Compiler Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @fileoverview Command-line interface for Google Closure Compiler in JS
*/
'use strict';
const fs = require('fs');
const path = require('path');
const minimist = require('minimist');
const compile = require('./compile.js');
const logger = require('./logger.js');
const argv = minimist(process.argv.slice(2), {
alias: {h: 'help', v: 'version'},
});
if (argv.help) {
const helpfile = path.join(__dirname, 'usage.txt');
return fs.readFile(helpfile, 'utf8', (err, src) => {
if (err) {
error(err)
} else {
console.log(src);
}
});
}
if (argv.version) {
return console.log(require('./package.json').version);
}
const infiles = argv._.concat(toArray(argv.jsCode)); // treat jsCode as _
if (infiles.length === 0) {
infiles.push(null); // if no files were specified, read from stdin
}
const sources = readAllFiles(infiles);
delete argv._;
delete argv.jsCode;
const externs = readAllFiles(toArray(argv.externs));
delete argv.externs;
Promise.all([sources, externs]).then(arr => ready(arr[0], arr[1])).catch(error);
/**
* Minimist gives us a string, array or null: normalize to array.
*
* @param {(string|Array<string>)}
* @return {!Array<string>}
*/
function toArray(arg) {
if (typeof arg === 'string') {
return [arg];
} else if (arg) {
return arg;
} else {
return [];
}
}
/**
* @param {!Array<string>} paths
* @return {!Promise<!Array{src: string, path: string}>>}
*/
function readAllFiles(paths) {
return Promise.all(paths.map(path => readFile(path)));
}
/**
* @param {?string} path
* @return {!Promise<{src: string, path: string}>}
*/
function readFile(path) {
return new Promise((resolve, reject) => {
if (path === null) {
let src = '';
process.stdin.resume();
process.stdin.on('data', buf => src += buf.toString());
process.stdin.on('end', () => resolve({src, path: '-'}));
} else {
fs.readFile(path, 'utf8', (err, src) => err ? reject(err) : resolve({src, path}));
}
});
}
function parseDefines(flags){
/**
* compile() expects 'defines' to be object, but minimist may only return array or string
* so, we need to convert to proper object.
* The supported format will be similar to the following,
* https://github.com/google/closure-compiler/wiki/Annotating-JavaScript-for-the-Closure-Compiler#define-type-description
* but instead of using --define 'name=value' we should support --defines 'name=value'
*/
if (Array.isArray(flags.defines) || typeof flags.defines === "string"){
let defines = {};
function parseDefines(d) {
let key, value;
{
let a = d.split("=");
key = a[0];
value = a[1];
}
defines[key] =
/^(?:true|false)$/.test(value)
? value === 'true'
: !isNaN(parseFloat(value)) && isFinite(value)
? parseFloat(value)
: value;
}
if (Array.isArray(flags.defines)) {
flags.defines.forEach(parseDefines);
} else {
parseDefines(flags.defines);
}
flags.defines = defines;
}
}
function cmdCompile(flags) {
parseDefines(flags);
return compile(flags);
}
//Mostly for test purpuses.
module.exports = cmdCompile;
/**
* @param {!Array<{src: string, path: string}>} sources
* @param {!Array<{src: string, path: string}>} externs
*/
function ready(sources, externs) {
const flags = Object.assign(Object.assign({}, argv), {jsCode: sources, externs: externs});
const output = cmdCompile(flags);
let code = 0;
if (logger(flags, output)) {
code = 1;
}
console.log(output.compiledCode);
process.exit(code);
}
function error(err) {
console.error(err);
process.exit(1);
}