electron-vite-vue/scripts/build-main.ts

56 lines
1.6 KiB
TypeScript
Raw Normal View History

2021-09-09 09:35:21 +08:00
import path from 'path'
import { watch, rollup, OutputOptions } from 'rollup'
2020-12-24 10:17:27 +08:00
import { spawn, ChildProcess } from 'child_process'
2021-09-09 09:35:21 +08:00
import electron from 'electron'
2020-12-24 10:17:27 +08:00
import minimist from 'minimist'
import chalk from 'chalk'
import ora from 'ora'
import { waitOn } from './utils'
2021-09-09 09:35:21 +08:00
import options from './rollup.config'
import { main, env } from '../package.json'
2020-12-24 10:17:27 +08:00
const argv = minimist(process.argv.slice(2))
2021-09-09 09:35:21 +08:00
const opt = options({ proc: 'main', env: argv.env })
const TAG = '[build-main.ts]'
2021-09-09 09:35:21 +08:00
const spinner = ora(`${TAG} Electron main build...`)
; (async () => {
if (argv.watch) {
// Wait on vite server launched
const waitOnState = waitOn({ port: env.PORT })
2020-12-24 10:17:27 +08:00
2021-09-09 09:35:21 +08:00
const watcher = watch(opt)
2020-12-24 10:17:27 +08:00
let child: ChildProcess
watcher.on('change', filename => {
const log = chalk.green(`change -- ${filename}`)
console.log(TAG, log)
})
2021-10-09 16:14:19 +08:00
watcher.on('event', async ev => {
await waitOnState
2020-12-24 10:17:27 +08:00
if (ev.code === 'END') {
if (child) child.kill()
2021-07-10 08:51:15 +08:00
child = spawn(
2021-09-09 09:35:21 +08:00
electron as unknown as string,
[path.join(__dirname, `../${main}`)],
{ env: Object.assign(process.env, env), stdio: 'inherit' },
2021-09-09 09:35:21 +08:00
)
2020-12-24 10:17:27 +08:00
} else if (ev.code === 'ERROR') {
console.log(ev.error)
}
})
2021-09-09 09:35:21 +08:00
} else {
spinner.start()
try {
const build = await rollup(opt)
await build.write(opt.output as OutputOptions)
spinner.succeed()
process.exit()
} catch (error) {
2020-12-24 10:17:27 +08:00
console.log(`\n${TAG} ${chalk.red('构建报错')}\n`, error, '\n')
2021-09-09 09:35:21 +08:00
spinner.fail()
process.exit(1)
}
2021-08-05 09:25:34 +08:00
}
2021-09-09 09:35:21 +08:00
})();