1
- const fs = require ( "fs" ) ;
2
- const path = require ( " path" ) ;
3
- const ora = require ( " ora" ) ;
4
- const copy = require ( " copy" ) ;
5
- const chalk = require ( " chalk" ) ;
6
- const webpack = require ( " webpack" ) ;
7
- const chokidar = require ( " chokidar" ) ;
8
- const bozon = require ( " ../utils/bozon" ) ;
9
- const WebpackConfig = require ( " ./webpack" ) ;
1
+ const fs = require ( 'fs' )
2
+ const path = require ( ' path' )
3
+ const ora = require ( ' ora' )
4
+ const copy = require ( ' copy' )
5
+ const chalk = require ( ' chalk' )
6
+ const webpack = require ( ' webpack' )
7
+ const chokidar = require ( ' chokidar' )
8
+ const bozon = require ( ' ../utils/bozon' )
9
+ const WebpackConfig = require ( ' ./webpack' )
10
10
11
11
class Builder {
12
12
constructor ( platform , environment ) {
13
- this . platform = platform ;
14
- this . environment = environment ;
15
- this . config = new WebpackConfig ( this . environment , this . platform ) . build ( ) ;
13
+ this . platform = platform
14
+ this . environment = environment
15
+ this . config = new WebpackConfig ( this . environment , this . platform ) . build ( )
16
16
this . spinner = ora ( {
17
- text : chalk . cyan ( " Building Electron application" ) ,
18
- color : " cyan"
19
- } ) ;
17
+ text : chalk . cyan ( ' Building Electron application' ) ,
18
+ color : ' cyan'
19
+ } )
20
20
}
21
21
22
22
run ( ) {
23
- this . spinner . start ( ) ;
23
+ this . spinner . start ( )
24
24
return Promise . all ( this . buildQueue ( ) )
25
25
. then ( ( ) => {
26
- this . manifest ( ) ;
27
- this . spinner . succeed ( ) ;
28
- if ( this . environment === " development" ) {
29
- this . watch ( ) ;
26
+ this . manifest ( )
27
+ this . spinner . succeed ( )
28
+ if ( this . environment === ' development' ) {
29
+ this . watch ( )
30
30
}
31
31
} )
32
- . catch ( error => {
33
- this . spinner . fail ( " Failed to build application" ) ;
34
- console . error ( error ) ;
35
- } ) ;
32
+ . catch ( ( error ) => {
33
+ this . spinner . fail ( ' Failed to build application' )
34
+ console . error ( error )
35
+ } )
36
36
}
37
37
38
38
buildQueue ( ) {
39
- let queue = [
40
- this . copy ( " renderer/*.html" , " renderer" ) ,
41
- this . copy ( " images/**/*" , " images" ) ,
39
+ const queue = [
40
+ this . copy ( ' renderer/*.html' , ' renderer' ) ,
41
+ this . copy ( ' images/**/*' , ' images' ) ,
42
42
this . bundle ( this . config . renderer ) ,
43
43
this . bundle ( this . config . main )
44
- ] ;
45
- if ( this . config . preload ) queue . push ( this . bundle ( this . config . preload ) ) ;
46
- return queue ;
44
+ ]
45
+ if ( this . config . preload ) queue . push ( this . bundle ( this . config . preload ) )
46
+ return queue
47
47
}
48
48
49
49
bundle ( config , callback ) {
50
50
return new Promise ( ( resolve , reject ) => {
51
51
webpack ( config , ( error , stats ) => {
52
52
if ( error || stats . hasErrors ( ) ) {
53
- this . spinner . fail ( chalk . red ( " Webpack failed to bundle application" ) ) ;
54
- bozon . log ( stats . compilation . errors ) ;
55
- return reject ( error ) ;
53
+ this . spinner . fail ( chalk . red ( ' Webpack failed to bundle application' ) )
54
+ bozon . log ( stats . compilation . errors )
55
+ return reject ( error )
56
56
}
57
57
if ( stats . compilation . warnings . length ) {
58
58
this . spinner . fail (
59
- chalk . yellow ( " Webpack bundled application with warnings" )
60
- ) ;
61
- bozon . log ( stats . compilation . warnings ) ;
59
+ chalk . yellow ( ' Webpack bundled application with warnings' )
60
+ )
61
+ bozon . log ( stats . compilation . warnings )
62
62
}
63
- if ( callback ) callback ( ) ;
64
- return resolve ( ) ;
65
- } ) ;
66
- } ) ;
63
+ if ( callback ) callback ( )
64
+ return resolve ( )
65
+ } )
66
+ } )
67
67
}
68
68
69
69
copy ( input , output , callback ) {
@@ -73,93 +73,93 @@ class Builder {
73
73
bozon . destinationPath ( output , this . environment ) ,
74
74
( error , file ) => {
75
75
if ( error ) {
76
- return reject ( error ) ;
76
+ return reject ( error )
77
77
} else {
78
- if ( callback ) callback ( ) ;
79
- return resolve ( ) ;
78
+ if ( callback ) callback ( )
79
+ return resolve ( )
80
80
}
81
81
}
82
- ) ;
83
- } ) ;
82
+ )
83
+ } )
84
84
}
85
85
86
86
manifest ( ) {
87
87
return new Promise ( ( resolve , reject ) => {
88
- const json = JSON . parse ( fs . readFileSync ( bozon . source ( " package.json" ) ) ) ;
89
- let settings = {
88
+ const json = JSON . parse ( fs . readFileSync ( bozon . source ( ' package.json' ) ) )
89
+ const settings = {
90
90
name : json . name ,
91
91
version : json . version ,
92
92
description : json . description ,
93
- author : json . author || " Anonymous" ,
94
- main : " main/index.js" ,
93
+ author : json . author || ' Anonymous' ,
94
+ main : ' main/index.js' ,
95
95
repository : json . repository
96
- } ;
96
+ }
97
97
fs . writeFile (
98
- bozon . destinationPath ( " package.json" , this . environment ) ,
98
+ bozon . destinationPath ( ' package.json' , this . environment ) ,
99
99
JSON . stringify ( settings ) ,
100
- err => {
100
+ ( err ) => {
101
101
if ( err ) {
102
- reject ( err ) ;
102
+ reject ( err )
103
103
} else {
104
- resolve ( ) ;
104
+ resolve ( )
105
105
}
106
106
}
107
- ) ;
108
- } ) ;
107
+ )
108
+ } )
109
109
}
110
110
111
111
watch ( ) {
112
- let watcher = chokidar . watch ( bozon . sourcePath ( " **/*.*" ) , {
112
+ const watcher = chokidar . watch ( bozon . sourcePath ( ' **/*.*' ) , {
113
113
ignored : / n o d e _ m o d u l e s / ,
114
114
persistent : true
115
- } ) ;
116
- watcher . on ( " ready" , ( ) => {
115
+ } )
116
+ watcher . on ( ' ready' , ( ) => {
117
117
this . spinner . stopAndPersist ( {
118
- text : chalk . green ( " Watching for changes.." ) ,
119
- symbol : "👀"
120
- } ) ;
121
- } ) ;
122
- watcher . on ( " change" , ( file , stats ) => {
118
+ text : chalk . green ( ' Watching for changes..' ) ,
119
+ symbol : '👀'
120
+ } )
121
+ } )
122
+ watcher . on ( ' change' , ( file , stats ) => {
123
123
this . log (
124
- chalk . green ( " CHANGE" ) ,
124
+ chalk . green ( ' CHANGE' ) ,
125
125
`File '${ chalk . yellow (
126
126
path . relative ( bozon . source ( ) , file )
127
127
) } ' has been changed`
128
- ) ;
128
+ )
129
129
if ( file . match ( / m a i n / ) ) {
130
- let key = chalk . grey ( " ~MAIN~" ) ;
131
- this . log ( key , " Compiling.." ) ;
132
- this . bundle ( this . config . main , ( ) => this . log ( key , chalk . cyan ( " Done" ) ) ) ;
130
+ const key = chalk . grey ( ' ~MAIN~' )
131
+ this . log ( key , ' Compiling..' )
132
+ this . bundle ( this . config . main , ( ) => this . log ( key , chalk . cyan ( ' Done' ) ) )
133
133
} else if ( file . match ( / \. h t m l / ) ) {
134
- let key = chalk . grey ( " RENDER" ) ;
135
- this . log ( key , " Updating.." ) ;
136
- this . copy ( " *.html" , "" , ( ) => this . log ( key , chalk . cyan ( " Done" ) ) ) ;
134
+ const key = chalk . grey ( ' RENDER' )
135
+ this . log ( key , ' Updating..' )
136
+ this . copy ( ' *.html' , '' , ( ) => this . log ( key , chalk . cyan ( ' Done' ) ) )
137
137
} else if ( file . match ( / \. j s o n / ) ) {
138
- let key = chalk . grey ( " RENDER" ) ;
139
- this . log ( key , " Updating.." ) ;
140
- this . copy ( " *.json" , "" , ( ) => this . log ( key , chalk . cyan ( " Done" ) ) ) ;
138
+ const key = chalk . grey ( ' RENDER' )
139
+ this . log ( key , ' Updating..' )
140
+ this . copy ( ' *.json' , '' , ( ) => this . log ( key , chalk . cyan ( ' Done' ) ) )
141
141
} else {
142
- let key = chalk . grey ( " RENDER" ) ;
143
- this . log ( key , " Compiling.." ) ;
142
+ const key = chalk . grey ( ' RENDER' )
143
+ this . log ( key , ' Compiling..' )
144
144
this . bundle ( this . config . renderer , ( ) =>
145
- this . log ( key , chalk . cyan ( " Done" ) )
146
- ) ;
145
+ this . log ( key , chalk . cyan ( ' Done' ) )
146
+ )
147
147
}
148
- } ) ;
148
+ } )
149
149
}
150
150
151
151
log ( key , message ) {
152
- let time = this . timestamp ( ) ;
153
- bozon . log ( `[${ chalk . grey ( time ) } ] [${ key } ] ${ message } ` ) ;
152
+ const time = this . timestamp ( )
153
+ bozon . log ( `[${ chalk . grey ( time ) } ] [${ key } ] ${ message } ` )
154
154
}
155
155
156
156
timestamp ( ) {
157
- let date = new Date ( ) ;
158
- let hours = String ( date . getHours ( ) ) . padStart ( 2 , "0" ) ;
159
- let minutes = String ( date . getMinutes ( ) ) . padStart ( 2 , "0" ) ;
160
- let seconds = String ( date . getSeconds ( ) ) . padStart ( 2 , "0" ) ;
161
- return `${ hours } :${ minutes } :${ seconds } ` ;
157
+ const date = new Date ( )
158
+ const hours = String ( date . getHours ( ) ) . padStart ( 2 , '0' )
159
+ const minutes = String ( date . getMinutes ( ) ) . padStart ( 2 , '0' )
160
+ const seconds = String ( date . getSeconds ( ) ) . padStart ( 2 , '0' )
161
+ return `${ hours } :${ minutes } :${ seconds } `
162
162
}
163
163
}
164
164
165
- module . exports = Builder ;
165
+ module . exports = Builder
0 commit comments