@@ -53,6 +53,21 @@ var Generator = module.exports = function Generator(args, options) {
5353 args . push ( '--minsafe' ) ;
5454 }
5555
56+ if ( typeof this . env . options . jade === 'undefined' ) {
57+ this . option ( 'jade' , {
58+ desc : 'Generate views using Jade templates'
59+ } ) ;
60+
61+ // attempt to detect if user is using jade or not
62+ // if cml arg provided, use that; else look for the existence of cs
63+ if ( ! this . options . coffee &&
64+ this . expandFiles ( path . join ( this . appPath , '/views/**/*.jade' ) , { } ) . length > 0 ) {
65+ this . options . jade = true ;
66+ }
67+
68+ this . env . options . jade = this . options . jade ;
69+ }
70+
5671 this . hookFor ( 'angular-fullstack:common' , {
5772 args : args
5873 } ) ;
@@ -201,7 +216,13 @@ Generator.prototype.askForMongo = function askForMongo() {
201216} ;
202217
203218Generator . prototype . readIndex = function readIndex ( ) {
204- this . indexFile = this . engine ( this . read ( '../../templates/common/index.html' ) , this ) ;
219+ this . jade = this . env . options . jade ;
220+
221+ if ( this . jade ) {
222+ this . indexFile = this . engine ( this . read ( '../../templates/views/jade/index.jade' ) , this ) ;
223+ } else {
224+ this . indexFile = this . engine ( this . read ( '../../templates/views/html/index.html' ) , this ) ;
225+ }
205226} ;
206227
207228// Waiting a more flexible solution for #138
@@ -224,15 +245,21 @@ Generator.prototype.bootstrapFiles = function bootstrapFiles() {
224245 this . copy ( source + file , 'app/styles/' + file ) ;
225246 } . bind ( this ) ) ;
226247
227- this . indexFile = this . appendFiles ( {
248+ var appendOptions = {
228249 html : this . indexFile ,
229250 fileType : 'css' ,
230251 optimizedPath : 'styles/main.css' ,
231252 sourceFileList : files . map ( function ( file ) {
232253 return 'styles/' + file . replace ( '.scss' , '.css' ) ;
233254 } ) ,
234255 searchPath : [ '.tmp' , 'app' ]
235- } ) ;
256+ } ;
257+
258+ if ( this . jade ) {
259+ this . indexFile = appendFilesToJade ( appendOptions ) ;
260+ } else {
261+ this . indexFile = this . appendFiles ( appendOptions ) ;
262+ }
236263} ;
237264
238265Generator . prototype . bootstrapJS = function bootstrapJS ( ) {
@@ -241,7 +268,7 @@ Generator.prototype.bootstrapJS = function bootstrapJS() {
241268 }
242269
243270 // Wire Twitter Bootstrap plugins
244- this . indexFile = this . appendFiles ( {
271+ var appendOptions = {
245272 html : this . indexFile ,
246273 fileType : 'js' ,
247274 optimizedPath : 'scripts/plugins.js' ,
@@ -260,7 +287,13 @@ Generator.prototype.bootstrapJS = function bootstrapJS() {
260287 'bower_components/sass-bootstrap/js/popover.js'
261288 ] ,
262289 searchPath : 'app'
263- } ) ;
290+ } ;
291+
292+ if ( this . jade ) {
293+ this . indexFile = appendFilesToJade ( appendOptions ) ;
294+ } else {
295+ this . indexFile = this . appendFiles ( appendOptions ) ;
296+ }
264297} ;
265298
266299Generator . prototype . extraModules = function extraModules ( ) {
@@ -282,28 +315,110 @@ Generator.prototype.extraModules = function extraModules() {
282315 }
283316
284317 if ( modules . length ) {
285- this . indexFile = this . appendFiles ( {
318+ var appendOptions = {
286319 html : this . indexFile ,
287320 fileType : 'js' ,
288321 optimizedPath : 'scripts/modules.js' ,
289322 sourceFileList : modules ,
290323 searchPath : 'app'
291- } ) ;
324+ } ;
325+
326+ if ( this . jade ) {
327+ this . indexFile = appendFilesToJade ( appendOptions ) ;
328+ } else {
329+ this . indexFile = this . appendFiles ( appendOptions ) ;
330+ }
292331 }
293332} ;
294333
334+ function generateJadeBlock ( blockType , optimizedPath , filesBlock , searchPath , prefix ) {
335+ var blockStart , blockEnd ;
336+ var blockSearchPath = '' ;
337+
338+ if ( searchPath !== undefined ) {
339+ if ( util . isArray ( searchPath ) ) {
340+ searchPath = '{' + searchPath . join ( ',' ) + '}' ;
341+ }
342+ blockSearchPath = '(' + searchPath + ')' ;
343+ }
344+
345+ blockStart = '\n' + prefix + '<!-- build:' + blockType + blockSearchPath + ' ' + optimizedPath + ' -->\n' ;
346+ blockEnd = prefix + '<!-- endbuild -->\n' + prefix ;
347+ return blockStart + filesBlock + blockEnd ;
348+ }
349+
350+ function appendJade ( jade , tag , blocks ) {
351+ var mark = "//- build:" + tag ,
352+ position = jade . indexOf ( mark ) ;
353+ return [ jade . slice ( 0 , position ) , blocks , jade . slice ( position ) ] . join ( '' ) ;
354+ }
355+
356+ function appendFilesToJade ( jadeOrOptions , fileType , optimizedPath , sourceFileList , attrs , searchPath ) {
357+ var blocks , updatedContent ,
358+ jade = jadeOrOptions ,
359+ prefix = ' ' ,
360+ files = '' ;
361+
362+ if ( typeof jadeOrOptions === 'object' ) {
363+ jade = jadeOrOptions . html ;
364+ fileType = jadeOrOptions . fileType ;
365+ optimizedPath = jadeOrOptions . optimizedPath ;
366+ sourceFileList = jadeOrOptions . sourceFileList ;
367+ attrs = jadeOrOptions . attrs ;
368+ searchPath = jadeOrOptions . searchPath ;
369+ }
370+
371+ if ( fileType === 'js' ) {
372+ sourceFileList . forEach ( function ( el ) {
373+ files += prefix + '<script ' + ( attrs || '' ) + 'src="' + el + '"></script>\n' ;
374+ } ) ;
375+ blocks = generateJadeBlock ( 'js' , optimizedPath , files , searchPath , prefix ) ;
376+ updatedContent = appendJade ( jade , 'body' , blocks ) ;
377+ } else if ( fileType === 'css' ) {
378+ sourceFileList . forEach ( function ( el ) {
379+ files += prefix + '<link ' + ( attrs || '' ) + 'rel="stylesheet" href="' + el + '">\n' ;
380+ } ) ;
381+ blocks = generateJadeBlock ( 'css' , optimizedPath , files , searchPath , prefix ) ;
382+ updatedContent = appendJade ( jade , 'head' , blocks ) ;
383+ }
384+ return updatedContent ;
385+ }
386+
295387Generator . prototype . appJs = function appJs ( ) {
296- this . indexFile = this . appendFiles ( {
388+ var appendOptions = {
297389 html : this . indexFile ,
298390 fileType : 'js' ,
299391 optimizedPath : 'scripts/scripts.js' ,
300392 sourceFileList : [ 'scripts/app.js' , 'scripts/controllers/main.js' ] ,
301393 searchPath : [ '.tmp' , 'app' ]
302- } ) ;
394+ } ;
395+ if ( this . jade ) {
396+ this . indexFile = appendFilesToJade ( appendOptions ) ;
397+ } else {
398+ this . indexFile = this . appendFiles ( appendOptions ) ;
399+ }
400+ } ;
401+
402+ Generator . prototype . createIndex = function createIndex ( ) {
403+ if ( this . jade ) {
404+ this . write ( path . join ( this . appPath , 'views' , 'index.jade' ) , this . indexFile ) ;
405+ } else {
406+ this . write ( path . join ( this . appPath , 'views' , 'index.html' ) , this . indexFile ) ;
407+ }
303408} ;
304409
305- Generator . prototype . createIndexHtml = function createIndexHtml ( ) {
306- this . write ( path . join ( this . appPath , 'views' , 'index.html' ) , this . indexFile ) ;
410+ Generator . prototype . addJadeViews = function addHtmlJade ( ) {
411+ if ( this . jade ) {
412+ this . copy ( '../../templates/views/jade/partials/main.jade' , 'app/views/partials/main.jade' ) ;
413+ this . copy ( '../../templates/views/jade/404.jade' , 'app/views/404.jade' ) ;
414+ }
415+ } ;
416+
417+ Generator . prototype . addHtmlViews = function addHtmlViews ( ) {
418+ if ( ! this . jade ) {
419+ this . copy ( '../../templates/views/html/partials/main.html' , 'app/views/partials/main.html' ) ;
420+ this . copy ( '../../templates/views/html/404.html' , 'app/views/404.html' ) ;
421+ }
307422} ;
308423
309424Generator . prototype . packageFiles = function ( ) {
0 commit comments