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