Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
var fs = require('fs');
var path = require('path');
var recursive = require('recursive-readdir');
var rimraf = require('rimraf');
var asar = require('asar');
var javaScriptObfuscator = require('javascript-obfuscator');
// Set your resources folder
var resourcesFolder = 'C:\\example-electron\\dist\\win-unpacked\\resources';
console.log('\n\nasar package javascript obfuscator\n\n');
console.log('Unpacking archive');
asar.extractAll(resourcesFolder + '\\app.asar', resourcesFolder + '\\src');
console.log('Deleting app.asar');
fs.unlinkSync(resourcesFolder + '\\app.asar');
// Enter the directories to be ignored
recursive(resourcesFolder + '\\src', ['node_modules', 'app'], function (err, files) {
files.forEach(file => {
if (path.extname(file) === '.js') {
let contents = fs.readFileSync(file, 'utf8');
console.log('Protecting ' + file);
// Change the settings here - https://github.com/javascript-obfuscator/javascript-obfuscator
let ret = javaScriptObfuscator.obfuscate(contents, {
compact: true
, controlFlowFlattening: false
, controlFlowFlatteningThreshold: 0.75
var code = fs.readFileSync(`${rootFolder}/code.js`, 'utf8');
if (fs.existsSync(output)) removeSync(output);
if (fs.existsSync(input)) removeSync(input);
// replace injected packs with backups
if (fs.existsSync(backupAsar)) {
if (fs.existsSync(originalAsar)) removeSync(originalAsar);
removeSync(originalAsar);
originalFs.copyFileSync(backupAsar, originalAsar);
removeSync(backupAsar);
}
// bring figma files
asar.extractAll(originalAsar, input);
// inject code
const useLocalPluginsManager = store.get('useLocalPluginsManager', false);
if (useLocalPluginsManager) {
const localPluginsManagerUrl = store.get('localPluginsManagerUrl', "https://jachui.github.io/figma-plugin-manager");
code = code.replace(/SERVER_URL/g, localPluginsManagerUrl.replace(/\/$/, ""));
}
else {
code = code.replace(/SERVER_URL/g, "https://jachui.github.io/figma-plugin-manager");
}
const devMode = store.get('devMode', false);
fs.mkdirSync(_discordPath + "/node_modules/BetterDiscord");
wrench.copyDirSyncRecursive(__dirname + "/BetterDiscord/", _discordPath + "/node_modules/BetterDiscord/", {forceDelete: true});
console.log("Looking for app archive");
if(fs.existsSync(_discordPath + _appArchive)) {
console.log("App archive found at: " + _discordPath + _appArchive);
} else {
console.log("Failed to locate app archive at: " + _discordPath + _appArchive);
process.exit();
}
console.log("Extracting app archive");
asar.extractAll(_discordPath + _appArchive, _discordPath + _appFolder);
if(!fs.existsSync("splice")) {
console.log("Missing splice file");
process.exit();
}
var splice = fs.readFileSync("splice");
fs.exists(_discordPath + _appFolder, function(exists) {
if(exists) {
console.log("Extracted to: " + _discordPath + _appFolder);
console.log("Injecting index.js");
var data = fs.readFileSync(_discordPath + _index).toString().split("\n");
data.splice(_importSplice, 0, 'var _betterDiscord = require(\'betterdiscord\');\n');
var extractSlaxFile = function(slaxFileName,slaxAppDir) {
try {
ensureAppDir();
zipper.sync.unzip(slaxFileName).save(slaxAppDir);
} catch (e) {
console.log("The slax file is not a zipped file? extract as a asar file",e);
ensureAppDir();
asar.extractAll(slaxFileName,slaxAppDir);
}
};
function unpack(slaxFileName,slaxAppDir) {
try {
ensureAppDir(slaxAppDir);
zipper.sync.unzip(slaxFileName).save(slaxAppDir);
} catch (e) {
console.log("The slax file is not a zipped file? extract as a asar file",e);
ensureAppDir(slaxAppDir);
asar.extractAll(slaxFileName,slaxAppDir);
}
}
needInstall = true
}
}
if (needInstall) {
if (fs.existsSync(path.join(libRoot, 'node_modules.asar'))) fs.removeSync(path.join(libRoot, 'node_modules.asar'))
if (fs.existsSync(path.join(libRoot, 'node_modules.asar.unpacked'))) fs.removeSync(path.join(libRoot, 'node_modules.unpacked'))
execSync(`npm install --no-package-lock --production --arch=${arch} --target_arch=${arch} --build-from-source --runtime=electron --target=${pkg.devDependencies.electron} --dist-url=https://atom.io/download/electron`, { cwd: libRoot, stdio: 'inherit' })
removeBuild(libRoot)
await createPackageWithOptions(path.join(libRoot, 'node_modules'), path.join(libRoot, 'node_modules.asar'), {})
await fs.remove(path.join(libRoot, 'node_modules'))
}
extractAll(path.join(libRoot, 'node_modules.asar'), path.join(root, 'node_modules'))
}
process.on("message", function (data) {
if (data.type == "extract") {
process.noAsar = true;
asar.extractAll(data.source, data.target);
process.send({
type: "extract",
status: "success"
});
}
if (data.type == "complete-patch") {
rootPath = data.root;
_this.completePatch();
}
if (data.type == "stop") {
_this.stop();
}
});
let unzipping = ()=> {
if (ASAR.length == 0) {
res.send({status: true});
return;
}
let PRJ_PATH = ASAR.splice(0, 1)[0];
asar.extractAll(PRJ_PATH.tmp, PRJ_PATH.dest);
unzipping();
};