1
1
#!/usr/bin/env node
2
2
3
3
const { spawn } = require ( "child_process" ) ;
4
- const { resolve : pathResolve } = require ( "path" ) ;
4
+ const { resolve : pathResolve , join } = require ( "path" ) ;
5
5
const { existsSync } = require ( "fs" ) ;
6
6
const { getPackageJson } = require ( "../projectHelpers" ) ;
7
7
const { isVersionGte } = require ( "../utils" ) ;
@@ -13,14 +13,16 @@ if (!process.env.npm_config_argv) {
13
13
throwError ( { message : "No flags provided." } ) ;
14
14
}
15
15
16
- const escape = arg => `"${ arg } "` ;
16
+ const escapeWithQuotes = arg => `"${ arg } "` ;
17
17
const isTnsCommand = flag => flag . endsWith ( "-app" ) ;
18
18
const isEnvCommand = flag => flag . indexOf ( "env." ) > - 1 ;
19
19
const shouldUglify = ( ) => process . env . npm_config_uglify ;
20
- const shouldSnapshot = ( platform ) => platform == "android" && require ( "os" ) . type ( ) != "Windows_NT" && process . env . npm_config_snapshot ;
20
+ const shouldSnapshot = platform => platform == "android" &&
21
+ require ( "os" ) . type ( ) != "Windows_NT" &&
22
+ process . env . npm_config_snapshot ;
21
23
22
24
const npmArgs = JSON . parse ( process . env . npm_config_argv ) . original ;
23
- const tnsArgs = getTnsArgs ( npmArgs ) . map ( escape ) ;
25
+ const tnsArgs = getTnsArgs ( npmArgs ) . map ( escapeWithQuotes ) ;
24
26
const flags = npmArgs . filter ( a => a . startsWith ( "--" ) ) . map ( a => a . substring ( 2 ) ) ;
25
27
const options = getOptions ( flags ) ;
26
28
@@ -51,7 +53,7 @@ function execute(options) {
51
53
...commands ,
52
54
( ) => cleanApp ( platform ) ,
53
55
( ) => cleanSnapshotArtefacts ( ) ,
54
- ( ) => cleanBuildArtifacts ( platform ) ,
56
+ ( ) => cleanBuildArtefacts ( platform ) ,
55
57
( ) => webpack ( platform , options . env ) ,
56
58
] ;
57
59
}
@@ -68,14 +70,14 @@ function execute(options) {
68
70
return commands . reduce ( ( current , next ) => current . then ( next ) , Promise . resolve ( ) ) ;
69
71
}
70
72
71
- function cleanBuildArtifacts ( platform ) {
73
+ function cleanBuildArtefacts ( platform ) {
72
74
return new Promise ( ( resolve , reject ) => {
73
75
if ( platform !== "android" ) {
74
76
return resolve ( ) ;
75
77
}
76
78
77
79
getTnsVersion ( ) . then ( version => {
78
- // the android build artifacts should be cleaned manually
80
+ // the android build artefacts should be cleaned manually
79
81
// for nativescript-cli v3.0.1 and below or if using uglify
80
82
if ( isVersionGte ( version , "3.0.1" ) || shouldUglify ( ) ) {
81
83
gradlewClean ( ) . then ( resolve ) . catch ( throwError ) ;
@@ -96,7 +98,7 @@ function installSnapshotArtefacts() {
96
98
97
99
function gradlewClean ( ) {
98
100
return new Promise ( ( resolve , reject ) => {
99
- const platformsPath = pathResolve ( PROJECT_DIR , "platforms" , "android" )
101
+ const platformsPath = join ( PROJECT_DIR , "platforms" , "android" )
100
102
const gradlew = pathResolve ( platformsPath , "gradlew" ) ;
101
103
if ( ! existsSync ( gradlew ) ) {
102
104
return resolve ( ) ;
@@ -134,6 +136,7 @@ function versionToNumber(version) {
134
136
// Clear platform/**/app folder contents
135
137
function cleanApp ( platform ) {
136
138
return new Promise ( ( resolve , reject ) => {
139
+
137
140
spawnChildProcess ( "tns" , "clean-app" , platform )
138
141
. then ( resolve )
139
142
. catch ( throwError )
@@ -171,18 +174,14 @@ function runTns(command, platform) {
171
174
}
172
175
173
176
function getOptions ( flags ) {
174
- let options = { } ;
175
- options . platform = getPlatform ( flags ) ;
176
- options . command = getCommand ( flags ) ;
177
- options . env = getEnv ( flags ) ;
178
- options . bundle = ! flags . includes ( "nobundle" ) ;
179
-
180
- return options ;
177
+ return {
178
+ platform : getPlatform ( flags ) ,
179
+ command : getCommand ( flags ) ,
180
+ env : flags . filter ( isEnvCommand ) ,
181
+ bundle : ! flags . includes ( "nobundle" ) ,
182
+ } ;
181
183
}
182
184
183
- function getEnv ( flags ) {
184
- return flags . filter ( item => isEnvCommand ( item ) ) ;
185
- }
186
185
187
186
function getPlatform ( flags ) {
188
187
if ( flags . includes ( "android" ) && flags . includes ( "ios" ) ) {
@@ -194,7 +193,9 @@ function getPlatform(flags) {
194
193
} else if ( flags . includes ( "ios" ) ) {
195
194
return "ios" ;
196
195
} else {
197
- throwError ( { message : "You must provide a target platform! Use either --android, or --ios flag." } ) ;
196
+ throwError ( { message :
197
+ "You must provide a target platform! " +
198
+ "Use either --android, or --ios flag." } ) ;
198
199
}
199
200
}
200
201
@@ -209,7 +210,10 @@ function getCommand(flags) {
209
210
210
211
function spawnChildProcess ( command , ...args ) {
211
212
return new Promise ( ( resolve , reject ) => {
212
- const childProcess = spawn ( command , args , {
213
+ const escapedArgs = args . map ( escapeWithQuotes )
214
+ const escapedCommand = escapeWithQuotes ( command )
215
+
216
+ const childProcess = spawn ( escapedCommand , escapedArgs , {
213
217
stdio : "inherit" ,
214
218
pwd : PROJECT_DIR ,
215
219
shell : true ,
0 commit comments