-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.js
145 lines (125 loc) · 3.64 KB
/
template.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
* icat-build
* https://github.com/valleykid/icatBuild
*
* Copyright (c) 2013 valleykid
* Licensed under the MIT license.
*/
'use strict';
// Basic template description.
exports.description = '创建一个icat项目.';
// Template-specific notes to be displayed before question prompts.
exports.notes = '';
// Template-specific notes to be displayed after question prompts.
exports.after = '请先执行npm install, 之后再使用grunt.';
// Any existing file or directory matching this wildcard will cause a warning.
exports.warnOn = '*';
// The actual init template.
exports.template = function(grunt, init, done){
init.process({}, [
init.prompt('name'),
init.prompt('author_name', 'author'),
init.prompt('version', '0.0.1'),
{
name: 'icat_mvc',
message: 'Do you want to use icat-mvc?',
default: 'Y/n'
},
{
name: 'css_tool',
message: 'Which one do you use, Stylus or compass?',
default: 'S/c'
},
{
name: 'merge_file',
message: 'Do you want to merge css/js files?',
default: 'Y/n'
},
{
name: 'static_path',
message: 'Set the static path, such as "../../repos".',
default: ''
}
], function(err, props){
props.name = props.name.toLocaleLowerCase();
props.devDependencies = {
'grunt-yui-compressor': '~0.3.0',
'grunt-contrib-watch': '~0.5.3'
};
var staticPath = props.static_path.replace(/\s+/, ''),
appName = props.name, cssStylus = /s/i.test(props.css_tool),
hasmvc = /y/i.test(props.icat_mvc), noMerged = !/y/i.test(props.merge_file),
gfReg = new RegExp('Gruntfile' + (cssStylus? '1':'0') + (noMerged? '0':'1') + '\\.js$');
props.keywords = [];
props.repository = '';
props.description = 'This is an applation.';
props.icat_mvc = hasmvc;
props.subapp = '';
props.prefix = '';
if(staticPath){
props.static_path = staticPath.replace(/(\w)$/, '$1/');
props.prefix = '~';
}
if(appName.indexOf('.')>-1){
var arrName = appName.split('.');
props.name = arrName[0];
props.subapp = arrName[1];
}
props.appName = props.name.toLocaleUpperCase();
props.cssStylus = cssStylus;
props.noMerged = noMerged;
// Files to copy (and process).
var exp = /^apps\//i, files = init.filesToCopy(props),
folders = noMerged? ['src', 'pic', 'assets/css', 'assets/img'] : ['pic', 'assets/css', 'assets/img'],
path = props.static_path + 'apps/' + props.name + (props.subapp? '/'+props.subapp : '') + '/';
// adjustment
if(!noMerged){
props.devDependencies['grunt-contrib-concat'] = '~0.3.0';
}
if(cssStylus){
props.devDependencies['grunt-contrib-stylus'] = '~0.10.0';
} else {
props.devDependencies['grunt-contrib-compass'] = '~0.6.0';
}
for(var k in files){
if(exp.test(k)){
if(!hasmvc && /mvc\//i.test(k)){
delete files[k];
continue;
}
if(noMerged && /src[\w\/\.]+js$/i.test(k)){
delete files[k];
continue;
}
if(cssStylus && /src[\w\/\.]+scss$/i.test(k)){
delete files[k];
continue;
}
if(!cssStylus && /src[\w\/\.]+styl$/i.test(k)){
delete files[k];
continue;
}
var _k = k.replace(exp, path)
.replace(/name([^\/]+)$/gi, props.name + (noMerged? '.source':'') + '$1');
files[_k] = files[k];
delete files[k];
}
if(/Gruntfile/.test(k)){
if(gfReg.test(k)){
files[k.replace(/\d+(\.js)/g, '$1')] = files[k];
delete files[k];
} else {
delete files[k];
continue;
}
}
}
// Actually copy (and process) files.
for(var i=0, len=folders.length; i<len; i++){ grunt.file.mkdir(path+folders[i]); }
init.copyAndProcess(files, props);
// Generate package.json file.
init.writePackageJSON('package.json', props);
// All done!
done();
});
};