refactor install and build scripts

Change devInstall to use a script instead of a one-liner

Move all scripts to "scripts" directory.
This commit is contained in:
hormelcookies 2020-10-20 13:28:34 -07:00
parent d0b3b73a85
commit 0816b1ef12
12 changed files with 133 additions and 80 deletions

4
.gitignore vendored
View File

@ -125,3 +125,7 @@ discord-webrtc_0
LightcordApi/js
DiscordJS/js
BetterDiscordApp/dist
downloaded_modules
workspace_modules
new_modules_version.json

View File

@ -30,7 +30,7 @@ To run from source, follow these instructions:
```sh
git clone https://github.com/Lightcord/Lightcord
cd Lightcord
npm run devInstall # Mac/Linux, run "npm run devInstall:64"
npm run devInstall
npm test
```
*You will have to do that everytime you pull/clone*

View File

@ -6,7 +6,7 @@ directories:
app: distApp
output: builds
# This is to get around the bug in electron-builder with not including nested node_modules.
afterPack: "afterpack.js"
afterPack: "scripts/afterpack.js"
win:
artifactName: ${name}-win32-${arch}.${ext}
target:

View File

@ -4,22 +4,21 @@
"description": "A simple - customizable - Discord Client.",
"main": "dist/index.js",
"scripts": {
"compile": "node compile.js",
"compile": "node scripts/compile.js",
"test": "npm run compile && electron .",
"run": "electron .",
"build": "npm run build:minify && npm run build:electron",
"build:electron": "node build_electron.js",
"build:electron": "node scripts/build_electron.js",
"build:electron_win": "electron-builder build --win",
"build:electron_linux": "electron-builder build --linux",
"build:electron_mac": "electron-builder build --mac",
"start": "electron .",
"build:minify": "node build.js",
"build:minify": "node scripts/build.js",
"clean": "node scripts/clean.js",
"clean:all": "node scripts/clean.js --all",
"devInstall": "npm i --save-dev --arch=ia32 electron@9.3.1 && node installSubModules.js && echo \"Everything is installed. You should be able to do npm test to compile everything and launch.\"",
"devInstall:64": "npm i --save-dev --arch=x64 electron@9.3.1 && node installSubModules.js && echo \"Everything is installed. You should be able to do npm test to compile everything and launch.\"",
"devInstall": "node scripts/devInstall.js",
"versions": "echo Electron: && electron -v && echo Typescript: && tsc -v",
"ci": "node ci-deploy"
"ci": "node scripts/ci-deploy"
},
"author": "JeanOUINA",
"license": "MIT",
@ -46,7 +45,8 @@
"@types/yauzl": "^2.9.1",
"asar": "^3.0.3",
"cross-spawn": "^7.0.3",
"electron": "9.3.1",
"dotenv": "^8.2.0",
"electron": "^9.3.1",
"electron-builder": "^22.8.0",
"fast-glob": "^3.2.4",
"fs-extra": "^9.0.1",

View File

