-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGruntfile.js
318 lines (285 loc) · 13.8 KB
/
Gruntfile.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/* jshint node:true */
'use strict';
var _fs = require('fs');
var _folder = require('wysknd-lib').folder;
var _utils = require('wysknd-lib').utils;
// -------------------------------------------------------------------------------
// Help documentation
// -------------------------------------------------------------------------------
var HELP_TEXT =
'--------------------------------------------------------------------------------\n' +
' Defines tasks that are commonly used during the development process. This \n' +
' includes tasks for linting, building and testing. \n' +
' \n' +
' Supported Tasks: \n' +
' [default] : Performs standard pre-checkin activities. Runs \n' +
' jsbeautifier on all source files, validates the files \n' +
' (linting), and then executes tests against the files. \n' +
' \n' +
' env : Provides information regarding the current environment. \n' +
' This an information only task that does not alter any \n' +
' file/folder in the environment. \n' +
' \n' +
' help : Shows this help message. \n' +
' \n' +
' clean : Cleans out all build artifacts and other temporary files \n' +
' or directories. \n' +
' \n' +
' monitor:[opt1]: : Monitors files for changes, and triggers actions based \n' +
' [opt2]: on specified options. Supported options are as follows: \n' +
' [lint] : Executes jshint with default options against\n' +
' all source files. \n' +
' [unit] : Executes unit tests against all source \n' +
' files. \n' +
' \n' +
' Multiple options may be specified, and the triggers will \n' +
' be executed in the order specified. If a specific task \n' +
' requires a web server to be launched, this will be done \n' +
' automatically. \n' +
' \n' +
' jshint:dev : Executes jshint against all source files. \n' +
' \n' +
' test:unit : Executes unit tests against source files. \n' +
' \n' +
' bump:[major|minor]: Updates the version number of the package. By default, \n' +
' this task only increments the patch version number. Major\n' +
' and minor version numbers can be incremented by \n' +
' specifying the "major" or "minor" subtask. \n' +
' \n' +
' Supported Options: \n' +
' --unitTestSuite : Can be used to specify a unit test suite to execute when \n' +
' running tests. Useful when development is focused on a \n' +
' small section of the app, and there is no need to retest \n' +
' all components when runing a watch. \n' +
' \n' +
' IMPORTANT: Please note that while the grunt file exposes tasks in addition to \n' +
' --------- the ones listed below (no private tasks in grunt yet :( ), it is \n' +
' strongly recommended that just the tasks listed below be used \n' +
' during the dev/build process. \n' +
' \n' +
'--------------------------------------------------------------------------------';
module.exports = function(grunt) {
/* ------------------------------------------------------------------------
* Initialization of dependencies.
* ---------------------------------------------------------------------- */
//Time the grunt process, so that we can understand time consumed per task.
require('time-grunt')(grunt);
//Load all grunt tasks by reading package.json.
require('load-grunt-tasks')(grunt);
/* ------------------------------------------------------------------------
* Build configuration parameters
* ---------------------------------------------------------------------- */
var packageConfig = grunt.file.readJSON('package.json') || {};
var ENV = {
appName: packageConfig.name || '__UNKNOWN__',
appVersion: packageConfig.version || '__UNKNOWN__',
tree: { /* ------------------------------ */
/* <ROOT> */
'lib': { /* |--- lib */
}, /* | */
'test': { /* |--- test */
'unit': null /* | |--- unit */
}, /* | */
'coverage': null /* |--- coverage */
} /* ------------------------------ */
};
ENV.ROOT = _folder.createFolderTree('./', ENV.tree);
// This is the root url prefix for the app, and represents the path
// (relative to root), where the app will be available. This value should
// remain unchanged for most apps, but can be tweaked here if necessary.
ENV.appRoot = '/' + ENV.appName;
(function _createTreeRefs(parent, subTree) {
for(var folder in subTree) {
var folderName = folder.replace('.', '_');
parent[folderName] = parent.getSubFolder(folder);
var children = subTree[folder];
if(typeof children === 'object') {
_createTreeRefs(parent[folder], children);
}
}
})(ENV.ROOT, ENV.tree);
// Shorthand references to key folders.
var LIB = ENV.ROOT.lib;
var TEST = ENV.ROOT.test;
/* ------------------------------------------------------------------------
* Grunt task configuration
* ---------------------------------------------------------------------- */
grunt.initConfig({
/**
* Configuration for grunt-contrib-clean, which is used to:
* - Remove temporary files and folders.
*/
clean: {
tempFiles: [ ENV.ROOT.getChildPath('ep-*') ],
coverage: [ ENV.ROOT.coverage.getPath() ]
},
/**
* Configuration for grunt-mocha-istanbul, which is used to:
* - Execute server side node.js tests, with code coverage
*/
mocha_istanbul: {
options: {
reportFormats: [ 'text', 'html' ],
reporter: 'spec',
colors: true
},
default: [ TEST.unit.allFilesPattern('js') ]
},
/**
* Configuration for grunt-jsbeautifier, which is used to:
* - Beautify all javascript, html and css files prior to checkin.
*/
jsbeautifier: {
dev: [ LIB.allFilesPattern('js') ]
},
/**
* Configuration for grunt-contrib-jshint, which is used to:
* - Monitor all source/test files and trigger actions when these
* files change.
*/
jshint: {
options: {
reporter: require('jshint-stylish'),
jshintrc: true
},
dev: [ 'Gruntfile.js',
LIB.allFilesPattern('js'),
TEST.allFilesPattern('js') ]
},
/**
* Configuration for grunt-contrib-watch, which is used to:
* - Monitor all source/test files and trigger actions when these
* files change.
*/
watch: {
allSources: {
files: [ LIB.allFilesPattern(), TEST.allFilesPattern() ],
tasks: [ ]
}
},
/**
* Configuration for grunt-bump, which is used to:
* - Update the version number on package.json
*/
bump: {
options: {
push: false
}
}
});
/* ------------------------------------------------------------------------
* Task registrations
* ---------------------------------------------------------------------- */
/**
* Default task. Performs default tasks prior to checkin, including:
* - Beautifying files
* - Linting files
* - Building sources
* - Testing build artifacts
* - Cleaning up build results
*/
grunt.registerTask('default', [ 'jsbeautifier:dev',
'jshint:dev',
'test:unit',
'clean' ]);
/**
* Test task - executes client only tests, server only tests or end to end
* tests based on the test type passed in. Tests may be executed against
* dev code or build artifacts.
*/
grunt.registerTask('test',
'Executes tests against sources',
function(testType, target) {
var testAction;
target = target || 'dev';
if(testType === 'unit') {
testAction = 'mocha_istanbul:default';
var unitTestSuite = grunt.option('unitTestSuite');
if(typeof unitTestSuite === 'string' && unitTestSuite.length > 0) {
grunt.log.writeln('Running test suite: ', unitTestSuite);
grunt.config.set('mocha_istanbul.default', TEST.unit.getChildPath(unitTestSuite));
}
}
if(testAction) {
grunt.task.run(testAction);
} else {
grunt.log.warn('Unrecognized test type. Please see help (grunt help) for task usage information');
}
}
);
// Monitor task - track changes on different sources, and enable auto
// execution of tests if requested.
// - If arguments are specified (see help) execute the necessary actions
// on changes.
grunt.registerTask('monitor',
'Monitors source files for changes, and performs actions as necessary',
function() {
var tasks = [];
// Process the arguments (specified as subtasks).
for (var index = 0; index < arguments.length; index++) {
var arg = arguments[index];
var task = null;
if (arg === 'lint') {
tasks.push('jshint:dev');
} else if ('unit' === arg) {
tasks.push('test:unit');
} else {
// Unrecognized argument.
console.warn('Unrecognized argument: %s', arg);
}
}
if(tasks.length > 0) {
grunt.config.set('watch.allSources.tasks', tasks);
grunt.log.writeln('Tasks to run on change: [' + tasks + ']');
grunt.task.run('watch:allSources');
} else {
grunt.log.writeln('No tasks specified to execute on change');
}
}
);
/**
* Shows the environment setup.
*/
grunt.registerTask('env',
'Shows the current environment setup',
function() {
var separator = new Array(80).join('-');
function _showRecursive(root, indent) {
var indentChars = ' ';
if(!indent) {
indent = 0;
} else {
indentChars += '|';
}
indentChars += new Array(indent).join(' ');
indentChars += '|--- ';
var hasChildren = false;
for(var prop in root) {
var member = root[prop];
if(typeof member === 'object') {
var maxLen = 74 - (indentChars.length + prop.length);
var status = _utils.padLeft(member.getStatus(), maxLen);
grunt.log.writeln(indentChars + prop + status);
hasChildren = true;
if(_showRecursive(member, indent + 4)) {
grunt.log.writeln(' |');
}
}
}
return hasChildren;
}
grunt.log.writeln('\n' + separator);
_showRecursive(ENV.ROOT, 0);
grunt.log.writeln(separator + '\n');
}
);
/**
* Shows help information on how to use the Grunt tasks.
*/
grunt.registerTask('help',
'Displays grunt help documentation',
function(){
grunt.log.writeln(HELP_TEXT);
}
);
};