This repository has been archived by the owner on Jun 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
template.js
264 lines (230 loc) · 7.55 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
'use strict';
// Basic template description.
exports.description = 'Create a complete Chaplin.js application.';
// Template-specific notes to be displayed before question prompts.
exports.notes = '';
// Any existing file or directory matching this wildcard will cause a warning.
exports.warnOn = '*';
// The initialize template.
exports.template = function(grunt, init, done) {
var _ = grunt.util._;
init.process({}, [
// Prompt for the following values.
// Built-In
init.prompt('name'),
init.prompt('description'),
init.prompt('version'),
init.prompt('repository'),
init.prompt('homepage'),
init.prompt('bugs'),
init.prompt('licenses'),
init.prompt('author_name'),
init.prompt('author_email'),
init.prompt('author_url'),
// Custom
{
name: 'language',
message: 'Language',
default: 'coffee',
validator: /coffee|js/,
warning: 'Must be either "coffee" for coffee-script or "js" ' +
'for javascript'
}, {
name: 'templateLanguage',
message: 'Micro-templating language',
default: 'handlebars',
validator: /handlebars|haml|underscore/,
warning: 'Must be one of the following template languages: ' +
'handlebars, haml, or underscore.'
}, {
name: 'preprocessor',
message: 'Stylesheet preprocessor',
default: 'stylus',
validator: /sass|scss|less|stylus|none/,
warning: 'Must be one of the following preprocessors: ' +
'sass, scss, less, stylus, or none (to indicate no preprocessor).'
}, {
name: 'bootstrap',
message: 'Include Twitter Bootstrap?',
default: 'Y/n',
warning: 'Yes: Twitter bootstrap is included; no: it\'s not.',
}
], function(err, props) {
// Files to copy (and process).
var files = init.filesToCopy(props);
// Normalize options.
props.bootstrap = /y/i.test(props.bootstrap);
props.preprocessor = props.preprocessor ? props.preprocessor : 'none';
// Remove files that aren't of the language requested.
var lang = props.language === 'coffee' ? 'js' : 'coffee';
files = _.omit(files, function(value, key) {
if (/^src\/scripts\/vendor/.test(key)) return false;
return _.endsWith(key, lang);
});
// Remove files that aren't of the template language requested.
var templateLanguages = {
haml: '.haml',
handlebars: '.hbs'
};
delete templateLanguages[props.templateLanguage];
files = _.omit(files, function(value, key) {
return _.any(templateLanguages, function(lang) {
return _.endsWith(key, lang);
});
});
if (props.templateLanguage === 'haml') {
// Haml can provide the index file.
// TODO: Should we generalize this check so other template languages
// that can be rendered as well as pre-compiled could be used?
delete files['src/index.html'];
}
// Remove stylesheets that aren't part of the preprocessor requested.
var preprocessors = {
none: '.css',
scss: '.scss',
sass: '.sass',
less: '.less',
stylus: '.styl'
};
delete preprocessors[props.preprocessor];
files = _.omit(files, function(value, key) {
return _.any(preprocessors, function(lang) {
return _.endsWith(key, lang);
});
});
// Gather standard and additional dependencies.
var devDependencies = {
'grunt': '0.4.x',
'grunt-contrib-clean': '0.4.0rc6',
'grunt-contrib-copy': '0.4.0rc7',
'grunt-contrib-connect': '0.1.1rc6',
'grunt-contrib-watch': '0.2.0rc7',
'grunt-requirejs': '~0.4.x',
'grunt-contrib-mincss': '0.4.0rc7',
'grunt-contrib-htmlmin': '0.1.1rc7',
'grunt-bower-task': '0.3.x',
'grunt-urequire': '~0.4.2',
'lodash': '0.10.x',
'connect-url-rewrite': '0.1.x'
};
// Language libraries
switch (props.language) {
case 'js':
devDependencies['grunt-contrib-jshint'] = '0.1.1rc6';
break;
case 'coffee':
devDependencies['grunt-contrib-coffee'] = '0.4.0rc7';
devDependencies['grunt-coffeelint'] =
// NPM seems to be out-dated; master works with grunt 0.4.x
'git://github.com/vojtajina/grunt-coffeelint.git';
break;
}
// Micro-templating library
switch (props.templateLanguage) {
case 'haml':
devDependencies['grunt-haml'] = '0.3.x';
break;
case 'handlebars':
devDependencies['grunt-contrib-handlebars'] = '~0.6.0';
break;
}
if (props.templateLanguage !== 'haml') {
// Remove the HAML runtime.
delete files['src/scripts/lib/haml.coffee'];
delete files['src/scripts/lib/haml.js'];
}
// Stylesheet preprocessor
switch (props.preprocessor) {
case 'sass':
case 'scss':
devDependencies['grunt-contrib-compass'] = '0.1.x';
break;
case 'stylus':
devDependencies['grunt-contrib-stylus'] = '0.4.0rc7';
break;
case 'less':
devDependencies['grunt-contrib-less'] = '0.5.x';
break;
}
// Add properly-named license files.
init.addLicenseFiles(files, props.licenses);
// Actually copy (and process) files.
init.copyAndProcess(files, props);
// Generate package.json file.
init.writePackageJSON('package.json', _.extend(props, {
keywords: [],
node_version: '0.8.x',
devDependencies: devDependencies
}));
// Gather client-side, browser dependencies
// Collect the standard ones.
var dependencies = {
'jquery': '1.9.x',
'underscore': '1.5.x',
'backbone': '1.1.x',
'requirejs': '2.1.x',
'almond': '0.2.x',
'chaplin': '0.11.x'
};
if (props.templateLanguage === 'handlebars') {
// Handlebars requires a run-time library.
dependencies['handlebars'] = '1.0.x'
}
// Gatrher the export overrides (these are consumed by grunt-bower-task
// to replace the main declaration in the component.json).
// Collect the standard ones
var exportsOverride = {
'jquery': {'scripts': 'jquery.js' },
'almond': {'scripts': 'almond.js' },
'backbone': {'scripts': 'backbone.js'},
'requirejs': {'scripts': 'require.js'},
'underscore': {'scripts': 'underscore.js'},
'chaplin': {'scripts': 'amd/chaplin.js'},
};
if (props.templateLanguage === 'handlebars') {
// Handlebars requires a run-time library.
exportsOverride['handlebars'] = {'scripts': 'handlebars.runtime.js'};
}
if (props.bootstrap) {
// Include the bootstrap libraries.
switch (props.preprocessor) {
case 'sass':
case 'scss':
dependencies['bootstrap-sass'] = '2.2.x';
exportsOverride['bootstrap-sass'] = {
'scripts': 'js/**/*.js',
'styles': 'lib/**/*.scss',
'images': 'img/glyphicons-*.png',
};
break;
case 'less':
dependencies['bootstrap'] = '2.2.x';
exportsOverride['bootstrap'] = {
'styles': 'less/*.less',
'scripts': 'js/*.js',
'images': 'img/glyphicons-*.png'
};
break;
case 'stylus':
default:
dependencies['bootstrap'] = '2.2.x';
exportsOverride['bootstrap'] = {
'styles': 'docs/assets/css/bootstrap*.css',
'scripts': 'js/*.js',
'images': 'img/glyphicons-*.png'
};
}
}
// Generate a bower.json file.
init.writePackageJSON('bower.json', {
name: props.name,
version: props.version,
dependencies: dependencies
}, function(pkg, props) {
pkg.exportsOverride = exportsOverride;
return pkg;
});
// All done!
done();
});
};