@ -12,6 +12,7 @@ exports.default = async function beforeBuild(context){
await main()
return true
}
const PROJECT_DIR = path.resolve(__dirname, "..");
console.log = (...args) => {
process.stdout.write(Buffer.from(util.formatWithOptions({colors: true}, ...args)+"\n", "binary").toString("utf8"))
@ -109,7 +110,7 @@ async function main(){
console.info("Reseting existent directory...")
await fs.promises.rmdir("./distApp", {"recursive": true})
await fs.promises.mkdir(__dirname+"/distApp/dist", {"recursive": true})
await fs.promises.mkdir(PROJECT_DIR+"/distApp/dist", {"recursive": true})
console.info("Executing command `npm run compile`")
child_process.execSync("npm run compile", {
@ -117,8 +118,8 @@ async function main(){
stdio: "inherit"
})
let startDir = path.join(__dirname, "./dist")
let newDir = path.join(__dirname, "./distApp/dist")
let startDir = path.join(PROJECT_DIR, "./dist")
let newDir = path.join(PROJECT_DIR, "./distApp/dist")
console.info("No error detected. Copying files from "+startDir+".")
await fs.promises.mkdir(startDir, {recursive: true})
@ -137,19 +138,19 @@ async function main(){
console.info(`Copied files and minified them from ${startDir}.`)
})
await processNextDir(path.join(__dirname, "modules"), {
startDir: path.join(__dirname, "modules"),
newDir: path.join(__dirname, "distApp", "modules"),
await processNextDir(path.join(PROJECT_DIR, "modules"), {
startDir: path.join(PROJECT_DIR, "modules"),
newDir: path.join(PROJECT_DIR, "distApp", "modules"),
exclude: /discord_spellcheck/g
}, ((filepath) => filepath.endsWith(".js")), async (filepath, newpath) => {
console.info(`Minifying ${filepath} to ${newpath}`)
await fs.promises.writeFile(newpath, terser.minify(await fs.promises.readFile(filepath, "utf8")).code, "utf8")
}, true).then(() => {
console.info(`Copied files and minified them from ${path.join(__dirname, "modules")}.`)
console.info(`Copied files and minified them from ${path.join(PROJECT_DIR, "modules")}.`)
})
await Promise.all((await fs.promises.readdir(path.join(__dirname, "distApp", "modules"))).map(async mdl => {
let dir = path.join(__dirname, "distApp", "modules", mdl)
await Promise.all((await fs.promises.readdir(path.join(PROJECT_DIR, "distApp", "modules"))).map(async mdl => {
let dir = path.join(PROJECT_DIR, "distApp", "modules", mdl)
if(!fs.existsSync(path.join(dir, "package.json"))){
if(mdl === "discord_desktop_core"){
@ -167,48 +168,48 @@ async function main(){
})
}))
await fs.promises.mkdir(path.join(__dirname, "distApp", "modules", "discord_spellcheck"), {recursive: true})
await processNextDir(path.join(__dirname, "modules", "discord_spellcheck"), {
startDir: path.join(__dirname, "modules", "discord_spellcheck"),
newDir: path.join(__dirname, "distApp", "modules", "discord_spellcheck")
await fs.promises.mkdir(path.join(PROJECT_DIR, "distApp", "modules", "discord_spellcheck"), {recursive: true})
await processNextDir(path.join(PROJECT_DIR, "modules", "discord_spellcheck"), {
startDir: path.join(PROJECT_DIR, "modules", "discord_spellcheck"),
newDir: path.join(PROJECT_DIR, "distApp", "modules", "discord_spellcheck")
}, ((filepath) => filepath.endsWith(".js")), async (filepath, newpath) => {
console.info(`Minifying ${filepath} to ${newpath}`)
await fs.promises.writeFile(newpath, terser.minify(await fs.promises.readFile(filepath, "utf8")).code, "utf8")
}, false).then(() => {
console.info(`Copied files and minified them from ${path.join(__dirname, "modules")}.`)
console.info(`Copied files and minified them from ${path.join(PROJECT_DIR, "modules")}.`)
})
await processNextDir(path.join(__dirname, "LightcordApi"), {
startDir: path.join(__dirname, "LightcordApi"),
newDir: path.join(__dirname, "distApp", "LightcordApi"),
await processNextDir(path.join(PROJECT_DIR, "LightcordApi"), {
startDir: path.join(PROJECT_DIR, "LightcordApi"),
newDir: path.join(PROJECT_DIR, "distApp", "LightcordApi"),
exclude: /(src|webpack\.config\.js|tsconfig\.json|dist|docs)/g
}, ((filepath) => filepath.endsWith(".js") && (!production ? !filepath.includes("node_modules") : true)), async (filepath, newpath) => {
await fs.promises.copyFile(filepath, newpath)
}, true).then(() => {
console.info(`Copied files and minified them from ${path.join(__dirname, "LightcordApi")}.`)
console.info(`Copied files and minified them from ${path.join(PROJECT_DIR, "LightcordApi")}.`)
})
child_process.execSync("npm install --only=prod", {
encoding: "binary",
cwd: path.join(__dirname, "distApp", "LightcordApi"),
cwd: path.join(PROJECT_DIR, "distApp", "LightcordApi"),
stdio: "inherit"
})
function processDJS(dir){
fs.mkdirSync(path.join(__dirname, "distApp", "DiscordJS", dir), {recursive: true})
return processNextDir(path.join(__dirname, "DiscordJS", dir), {
startDir: path.join(__dirname, "DiscordJS", dir),
newDir: path.join(__dirname, "distApp", "DiscordJS", dir),
fs.mkdirSync(path.join(PROJECT_DIR, "distApp", "DiscordJS", dir), {recursive: true})
return processNextDir(path.join(PROJECT_DIR, "DiscordJS", dir), {
startDir: path.join(PROJECT_DIR, "DiscordJS", dir),
newDir: path.join(PROJECT_DIR, "distApp", "DiscordJS", dir),
exclude: /node_modules/g
}, ((filepath) => filepath.endsWith(".js")), 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, "DiscordJS", dir)}.`)
console.info(`Copied files and minified them from ${path.join(PROJECT_DIR, "DiscordJS", dir)}.`)
})
}
async function copyFileDJS(file){
await fs.promises.writeFile(path.join(__dirname, "distApp", "DiscordJS", file), await fs.promises.readFile(path.join(__dirname, "DiscordJS", file)))
await fs.promises.writeFile(path.join(PROJECT_DIR, "distApp", "DiscordJS", file), await fs.promises.readFile(path.join(PROJECT_DIR, "DiscordJS", file)))
}
await processDJS("dist")
@ -216,13 +217,13 @@ async function main(){
child_process.execSync("npm install --only=prod", {
encoding: "binary",
cwd: path.join(__dirname, "distApp", "DiscordJS"),
cwd: path.join(PROJECT_DIR, "distApp", "DiscordJS"),
stdio: "inherit"
})
fs.mkdirSync(path.join(__dirname, "distApp", "BetterDiscordApp", "dist"), {recursive: true})
const BDPackageJSON = require("./BetterDiscordApp/package.json")
fs.writeFileSync(path.join(__dirname, "distApp", "BetterDiscordApp", "package.json"), JSON.stringify(BDPackageJSON), "utf8")
fs.mkdirSync(path.join(PROJECT_DIR, "distApp", "BetterDiscordApp", "dist"), {recursive: true})
const BDPackageJSON = require("../BetterDiscordApp/package.json")
fs.writeFileSync(path.join(PROJECT_DIR, "distApp", "BetterDiscordApp", "package.json"), JSON.stringify(BDPackageJSON), "utf8")
const files = [
"index.min.js",
"style.min.css"
@ -231,21 +232,21 @@ async function main(){
files.push(e + ".map")
})
files.forEach(e => {
const pth = path.join(__dirname, "BetterDiscordApp", "dist", e)
const pth = path.join(PROJECT_DIR, "BetterDiscordApp", "dist", e)
if(!fs.existsSync(pth))return console.error(`\x1b[31mFile ${pth} from betterdiscord does not exist.\x1b[0m`)
if(e.endsWith(".map")){
const data = JSON.parse(fs.readFileSync(pth, "utf8"))
data.sourcesContent = []
fs.writeFileSync(path.join(__dirname, "distApp", "BetterDiscordApp", "dist", e), JSON.stringify(data))
fs.writeFileSync(path.join(PROJECT_DIR, "distApp", "BetterDiscordApp", "dist", e), JSON.stringify(data))
}else{
fs.copyFileSync(pth, path.join(__dirname, "distApp", "BetterDiscordApp", "dist", e))
fs.copyFileSync(pth, path.join(PROJECT_DIR, "distApp", "BetterDiscordApp", "dist", e))
}
})
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"),
await fs.promises.mkdir(path.join(PROJECT_DIR, "distApp", "splash", "videos"), {recursive: true})
await processNextDir(path.join(PROJECT_DIR, "splash"), {
startDir: path.join(PROJECT_DIR, "splash"),
newDir: path.join(PROJECT_DIR, "distApp", "splash"),
exclude: /node_modules/g
}, (filepath) => {
if(filepath.endsWith(".js"))return true
@ -254,21 +255,21 @@ async function main(){
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, "splash")}.`)
console.info(`Copied files and minified them from ${path.join(PROJECT_DIR, "splash")}.`)
})
fs.writeFileSync(path.join(__dirname, "distApp", "LICENSE"), fs.readFileSync(path.join(__dirname, "LICENSE")))
fs.writeFileSync(path.join(PROJECT_DIR, "distApp", "LICENSE"), fs.readFileSync(path.join(PROJECT_DIR, "LICENSE")))
let packageJSON = require("./package.json")
let packageJSON = require("../package.json")
packageJSON.scripts["build:electron_linux"] = packageJSON.scripts["build:electron_linux"].replace("./distApp", ".")
packageJSON.scripts["build:electron_win"] = packageJSON.scripts["build:electron_win"].replace("./distApp", ".")
packageJSON.scripts["build:electron_mac"] = packageJSON.scripts["build:electron_mac"].replace("./distApp", ".")
fs.writeFileSync(path.join(__dirname, "distApp", "package.json"), JSON.stringify(packageJSON), "utf8")
fs.writeFileSync(path.join(PROJECT_DIR, "distApp", "package.json"), JSON.stringify(packageJSON), "utf8")
console.info(`Installing ${Object.keys(packageJSON.dependencies).length} packages...`)
child_process.execSync("npm install --only=prod", {
encoding: "binary",
cwd: path.join(__dirname, "distApp"),
cwd: path.join(PROJECT_DIR, "distApp"),
stdio: "inherit"
})
console.info("Build took "+(Date.now() - startTimestamp) +"ms.")

View File

@ -3,6 +3,8 @@ const path = require("path")
const { existsSync, promises: fsPromises, createWriteStream } = require("fs")
const yazl = require("yazl")
const PROJECT_DIR = path.resolve(__dirname, "..");
const supportedPlatforms = []
const Platforms = {
linux: {
@ -27,8 +29,8 @@ const Platforms = {
name: "mac",
experimental: true,
run: async () => {
const basePath = path.join(__dirname, "..", "lightcord-darwin-x64")
const nextPath = path.join(__dirname, "builds", "lightcord-darwin-x64")
const basePath = path.join(PROJECT_DIR, "..", "lightcord-darwin-x64")
const nextPath = path.join(PROJECT_DIR, "builds", "lightcord-darwin-x64")
if(existsSync(nextPath)){
console.log(`Cleaning ${nextPath}.`)
await fsPromises.rmdir(nextPath, {recursive: true})
@ -54,11 +56,11 @@ const Platforms = {
const asarUnpackPath = path.join(nextPath, "lightcord.app", "Contents", "Resources", "app.asar.unpacked")
if(existsSync(asarUnpackPath))await fsPromises.rmdir(asarUnpackPath, {recursive: true})
const asar = require("asar")
await asar.createPackageWithOptions(path.join(__dirname, "distApp"), asarPath, {
await asar.createPackageWithOptions(path.join(PROJECT_DIR, "distApp"), asarPath, {
unpack: "*.{node,dylib,so.4,dll}",
unpackDir: asarUnpackPath
})
const iconPath = path.join(__dirname, "discord.icns")
const iconPath = path.join(PROJECT_DIR, "discord.icns")
if(existsSync(iconPath)){
console.log(`Setting icon.`)
const newIconPath = path.join(nextPath, "lightcord.app", "Contents", "Resources", "electron.icns")
@ -66,11 +68,11 @@ const Platforms = {
}
console.log("zipping")
const zip = new yazl.ZipFile();
zip.outputStream.pipe(createWriteStream(path.join(__dirname, "builds", "lightcord-darwin-x64.zip")))
zip.outputStream.pipe(createWriteStream(path.join(PROJECT_DIR, "builds", "lightcord-darwin-x64.zip")))
.on("close", function() {
console.log("Finished zipping.");
});
const startDir = path.join(__dirname, "builds", "lightcord-darwin-x64")
const startDir = path.join(PROJECT_DIR, "builds", "lightcord-darwin-x64")
async function nextDir2(dir){
for(let file of await fsPromises.readdir(dir, {withFileTypes: true})){
if(file.isDirectory()){
@ -90,13 +92,13 @@ switch(process.platform){
case "win32":
supportedPlatforms.push(Platforms.win)
supportedPlatforms.push(Platforms.linux)
if(existsSync(path.join(__dirname, "..", "lightcord-darwin-x64"))){
if(existsSync(path.join(PROJECT_DIR, "..", "lightcord-darwin-x64"))){
supportedPlatforms.push(Platforms.mac_experimental)
}
break
case "linux":
supportedPlatforms.push(Platforms.linux)
if(existsSync(path.join(__dirname, "..", "lightcord-darwin-x64"))){
if(existsSync(path.join(PROJECT_DIR, "..", "lightcord-darwin-x64"))){
supportedPlatforms.push(Platforms.mac_experimental)
}
break

View File

@ -1,4 +1,4 @@
const package = require("./package.json")
const package = require("../package.json")
const child_process = require("child_process")
const version = package.version+"-"+Date.now()+ "-" + child_process.execSync("git rev-parse HEAD").toString().split("\n")[0].trim().slice(0, 7)

View File

@ -1,22 +1,24 @@
const spawn = require("cross-spawn")
const { mkdirSync, existsSync } = require("fs")
const { join } = require("path")
const { join, resolve } = require("path")
const PROJECT_DIR = resolve(__dirname, "..");
/** Main Project */
spawnSync("tsc")
if (!existsSync(join(__dirname, "BetterDiscordApp", "dist"))){
mkdirSync(join(__dirname, "BetterDiscordApp", "dist"))
}
spawnSync("tsc", PROJECT_DIR)
/** BetterDiscord */
spawnSync("npm run build", join(__dirname, "BetterDiscordApp"))
spawnSync("npm run build-prod", join(__dirname, "BetterDiscordApp"))
spawnSync("npm run minify-css", join(__dirname, "BetterDiscordApp"))
if (!existsSync(join(PROJECT_DIR, "BetterDiscordApp", "dist"))){
mkdirSync(join(PROJECT_DIR, "BetterDiscordApp", "dist"))
}
spawnSync("npm run build", join(PROJECT_DIR, "BetterDiscordApp"))
spawnSync("npm run build-prod", join(PROJECT_DIR, "BetterDiscordApp"))
spawnSync("npm run minify-css", join(PROJECT_DIR, "BetterDiscordApp"))
/** DiscordJS */
spawnSync("npm run build", join(__dirname, "DiscordJS"))
spawnSync("npm run build", join(PROJECT_DIR, "DiscordJS"))
/** LightcordApi */
spawnSync("npm run build", join(__dirname, "LightcordApi"))
spawnSync("npm run build-prod", join(__dirname, "LightcordApi"))
spawnSync("tsc", join(__dirname, "LightcordApi"))
spawnSync("npm run build", join(PROJECT_DIR, "LightcordApi"))
spawnSync("npm run build-prod", join(PROJECT_DIR, "LightcordApi"))
spawnSync("tsc", join(PROJECT_DIR, "LightcordApi"))
var exitedWithErrorProcessList = [];
@ -28,10 +30,10 @@ function spawnSync(cmdString, cwd){
env: process.env,
stdio: "inherit"
}).on("error", (err) => {
const DIRSTRING = cwd != undefined ? cwd : process.env.PWD
console.log("Error while running " + cmdString + " in target directory " + DIRSTRING)
const DIRSTRING = cwd || process.env.PWD
console.error("Error while running " + cmdString + " in target directory " + DIRSTRING)
console.error(err)
exitedWithErrorProcessList.push({cmd: cmdString, cwd: DIRSTRING})
exitedWithErrorProcessList.push({cmd: cmdString, cwd: DIRSTRING, err: err})
})
}
@ -40,6 +42,7 @@ process.on("beforeExit", () => {
console.error("Commands exited with errors:\n")
exitedWithErrorProcessList.forEach((val)=>{
console.error("\tcommand:\t\t" + val.cmd + "\n\ttarget directory:\t" + val.cwd + "\n")
console.error(val.err)
});
}
console.log(`Exiting compilation`)

38
scripts/devInstall.js Normal file
View File

@ -0,0 +1,38 @@
//require('dotenv').config()
const { join } = require("path")
const PROJECT_DIRNAME=join(__dirname, "..")
const child_process = require("child_process")
let env = process.env
function spawnProcess(cmd, ...args) {
const postfix = cmd === "node" ? ".exe" : ".cmd"
cmd = process.platform === "win32" ? cmd + postfix : cmd
let result = child_process.spawnSync(cmd, args, {
cwd: PROJECT_DIRNAME,
env: env,
stdio: "inherit"
})
if (result.error){
console.error("error!")
console.error(result)
console.error(result.stderr)
process.exit(1)
}
}
let result;
spawnProcess("npm", "rm", "electron")
spawnProcess("npm", "i")
if (process.platform === "win32"){
result = spawnProcess("npm", "i", "--save-dev", "--arch=ia32", "electron@9.3.1")
} else {
result = spawnProcess("npm", "i", "--save-dev", "--arch=x64", "electron@9.3.1")
}
let runString = "`npm test`"
spawnProcess("node", join(PROJECT_DIRNAME,"scripts", "installSubModules.js"))
console.log("Everything is installed. You should be able to do "+runString+" to compile everything and launch.")

View File

@ -4,8 +4,11 @@ const tmp = require("tmp")
const fs = require("fs")
const util = require('util')
const { join, dirname } = require("path")
const { resolve: resolvePath } = require("path")
const streamPipeline = util.promisify(require('stream').pipeline)
const PROJECT_DIR = resolvePath(__dirname, "..");
const API_URL = "https://discord.com/api"
const branch = "stable"
const platforms = ["win", "linux", "osx"]
@ -49,7 +52,7 @@ const patchedJS = []
resolve(zip)
})
})
const modulePath = join(__dirname, "modules", module)
const modulePath = join(PROJECT_DIR, "modules", module)
const exists = fs.existsSync(modulePath)
let hasNode = false
let hasBinaries = ["discord_hook", "discord_modules"].includes(module)

View File

@ -2,7 +2,9 @@ const child_process = require("child_process")
const fs = require("fs")
const path = require("path")
const MODULES_DIRNAME = path.join(__dirname, "modules")
const PROJECT_DIR = path.resolve(__dirname, "..");
const MODULES_DIRNAME = path.join(PROJECT_DIR, "modules")
var exitedWithErrorProcessList = [];
@ -30,10 +32,10 @@ fs.readdirSync(MODULES_DIRNAME, {withFileTypes: true})
})
const MODULE_DIRNAME = path.join(__dirname, "modules", "discord_desktop_core", "core")
const BETTERDISCORD_DIRNAME = path.join(__dirname, "BetterDiscordApp")
const DISCORDJS_DIRNAME = path.join(__dirname, "DiscordJS")
const LIGHTCORDAPI_DIRNAME = path.join(__dirname, "LightcordApi")
const MODULE_DIRNAME = path.join(PROJECT_DIR, "modules", "discord_desktop_core", "core")
const BETTERDISCORD_DIRNAME = path.join(PROJECT_DIR, "BetterDiscordApp")
const DISCORDJS_DIRNAME = path.join(PROJECT_DIR, "DiscordJS")
const LIGHTCORDAPI_DIRNAME = path.join(PROJECT_DIR, "LightcordApi")
spawnNpmInstallProcess(MODULE_DIRNAME)
spawnNpmInstallProcess(BETTERDISCORD_DIRNAME)