@@ -17,60 +17,135 @@ if (isAngular) {
17
17
isTypeScript = true ;
18
18
}
19
19
20
- function addProjectFiles ( ) {
21
- copyProjectTemplate ( "webpack.android.js.template" , "webpack.android.js" ) ;
22
- copyProjectTemplate ( "webpack.ios.js.template" , "webpack.ios.js" ) ;
20
+ function getProjectTemplates ( ) {
21
+ var templates = {
22
+ "webpack.android.js.template" : "webpack.android.js" ,
23
+ "webpack.ios.js.template" : "webpack.ios.js" ,
24
+ } ;
23
25
24
26
if ( isAngular ) {
25
- copyProjectTemplate ( "webpack.common.js.angular.template" , "webpack.common.js" ) ;
26
- copyProjectTemplate ( "tsconfig.aot.json.template" , "tsconfig.aot.json" ) ;
27
+ templates [ "webpack.common.js.angular.template" ] = "webpack.common.js" ;
28
+ templates [ "tsconfig.aot.json.template" ] = "tsconfig.aot.json" ;
27
29
} else {
28
- copyProjectTemplate ( "webpack.common.js.nativescript.template" , "webpack.common.js" ) ;
30
+ templates [ "webpack.common.js.nativescript.template" ] = "webpack.common.js" ;
29
31
}
32
+ return templates ;
33
+ }
34
+
35
+ function getAppTemplates ( ) {
36
+ var templates = {
37
+ "vendor-platform.android.ts.template" : tsOrJs ( "vendor-platform.android" ) ,
38
+ "vendor-platform.ios.ts.template" : tsOrJs ( "vendor-platform.ios" ) ,
39
+ } ;
30
40
31
- copyAppTemplate ( "vendor-platform.android.ts.template" , tsOrJs ( "vendor-platform.android" ) ) ;
32
- copyAppTemplate ( "vendor-platform.ios.ts.template" , tsOrJs ( "vendor-platform.ios" ) ) ;
33
41
if ( isAngular ) {
34
- copyAppTemplate ( "vendor.ts.angular.template" , tsOrJs ( "vendor" ) ) ;
42
+ templates [ "vendor.ts.angular.template" ] = tsOrJs ( "vendor" ) ;
35
43
} else {
36
- copyAppTemplate ( "vendor.ts.nativescript.template" , tsOrJs ( "vendor" ) ) ;
44
+ templates [ "vendor.ts.nativescript.template" ] = tsOrJs ( "vendor" ) ;
37
45
}
46
+ return templates ;
47
+ }
48
+
49
+ function addProjectFiles ( ) {
50
+ var projectTemplates = getProjectTemplates ( ) ;
51
+ Object . keys ( projectTemplates ) . forEach ( function ( templateName ) {
52
+ var templateDestination = projectTemplates [ templateName ] ;
53
+ copyProjectTemplate ( templateName , templateDestination ) ;
54
+ } ) ;
55
+
56
+ var appTemplates = getAppTemplates ( ) ;
57
+ Object . keys ( appTemplates ) . forEach ( function ( templateName ) {
58
+ var templateDestination = appTemplates [ templateName ] ;
59
+ copyAppTemplate ( templateName , templateDestination ) ;
60
+ } ) ;
38
61
}
39
62
exports . addProjectFiles = addProjectFiles ;
40
63
64
+ function removeProjectFiles ( ) {
65
+ var projectTemplates = getProjectTemplates ( ) ;
66
+ Object . keys ( projectTemplates ) . forEach ( function ( templateName ) {
67
+ var templateDestination = projectTemplates [ templateName ] ;
68
+ deleteProjectFile ( templateDestination ) ;
69
+ } ) ;
70
+
71
+ var appTemplates = getAppTemplates ( ) ;
72
+ Object . keys ( appTemplates ) . forEach ( function ( templateName ) {
73
+ var templateDestination = appTemplates [ templateName ] ;
74
+ deleteAppFile ( templateDestination ) ;
75
+ } ) ;
76
+ }
77
+ exports . removeProjectFiles = removeProjectFiles ;
78
+
79
+ function getScriptTemplates ( ) {
80
+ return {
81
+ "clean-[PLATFORM]" : "tns clean-app [PLATFORM]" ,
82
+ "prewebpack-[PLATFORM]" : "npm run clean-[PLATFORM]" ,
83
+ "webpack-[PLATFORM]" : "webpack --config=webpack.[PLATFORM].js --progress" ,
84
+ "prestart-[PLATFORM]-bundle" : "npm run webpack-[PLATFORM]" ,
85
+ "start-[PLATFORM]-bundle" : "tns run [PLATFORM] --bundle --disable-npm-install" ,
86
+ "prebuild-[PLATFORM]-bundle" : "npm run webpack-[PLATFORM]" ,
87
+ "build-[PLATFORM]-bundle" : "tns build [PLATFORM] --bundle --disable-npm-install" ,
88
+ } ;
89
+ }
90
+
41
91
function addNpmScripts ( ) {
42
- addPlatformScript ( packageJson , "clean-[PLATFORM]" , "tns clean-app [PLATFORM]" ) ;
43
- addPlatformScript ( packageJson , "prewebpack-[PLATFORM]" , "npm run clean-[PLATFORM]" ) ;
44
- addPlatformScript ( packageJson , "webpack-[PLATFORM]" , "webpack --config=webpack.[PLATFORM].js --progress" ) ;
45
- addPlatformScript ( packageJson , "prestart-[PLATFORM]-bundle" , "npm run webpack-[PLATFORM]" ) ;
46
- addPlatformScript ( packageJson , "start-[PLATFORM]-bundle" , "tns run [PLATFORM] --bundle --disable-npm-install" ) ;
47
- addPlatformScript ( packageJson , "prebuild-[PLATFORM]-bundle" , "npm run webpack-[PLATFORM]" ) ;
48
- addPlatformScript ( packageJson , "build-[PLATFORM]-bundle" , "tns build [PLATFORM] --bundle --disable-npm-install" ) ;
92
+ var scriptTemplates = getScriptTemplates ( ) ;
93
+ Object . keys ( scriptTemplates ) . forEach ( function ( templateName ) {
94
+ addPlatformScript ( packageJson , templateName , scriptTemplates [ templateName ] ) ;
95
+ } ) ;
49
96
}
50
97
exports . addNpmScripts = addNpmScripts ;
51
98
99
+ function removeNpmScripts ( ) {
100
+ var scriptTemplates = getScriptTemplates ( ) ;
101
+ Object . keys ( scriptTemplates ) . forEach ( function ( templateName ) {
102
+ removePlatformScripts ( packageJson , templateName ) ;
103
+ } ) ;
104
+ }
105
+ exports . removeNpmScripts = removeNpmScripts ;
106
+
107
+ function getProjectDependencies ( ) {
108
+ var dependencies = {
109
+ "webpack" : "~2.1.0-beta.27" ,
110
+ "webpack-sources" : "~0.1.3" ,
111
+ "copy-webpack-plugin" : "~3.0.1" ,
112
+ "raw-loader" : "~0.5.1" ,
113
+ "nativescript-css-loader" : "~0.26.0" ,
114
+ "resolve-url-loader" : "~1.6.0" ,
115
+ "extract-text-webpack-plugin" : "~2.0.0-beta.4" ,
116
+ } ;
117
+
118
+ if ( isAngular ) {
119
+ dependencies [ "@angular/compiler-cli" ] = "2.4.3" ;
120
+ dependencies [ "@ngtools/webpack" ] = "1.2.1" ;
121
+ dependencies [ "typescript" ] = "^2.0.10" ;
122
+ dependencies [ "htmlparser2" ] = "~3.9.2" ;
123
+ } else {
124
+ dependencies [ "awesome-typescript-loader" ] = "~3.0.0-beta.9" ;
125
+ }
126
+ return dependencies ;
127
+ }
128
+
52
129
function addProjectDependencies ( ) {
53
130
configureDevDependencies ( packageJson , function ( add ) {
54
- add ( "webpack" , "~2.1.0-beta.27" ) ;
55
- add ( "webpack-sources" , "~0.1.3" ) ;
56
- add ( "copy-webpack-plugin" , "~3.0.1" ) ;
57
- add ( "raw-loader" , "~0.5.1" ) ;
58
- add ( "nativescript-css-loader" , "~0.26.0" ) ;
59
- add ( "resolve-url-loader" , "~1.6.0" ) ;
60
- add ( "extract-text-webpack-plugin" , "~2.0.0-beta.4" ) ;
61
-
62
- if ( isAngular ) {
63
- add ( "@angular/compiler-cli" , "2.4.3" ) ;
64
- add ( "@ngtools/webpack" , "1.2.1" ) ;
65
- add ( "typescript" , "^2.0.10" ) ;
66
- add ( "htmlparser2" , "~3.9.2" ) ;
67
- } else {
68
- add ( "awesome-typescript-loader" , "~3.0.0-beta.9" ) ;
69
- }
131
+ var dependencies = getProjectDependencies ( ) ;
132
+ Object . keys ( dependencies ) . forEach ( function ( dependencyName ) {
133
+ add ( dependencyName , dependencies [ dependencyName ] ) ;
134
+ } ) ;
70
135
} ) ;
71
136
}
72
137
exports . addProjectDependencies = addProjectDependencies ;
73
138
139
+ function removeProjectDependencies ( ) {
140
+ configureDevDependencies ( packageJson , function ( add , remove ) {
141
+ var dependencies = getProjectDependencies ( ) ;
142
+ Object . keys ( dependencies ) . forEach ( function ( dependencyName ) {
143
+ remove ( dependencyName ) ;
144
+ } ) ;
145
+ } ) ;
146
+ }
147
+ exports . removeProjectDependencies = removeProjectDependencies ;
148
+
74
149
75
150
function addPlatformScript ( packageJson , nameTemplate , commandTemplate ) {
76
151
if ( ! packageJson . scripts ) {
@@ -82,12 +157,25 @@ function addPlatformScript(packageJson, nameTemplate, commandTemplate) {
82
157
var name = nameTemplate . replace ( / \[ P L A T F O R M \] / g, platform ) ;
83
158
var command = commandTemplate . replace ( / \[ P L A T F O R M \] / g, platform ) ;
84
159
if ( ! scripts [ name ] ) {
85
- scripts [ name ] = command ;
86
160
console . log ( "Registering script: " + name ) ;
161
+ scripts [ name ] = command ;
87
162
}
88
163
} ) ;
89
164
}
90
165
166
+ function removePlatformScripts ( packageJson , nameTemplate ) {
167
+ if ( ! packageJson . scripts ) {
168
+ return ;
169
+ }
170
+
171
+ var scripts = packageJson . scripts ;
172
+ [ "android" , "ios" ] . forEach ( function ( platform ) {
173
+ var name = nameTemplate . replace ( / \[ P L A T F O R M \] / g, platform ) ;
174
+ console . log ( "Removing script: " + name ) ;
175
+ delete scripts [ name ] ;
176
+ } ) ;
177
+ }
178
+
91
179
function configureDevDependencies ( packageJson , adderCallback ) {
92
180
var pendingNpmInstall = false ;
93
181
if ( ! packageJson . devDependencies ) {
@@ -103,6 +191,9 @@ function configureDevDependencies(packageJson, adderCallback) {
103
191
} else {
104
192
console . info ( "Dev dependency: '" + name + "' already added. Leaving version: " + dependencies [ name ] ) ;
105
193
}
194
+ } , function ( name ) {
195
+ console . info ( "Removing dev dependency: " + name ) ;
196
+ delete dependencies [ name ] ;
106
197
} ) ;
107
198
108
199
fs . writeFileSync ( packageJsonPath , JSON . stringify ( packageJson , null , 2 ) ) ;
@@ -139,16 +230,28 @@ function copyProjectTemplate(templateName, projectPath) {
139
230
copyTemplate ( templateName , destinationPath ) ;
140
231
}
141
232
233
+ function deleteProjectFile ( projectPath ) {
234
+ var destinationPath = path . join ( projectDir , projectPath ) ;
235
+ console . log ( "Deleting file: " + destinationPath ) ;
236
+ fs . unlink ( destinationPath ) ;
237
+ }
238
+
142
239
function copyAppTemplate ( templateName , appPath ) {
143
240
var destinationPath = path . join ( appDir , appPath ) ;
144
241
copyTemplate ( templateName , destinationPath ) ;
145
242
}
146
243
244
+ function deleteAppFile ( appPath ) {
245
+ var destinationPath = path . join ( appDir , appPath ) ;
246
+ console . log ( "Deleting file: " + destinationPath ) ;
247
+ fs . unlink ( destinationPath ) ;
248
+ }
249
+
147
250
function copyTemplate ( templateName , destinationPath ) {
148
251
var templatePath = path . join ( __dirname , templateName ) ;
149
252
// Create destination file, only if not present.
150
253
if ( ! fs . existsSync ( destinationPath ) ) {
151
- console . log ( "Creating: " + destinationPath ) ;
254
+ console . log ( "Creating file : " + destinationPath ) ;
152
255
var content = fs . readFileSync ( templatePath , "utf8" ) ;
153
256
fs . writeFileSync ( destinationPath , content ) ;
154
257
}
0 commit comments