build.js 3.5 KB

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