2
2
3
3
const { spawn } = require ( "child_process" ) ;
4
4
const { resolve : pathResolve } = require ( "path" ) ;
5
+ const { existsSync } = require ( "fs" ) ;
5
6
const { getPackageJson } = require ( "../projectHelpers" ) ;
6
7
7
8
const PROJECT_DIR = pathResolve ( __dirname , "../../../" ) ;
@@ -16,11 +17,6 @@ const tnsArgs = getTnsArgs(npmArgs).map(escape);
16
17
const flags = npmArgs . filter ( a => a . startsWith ( "--" ) ) . map ( a => a . substring ( 2 ) ) ;
17
18
const options = getOptions ( flags ) ;
18
19
19
- let platformVersion ;
20
- try {
21
- platformVersion = packageJson . nativescript [ `tns-${ options . platform } ` ] . version ;
22
- } catch ( e ) { }
23
-
24
20
function getTnsArgs ( args ) {
25
21
const other = [
26
22
"run" ,
@@ -44,24 +40,52 @@ execute(options);
44
40
45
41
function execute ( options ) {
46
42
let commands = [ ] ;
43
+ const platform = options . platform ;
47
44
48
45
if ( options . bundle ) {
49
- commands . push ( ( ) => clearPlatform ( options . platform ) ) ;
50
- commands . push ( ( ) => webpack ( options . platform ) ) ;
46
+ commands . push ( ( ) => cleanApp ( platform ) ) ;
47
+ commands . push ( ( ) => webpack ( platform ) ) ;
48
+ }
49
+
50
+ if ( platform === "android" ) {
51
+ commands . push ( ( ) => gradlewClean ( ) ) ;
51
52
}
52
53
53
54
// If "build-app" or "start-app" is specified,
54
55
// the respective command should be run last.
55
56
// Otherwise, the app should be just prepared.
56
57
if ( options . command ) {
57
- commands . push ( ( ) => runTns ( options . command , options . platform ) ) ;
58
+ commands . push ( ( ) => runTns ( options . command , platform ) ) ;
58
59
} else {
59
- commands . shift ( ( ) => runTns ( "prepare" , options . platform ) )
60
+ commands . shift ( ( ) => runTns ( "prepare" , platform ) )
60
61
}
61
62
62
63
return commands . reduce ( ( current , next ) => current . then ( next ) , Promise . resolve ( ) ) ;
63
64
}
64
65
66
+ // Clear platform/**/app folder contents
67
+ function cleanApp ( platform ) {
68
+ return new Promise ( ( resolve , reject ) => {
69
+ spawnChildProcess ( true , "tns" , "clean-app" , platform )
70
+ . then ( resolve )
71
+ . catch ( throwError )
72
+ } ) ;
73
+ }
74
+
75
+ function gradlewClean ( ) {
76
+ return new Promise ( ( resolve , reject ) => {
77
+ const platformsPath = pathResolve ( PROJECT_DIR , "platforms" , "android" )
78
+ const gradlew = pathResolve ( platformsPath , "gradlew" ) ;
79
+ if ( ! existsSync ( gradlew ) ) {
80
+ resolve ( ) ;
81
+ }
82
+
83
+ spawnChildProcess ( true , gradlew , "-p" , platformsPath , "clean" )
84
+ . then ( resolve )
85
+ . catch ( throwError ) ;
86
+ } ) ;
87
+ }
88
+
65
89
function webpack ( platform ) {
66
90
return new Promise ( function ( resolve , reject ) {
67
91
console . log ( `Running webpack for ${ platform } ...` ) ;
@@ -81,32 +105,6 @@ function webpack(platform) {
81
105
} ) ;
82
106
}
83
107
84
- function clearPlatform ( platform ) {
85
- return removePlatform ( platform )
86
- . then ( ( ) => addPlatform ( platform ) ) ;
87
- }
88
-
89
- function removePlatform ( platform ) {
90
- return new Promise ( function ( resolve , reject ) {
91
- console . log ( `Removing platform ${ platform } ...` ) ;
92
-
93
- spawnChildProcess ( false , "tns" , "platform" , "remove" , platform )
94
- . then ( resolve )
95
- . catch ( resolve ) ;
96
- } ) ;
97
- }
98
-
99
- function addPlatform ( platform ) {
100
- return new Promise ( function ( resolve , reject ) {
101
- const platformToAdd = platformVersion ? `${ platform } @${ platformVersion } ` : platform ;
102
- console . log ( `Adding platform ${ platformToAdd } ...` ) ;
103
-
104
- spawnChildProcess ( false , "tns" , "platform" , "add" , platformToAdd )
105
- . then ( resolve )
106
- . catch ( resolve ) ;
107
- } ) ;
108
- }
109
-
110
108
function runTns ( command , platform ) {
111
109
return new Promise ( ( resolve , reject ) => {
112
110
console . log ( `Running tns ${ command } ...` ) ;
0 commit comments