2022-03-29 16:52:53 +08:00
|
|
|
import { build } from 'esbuild'
|
|
|
|
import ora from 'ora'
|
|
|
|
import { builtinModules } from 'module'
|
|
|
|
import electron from 'electron'
|
|
|
|
import { spawn } from 'child_process'
|
|
|
|
import path from 'path'
|
|
|
|
import waitOn from 'wait-on'
|
2022-04-02 00:45:20 +08:00
|
|
|
import dotenv from 'dotenv'
|
2022-03-29 16:52:53 +08:00
|
|
|
import pc from 'picocolors'
|
|
|
|
import minimist from 'minimist'
|
|
|
|
|
2022-04-02 00:45:20 +08:00
|
|
|
const env = dotenv.config({
|
2022-05-12 02:45:43 +08:00
|
|
|
path: path.resolve(process.cwd(), '../../.env'),
|
2022-04-02 00:45:20 +08:00
|
|
|
})
|
|
|
|
const envForEsbuild = {}
|
2022-05-12 02:45:43 +08:00
|
|
|
Object.entries(env.parsed || {}).forEach(([key, value]) => {
|
2022-04-02 00:45:20 +08:00
|
|
|
envForEsbuild[`process.env.${key}`] = `"${value}"`
|
|
|
|
})
|
|
|
|
console.log(envForEsbuild)
|
|
|
|
|
2022-03-29 16:52:53 +08:00
|
|
|
const argv = minimist(process.argv.slice(2))
|
|
|
|
const TAG = '[script/build.main.ts]'
|
|
|
|
const spinner = ora(`${TAG} Main Process Building...`)
|
|
|
|
|
|
|
|
const options = {
|
2022-05-13 19:30:13 +08:00
|
|
|
entryPoints: ['./main/index.ts', './main/rendererPreload.ts'],
|
2022-05-12 02:45:43 +08:00
|
|
|
outdir: './dist',
|
2022-03-29 16:52:53 +08:00
|
|
|
platform: 'node',
|
|
|
|
format: 'cjs',
|
|
|
|
bundle: true,
|
2022-04-02 00:45:20 +08:00
|
|
|
define: envForEsbuild,
|
2022-05-13 19:30:13 +08:00
|
|
|
minify: true,
|
2022-03-29 16:52:53 +08:00
|
|
|
external: [
|
|
|
|
...builtinModules.filter(
|
|
|
|
x => !/^_|^(internal|v8|node-inspect)\/|\//.test(x)
|
|
|
|
),
|
|
|
|
'electron',
|
|
|
|
'NeteaseCloudMusicApi',
|
2022-03-30 00:53:05 +08:00
|
|
|
'better-sqlite3',
|
2022-05-12 02:45:43 +08:00
|
|
|
'@unblockneteasemusic/rust-napi',
|
2022-03-29 16:52:53 +08:00
|
|
|
],
|
|
|
|
}
|
|
|
|
|
|
|
|
const runApp = () => {
|
2022-05-12 02:45:43 +08:00
|
|
|
return spawn(electron, [path.resolve(process.cwd(), './dist/index.js')], {
|
|
|
|
stdio: 'inherit',
|
|
|
|
env: {
|
|
|
|
...process.env,
|
|
|
|
NODE_ENV: 'development',
|
|
|
|
},
|
|
|
|
})
|
2022-03-29 16:52:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (argv.watch) {
|
|
|
|
waitOn(
|
|
|
|
{
|
|
|
|
resources: [
|
|
|
|
`http://127.0.0.1:${process.env.ELECTRON_WEB_SERVER_PORT}/index.html`,
|
|
|
|
],
|
|
|
|
timeout: 5000,
|
|
|
|
},
|
|
|
|
err => {
|
|
|
|
if (err) {
|
|
|
|
console.log(err)
|
|
|
|
process.exit(1)
|
|
|
|
} else {
|
|
|
|
let child
|
|
|
|
build({
|
|
|
|
...options,
|
|
|
|
watch: {
|
|
|
|
onRebuild(error) {
|
|
|
|
if (error) {
|
|
|
|
console.error(pc.red('Rebuild Failed:'), error)
|
|
|
|
} else {
|
|
|
|
console.log(pc.green('Rebuild Succeeded'))
|
|
|
|
if (child) child.kill()
|
|
|
|
child = runApp()
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
2022-04-29 19:16:34 +08:00
|
|
|
sourcemap: true,
|
2022-03-29 16:52:53 +08:00
|
|
|
}).then(() => {
|
|
|
|
console.log(pc.yellow(`⚡ Run App`))
|
|
|
|
if (child) child.kill()
|
|
|
|
child = runApp()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
spinner.start()
|
|
|
|
build({
|
|
|
|
...options,
|
2022-04-02 19:29:43 +08:00
|
|
|
define: {
|
|
|
|
...options.define,
|
|
|
|
'process.env.NODE_ENV': '"production"',
|
|
|
|
},
|
2022-03-29 16:52:53 +08:00
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
console.log(TAG, pc.green('Main Process Build Succeeded.'))
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
console.log(
|
|
|
|
`\n${TAG} ${pc.red('Main Process Build Failed')}\n`,
|
|
|
|
error,
|
|
|
|
'\n'
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.finally(() => {
|
|
|
|
spinner.stop()
|
|
|
|
})
|
|
|
|
}
|