@@ -8,96 +8,39 @@ var Funnel = require('broccoli-funnel');
8
8
var mergeTrees = require ( 'broccoli-merge-trees' ) ;
9
9
var uglify = require ( 'broccoli-uglify-js' ) ;
10
10
var Project = require ( 'ember-cli/lib/models/project' ) ;
11
+ var sourceDir = 'src' ;
11
12
12
13
module . exports = Angular2App ;
13
14
14
15
function Angular2App ( defaults , options ) {
15
16
this . _initProject ( ) ;
16
17
this . _notifyAddonIncluded ( ) ;
17
- this . options = options ;
18
+ this . options = options || { } ;
18
19
}
19
20
20
- Angular2App . prototype . toTree = function ( ) {
21
- var sourceDir = 'src' ;
22
-
23
- var sourceTree = new Funnel ( 'src' , {
24
- include : [ '*.ts' , '**/*.ts' , '**/*.d.ts' ] ,
25
- destDir : 'src'
26
- } ) ;
27
-
28
- var typingsTree = new Funnel ( 'typings' , {
29
- include : [ 'browser.d.ts' , 'browser/**' ] ,
30
- destDir : 'typings'
31
- } ) ;
32
-
33
- var vendorNpmFiles = [
34
- 'systemjs/dist/system-polyfills.js' ,
35
- 'systemjs/dist/system.src.js' ,
36
- 'es6-shim/es6-shim.js' ,
37
- 'angular2/bundles/angular2-polyfills.js' ,
38
- 'rxjs/bundles/Rx.js' ,
39
- 'angular2/bundles/angular2.dev.js' ,
40
- 'angular2/bundles/http.dev.js' ,
41
- 'angular2/bundles/router.dev.js' ,
42
- 'angular2/bundles/upgrade.dev.js'
43
- ] ;
44
-
45
- if ( this . options && this . options . vendorNpmFiles ) {
46
- vendorNpmFiles = vendorNpmFiles . concat ( this . options . vendorNpmFiles ) ;
47
- }
48
-
49
- var tsconfig = JSON . parse ( fs . readFileSync ( 'src/tsconfig.json' , 'utf-8' ) ) ;
50
- // Add all spec files to files. We need this because spec files are their own entry
51
- // point.
52
- fs . readdirSync ( sourceDir ) . forEach ( function addPathRecursive ( name ) {
53
- const filePath = path . join ( sourceDir , name ) ;
54
- if ( filePath . match ( / \. s p e c \. [ j t ] s $ / ) ) {
55
- tsconfig . files . push ( name ) ;
56
- } else if ( fs . statSync ( filePath ) . isDirectory ( ) ) {
57
- // Recursively call this function with the full sub-path.
58
- fs . readdirSync ( filePath ) . forEach ( function ( n ) {
59
- addPathRecursive ( path . join ( name , n ) ) ;
60
- } ) ;
61
- }
62
- } ) ;
63
-
64
- // Because the tsconfig does not include the source directory, add this as the first path
65
- // element.
66
- tsconfig . files = tsconfig . files . map ( name => path . join ( sourceDir , name ) ) ;
67
-
68
- var srcAndTypingsTree = mergeTrees ( [ sourceTree , typingsTree ] ) ;
69
- var tsTree = new compileWithTypescript ( srcAndTypingsTree , tsconfig ) ;
70
-
71
- var tsTreeExcludes = [ '*.d.ts' , 'tsconfig.json' ] ;
72
- var excludeSpecFiles = '**/*.spec.*' ;
73
21
74
- if ( isProduction ) {
75
- tsTreeExcludes . push ( excludeSpecFiles ) ;
76
- tsTree = uglify ( tsTree ) ;
77
- }
78
-
79
- tsTree = new Funnel ( tsTree , {
80
- srcDir : 'src' ,
81
- exclude : tsTreeExcludes
82
- } ) ;
83
-
84
- var tsSrcTree = new Funnel ( sourceDir , {
85
- include : [ '**/*.ts' ] ,
86
- allowEmpty : true
87
- } ) ;
88
-
89
- var assetTree = new Funnel ( sourceDir , {
90
- include : [ '**/*.*' ] ,
91
- exclude : [ '**/*.ts' , '**/*.js' ] ,
92
- allowEmpty : true
93
- } ) ;
94
-
95
- var vendorNpmTree = new Funnel ( 'node_modules' , {
96
- include : vendorNpmFiles ,
97
- destDir : 'vendor'
98
- } ) ;
22
+ /**
23
+ Create and return the app build system tree that:
24
+ - Get the `assets` tree
25
+ - Get the TS tree
26
+ - Get the TS src tree
27
+ - Get the index.html tree
28
+ - Get the NPM modules tree
29
+ - Apply/remove stuff based on the environment (dev|prod)
30
+ - Return the app trees to be extended
31
+
32
+ @public
33
+ @method toTree
34
+ @return {Array<Tree> } The app trees that can be used to extend the build
35
+ */
36
+ Angular2App . prototype . toTree = function ( ) {
37
+ var assetTree = this . _getAssetsTree ( ) ;
38
+ var tsTree = this . _getTsTree ( ) ;
39
+ var tsSrcTree = this . _getTsSrcTree ( ) ;
40
+ var indexTree = this . _getIndexTree ( ) ;
41
+ var vendorNpmTree = this . _getVendorNpmTree ( ) ;
99
42
100
- var buildTrees = [ assetTree , tsTree , this . index ( ) , vendorNpmTree ] ;
43
+ var buildTrees = [ assetTree , tsTree , indexTree , vendorNpmTree ] ;
101
44
102
45
if ( ! isProduction ) {
103
46
buildTrees . push ( tsSrcTree ) ;
@@ -108,17 +51,14 @@ Angular2App.prototype.toTree = function () {
108
51
return mergeTrees ( [ merged , new SwManifest ( [ merged ] ) ] ) ;
109
52
} ;
110
53
54
+
111
55
/**
112
56
@private
113
57
@method _initProject
114
58
@param {Object } options
115
59
*/
116
60
Angular2App . prototype . _initProject = function ( ) {
117
61
this . project = Project . closestSync ( process . cwd ( ) ) ;
118
-
119
- /*if (options.configPath) {
120
- this.project.configPath = function() { return options.configPath; };
121
- }*/
122
62
} ;
123
63
124
64
/**
@@ -140,6 +80,7 @@ Angular2App.prototype._notifyAddonIncluded = function () {
140
80
} , this ) ;
141
81
} ;
142
82
83
+
143
84
/**
144
85
Loads and initializes addons for this project.
145
86
Calls initializeAddons on the Project.
@@ -151,6 +92,7 @@ Angular2App.prototype.initializeAddons = function () {
151
92
this . project . initializeAddons ( ) ;
152
93
} ;
153
94
95
+
154
96
/**
155
97
Returns the content for a specific type (section) for index.html.
156
98
@@ -167,12 +109,12 @@ Angular2App.prototype.initializeAddons = function () {
167
109
types (eg. 'some-addon-section').
168
110
169
111
@private
170
- @method contentFor
112
+ @method _contentFor
171
113
@param {RegExP } match Regular expression to match against
172
114
@param {String } type Type of content
173
115
@return {String } The content.
174
116
*/
175
- Angular2App . prototype . contentFor = function ( match , type ) {
117
+ Angular2App . prototype . _contentFor = function ( match , type ) {
176
118
var content = [ ] ;
177
119
178
120
/*switch (type) {
@@ -194,15 +136,16 @@ Angular2App.prototype.contentFor = function (match, type) {
194
136
return content . join ( '\n' ) ;
195
137
} ;
196
138
139
+
197
140
/**
198
141
@private
199
- @method _configReplacePatterns
200
- @return
142
+ @method _getReplacePatterns
143
+ @return Array<Pattern> Replace patterns
201
144
*/
202
- Angular2App . prototype . _configReplacePatterns = function ( ) {
145
+ Angular2App . prototype . _getReplacePatterns = function ( ) {
203
146
return [ {
204
147
match : / \{ \{ c o n t e n t - f o r [ ' " ] ( .+ ) [ " ' ] \} \} / g,
205
- replacement : isProduction ? '' : this . contentFor . bind ( this )
148
+ replacement : isProduction ? '' : this . _contentFor . bind ( this )
206
149
} ] ;
207
150
} ;
208
151
@@ -211,10 +154,10 @@ Angular2App.prototype._configReplacePatterns = function () {
211
154
Returns the tree for app/index.html
212
155
213
156
@private
214
- @method index
157
+ @method _getIndexTree
215
158
@return {Tree } Tree for app/index.html
216
159
*/
217
- Angular2App . prototype . index = function ( ) {
160
+ Angular2App . prototype . _getIndexTree = function ( ) {
218
161
var htmlName = 'index.html' ;
219
162
var files = [
220
163
'index.html'
@@ -228,6 +171,152 @@ Angular2App.prototype.index = function () {
228
171
229
172
return configReplace ( index , {
230
173
files : [ htmlName ] ,
231
- patterns : this . _configReplacePatterns ( )
174
+ patterns : this . _getReplacePatterns ( )
175
+ } ) ;
176
+ } ;
177
+
178
+
179
+ /**
180
+ Returns the source root dir tree
181
+
182
+ @private
183
+ @method _getSourceTree
184
+ @return {Tree } Tree for the src dir
185
+ */
186
+ Angular2App . prototype . _getSourceTree = function ( ) {
187
+ return new Funnel ( 'src' , {
188
+ include : [ '*.ts' , '**/*.ts' , '**/*.d.ts' ] ,
189
+ destDir : 'src'
232
190
} ) ;
233
191
} ;
192
+
193
+
194
+ /**
195
+ Returns the typings tree
196
+
197
+ @private
198
+ @method _getTypingsTree
199
+ @return {Tree } Tree for the src dir
200
+ */
201
+ Angular2App . prototype . _getTypingsTree = function ( ) {
202
+ return new Funnel ( 'typings' , {
203
+ include : [ 'browser.d.ts' , 'browser/**' ] ,
204
+ destDir : 'typings'
205
+ } ) ;
206
+ } ;
207
+
208
+
209
+ /**
210
+ Returns the TS tree
211
+
212
+ @private
213
+ @method _getTsTree
214
+ @return {Tree } Tree for TypeScript files
215
+ */
216
+ Angular2App . prototype . _getTsTree = function ( ) {
217
+ var typingsTree = this . _getTypingsTree ( ) ;
218
+ var sourceTree = this . _getSourceTree ( ) ;
219
+
220
+ var tsconfig = JSON . parse ( fs . readFileSync ( 'src/tsconfig.json' , 'utf-8' ) ) ;
221
+ // Add all spec files to files. We need this because spec files are their own entry
222
+ // point.
223
+ fs . readdirSync ( sourceDir ) . forEach ( function addPathRecursive ( name ) {
224
+ const filePath = path . join ( sourceDir , name ) ;
225
+ if ( filePath . match ( / \. s p e c \. [ j t ] s $ / ) ) {
226
+ tsconfig . files . push ( name ) ;
227
+ } else if ( fs . statSync ( filePath ) . isDirectory ( ) ) {
228
+ // Recursively call this function with the full sub-path.
229
+ fs . readdirSync ( filePath ) . forEach ( function ( n ) {
230
+ addPathRecursive ( path . join ( name , n ) ) ;
231
+ } ) ;
232
+ }
233
+ } ) ;
234
+
235
+ // Because the tsconfig does not include the source directory, add this as the first path
236
+ // element.
237
+ tsconfig . files = tsconfig . files . map ( name => path . join ( sourceDir , name ) ) ;
238
+
239
+ var srcAndTypingsTree = mergeTrees ( [ sourceTree , typingsTree ] ) ;
240
+ var tsTree = new compileWithTypescript ( srcAndTypingsTree , tsconfig ) ;
241
+
242
+ var tsTreeExcludes = [ '*.d.ts' , 'tsconfig.json' ] ;
243
+ var excludeSpecFiles = '**/*.spec.*' ;
244
+
245
+ if ( isProduction ) {
246
+ tsTreeExcludes . push ( excludeSpecFiles ) ;
247
+ tsTree = uglify ( tsTree ) ;
248
+ }
249
+
250
+ tsTree = new Funnel ( tsTree , {
251
+ srcDir : 'src' ,
252
+ exclude : tsTreeExcludes
253
+ } ) ;
254
+
255
+ return tsTree ;
256
+ } ;
257
+
258
+
259
+ /**
260
+ Returns the `vendorNpm` tree by merging the CLI dependencies plus the ones
261
+ passed by the user
262
+
263
+ @private
264
+ @method getVendorNpmTree
265
+ @return {Tree } The NPM tree.
266
+ */
267
+ Angular2App . prototype . _getVendorNpmTree = function ( ) {
268
+ var vendorNpmFiles = [
269
+ 'systemjs/dist/system-polyfills.js' ,
270
+ 'systemjs/dist/system.src.js' ,
271
+ 'es6-shim/es6-shim.js' ,
272
+ 'angular2/bundles/angular2-polyfills.js' ,
273
+ 'rxjs/bundles/Rx.js' ,
274
+ 'angular2/bundles/angular2.dev.js' ,
275
+ 'angular2/bundles/http.dev.js' ,
276
+ 'angular2/bundles/router.dev.js' ,
277
+ 'angular2/bundles/upgrade.dev.js'
278
+ ] ;
279
+
280
+ if ( this . options . vendorNpmFiles ) {
281
+ vendorNpmFiles = vendorNpmFiles . concat ( this . options . vendorNpmFiles ) ;
282
+ }
283
+
284
+ var vendorNpmTree = new Funnel ( 'node_modules' , {
285
+ include : vendorNpmFiles ,
286
+ destDir : 'vendor'
287
+ } ) ;
288
+
289
+ return vendorNpmTree ;
290
+ } ;
291
+
292
+
293
+ /**
294
+ Returns the `assets` tree
295
+
296
+ @private
297
+ @method getAssetsTree
298
+ @return {Tree } The assets tree.
299
+ */
300
+ Angular2App . prototype . _getAssetsTree = function ( ) {
301
+ return new Funnel ( sourceDir , {
302
+ include : [ '**/*.*' ] ,
303
+ exclude : [ '**/*.ts' , '**/*.js' ] ,
304
+ allowEmpty : true
305
+ } ) ;
306
+ } ;
307
+
308
+
309
+ /**
310
+ Returns the `tsSrc` tree
311
+
312
+ @private
313
+ @method getTsSrcTree
314
+ @return {Tree } The TS src tree.
315
+ */
316
+ Angular2App . prototype . _getTsSrcTree = function ( ) {
317
+ return new Funnel ( sourceDir , {
318
+ include : [ '**/*.ts' ] ,
319
+ allowEmpty : true
320
+ } ) ;
321
+ } ;
322
+
0 commit comments