build.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. process.exit();
  22. }
  23. function build() {
  24. greeting();
  25. del.sync(['dist/electron/*', '!.gitkeep']);
  26. const tasks = ['main', 'renderer'];
  27. const m = new Multispinner(tasks, {
  28. preText: 'building',
  29. postText: 'process'
  30. });
  31. let results = '';
  32. m.on('success', () => {
  33. process.stdout.write('\x1B[2J\x1B[0f');
  34. process.exit();
  35. });
  36. pack(mainConfig)
  37. .then(result => {
  38. results += result + '\n\n';
  39. m.success('main');
  40. })
  41. .catch(err => {
  42. m.error('main');
  43. process.exit(1);
  44. });
  45. pack(rendererConfig)
  46. .then(result => {
  47. results += result + '\n\n';
  48. m.success('renderer');
  49. })
  50. .catch(err => {
  51. m.error('renderer');
  52. process.exit(1);
  53. });
  54. }
  55. function pack(config) {
  56. return new Promise((resolve, reject) => {
  57. webpack(config, (err, stats) => {
  58. if (err) reject(err.stack || err);
  59. else if (stats.hasErrors()) {
  60. let err = '';
  61. stats
  62. .toString({
  63. chunks: false,
  64. colors: true
  65. })
  66. .split(/\r?\n/)
  67. .forEach(line => {
  68. err += ` ${line}\n`;
  69. });
  70. reject(err);
  71. } else {
  72. resolve(
  73. stats.toString({
  74. chunks: false,
  75. colors: true
  76. })
  77. );
  78. }
  79. });
  80. });
  81. }
  82. function web() {
  83. del.sync(['dist/web/*', '!.gitkeep']);
  84. webpack(webConfig, (err, stats) => {
  85. if (err || stats.hasErrors()) console.log(err);
  86. process.exit();
  87. });
  88. }
  89. function greeting() {
  90. const cols = process.stdout.columns;
  91. let text = '';
  92. if (cols > 85) text = 'lets-build';
  93. else if (cols > 60) text = 'lets-|build';
  94. else text = false;
  95. if (text && !isCI) {
  96. say(text, {
  97. colors: ['yellow'],
  98. font: 'simple3d',
  99. space: false
  100. });
  101. }
  102. }