2020-05-16 23:24:51 +02:00
const child _process = require ( "child_process" )
const path = require ( "path" )
const terser = require ( "terser" )
const util = require ( "util" )
var rimraf = require ( "rimraf" ) ;
let electron
try {
electron = require ( "electron" )
} catch ( e ) {
electron = null
}
let fs = electron ? require ( "original-fs" ) : require ( "fs" )
console . log = ( ... args ) => {
process . stdout . write ( Buffer . from ( util . formatWithOptions ( { colors : true } , ... args ) + "\n" , "binary" ) . toString ( "utf8" ) )
}
console . info = ( ... args ) => {
console . log ( ` \x 1b[34m[INFO] \x 1b[0m ` , ... args )
}
async function main ( ) {
if ( electron ) await electron . app . whenReady ( )
console . log ( _ _dirname , process . cwd ( ) )
console . info ( "Reseting existent directory..." )
child _process . execSync ( "node remove.js" ) // why can't electron remove directory ? it just doesn't work and I am required to use electron for the .jsc compilation.
await fs . promises . mkdir ( _ _dirname + "/distApp/dist" , { "recursive" : true } )
console . info ( "Executing command `tsc`" )
console . log ( child _process . execSync ( "tsc" , { encoding : "binary" } ) )
let startDir = path . join ( _ _dirname , "./dist" )
let newDir = path . join ( _ _dirname , "./distApp/dist" )
console . info ( "No error detected. Copying files from " + startDir + "." )
await fs . promises . mkdir ( startDir , { recursive : true } )
async function processNextDir ( folder , folders , predicate , compile ) {
for ( let file of fs . readdirSync ( folder , { withFileTypes : true } ) ) {
if ( file . isFile ( ) ) {
let filepath = path . join ( folder , file . name )
if ( predicate ( filepath ) ) {
await compile ( filepath , path . join ( filepath . replace ( folders . startDir , folders . newDir ) ) , ".." )
} else {
await fs . promises . copyFile ( filepath , filepath . replace ( folders . startDir , folders . newDir ) )
}
} else if ( file . isDirectory ( ) ) {
await fs . promises . mkdir ( path . join ( folder , file . name ) . replace ( folders . startDir , folders . newDir ) , { recursive : true } )
await processNextDir ( path . join ( folder , file . name ) , ... Array . from ( arguments ) . slice ( 1 ) )
}
}
}
await processNextDir ( startDir , {
startDir ,
newDir
2020-06-06 02:12:40 +02:00
} , ( ( filepath ) => filepath . endsWith ( ".js" ) && ! filepath . includes ( "node_modules" ) ) , async ( filepath , newpath ) => {
2020-06-06 01:21:03 +02:00
console . info ( ` Minifying ${ filepath } to ${ newpath } ` )
await fs . promises . writeFile ( newpath , terser . minify ( await fs . promises . readFile ( filepath , "utf8" ) ) . code , "utf8" )
2020-05-16 23:24:51 +02:00
} ) . then ( ( ) => {
console . info ( ` Copied files and minified them from ${ startDir } . ` )
} ) . catch ( console . error )
await processNextDir ( path . join ( _ _dirname , "modules" ) , {
startDir : path . join ( _ _dirname , "modules" ) ,
newDir : path . join ( _ _dirname , "distApp" , "modules" )
2020-06-06 02:12:40 +02:00
} , ( ( filepath ) => filepath . endsWith ( ".js" ) && ! filepath . includes ( "node_modules" ) ) , async ( filepath , newpath ) => {
console . info ( ` Minifying ${ filepath } to ${ newpath } ` )
await fs . promises . writeFile ( newpath , terser . minify ( await fs . promises . readFile ( filepath , "utf8" ) ) . code , "utf8" )
} ) . then ( ( ) => {
console . info ( ` Copied files and minified them from ${ path . join ( _ _dirname , "modules" ) } . ` )
} )
fs . mkdirSync ( path . join ( _ _dirname , "distApp" , "BetterDiscordApp" , "js" ) , { recursive : true } )
fs . mkdirSync ( path . join ( _ _dirname , "distApp" , "BetterDiscordApp" , "css" ) , { recursive : true } )
const BDPackageJSON = require ( "./BetterDiscordApp/package.json" )
fs . writeFileSync ( path . join ( _ _dirname , "distApp" , "BetterDiscordApp" , "package.json" ) , JSON . stringify ( BDPackageJSON ) , "utf8" )
fs . writeFileSync ( path . join ( _ _dirname , "distApp" , "BetterDiscordApp" , "css" , "main.css" ) , fs . readFileSync ( path . join ( _ _dirname , "BetterDiscordApp" , "css" , "main.css" ) ) )
fs . writeFileSync ( path . join ( _ _dirname , "distApp" , "BetterDiscordApp" , "css" , "main.min.css" ) , fs . readFileSync ( path . join ( _ _dirname , "BetterDiscordApp" , "css" , "main.min.css" ) ) )
fs . writeFileSync ( path . join ( _ _dirname , "distApp" , "BetterDiscordApp" , "js" , "main.js" ) , fs . readFileSync ( path . join ( _ _dirname , "BetterDiscordApp" , "js" , "main.js" ) ) )
fs . writeFileSync ( path . join ( _ _dirname , "distApp" , "BetterDiscordApp" , "js" , "main.min.js" ) , fs . readFileSync ( path . join ( _ _dirname , "BetterDiscordApp" , "js" , "main.min.js" ) ) )
await fs . promises . mkdir ( path . join ( _ _dirname , "distApp" , "splash" , "videos" ) , { recursive : true } )
await processNextDir ( path . join ( _ _dirname , "splash" ) , {
startDir : path . join ( _ _dirname , "splash" ) ,
newDir : path . join ( _ _dirname , "distApp" , "splash" )
2020-05-16 23:24:51 +02:00
} , ( filepath ) => {
2020-06-06 01:21:03 +02:00
if ( filepath . endsWith ( ".js" ) ) return true
2020-05-16 23:24:51 +02:00
return false
2020-06-06 01:21:03 +02:00
} , async ( filepath , newpath ) => {
console . info ( ` Minifying ${ filepath } to ${ newpath } ` )
await fs . promises . writeFile ( newpath , terser . minify ( await fs . promises . readFile ( filepath , "utf8" ) ) . code , "utf8" )
2020-05-16 23:24:51 +02:00
} ) . then ( ( ) => {
2020-06-06 02:12:40 +02:00
console . info ( ` Copied files and minified them from ${ path . join ( _ _dirname , "splash" ) } . ` )
} )
/ *
2020-05-16 23:24:51 +02:00
await processNextDir ( startDir , {
startDir ,
newDir
} , ( ( filepath ) => false ) , ( ) => { } ) . then ( ( ) => {
console . info ( ` Copied files and minified them from ${ startDir } . ` )
2020-06-06 01:21:03 +02:00
} ) . catch ( console . error ) * /
2020-05-16 23:24:51 +02:00
let packageJSON = require ( "./package.json" )
packageJSON . scripts . build = packageJSON . scripts . build . replace ( "./distApp" , "." )
fs . writeFileSync ( path . join ( _ _dirname , "distApp" , "package.json" ) , JSON . stringify ( packageJSON ) , "utf8" )
console . info ( ` Installing ${ Object . keys ( packageJSON . dependencies ) . length + Object . keys ( packageJSON . devDependencies ) . length } packages... ` )
console . log ( child _process . execSync ( "npm i" , {
encoding : "binary" ,
cwd : path . join ( _ _dirname , "distApp" )
} ) )
}
main ( )
. then ( ( ) => {
if ( electron ) {
electron . app . exit ( )
}
} )