2015-10-27 19:29:59 +01:00
/ *
* BetterDiscordApp Installer v0 . 3.2
* /
2016-02-12 18:04:26 +01:00
var dver = "0.0.284" ;
2015-10-27 19:29:59 +01:00
var asar = require ( 'asar' ) ;
var wrench = require ( 'wrench' ) ;
var fs = require ( 'fs' ) ;
var readline = require ( 'readline' ) ;
var util = require ( 'util' ) ;
2015-10-31 23:25:05 +01:00
var _importSplice ;
var _functionSplice ;
var _functionCallSplice ;
2015-10-27 19:29:59 +01:00
var _discordPath ;
var _appFolder = "/app" ;
var _appArchive = "/app.asar" ;
var _packageJson = _appFolder + "/package.json" ;
var _index = _appFolder + "/app/index.js" ;
2015-10-31 22:17:16 +01:00
var _force = false ;
// Get Arguments
process . argv . forEach ( function ( val , index , array ) {
if ( val == "--force" ) {
_force = true ;
}
if ( val == "-d" || val == "--directory" ) {
_discordPath = array [ ( index + 1 ) ]
}
} ) ;
2015-10-27 19:29:59 +01:00
function install ( ) {
2015-10-31 22:17:16 +01:00
if ( typeof _discordPath == 'undefined' ) {
var _os = process . platform ;
if ( _os == "win32" ) {
2016-02-14 01:46:18 +01:00
_importSplice = 89 ;
_functionCallSplice = 497 ;
_functionSplice = 601 ;
2015-10-31 22:17:16 +01:00
_discordPath = process . env . LOCALAPPDATA + "/Discord/app-" + dver + "/resources" ;
} else if ( _os == "darwin" ) {
2016-02-14 01:46:18 +01:00
_importSplice = 67 ;
_functionCallSplice = 446 ;
_functionSplice = 547 ;
2015-10-31 22:17:16 +01:00
_discordPath = "/Applications/Discord.app/Contents/Resources" // Defaults to Applications directory
}
2015-10-27 19:29:59 +01:00
}
console . log ( "Looking for discord resources at: " + _discordPath ) ;
fs . exists ( _discordPath , function ( exists ) {
if ( exists ) {
console . log ( "Discord resources found at: " + _discordPath + "\nLooking for app folder" ) ;
if ( fs . existsSync ( _discordPath + _appFolder ) ) {
console . log ( "Deleting " + _discordPath + _appFolder + " folder." ) ;
wrench . rmdirSyncRecursive ( _discordPath + _appFolder ) ;
console . log ( "Deleted " + _discordPath + _appFolder + " folder." ) ;
}
if ( fs . existsSync ( _discordPath + "/node_modules/BetterDiscord" ) ) {
console . log ( "Deleting " + _discordPath + "/node_modules/BetterDiscord" + " folder." ) ;
wrench . rmdirSyncRecursive ( _discordPath + "/node_modules/BetterDiscord" ) ;
console . log ( "Deleted " + _discordPath + "/node_modules/BetterDiscord" + " folder." ) ;
}
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" ) ;
2016-02-14 01:46:18 +01:00
asar . extractAll ( _discordPath + _appArchive , _discordPath + _appFolder ) ;
2015-10-27 19:29:59 +01:00
2016-02-14 01:46:18 +01:00
console . log ( "Copying BetterDiscord" ) ;
2015-10-27 19:29:59 +01:00
2016-02-14 01:46:18 +01:00
fs . mkdirSync ( _discordPath + "/node_modules/BetterDiscord" ) ;
wrench . copyDirSyncRecursive ( _ _dirname + "/BetterDiscord/" , _discordPath + _appFolder + "/node_modules/BetterDiscord/" , { forceDelete : true } ) ;
2015-10-27 19:29:59 +01:00
2015-12-13 13:44:21 +01:00
if ( ! fs . existsSync ( "splice" ) ) {
console . log ( "Missing splice file" ) ;
process . exit ( ) ;
}
var splice = fs . readFileSync ( "splice" ) ;
2016-02-14 01:46:18 +01:00
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' ) ;
data . splice ( _functionCallSplice , 0 , splice ) ;
fs . writeFile ( _discordPath + _index , data . join ( "\n" ) , function ( err ) {
if ( err ) return console . log ( err ) ;
console . log ( "Injected index.js" ) ;
console . log ( "Deleting old cache files" ) ;
var counter = 0 ;
var _prefsPath = '/Library/Preferences/BetterDiscord/' ;
var emotes _twitch _global = 'emotes_twitch_global.json' ;
fs . exists ( process . env . HOME + _prefsPath + emotes _twitch _global , ( exists ) => {
if ( exists ) {
console . log ( "Deleting " + emotes _twitch _global ) ;
fs . unlinkSync ( process . env . HOME + _prefsPath + emotes _twitch _global , ( err ) => {
if ( err ) throw err ;
} ) ;
console . log ( "Deleted " + emotes _twitch _global ) ;
}
counter ++ ;
finished ( ) ;
} ) ;
var emotes _twitch _subscriber = 'emotes_twitch_subscriber.json' ;
fs . exists ( process . env . HOME + _prefsPath + emotes _twitch _subscriber , ( exists ) => {
if ( exists ) {
console . log ( "Deleting " + emotes _twitch _subscriber ) ;
fs . unlinkSync ( process . env . HOME + _prefsPath + emotes _twitch _subscriber , ( err ) => {
if ( err ) throw err ;
} ) ;
console . log ( "Deleted " + emotes _twitch _subscriber ) ;
}
counter ++ ;
finished ( ) ;
} ) ;
var emotes _bttv = 'emotes_bttv.json' ;
fs . exists ( process . env . HOME + _prefsPath + emotes _bttv , ( exists ) => {
if ( exists ) {
console . log ( "Deleting " + emotes _bttv ) ;
fs . unlinkSync ( process . env . HOME + _prefsPath + emotes _bttv , ( err ) => {
if ( err ) throw err ;
} ) ;
console . log ( "Deleted " + emotes _bttv ) ;
}
counter ++ ;
finished ( ) ;
} ) ;
var emotes _bttv _2 = "emotes_bttv_2.json" ;
fs . exists ( process . env . HOME + _prefsPath + emotes _bttv _2 , ( exists ) => {
if ( exists ) {
console . log ( "Deleting " + emotes _bttv _2 ) ;
fs . unlinkSync ( process . env . HOME + _prefsPath + emotes _bttv _2 , ( err ) => {
if ( err ) throw err ;
} ) ;
console . log ( "Deleted " + emotes _bttv _2 ) ;
}
counter ++ ;
finished ( ) ;
} ) ;
var emotes _ffz = "emotes_ffz.json" ;
fs . exists ( process . env . HOME + _prefsPath + emotes _ffz , ( exists ) => {
if ( exists ) {
console . log ( "Deleting " + emotes _ffz ) ;
fs . unlinkSync ( process . env . HOME + _prefsPath + emotes _ffz , ( err ) => {
if ( err ) throw err ;
} ) ;
console . log ( "Deleted " + emotes _ffz ) ;
}
counter ++ ;
finished ( ) ;
} ) ;
var user _pref = "user.json" ;
fs . exists ( process . env . HOME + _prefsPath + user _pref , ( exists ) => {
if ( exists ) {
console . log ( "Deleting " + user _pref ) ;
fs . unlinkSync ( process . env . HOME + _prefsPath + user _pref , ( err ) => {
if ( err ) throw err ;
} ) ;
console . log ( "Deleted " + user _pref ) ;
}
counter ++ ;
finished ( ) ;
} ) ;
function finished ( ) {
if ( counter => 6 ) {
console . log ( "Looks like we're done here" ) ;
process . exit ( ) ;
}
}
} ) ;
} else {
console . log ( "Something went wrong. Please try again." ) ;
process . exit ( ) ;
}
} ) ;
2015-10-27 19:29:59 +01:00
} else {
console . log ( "Discord resources not found at: " + _discordPath ) ;
process . exit ( ) ;
}
} ) ;
}
function init ( ) {
console . log ( "BetterDiscord Simple Installer v0.3 for Discord " + dver + " by Jiiks." ) ;
console . log ( "If Discord has updated then download the latest installer." ) ;
var rl = readline . createInterface ( { input : process . stdin , output : process . stdout } ) ;
2015-10-31 22:17:16 +01:00
if ( _force == false ) {
rl . question ( "The following directories will be deleted if they exists: discorpath/app, discordpath/node_modules/BetterDiscord, is this ok? Y/N" , function ( answer ) {
var alc = answer . toLowerCase ( ) ;
switch ( alc ) {
case "y" :
install ( ) ;
break ;
case "yes" :
install ( ) ;
break ;
case "n" :
process . exit ( ) ;
break ;
case "no" :
process . exit ( ) ;
break ;
}
} ) ;
} else {
install ( ) ;
}
2015-10-27 19:29:59 +01:00
}
init ( ) ;