-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
62 lines (60 loc) · 1.82 KB
/
index.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
const fs = require("fs");
const util = require("util");
const readFile = util.promisify(fs.readFile);
const writeFile = util.promisify(fs.writeFile);
const readdirp = require("readdirp");
const JavaScriptObfuscator = require("javascript-obfuscator");
var excludedDir = ["!node_modules", "!plugins"];
// listing all the '.js' files
const getJsFiles = async (directory, func_dir) => {
if (func_dir) {
excludedDir.push(`!${func_dir}`);
}
const files = await readdirp.promise(directory, {
fileFilter: "*.js",
directoryFilter: excludedDir,
});
return files.map((file) => file.fullPath);
};
// Reading the files -> obfuscating the code -> Writing the obfuscated code
const obfuscateCode = async (filePath, custom, utils) => {
var file = await readFile(filePath, "utf8");
try {
var obfuscationResult = JavaScriptObfuscator.obfuscate(file, {
compact: custom[0],
controlFlowFlattening: true,
controlFlowFlatteningThreshold: 1,
numbersToExpressions: custom[1],
simplify: custom[2],
shuffleStringArray: custom[3],
splitStrings: custom[4],
});
} catch (error) {
return utils.build.failBuild(`Failed to Obfuscate '${filePath}' .`, { error });
}
await writeFile(filePath, obfuscationResult.getObfuscatedCode());
return 1;
};
module.exports = {
onPostBuild: async ({ inputs, constants, utils }) => {
const jsFiles = await getJsFiles(
constants.PUBLISH_DIR,
constants.FUNCTIONS_SRC
);
for (const filePath of jsFiles) {
console.log(filePath);
await obfuscateCode(
filePath,
[
inputs.compact,
inputs.numbersToExpressions,
inputs.simplify,
inputs.shuffleStringArray,
inputs.splitStrings,
],
utils
);
}
console.log("JS files successfully Obfuscated!");
},
};