-
Notifications
You must be signed in to change notification settings - Fork 6
/
extension.js
193 lines (150 loc) · 5.76 KB
/
extension.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
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
const vscode = require('vscode');
const fs = require('fs');
const JavaScriptObfuscator = require('javascript-obfuscator');
// this method is called when your extension is activated
// your extension is activated the very first time the command is executed
/**
* @param {vscode.ExtensionContext} context
*/
function activate(context) {
let JSObfuscatorOutputChannel;
let settings = vscode.workspace.getConfiguration().get("JSObfuscator");
let _getAllFilesFromFolder = function (dir) {
let filesystem = require("fs");
let results = [];
filesystem.readdirSync(dir).forEach(function (file) {
file = dir + '/' + file;
let stat = filesystem.statSync(file);
if (stat && stat.isDirectory()) {
results = results.concat(_getAllFilesFromFolder(file))
} else {
results.push(file);
}
});
return results;
};
let _JSCodeToObfuscator = function (text) {
let obfuscationResult = JavaScriptObfuscator.obfuscate(
text,
settings['javascript-obfuscator']
);
return obfuscationResult.getObfuscatedCode();
}
let doObfuscateFile = function (filePath) {
if (typeof (filePath) === 'object') {
filePath = filePath.fileName;
}
let filePathSplit = filePath.split('\\');
let fileName = filePathSplit.pop();
let originalFileName = fileName;
let filePathOnly = filePathSplit.join('\\');
//check for js files
if (fileName.search(".js") == -1) {
return
}
// // check for min.js files
// if (settings.ignoreMinFiles) {
// if (fileName.search(".min.js") != -1) {
// return;
// }
// }
let text = fs.readFileSync(filePath).toString('utf-8');
let outName = filePath;
// if there is a value in changeFileExtension, remove the last extension and replace it with the provided one
if (settings.changeFileExtension != "") {
if (fileName.search("." + settings.changeFileExtension) == -1) {
fileName = fileName.split('.');
fileName.pop();
fileName.push(settings.changeFileExtension);
fileName = fileName.join('.');
outName = filePathOnly + '\\' + fileName
}
else {
return vscode.window.showInformationMessage('Skipped Obfuscating ' + originalFileName);
}
}
let writeFile = true;
if (!settings.overwriteExistingFiles) {
if (fs.existsSync(outName)) {
vscode.window.showInformationMessage('Skipped Obfuscating because file already exist ' + originalFileName);
// set boolean to skip this file
writeFile = false;
}
}
if (writeFile) {
fs.writeFile(outName, _JSCodeToObfuscator(text), function (err) {
if (err) {
JSObfuscatorOutputChannel.appendLine('Error writing to file: ' + outName);
JSObfuscatorOutputChannel.appendLine(err.message);
return vscode.window.showErrorMessage('Invalid Exception.');
}
else {
return vscode.window.showInformationMessage('Finished Obfuscating ' + originalFileName);
}
});
}
};
let disposable = vscode.commands.registerCommand('JSObfuscator.obfuscateWorkspace', function () {
let JSObfuscatorOutputChannel = vscode.window.createOutputChannel("JSObfuscator");
JSObfuscatorOutputChannel.clear();
JSObfuscatorOutputChannel.appendLine('Obfuscating Workspace process started');
let currentFile;
try {
if (typeof vscode.workspace.workspaceFolders === 'undefined' || vscode.workspace.workspaceFolders.length == 0) {
return vscode.window.showInformationMessage("Open a folder or workspace... (File -> Open Folder)");
}
let ignoreMinFiles = vscode.workspace.getConfiguration().get('JSObfuscator.ignoreMinFiles');
let subPathInWorkspace = "/" + vscode.workspace.getConfiguration().get('JSObfuscator.subPathInWorkspace');
let ignoreFile = vscode.workspace.getConfiguration().get('JSObfuscator.filesToIgnore');
let ignoreFileArray = ignoreFile.split(",");
const workspacePath = vscode.workspace.workspaceFolders[0].uri.fsPath + subPathInWorkspace;
let filePath = _getAllFilesFromFolder(workspacePath);
for (let i = 0; i < filePath.length; i++) {
currentFile = filePath[i];
let currentFileName = filePath[i].split("/");
currentFileName = currentFileName[currentFileName.length - 1];
// check settings and if we are supposed to ignore .min.js files skip this file
// if the current file name is supposed to be ignored skip this file
if ((ignoreMinFiles && filePath[i].search(".min.js") != -1) || ignoreFileArray.indexOf(currentFileName) != -1) {
continue;
}
let nameSplitted = filePath[i].split(".");
if (nameSplitted[nameSplitted.length - 1] == 'js') {
doObfuscateFile(filePath[i]);
}
}
return vscode.window.showInformationMessage('Workspace finished Obfuscating.');
} catch (error) {
JSObfuscatorOutputChannel.appendLine('Error while working with: ' + currentFile);
JSObfuscatorOutputChannel.appendLine(error.stack);
JSObfuscatorOutputChannel.appendLine(error);
return vscode.window.showErrorMessage('Invalid Exception.');
}
});
context.subscriptions.push(disposable);
disposable = vscode.commands.registerCommand('JSObfuscator.obfuscateFile', function () {
const active = vscode.window.activeTextEditor;
if (!active || !active.document) {
return;
}
if (active.document.isUntitled) {
return vscode.window.setStatusBarMessage("File must be saved before it can be obfuscated", 5000);
}
doObfuscateFile(active.document);
});
context.subscriptions.push(disposable);
disposable = vscode.workspace.onDidChangeConfiguration(() => {
settings = vscode.workspace.getConfiguration().get("JSObfuscator");
});
context.subscriptions.push(disposable);
}
exports.activate = activate;
// this method is called when your extension is deactivated
function deactivate() {
}
module.exports = {
activate,
deactivate
}