build.js 3.0 KB

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