1
1
'use strict' ;
2
- var grunt = require ( 'grunt' ) ;
3
2
var yeoman = require ( 'yeoman-generator' ) ;
4
3
5
4
var GruntfileGenerator = yeoman . generators . Base . extend ( {
6
5
initializing : function ( ) {
7
6
this . pkg = require ( '../package.json' ) ;
7
+
8
+ // Check if package.json or jshint config exist
9
+ // to determine if they should be created later
10
+ if ( this . dest . exists ( 'package.json' ) ) {
11
+ this . packageJSON = this . dest . readJSON ( 'package.json' ) ;
12
+ this . appname = this . packageJSON . name || this . appname ;
13
+ this . hasJshint = this . packageJSON . hasOwnProperty ( 'jshintConfig' ) || this . dest . exists ( '.jshintrc' ) ;
14
+ } else {
15
+ this . hasJshint = this . dest . exists ( '.jshintrc' ) ;
16
+ }
17
+
18
+ function prefer ( arr , preferred ) {
19
+ for ( var i = 0 ; i < preferred . length ; ++ i ) {
20
+ if ( arr . indexOf ( preferred [ i ] ) !== - 1 ) {
21
+ return preferred [ i ] ;
22
+ }
23
+ }
24
+ return preferred [ 0 ] ;
25
+ }
26
+
27
+ var dirs = this . dest . expand ( { filter : 'isDirectory' } , '*' ) . map ( function ( d ) {
28
+ return d . slice ( 0 , - 1 ) ;
29
+ } ) ;
30
+
31
+ this . jquery = this . dest . expand ( { filter : 'isFile' } , '**/jquery*.js' ) . length > 0 ;
32
+ this . libDir = prefer ( dirs , [ 'lib' , 'src' ] ) ;
33
+ this . testDir = prefer ( dirs , [ 'test' , 'tests' , 'unit' , 'spec' ] ) ;
8
34
} ,
9
35
10
36
prompting : function ( ) {
11
37
var done = this . async ( ) ;
12
38
13
- this . log ( this . yeoman ) ;
14
- this . log ( 'This template tries to guess file and directory paths, but ' +
15
- 'you will most likely need to edit the generated Gruntfile.js file before ' +
16
- 'running grunt. _If you run grunt after generating the Gruntfile, and ' +
17
- 'it exits with errors, edit the file!_' ) ;
18
-
39
+ if ( ! this . options [ 'skip-install-message' ] ) {
40
+ this . log ( this . yeoman ) ;
41
+ this . log ( 'This template tries to guess file and directory paths, but ' +
42
+ 'you will most likely need to edit the generated Gruntfile.js file before ' +
43
+ 'running grunt. _If you run grunt after generating the Gruntfile, and ' +
44
+ 'it exits with errors, edit the file!_' ) ;
45
+ }
19
46
20
47
var prompts = [ {
21
48
type : 'checkbox' ,
@@ -24,47 +51,22 @@ var GruntfileGenerator = yeoman.generators.Base.extend({
24
51
choices : [ {
25
52
name : 'Is the DOM involved in ANY way?' ,
26
53
value : 'dom' ,
27
- checked : false
54
+ checked : true
28
55
} , {
29
56
name : 'Will files be concatenated or minified?' ,
30
57
value : 'minConcat' ,
31
- checked : false
32
- } , {
33
- name : 'Will you use a package.json file?' ,
34
- value : 'packageJSON' ,
35
- checked : false
58
+ checked : true
36
59
} ]
37
60
} ] ;
38
61
39
- function prefer ( arr , preferred ) {
40
- for ( var i = 0 ; i < preferred . length ; ++ i ) {
41
- if ( arr . indexOf ( preferred [ i ] ) !== - 1 ) {
42
- return preferred [ i ] ;
43
- }
44
- }
45
- return preferred [ 0 ] ;
46
- }
47
-
48
- var dirs = grunt . file . expand ( { filter : 'isDirectory' } , '*' ) . map ( function ( d ) {
49
- return d . slice ( 0 , - 1 ) ;
50
- } ) ;
51
-
52
- this . jquery = grunt . file . expand ( { filter : 'isFile' } , '**/jquery*.js' ) . length > 0 ;
53
- this . libDir = prefer ( dirs , [ 'lib' , 'src' ] ) ;
54
- this . testDir = prefer ( dirs , [ 'test' , 'tests' , 'unit' , 'spec' ] ) ;
55
-
56
62
this . prompt ( prompts , function ( answers ) {
57
- var features = answers . features ;
58
-
59
63
function hasFeature ( feat ) {
60
- return features . indexOf ( feat ) !== - 1 ;
64
+ return answers . features . indexOf ( feat ) !== - 1 ;
61
65
}
62
66
63
67
this . dom = hasFeature ( 'dom' ) ;
64
68
this . minConcat = hasFeature ( 'minConcat' ) ;
65
- this . packageJSON = hasFeature ( 'packageJSON' ) ;
66
69
this . testTask = hasFeature ( 'dom' ) ? 'qunit' : 'nodeunit' ;
67
- this . fileName = hasFeature ( 'packageJSON' ) ? '<%= pkg.name %>' : 'FILE_NAME' ;
68
70
69
71
done ( ) ;
70
72
} . bind ( this ) ) ;
@@ -76,9 +78,15 @@ var GruntfileGenerator = yeoman.generators.Base.extend({
76
78
77
79
writing : function ( ) {
78
80
if ( ! this . packageJSON ) {
79
- this . template ( 'package.json' ) ;
81
+ this . template ( '_package.json' , 'package.json' ) ;
82
+ }
83
+ this . template ( '_Gruntfile.js' , 'Gruntfile.js' ) ;
84
+ } ,
85
+
86
+ install : function ( ) {
87
+ if ( ! this . options [ 'skip-install' ] ) {
88
+ this . installDependencies ( ) ;
80
89
}
81
- this . template ( 'gruntfile.js' ) ;
82
90
}
83
91
} ) ;
84
92
0 commit comments