@@ -43,12 +43,11 @@ function execute(options) {
43
43
const platform = options . platform ;
44
44
45
45
if ( options . bundle ) {
46
- commands . push ( ( ) => cleanApp ( platform ) ) ;
47
- commands . push ( ( ) => webpack ( platform ) ) ;
48
- }
49
-
50
- if ( platform === "android" ) {
51
- commands . push ( ( ) => gradlewClean ( ) ) ;
46
+ commands = [
47
+ ( ) => cleanApp ( platform ) ,
48
+ ( ) => cleanBuildArtifacts ( platform ) ,
49
+ ( ) => webpack ( platform ) ,
50
+ ] ;
52
51
}
53
52
54
53
// If "build-app" or "start-app" is specified,
@@ -63,12 +62,21 @@ function execute(options) {
63
62
return commands . reduce ( ( current , next ) => current . then ( next ) , Promise . resolve ( ) ) ;
64
63
}
65
64
66
- // Clear platform/**/app folder contents
67
- function cleanApp ( platform ) {
65
+ function cleanBuildArtifacts ( platform ) {
68
66
return new Promise ( ( resolve , reject ) => {
69
- spawnChildProcess ( true , "tns" , "clean-app" , platform )
70
- . then ( resolve )
71
- . catch ( throwError )
67
+ if ( platform !== "android" ) {
68
+ resolve ( ) ;
69
+ }
70
+
71
+ getTnsVersion ( ) . then ( versionString => {
72
+ const version = versionToNumber ( versionString ) ;
73
+
74
+ // for nativescript-cli v3.0.1 and below
75
+ // the android build artifacts should be cleaned manually
76
+ if ( version <= 301 ) {
77
+ gradlewClean ( ) . then ( resolve ) . catch ( throwError ) ;
78
+ }
79
+ } ) . catch ( throwError ) ;
72
80
} ) ;
73
81
}
74
82
@@ -80,18 +88,49 @@ function gradlewClean() {
80
88
resolve ( ) ;
81
89
}
82
90
83
- spawnChildProcess ( true , gradlew , "-p" , platformsPath , "clean" )
91
+ spawnChildProcess ( gradlew , "-p" , platformsPath , "clean" )
84
92
. then ( resolve )
85
93
. catch ( throwError ) ;
86
94
} ) ;
87
95
}
88
96
97
+ function getTnsVersion ( ) {
98
+ return new Promise ( ( resolve , reject ) => {
99
+ const childProcess = spawn ( "tns" , [ "--version" ] , { shell : true } ) ;
100
+
101
+ childProcess . stdout . on ( "data" , resolve ) ;
102
+
103
+ childProcess . on ( "close" , code => {
104
+ if ( code ) {
105
+ reject ( {
106
+ code,
107
+ message : `child process exited with code ${ code } ` ,
108
+ } ) ;
109
+ }
110
+ } ) ;
111
+ } ) ;
112
+ }
113
+
114
+ function versionToNumber ( version ) {
115
+ const VERSION_MATCHER = / ( \d + ) \. ( \d + ) \. ( \d + ) / ;
116
+
117
+ return Number ( VERSION_MATCHER . exec ( version ) . splice ( 1 ) . join ( "" ) ) ;
118
+ }
119
+
120
+ // Clear platform/**/app folder contents
121
+ function cleanApp ( platform ) {
122
+ return new Promise ( ( resolve , reject ) => {
123
+ spawnChildProcess ( "tns" , "clean-app" , platform )
124
+ . then ( resolve )
125
+ . catch ( throwError )
126
+ } ) ;
127
+ }
128
+
89
129
function webpack ( platform ) {
90
130
return new Promise ( function ( resolve , reject ) {
91
131
console . log ( `Running webpack for ${ platform } ...` ) ;
92
132
93
133
const args = [
94
- true , // show output on console
95
134
`webpack` ,
96
135
`--config=webpack.config.js` ,
97
136
`--progress` ,
@@ -109,7 +148,7 @@ function runTns(command, platform) {
109
148
return new Promise ( ( resolve , reject ) => {
110
149
console . log ( `Running tns ${ command } ...` ) ;
111
150
112
- spawnChildProcess ( true , "tns" , command , platform , "--bundle" , "--disable-npm-install" , ...tnsArgs )
151
+ spawnChildProcess ( "tns" , command , platform , "--bundle" , "--disable-npm-install" , ...tnsArgs )
113
152
. then ( resolve )
114
153
. catch ( throwError ) ;
115
154
} ) ;
@@ -150,22 +189,24 @@ function getCommand(flags) {
150
189
}
151
190
}
152
191
153
- function spawnChildProcess ( shouldPrintOutput , command , ...args ) {
154
- const stdio = shouldPrintOutput ? "inherit" : "ignore" ;
155
-
192
+ function spawnChildProcess ( command , ...args ) {
156
193
return new Promise ( ( resolve , reject ) => {
157
- const childProcess = spawn ( command , args , { stdio, pwd : PROJECT_DIR , shell : true } ) ;
158
-
159
- childProcess . on ( "close" , ( code ) => {
160
- if ( code === 0 ) {
161
- resolve ( ) ;
162
- } else {
163
- reject ( {
164
- code,
165
- message : `child process exited with code ${ code } ` ,
166
- } ) ;
167
- }
168
- } ) ;
194
+ const childProcess = spawn ( command , args , {
195
+ stdio : "inherit" ,
196
+ pwd : PROJECT_DIR ,
197
+ shell : true ,
198
+ } ) ;
199
+
200
+ childProcess . on ( "close" , code => {
201
+ if ( code === 0 ) {
202
+ resolve ( ) ;
203
+ } else {
204
+ reject ( {
205
+ code,
206
+ message : `child process exited with code ${ code } ` ,
207
+ } ) ;
208
+ }
209
+ } ) ;
169
210
} ) ;
170
211
}
171
212
0 commit comments