-
Notifications
You must be signed in to change notification settings - Fork 1
/
index2.js
85 lines (51 loc) · 1.47 KB
/
index2.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
'use strict';
const fs = require('fs');
const sourceFile = './input.js';
// const sourceFile = process.argv[2];
const UglifyJS = require('uglify-js');
const walk = require ( 'esprima-walk' )
const escodegen = require('escodegen');
const source = fs.readFileSync(sourceFile, 'utf8');
const esprima = require("esprima");
let tree = esprima.parseScript(source);
// console.log(JSON.stringify(tree, null, 2))
walk( tree, ( node ) => {
if(node.type === 'ArrowFunctionExpression' || node.type === 'FunctionExpression') {
const z = {
"type": "VariableDeclaration",
"declarations": [
{
"type": "VariableDeclarator",
"id": {
"type": "Identifier",
"name": "z"
},
"init": node
}
],
"kind": "const"
};
printFn(z);
};
if(node.type === 'FunctionDeclaration') {
// console.log(JSON.stringify(node, null, 2));
let name;
if (node && node.id && node.id.name) {
name = node.id.name;
node.id.name = 'MORK';
}
printFn(node);
};
})
function printFn(a, name) {
const b = escodegen.generate({
"type": "Program",
"body": [a],
"sourceType": "script"
})
// console.log('------------');
const code = UglifyJS.minify(b);
const base64Code = Buffer.from(code.code).toString('base64');
// fs.appendFileSync(`./functions/`, `\n// ${name}`);
console.log(`"${sourceFile}"; "${code.code}";`);
}