From 322badd209e165c16a77f8d325cc74a1ded19cbb Mon Sep 17 00:00:00 2001 From: Zack Rauen Date: Wed, 15 Aug 2018 23:01:10 -0400 Subject: [PATCH] add inject script --- package.json | 3 ++- scripts/inject.js | 56 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 scripts/inject.js diff --git a/package.json b/package.json index a8866611..04f80a03 100644 --- a/package.json +++ b/package.json @@ -86,6 +86,7 @@ "build_release": "npm run release --prefix client && npm run build --prefix core && npm run release --prefix csseditor", "package_release": "node scripts/package-release.js", "release": "npm run lint && npm run build_release && gulp release && npm run package_release", - "update_release": "npm run build_release && gulp build-release" + "update_release": "npm run build_release && gulp build-release", + "inject": "node scripts/inject.js" } } diff --git a/scripts/inject.js b/scripts/inject.js new file mode 100644 index 00000000..646e1216 --- /dev/null +++ b/scripts/inject.js @@ -0,0 +1,56 @@ +const args = process.argv; +const fs = require('fs'); +const path = require('path'); + +const releaseInput = args[2] ? args[2].toLowerCase() : ''; +const release = releaseInput == 'canary' ? 'DiscordCanary' : releaseInput == 'ptb' ? 'DiscordPTB' : 'Discord'; +console.log(`Injecting into version ${release}`); +const discordPath = (function() { + if (process.platform === 'win32') { + const basedir = path.join(process.env.LOCALAPPDATA, release); + if (!fs.existsSync(basedir)) throw new Error(`Cannot find directory for ${release}`); + const version = fs.readdirSync(basedir).filter(f => fs.lstatSync(path.join(basedir, f)).isDirectory() && f.split('.').length > 1).sort().reverse()[0]; + const dir = path.join(basedir, version, 'resources'); + if (!version || !fs.existsSync(dir)) throw new Error(`Cannot find directory for ${release}`); + return dir; + } + else if (process.platform == 'darwin') { + const dir = path.join('Applications', `${release}.app`, 'Contents', 'Resources'); + if (!fs.existsSync(dir)) throw new Error(`Cannot find directory for ${release}`); + return dir; + } + const dir = path.join('usr', 'share', release.toLowerCase(), 'resources'); + if (!fs.existsSync(dir)) throw new Error(`Cannot find directory for ${release}`); + return dir; +})(); +console.log(`Found ${release} in ${discordPath}`); + +const appPath = path.join(discordPath, 'app'); +const packageJson = path.join(appPath, 'package.json'); +const indexJs = path.join(appPath, 'index.js'); + +if (!fs.existsSync(appPath)) fs.mkdirSync(appPath); +if (fs.existsSync(packageJson)) fs.unlinkSync(packageJson); +if (fs.existsSync(indexJs)) fs.unlinkSync(indexJs); + +console.log(`Writing package.json`); +fs.writeFileSync(packageJson, JSON.stringify({ + name: 'betterdiscord', + description: 'BetterDiscord', + main: 'index.js', + private: true, + dependencies: {} +}, null, 4)); + +console.log(`Writing index.js`); +fs.writeFileSync(indexJs, `const path = require('path'); +const fs = require('fs'); +const Module = require('module'); +const electron = require('electron'); +const basePath = path.join(__dirname, '..', 'app.asar'); +const pkg = require(path.join(basePath, 'package.json')); +electron.app.getAppPath = () => basePath; +Module._load(path.join(basePath, pkg.main), null, true); +electron.app.on('ready', () => {new (require('${path.join(__dirname, '..', 'core').replace(/\\/g, '/')}').BetterDiscord)();});`); + +console.log(`Injection successful, please restart ${release}.`); \ No newline at end of file