|
| 1 | +'use strict' |
| 2 | + |
| 3 | +process.env.NODE_ENV = 'production' |
| 4 | + |
| 5 | +const { say } = require('cfonts') |
| 6 | +const chalk = require('chalk') |
| 7 | +const del = require('del') |
| 8 | +const { spawn } = require('child_process') |
| 9 | +const webpack = require('webpack') |
| 10 | +const Multispinner = require('multispinner') |
| 11 | + |
| 12 | + |
| 13 | +const mainConfig = require('./webpack.main.config') |
| 14 | +const rendererConfig = require('./webpack.renderer.config') |
| 15 | +const webConfig = require('./webpack.web.config') |
| 16 | + |
| 17 | +const doneLog = chalk.bgGreen.white(' DONE ') + ' ' |
| 18 | +const errorLog = chalk.bgRed.white(' ERROR ') + ' ' |
| 19 | +const okayLog = chalk.bgBlue.white(' OKAY ') + ' ' |
| 20 | +const isCI = process.env.CI || false |
| 21 | + |
| 22 | +if (process.env.BUILD_TARGET === 'clean') clean() |
| 23 | +else if (process.env.BUILD_TARGET === 'web') web() |
| 24 | +else build() |
| 25 | + |
| 26 | +function clean () { |
| 27 | + del.sync(['dist/*', 'build/cgfs', 'build/cgapi-consumer']) |
| 28 | + console.log(`\n${doneLog}\n`) |
| 29 | + process.exit() |
| 30 | +} |
| 31 | + |
| 32 | +function build () { |
| 33 | + greeting() |
| 34 | + |
| 35 | + del.sync(['dist/electron/*', '!.gitkeep']) |
| 36 | + |
| 37 | + const tasks = ['main', 'renderer'] |
| 38 | + const m = new Multispinner(tasks, { |
| 39 | + preText: 'building', |
| 40 | + postText: 'process' |
| 41 | + }) |
| 42 | + |
| 43 | + let results = '' |
| 44 | + |
| 45 | + m.on('success', () => { |
| 46 | + process.stdout.write('\x1B[2J\x1B[0f') |
| 47 | + console.log(`\n\n${results}`) |
| 48 | + console.log(`${okayLog}take it away ${chalk.yellow('`electron-builder`')}\n`) |
| 49 | + process.exit() |
| 50 | + }) |
| 51 | + |
| 52 | + pack(mainConfig).then(result => { |
| 53 | + results += result + '\n\n' |
| 54 | + m.success('main') |
| 55 | + }).catch(err => { |
| 56 | + m.error('main') |
| 57 | + console.log(`\n ${errorLog}failed to build main process`) |
| 58 | + console.error(`\n${err}\n`) |
| 59 | + process.exit(1) |
| 60 | + }) |
| 61 | + |
| 62 | + pack(rendererConfig).then(result => { |
| 63 | + results += result + '\n\n' |
| 64 | + m.success('renderer') |
| 65 | + }).catch(err => { |
| 66 | + m.error('renderer') |
| 67 | + console.log(`\n ${errorLog}failed to build renderer process`) |
| 68 | + console.error(`\n${err}\n`) |
| 69 | + process.exit(1) |
| 70 | + }) |
| 71 | +} |
| 72 | + |
| 73 | +function pack (config) { |
| 74 | + return new Promise((resolve, reject) => { |
| 75 | + config.mode = 'production' |
| 76 | + webpack(config, (err, stats) => { |
| 77 | + if (err) reject(err.stack || err) |
| 78 | + else if (stats.hasErrors()) { |
| 79 | + let err = '' |
| 80 | + |
| 81 | + stats.toString({ |
| 82 | + chunks: false, |
| 83 | + colors: true |
| 84 | + }) |
| 85 | + .split(/\r?\n/) |
| 86 | + .forEach(line => { |
| 87 | + err += ` ${line}\n` |
| 88 | + }) |
| 89 | + |
| 90 | + reject(err) |
| 91 | + } else { |
| 92 | + resolve(stats.toString({ |
| 93 | + chunks: false, |
| 94 | + colors: true |
| 95 | + })) |
| 96 | + } |
| 97 | + }) |
| 98 | + }) |
| 99 | +} |
| 100 | + |
| 101 | +function web () { |
| 102 | + del.sync(['dist/web/*', '!.gitkeep']) |
| 103 | + webConfig.mode = 'production' |
| 104 | + webpack(webConfig, (err, stats) => { |
| 105 | + if (err || stats.hasErrors()) console.log(err) |
| 106 | + |
| 107 | + console.log(stats.toString({ |
| 108 | + chunks: false, |
| 109 | + colors: true |
| 110 | + })) |
| 111 | + |
| 112 | + process.exit() |
| 113 | + }) |
| 114 | +} |
| 115 | + |
| 116 | +function greeting () { |
| 117 | + const cols = process.stdout.columns |
| 118 | + let text = '' |
| 119 | + |
| 120 | + if (cols > 85) text = 'lets-build' |
| 121 | + else if (cols > 60) text = 'lets-|build' |
| 122 | + else text = false |
| 123 | + |
| 124 | + if (text && !isCI) { |
| 125 | + say(text, { |
| 126 | + colors: ['yellow'], |
| 127 | + font: 'simple3d', |
| 128 | + space: false |
| 129 | + }) |
| 130 | + } else console.log(chalk.yellow.bold('\n lets-build')) |
| 131 | + console.log() |
| 132 | +} |
0 commit comments