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
|
Description: support node module commander release 9
This patch changes option parsing
to be compatible with NodeJS module commander v9
(and possibly v7 and v8 as well),
at the expense of compatibility with older releases of commander.
Author: Yadd <yadd@debian.org>
Bug-Debian: https://bugs.debian.org/963425
Bug-Debian: https://bugs.debian.org/1003039
Bug-Debian: https://bugs.debian.org/1011816
Last-Update: 2025-02-06
--- a/lib/cli.js
+++ b/lib/cli.js
@@ -26,6 +26,15 @@
program.parseArgv = program.parse;
program.parse = undefined;
+ var argv = [];
+ process.argv.forEach(function(arg){
+ if(arg.match(/^-([pcmbode]+)$/)) {
+ argv = argv.concat(RegExp.$1.split('').map(s => { return '-'+s }));
+ }
+ else argv.push(arg);
+ });
+ process.argv = argv;
+
if (process.argv.includes("ast")) program.helpInformation = describe_ast;
else if (process.argv.includes("options")) program.helpInformation = function() {
var text = [];
@@ -62,10 +71,11 @@
program.option("--toplevel", "Compress and/or mangle variables in toplevel scope.");
program.option("--wrap <name>", "Embed everything as a function with “exports” corresponding to “name” globally.");
program.arguments("[files...]").parseArgv(process.argv);
- if (program.configFile) {
- options = JSON.parse(read_file(program.configFile));
+ const opts = program.opts();
+ if (opts.configFile) {
+ options = JSON.parse(read_file(opts.configFile));
}
- if (!program.output && program.sourceMap && program.sourceMap.url != "inline") {
+ if (!opts.output && opts.sourceMap && opts.sourceMap.url != "inline") {
fatal("ERROR: cannot write source map to STDOUT");
}
@@ -80,76 +90,76 @@
"toplevel",
"wrap"
].forEach(function(name) {
- if (name in program) {
- options[name] = program[name];
+ if (name in opts) {
+ options[name] = opts[name];
}
});
- if ("ecma" in program) {
- if (program.ecma != (program.ecma | 0)) fatal("ERROR: ecma must be an integer");
- const ecma = program.ecma | 0;
+ if ("ecma" in opts) {
+ if (opts.ecma != (opts.ecma | 0)) fatal("ERROR: ecma must be an integer");
+ const ecma = opts.ecma | 0;
if (ecma > 5 && ecma < 2015)
options.ecma = ecma + 2009;
else
options.ecma = ecma;
}
- if (program.format || program.beautify) {
- const chosenOption = program.format || program.beautify;
+ if (opts.format || opts.beautify) {
+ const chosenOption = opts.format || opts.beautify;
options.format = typeof chosenOption === "object" ? chosenOption : {};
}
- if (program.comments) {
+ if (opts.comments) {
if (typeof options.format != "object") options.format = {};
- options.format.comments = typeof program.comments == "string" ? (program.comments == "false" ? false : program.comments) : "some";
+ options.format.comments = typeof opts.comments == "string" ? (opts.comments == "false" ? false : opts.comments) : "some";
}
- if (program.define) {
+ if (opts.define) {
if (typeof options.compress != "object") options.compress = {};
if (typeof options.compress.global_defs != "object") options.compress.global_defs = {};
- for (var expr in program.define) {
- options.compress.global_defs[expr] = program.define[expr];
+ for (var expr in opts.define) {
+ options.compress.global_defs[expr] = opts.define[expr];
}
}
- if (program.keepClassnames) {
+ if (opts.keepClassnames) {
options.keep_classnames = true;
}
- if (program.keepFnames) {
+ if (opts.keepFnames) {
options.keep_fnames = true;
}
- if (program.mangleProps) {
- if (program.mangleProps.domprops) {
- delete program.mangleProps.domprops;
+ if (opts.mangleProps) {
+ if (opts.mangleProps.domprops) {
+ delete opts.mangleProps.domprops;
} else {
- if (typeof program.mangleProps != "object") program.mangleProps = {};
- if (!Array.isArray(program.mangleProps.reserved)) program.mangleProps.reserved = [];
+ if (typeof opts.mangleProps != "object") opts.mangleProps = {};
+ if (!Array.isArray(opts.mangleProps.reserved)) opts.mangleProps.reserved = [];
}
if (typeof options.mangle != "object") options.mangle = {};
- options.mangle.properties = program.mangleProps;
+ options.mangle.properties = opts.mangleProps;
}
- if (program.nameCache) {
- options.nameCache = JSON.parse(read_file(program.nameCache, "{}"));
+ if (opts.nameCache) {
+ options.nameCache = JSON.parse(read_file(opts.nameCache, "{}"));
}
- if (program.output == "ast") {
+ if (opts.output == "ast") {
options.format = {
ast: true,
code: false
};
}
- if (program.parse) {
- if (!program.parse.acorn && !program.parse.spidermonkey) {
- options.parse = program.parse;
- } else if (program.sourceMap && program.sourceMap.content == "inline") {
+ if (opts.parse) {
+ if (!opts.parse.acorn && !opts.parse.spidermonkey) {
+ options.parse = opts.parse;
+ } else if (opts.sourceMap && opts.sourceMap.content == "inline") {
fatal("ERROR: inline source map only works with built-in parser");
}
}
if (~program.rawArgs.indexOf("--rename")) {
options.rename = true;
- } else if (!program.rename) {
+ } else if (!opts.rename) {
options.rename = false;
}
let convert_path = name => name;
- if (typeof program.sourceMap == "object" && "base" in program.sourceMap) {
+ if (typeof opts.sourceMap == "object" && "base" in opts.sourceMap) {
convert_path = function() {
- var base = program.sourceMap.base;
+ var base = opts.sourceMap.base;
delete options.sourceMap.base;
return function(name) {
return path.relative(base, name);
@@ -191,25 +201,25 @@
}
async function run_cli() {
- var content = program.sourceMap && program.sourceMap.content;
+ var content = opts.sourceMap && opts.sourceMap.content;
if (content && content !== "inline") {
options.sourceMap.content = read_file(content, content);
}
- if (program.timings) options.timings = true;
+ if (opts.timings) options.timings = true;
try {
- if (program.parse) {
- if (program.parse.acorn) {
+ if (opts.parse) {
+ if (opts.parse.acorn) {
files = convert_ast(function(toplevel, name) {
return require("acorn").parse(files[name], {
ecmaVersion: 2024,
locations: true,
program: toplevel,
sourceFile: name,
- sourceType: options.module || program.parse.module ? "module" : "script"
+ sourceType: options.module || opts.parse.module ? "module" : "script"
});
});
- } else if (program.parse.spidermonkey) {
+ } else if (opts.parse.spidermonkey) {
files = convert_ast(function(toplevel, name) {
var obj = JSON.parse(files[name]);
if (!toplevel) return obj;
@@ -253,7 +263,7 @@
return;
}
- if (program.output == "ast") {
+ if (opts.output == "ast") {
if (!options.compress && !options.mangle) {
result.ast.figure_out_scope({});
}
@@ -287,7 +297,7 @@
}
return value;
}, 2));
- } else if (program.output == "spidermonkey") {
+ } else if (opts.output == "spidermonkey") {
try {
const minified = await minify(
result.code,
@@ -306,17 +316,17 @@
fatal(ex);
return;
}
- } else if (program.output) {
- fs.mkdirSync(path.dirname(program.output), { recursive: true });
- fs.writeFileSync(program.output, result.code);
+ } else if (opts.output) {
+ fs.mkdirSync(path.dirname(opts.output), { recursive: true });
+ fs.writeFileSync(opts.output, result.code);
if (options.sourceMap && options.sourceMap.url !== "inline" && result.map) {
- fs.writeFileSync(program.output + ".map", result.map);
+ fs.writeFileSync(opts.output + ".map", result.map);
}
} else {
console.log(result.code);
}
if (program.nameCache) {
- fs.writeFileSync(program.nameCache, JSON.stringify(options.nameCache));
+ fs.writeFileSync(opts.nameCache, JSON.stringify(options.nameCache));
}
if (result.timings) for (var phase in result.timings) {
print_error("- " + phase + ": " + result.timings[phase].toFixed(3) + "s");
|