This repository has been archived by the owner on Aug 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inspector.js
83 lines (57 loc) · 2.25 KB
/
inspector.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
const fs = require('fs');
const UglifyJS = require('uglify-js');
const path = require('path');
const Terser = require("terser");
let copyright = `// Copyright © 2023 Sapsar S.T.
// The framework is under the managment and ownership of Nitlix S.T.
// Path: [FILENAME]
`
function ScanDirectory(directoryPath) {
let filenames = [];
function scanDirRecursive(dir, baseDir) {
const files = fs.readdirSync(dir);
files.forEach((file) => {
const filePath = path.join(dir, file);
const relativePath = path.relative(baseDir, filePath).replace(/\\/g, '/');
const stat = fs.statSync(filePath);
if (stat.isFile()) {
filenames.push(relativePath);
} else if (stat.isDirectory()) {
scanDirRecursive(filePath, baseDir);
}
});
}
scanDirRecursive(directoryPath, directoryPath);
return filenames;
}
async function ProcessFile(data, name){
//get anything between /** and */ and KEEP IT!
const regex = /\/\*\*([\s\S]*?)\*\//g;
let instructions = data.match(regex);
if (!instructions) instructions = "";
else instructions = `\n${instructions}\n`
const code = (await Terser.minify(data, {
mangle: false
})).code;
//now find the index of the space right between the first "async function" or the first "function"
const index = code.indexOf("async function") > -1 ? code.indexOf("async function") : code.indexOf("function");
//now insert the instructions right after the function declaration
let finalCode = code.slice(0, index) + instructions + code.slice(index);
//add copyright
finalCode = copyright.replace('[FILENAME]', name) + finalCode;
return finalCode;
}
async function main(){
const files = ScanDirectory(__dirname);
files.forEach(async file => {
//if file ENDS with .js
if(file.endsWith(".js") && !file.includes("node_modules") && !file.includes("inspector.js")){
const data = await fs.readFileSync(file, 'utf8').toString();
const finalCode = await ProcessFile(data, file);
await fs.writeFileSync(file, finalCode, 'utf8');
console.log(`Processed ${file}`);
}
})
console.log("Done!")
}
main()