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