-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain.js
46 lines (37 loc) · 1.38 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
const fs = require('fs');
const path = require('path');
const parser = require('@babel/parser');
const generate = require('@babel/generator').default;
const decodeHex = require('./transformers/decodeHex');
const removeDeadCode = require('./transformers/removeDeadCode');
const renameVariables = require('./transformers/renameVariables');
const initialTransformations = require('./transformers/initialTransformations');
function processCode(inputFilePath = 'script.js', outputFilePath, rename = false) {
const code = decodeHex(inputFilePath);
let ast = parser.parse(code, {
sourceType: "script",
plugins: ["jsx"]
});
ast = initialTransformations(ast);
const cleanedAst = removeDeadCode(ast);
if (rename) {
renameVariables(cleanedAst);
}
const output = generate(cleanedAst).code;
const outputFile = outputFilePath || path.basename(inputFilePath, '.js') + '.clean.js';
fs.writeFileSync(outputFile, output, 'utf8');
console.log(`Output written to ${outputFile}`);
}
// Command-line handling
const args = process.argv.slice(2);
let inputFile, outputFile, shouldRename = false;
args.forEach((arg, index) => {
if (arg === '--rename') {
shouldRename = true;
} else if (!inputFile) {
inputFile = arg;
} else if (!outputFile) {
outputFile = arg;
}
});
processCode(inputFile, outputFile, shouldRename);