2018-03-07 14:28:48 +01:00
|
|
|
'use strict'
|
|
|
|
|
|
|
|
const chalk = require('chalk')
|
|
|
|
const electron = require('electron')
|
|
|
|
const path = require('path')
|
|
|
|
const { say } = require('cfonts')
|
|
|
|
const { spawn } = require('child_process')
|
|
|
|
const webpack = require('webpack')
|
|
|
|
const WebpackDevServer = require('webpack-dev-server')
|
|
|
|
const webpackHotMiddleware = require('webpack-hot-middleware')
|
|
|
|
|
|
|
|
const mainConfig = require('./webpack.main.config')
|
|
|
|
const rendererConfig = require('./webpack.renderer.config')
|
|
|
|
|
|
|
|
let electronProcess = null
|
|
|
|
let manualRestart = false
|
|
|
|
let hotMiddleware
|
|
|
|
|
2020-05-23 14:57:08 +02:00
|
|
|
function logStats(proc, data) {
|
2018-03-07 14:28:48 +01:00
|
|
|
let log = ''
|
|
|
|
|
2020-05-23 14:57:08 +02:00
|
|
|
log += chalk.yellow.bold(`┏ ${proc} Process ${new Array(19 - proc.length + 1).join('-')}`)
|
2018-03-07 14:28:48 +01:00
|
|
|
log += '\n\n'
|
|
|
|
|
|
|
|
if (typeof data === 'object') {
|
2020-05-23 14:57:08 +02:00
|
|
|
data
|
|
|
|
.toString({
|
|
|
|
colors: true,
|
|
|
|
chunks: false
|
|
|
|
})
|
|
|
|
.split(/\r?\n/)
|
|
|
|
.forEach(line => {
|
|
|
|
log += ' ' + line + '\n'
|
|
|
|
})
|
2018-03-07 14:28:48 +01:00
|
|
|
} else {
|
|
|
|
log += ` ${data}\n`
|
|
|
|
}
|
|
|
|
|
|
|
|
log += '\n' + chalk.yellow.bold(`┗ ${new Array(28 + 1).join('-')}`) + '\n'
|
|
|
|
|
|
|
|
console.log(log)
|
|
|
|
}
|
|
|
|
|
2020-05-23 14:57:08 +02:00
|
|
|
function startRenderer() {
|
2018-03-07 14:28:48 +01:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
rendererConfig.entry.renderer = [path.join(__dirname, 'dev-client')].concat(rendererConfig.entry.renderer)
|
2018-11-12 15:54:39 +01:00
|
|
|
rendererConfig.mode = 'development'
|
2018-03-07 14:28:48 +01:00
|
|
|
const compiler = webpack(rendererConfig)
|
2018-11-12 15:54:39 +01:00
|
|
|
hotMiddleware = webpackHotMiddleware(compiler, {
|
|
|
|
log: false,
|
|
|
|
heartbeat: 2500
|
2018-03-07 14:28:48 +01:00
|
|
|
})
|
|
|
|
|
2018-11-12 15:54:39 +01:00
|
|
|
compiler.hooks.compilation.tap('compilation', compilation => {
|
2020-05-23 14:57:08 +02:00
|
|
|
const HtmlWebpackPlugin = require('html-webpack-plugin')
|
|
|
|
HtmlWebpackPlugin.getHooks(compilation).afterEmit.tapAsync('html-webpack-plugin-after-emit', (data, cb) => {
|
2018-03-07 14:28:48 +01:00
|
|
|
hotMiddleware.publish({ action: 'reload' })
|
|
|
|
cb()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2018-11-12 15:54:39 +01:00
|
|
|
compiler.hooks.done.tap('done', stats => {
|
2018-03-07 14:28:48 +01:00
|
|
|
logStats('Renderer', stats)
|
|
|
|
})
|
|
|
|
|
2020-05-23 14:57:08 +02:00
|
|
|
const server = new WebpackDevServer(compiler, {
|
|
|
|
contentBase: path.join(__dirname, '../'),
|
|
|
|
quiet: true,
|
|
|
|
before(app, ctx) {
|
|
|
|
app.use(hotMiddleware)
|
|
|
|
ctx.middleware.waitUntilValid(() => {
|
|
|
|
resolve()
|
|
|
|
})
|
2018-03-07 14:28:48 +01:00
|
|
|
}
|
2020-05-23 14:57:08 +02:00
|
|
|
})
|
2018-03-07 14:28:48 +01:00
|
|
|
|
|
|
|
server.listen(9080)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-05-23 14:57:08 +02:00
|
|
|
function startMain() {
|
2018-03-07 14:28:48 +01:00
|
|
|
return new Promise((resolve, reject) => {
|
2019-04-15 18:35:20 +02:00
|
|
|
mainConfig.entry.main = [path.join(__dirname, '../src/main/index.dev.ts')].concat(mainConfig.entry.main)
|
2018-11-12 15:54:39 +01:00
|
|
|
mainConfig.mode = 'development'
|
2018-03-07 14:28:48 +01:00
|
|
|
const compiler = webpack(mainConfig)
|
|
|
|
|
2018-11-12 15:54:39 +01:00
|
|
|
compiler.hooks.watchRun.tapAsync('watch-run', (compilation, done) => {
|
2018-03-07 14:28:48 +01:00
|
|
|
logStats('Main', chalk.white.bold('compiling...'))
|
|
|
|
hotMiddleware.publish({ action: 'compiling' })
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
|
|
|
|
compiler.watch({}, (err, stats) => {
|
|
|
|
if (err) {
|
|
|
|
console.log(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
logStats('Main', stats)
|
|
|
|
|
|
|
|
if (electronProcess && electronProcess.kill) {
|
|
|
|
manualRestart = true
|
|
|
|
process.kill(electronProcess.pid)
|
|
|
|
electronProcess = null
|
|
|
|
startElectron()
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
manualRestart = false
|
|
|
|
}, 5000)
|
|
|
|
}
|
|
|
|
|
|
|
|
resolve()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-05-23 14:57:08 +02:00
|
|
|
function startElectron() {
|
2018-03-07 14:28:48 +01:00
|
|
|
electronProcess = spawn(electron, ['--inspect=5858', path.join(__dirname, '../dist/electron/main.js')])
|
|
|
|
|
|
|
|
electronProcess.stdout.on('data', data => {
|
|
|
|
electronLog(data, 'blue')
|
|
|
|
})
|
|
|
|
electronProcess.stderr.on('data', data => {
|
|
|
|
electronLog(data, 'red')
|
|
|
|
})
|
|
|
|
|
|
|
|
electronProcess.on('close', () => {
|
2018-11-12 15:54:39 +01:00
|
|
|
if (!manualRestart) process.exit()
|
|
|
|
})
|
|
|
|
}
|
2020-05-23 14:57:08 +02:00
|
|
|
function startElectron() {
|
|
|
|
var args = ['--inspect=5858', path.join(__dirname, '../dist/electron/main.js')]
|
2018-11-12 15:54:39 +01:00
|
|
|
|
|
|
|
// detect yarn or npm and process commandline args accordingly
|
|
|
|
if (process.env.npm_execpath.endsWith('yarn.js')) {
|
|
|
|
args = args.concat(process.argv.slice(3))
|
|
|
|
} else if (process.env.npm_execpath.endsWith('npm-cli.js')) {
|
|
|
|
args = args.concat(process.argv.slice(2))
|
|
|
|
}
|
|
|
|
|
|
|
|
electronProcess = spawn(electron, args)
|
|
|
|
|
|
|
|
electronProcess.stdout.on('data', data => {
|
|
|
|
electronLog(data, 'blue')
|
|
|
|
})
|
|
|
|
electronProcess.stderr.on('data', data => {
|
|
|
|
electronLog(data, 'red')
|
|
|
|
})
|
|
|
|
|
|
|
|
electronProcess.on('close', () => {
|
2018-03-07 14:28:48 +01:00
|
|
|
if (!manualRestart) process.exit()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-05-23 14:57:08 +02:00
|
|
|
function electronLog(data, color) {
|
2018-03-07 14:28:48 +01:00
|
|
|
let log = ''
|
|
|
|
data = data.toString().split(/\r?\n/)
|
|
|
|
data.forEach(line => {
|
|
|
|
log += ` ${line}\n`
|
|
|
|
})
|
|
|
|
if (/[0-9A-z]+/.test(log)) {
|
|
|
|
console.log(
|
2020-05-23 14:57:08 +02:00
|
|
|
chalk[color].bold('┏ Electron -------------------') + '\n\n' + log + chalk[color].bold('┗ ----------------------------') + '\n'
|
2018-03-07 14:28:48 +01:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-23 14:57:08 +02:00
|
|
|
function greeting() {
|
2018-03-07 14:28:48 +01:00
|
|
|
const cols = process.stdout.columns
|
|
|
|
let text = ''
|
|
|
|
|
|
|
|
if (cols > 104) text = 'electron-vue'
|
|
|
|
else if (cols > 76) text = 'electron-|vue'
|
|
|
|
else text = false
|
|
|
|
|
|
|
|
if (text) {
|
|
|
|
say(text, {
|
|
|
|
colors: ['yellow'],
|
|
|
|
font: 'simple3d',
|
|
|
|
space: false
|
|
|
|
})
|
|
|
|
} else console.log(chalk.yellow.bold('\n electron-vue'))
|
|
|
|
console.log(chalk.blue(' getting ready...') + '\n')
|
|
|
|
}
|
|
|
|
|
2020-05-23 14:57:08 +02:00
|
|
|
function init() {
|
2018-03-07 14:28:48 +01:00
|
|
|
greeting()
|
|
|
|
|
|
|
|
Promise.all([startRenderer(), startMain()])
|
|
|
|
.then(() => {
|
|
|
|
startElectron()
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
console.error(err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
init()
|