Merge pull request #226 from JsSucks/inject

Add inject script
This commit is contained in:
Alexei Stukov 2018-08-16 10:56:32 +03:00 committed by GitHub
commit 42e2569569
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 58 additions and 1 deletions

View File

@ -86,6 +86,7 @@
"build_release": "npm run release --prefix client && npm run build --prefix core && npm run release --prefix csseditor", "build_release": "npm run release --prefix client && npm run build --prefix core && npm run release --prefix csseditor",
"package_release": "node scripts/package-release.js", "package_release": "node scripts/package-release.js",
"release": "npm run lint && npm run build_release && gulp release && npm run package_release", "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"
} }
} }

56
scripts/inject.js Normal file
View File

@ -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}.`);