1
1
#!/usr/bin/env node
2
2
3
- const { spawn } = require ( "child_process" ) ;
4
- const { resolve : pathResolve , join } = require ( "path" ) ;
5
- const { existsSync } = require ( "fs" ) ;
6
-
7
- const semver = require ( "semver" ) ;
8
-
9
- const { getPackageJson, getProjectDir } = require ( "../projectHelpers" ) ;
10
-
11
- const PROJECT_DIR = getProjectDir ( { nestingLvl : 2 } ) ;
12
- const packageJson = getPackageJson ( PROJECT_DIR ) ;
13
-
14
- if ( ! process . env . npm_config_argv ) {
15
- throwError ( { message : "No flags provided." } ) ;
16
- }
17
-
18
- const escapeWithQuotes = arg => `"${ arg } "` ;
19
- const isTnsCommand = flag => flag . endsWith ( "-app" ) ;
20
- const isEnvCommand = flag => flag . indexOf ( "env." ) > - 1 ;
21
- const shouldUglify = ( ) => process . env . npm_config_uglify ;
22
-
23
- const platformSupportsSnapshot = platform => platform === "android" ;
24
- const osSupportsSnapshot = ( ) => require ( "os" ) . type ( ) != "Windows_NT" ;
25
- const snapshotOption = env =>
26
- process . env . npm_config_snapshot || ( env && env . includes ( "env.snapshot" ) ) ;
27
-
28
- const shouldSnapshot = ( platform , env ) =>
29
- platformSupportsSnapshot ( platform ) &&
30
- osSupportsSnapshot ( ) &&
31
- snapshotOption ( env ) ;
32
-
33
- const npmArgs = JSON . parse ( process . env . npm_config_argv ) . original ;
34
- const tnsArgs = getTnsArgs ( npmArgs ) ;
35
- const flags = npmArgs . filter ( a => a . startsWith ( "--" ) ) . map ( a => a . substring ( 2 ) ) ;
36
- const options = getOptions ( flags ) ;
37
-
38
- function getTnsArgs ( args ) {
39
- const other = [
40
- "run" ,
41
- "ns-bundle" ,
42
- "--android" ,
43
- "--ios" ,
44
- "--uglify" ,
45
- "--snapshot" ,
46
- "--nobundle" ,
47
- ] ;
48
-
49
- return args . filter ( a => ! other . includes ( a ) && ! isTnsCommand ( a ) && ! isEnvCommand ( a ) ) ;
50
- }
51
-
52
- execute ( options ) ;
53
-
54
- function execute ( options ) {
55
- const platform = options . platform ;
56
- let commands = [
57
- ( ) => runTns ( "prepare" , platform )
58
- ] ;
59
-
60
- if ( options . bundle ) {
61
- commands = [
62
- ...commands ,
63
- ( ) => cleanApp ( platform ) ,
64
- ( ) => cleanSnapshotArtefacts ( ) ,
65
- ( ) => cleanBuildArtefacts ( platform ) ,
66
- ( ) => webpack ( platform , options . env ) ,
67
- ] ;
68
- }
69
-
70
- if ( shouldSnapshot ( platform , options . env ) ) {
71
- commands . push ( ( ) => installSnapshotArtefacts ( ) ) ;
72
- }
73
-
74
- // If "build-app" or "start-app" is specified,
75
- // the respective command should be run last.
76
- if ( options . command ) {
77
- commands . push ( ( ) => runTns ( options . command , platform ) ) ;
78
- }
79
- return commands . reduce ( ( current , next ) => current . then ( next ) , Promise . resolve ( ) ) ;
80
- }
81
-
82
- function cleanBuildArtefacts ( platform ) {
83
- return new Promise ( ( resolve , reject ) => {
84
- if ( platform !== "android" ) {
85
- return resolve ( ) ;
86
- }
87
-
88
- getTnsVersion ( ) . then ( version => {
89
- // the android build artefacts should be cleaned manually
90
- // for nativescript-cli v3.0.1 and below or if using uglify
91
-
92
- if ( ! semver . gte ( version , "3.0.1" ) || shouldUglify ( ) ) {
93
- gradlewClean ( ) . then ( resolve ) . catch ( throwError ) ;
94
- } else {
95
- return resolve ( ) ;
96
- }
97
- } ) . catch ( throwError ) ;
98
- } ) ;
99
- }
100
-
101
- function cleanSnapshotArtefacts ( ) {
102
- require ( "../snapshot/android/project-snapshot-generator" ) . cleanSnapshotArtefacts ( PROJECT_DIR ) ;
103
- }
104
-
105
- function installSnapshotArtefacts ( ) {
106
- require ( "../snapshot/android/project-snapshot-generator" ) . installSnapshotArtefacts ( PROJECT_DIR ) ;
107
- }
108
-
109
- function gradlewClean ( ) {
110
- return new Promise ( ( resolve , reject ) => {
111
- const platformsPath = join ( PROJECT_DIR , "platforms" , "android" )
112
- const gradlew = pathResolve ( platformsPath , "gradlew" ) ;
113
- if ( ! existsSync ( gradlew ) ) {
114
- return resolve ( ) ;
115
- }
116
-
117
- spawnChildProcess ( gradlew , "-p" , platformsPath , "clean" )
118
- . then ( resolve )
119
- . catch ( throwError ) ;
120
- } ) ;
121
- }
122
-
123
- function getTnsVersion ( ) {
124
- return new Promise ( ( resolve , reject ) => {
125
- const childProcess = spawn ( "tns" , [ "--version" ] , { shell : true } ) ;
126
- let stdoutData = "" ;
127
- childProcess . stdout . on ( "data" , data => stdoutData += data ) ;
128
-
129
- childProcess . on ( "close" , code => {
130
- if ( code ) {
131
- reject ( {
132
- code,
133
- message : `child process exited with code ${ code } ` ,
134
- } ) ;
135
- } else {
136
- const versionRegex = / ^ (?: \d + \. ) { 2 } \d + .* ?$ / m;
137
- const matches = stdoutData . toString ( ) . match ( versionRegex ) ;
138
- const version = matches && matches [ 0 ] && matches [ 0 ] . trim ( ) ;
139
- resolve ( version ) ;
140
- }
141
- } ) ;
142
- } ) ;
143
- }
144
-
145
- // Clear platform/**/app folder contents
146
- function cleanApp ( platform ) {
147
- return new Promise ( ( resolve , reject ) => {
148
-
149
- spawnChildProcess ( "tns" , "clean-app" , platform )
150
- . then ( resolve )
151
- . catch ( throwError )
152
- } ) ;
153
- }
154
-
155
- function webpack ( platform , env ) {
156
- return new Promise ( function ( resolve , reject ) {
157
- console . log ( `Running webpack for ${ platform } ...` ) ;
158
-
159
- const args = [
160
- `node` ,
161
- `--preserve-symlinks` ,
162
- `./node_modules/.bin/webpack` ,
163
- `--config=webpack.config.js` ,
164
- `--progress` ,
165
- `--env.${ platform } ` ,
166
- ...env . map ( item => `--${ item } ` ) ,
167
- shouldUglify ( ) && `--env.uglify` ,
168
- shouldSnapshot ( platform ) && `--env.snapshot`
169
- ] . filter ( a => ! ! a ) ;
170
-
171
- spawnChildProcess ( ...args )
172
- . then ( resolve )
173
- . catch ( throwError ) ;
174
- } ) ;
175
- }
176
-
177
- function runTns ( command , platform ) {
178
- return new Promise ( ( resolve , reject ) => {
179
- console . log ( `Running tns ${ command } ...` ) ;
180
-
181
- spawnChildProcess ( "tns" , command , platform , "--bundle" , "--disable-npm-install" , ...tnsArgs )
182
- . then ( resolve )
183
- . catch ( throwError ) ;
184
- } ) ;
185
- }
186
-
187
- function getOptions ( flags ) {
188
- return {
189
- platform : getPlatform ( flags ) ,
190
- command : getCommand ( flags ) ,
191
- env : flags . filter ( isEnvCommand ) ,
192
- bundle : ! flags . includes ( "nobundle" ) ,
193
- } ;
194
- }
195
-
196
-
197
- function getPlatform ( flags ) {
198
- if ( flags . includes ( "android" ) && flags . includes ( "ios" ) ) {
199
- throwError ( { message : "You cannot use both --android and --ios flags!" } ) ;
200
- }
201
-
202
- if ( flags . includes ( "android" ) ) {
203
- return "android" ;
204
- } else if ( flags . includes ( "ios" ) ) {
205
- return "ios" ;
206
- } else {
207
- throwError ( { message :
208
- "You must provide a target platform! " +
209
- "Use either --android, or --ios flag." } ) ;
210
- }
211
- }
212
-
213
- function getCommand ( flags ) {
214
- const commands = flags . filter ( isTnsCommand ) ;
215
- if ( commands . length > 1 ) {
216
- throwError ( { message : `You can't use ${ commands . join ( ", " ) } together!` } ) ;
217
- }
218
-
219
- return commands . length && commands [ 0 ] . replace ( / - a p p / , "" ) ;
220
- }
221
-
222
- function spawnChildProcess ( command , ...args ) {
223
- return new Promise ( ( resolve , reject ) => {
224
- const escapedArgs = args . map ( escapeWithQuotes )
225
-
226
- const childProcess = spawn ( command , escapedArgs , {
227
- stdio : "inherit" ,
228
- cwd : PROJECT_DIR ,
229
- shell : true ,
230
- } ) ;
231
-
232
- childProcess . on ( "close" , code => {
233
- if ( code === 0 ) {
234
- resolve ( ) ;
235
- } else {
236
- reject ( {
237
- code,
238
- message : `child process exited with code ${ code } ` ,
239
- } ) ;
240
- }
241
- } ) ;
242
- } ) ;
243
- }
244
-
245
- function throwError ( error ) {
246
- console . error ( error . message ) ;
247
- process . exit ( error . code || 1 ) ;
248
- }
249
-
3
+ console . error ( "Using npm run scripts is no longer supported. Use CLI commands and pass the --bundle flag instead.\nExample:\ntns build android --bundle" )
4
+ process . exit ( 1 ) ;
0 commit comments