-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
221 lines (194 loc) · 6.68 KB
/
main.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
import path from "node:path";
import process from "node:process";
import {parseArgs} from "node:util";
import fs from "node:fs";
import { minify } from "./minify.js";
import * as obfuscate from "./obfuscate.js"
import { tokenize, formatTokens } from "./tokenize.js"
import { confirmContinue } from "./utils.js";
const modes = ["minify", "obfuscate", "tokenize"];
function usage(full = false) {
// abbreviate full path to node and script with just base names
const program = path.basename(process.argv[0]) + " " + path.basename(process.argv[1]);
console.log(`usage: ${program} [--mode=mode] [--outdir=dir] --infile <input.js>`);
console.log(` ${program} [--mode=mode] [--outdir=dir] --batch <input_files.txt>`);
if (full) {
// transform modes list to indicate initial can also be used as mode specifier
// "minify" -> "m[inify]"
const modesList = modes.map((m) => {
return m[0] + `[${m.slice(1)}]`;
}).join(", ")
console.log();
console.log("--infile accepts a single JS file for processing");
console.log("--batch accepts a text file where each line is the path of a JS file to process");
console.log("--outdir sets the directory to output files to (default: current dir)");
console.log();
console.log("Valid modes: " + modesList + " (default: obfuscate)");
}
}
const cliOptions = {
infile: { type: "string", short: "i", default: ""},
batch: { type: "string", short: "b", default: ""},
outdir: { type: "string", short: "d", default: "" },
// obfuscate: { type: "boolean", short: "o", default: true },
// minify: { type: "boolean", short: "m", default: false },
// tokenize: { type: "boolean", short: "t", default: false },
mode: { type: "string", short: "m", default: "obfuscate" },
help: { type: "boolean", short: "h", default: false },
};
// Parse command line arguments
function parseCliArgs(args) {
let argValues = null;
try {
const parsedArgs = parseArgs({
args: args,
allowPositionals: true,
options: cliOptions
});
argValues = parsedArgs.values;
// record single positional value as infiles path, if present
argValues.infiles = (parsedArgs.positionals.length === 1) ? parsedArgs.positionals[0] : null;
} catch (e) {
if (e instanceof TypeError) {
console.log(e.message + "\n");
} else {
throw e;
}
}
return argValues;
}
function parseMode(cliModeArg) {
let mode = "";
for (let m of modes) {
// we already tested cliArgs.mode === "" before, so can assume length > 0
if (cliModeArg === m || cliModeArg[0] === m[0]) {
mode = m;
}
}
return mode;
}
function doSingleFile(inputFile, mode, outputDir) {
switch (mode) {
case "minify":
const outputPath = minify(inputFile, outputDir);
console.log(outputPath);
break;
case "obfuscate":
console.log(`Output directory: ${outputDir}`)
const combinations = obfuscate.generateOptionCombinations();
obfuscate.generateObfuscatedVersions(inputFile, inputFile, combinations, outputDir)
break;
case "tokenize":
const tokens = tokenize(inputFile);
const formattedTokens = formatTokens(tokens, false);
console.log(formattedTokens);
}
}
function doMultipleFiles(filenamesFile, mode, outputDir) {
const filenames = fs.readFileSync(filenamesFile, {encoding: "utf8"})
.split("\n")
.filter(filename => filename !== "");
if (mode === "minify") {
batch(filenames, (filename, _) => {
const outputPath = minify(filename, outputDir);
console.log(outputPath);
});
return;
}
if (mode === "tokenize") {
console.log("tokenize for multiple files is not implemented yet, sorry!")
return;
}
console.log(`Output directory: ${outputDir}`)
console.log(`Read ${filenames.length} filenames`);
const combinations = obfuscate.generateOptionCombinations();
const numFiles = filenames.length * combinations.length
console.log(`\n## This program will generate ${numFiles} files ##\n`);
confirmContinue().then((confirm) => {
if (confirm) {
console.log("begin");
batch(filenames, (filename, progress) => {
obfuscate.generateObfuscatedVersions(filename, progress, combinations, outputDir);
});
} else {
console.log("aborted");
}
});
}
/*
batch iterates over a list of filenames and calls
processFn(filename, progress);
for each filename in the list.
progress is a string indicating progress through the list,
may be prepended to console.log messages inside processFn.
All errors thrown by processFn while processing files are
recorded, and then printed along with the associated file
at the end of processing.
*/
function batch(filenames, processFn) {
const fileErrors = {};
let fileCount = 0;
for (let filename of filenames) {
const progress = `[${fileCount+1}/${filenames.length}]`
console.log(`${progress} ${filename}`);
try {
processFn(filename, progress);
} catch (err) {
console.log(`${progress} error processing file`);
fileErrors[filename] = err;
}
fileCount++;
}
if (fileErrors.length > 0) {
console.log("\nThe following errors occurred during processing");
for (let problemFile in fileErrors) {
console.log(`path: ${problemFile}, error: ${fileErrors[problemFile]}`);
}
console.log("\nList of files for which errors occurred:");
for (let filename of Object.keys(fileErrors)) {
console.log(filename);
}
}
}
function main() {
const args = process.argv.slice(2);
const cliArgs = parseCliArgs(args);
if (args.length === 0 || cliArgs === null || cliArgs.help || cliArgs.mode === "") {
const printFull = cliArgs !== null; // if null, then there was also an error message printed
usage(printFull);
process.exit(-1);
}
if (cliArgs.infile !== "" && cliArgs.batch !== "") {
console.log("cannot specify both --infile (parse single file) and --batch (parse multiple files)");
console.log();
usage();
process.exit(-1);
}
if (cliArgs.infile === "" && cliArgs.batch === "") {
console.log("no input files specified");
console.log();
usage(true);
process.exit(-1);
}
const mode = parseMode(cliArgs.mode);
if (mode === "") {
console.log(`unrecognised mode: ${cliArgs.mode}`);
usage(true);
process.exit(-1);
}
if (cliArgs.outdir === "") {
cliArgs.outdir = process.cwd();
}
const outputDir = cliArgs.outdir;
if (!fs.existsSync(outputDir) || !fs.statSync(outputDir).isDirectory()) {
console.log(`output path '${outputDir}' does not exist or is not a directory`);
process.exit(1);
}
if (cliArgs.batch !== "") {
doMultipleFiles(cliArgs.batch, mode, outputDir);
}
if (cliArgs.infile !== "") {
doSingleFile(cliArgs.infile, mode, outputDir);
}
}
main();