forked from jamesshore/lets_code_javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJakefile.js
More file actions
341 lines (269 loc) · 8.77 KB
/
Jakefile.js
File metadata and controls
341 lines (269 loc) · 8.77 KB
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
// Copyright (c) 2012 Titanium I.T. LLC. All rights reserved. See LICENSE.txt for details.
/*global desc, task, file, jake, rule, fail, complete, directory*/
(function() {
"use strict";
var startTime = Date.now();
if (!process.env.loose) console.log("For more forgiving test settings, use 'loose=true'");
var REQUIRED_BROWSERS = [
"IE 8.0.0 (Windows 7)",
"IE 9.0.0 (Windows 7)",
"Firefox 35.0.0 (Mac OS X 10.10)",
"Chrome 40.0.2214 (Mac OS X 10.10.2)",
"Safari 8.0.3 (Mac OS X 10.10.2)",
"Mobile Safari 7.0.0 (iOS 7.1)"
];
var GENERATED_DIR = "generated";
var TEMP_TESTFILE_DIR = GENERATED_DIR + "/test";
var BUILD_DIR = GENERATED_DIR + "/build";
var BUILD_CLIENT_DIR = BUILD_DIR + "/client";
var INCREMENTAL_DIR = "generated/incremental";
var SERVER_TEST_TARGET = INCREMENTAL_DIR + "/server.test";
var CLIENT_TEST_TARGET = INCREMENTAL_DIR + "/client.test";
//*** DIRECTORIES
directory(TEMP_TESTFILE_DIR);
directory(BUILD_DIR);
directory(BUILD_CLIENT_DIR);
directory(INCREMENTAL_DIR);
//*** GENERAL
desc("Delete all generated files");
task("clean", [], function() {
jake.rmRf(GENERATED_DIR);
});
desc("Lint and test everything");
task("default", [ "clean", "quick", "smoketest" ], function() {
buildOk();
});
desc("Incrementally lint and test fast targets");
task("quick", [ "nodeVersion", "lint", "testServer", "testClient" ], function() {
buildOk();
});
desc("Start Karma server for testing");
task("karma", function() {
karma().serve("build/karma.conf.js", complete, fail);
}, {async: true});
desc("Start localhost server for manual testing");
task("run", [ "build" ], function() {
var runServer = require("./src/_run_server.js");
console.log("Running server. Press Ctrl-C to stop.");
runServer.runInteractively();
// We never call complete() because we want the task to hang until the user presses 'Ctrl-C'.
}, {async: true});
//*** LINT
desc("Lint everything");
task("lint", nodeLintDirectories());
task("lint", nodeLintOutput());
createDirectoryDependencies(nodeLintDirectories());
rule(".lint", determineLintDependency, function() {
var passed = lint().validateFile(this.source, lintOptions(), lintGlobals());
if (passed) fs().writeFileSync(this.name, "lint ok");
else fail("Lint failed");
});
//*** TEST
desc("Test server code");
task("testServer", [INCREMENTAL_DIR, TEMP_TESTFILE_DIR, SERVER_TEST_TARGET ]);
file(SERVER_TEST_TARGET, serverFiles(), function() {
nodeunit().runTests(serverTestFiles(), succeed, fail);
function succeed() {
fs().writeFileSync(SERVER_TEST_TARGET, "test ok");
complete();
}
}, {async: true});
desc("Test client code");
task("testClient", [ INCREMENTAL_DIR, CLIENT_TEST_TARGET ]);
file(CLIENT_TEST_TARGET, clientFiles(), function() {
karma().runTests(REQUIRED_BROWSERS, succeed, fail);
function succeed() {
fs().writeFileSync(CLIENT_TEST_TARGET, "test ok");
complete();
}
}, {async: true});
desc("End-to-end smoke tests");
task("smoketest", [ "build" ], function() {
nodeunit().runTests(smokeTestFiles(), complete, fail);
}, {async: true});
//*** BUILD
desc("Bundle and build code");
task("build", [ BUILD_CLIENT_DIR ], function() {
var fs = require("fs");
var shell = require("shelljs");
var browserify = require("browserify");
shell.rm("-rf", BUILD_CLIENT_DIR + "/*");
shell.cp(
"-R",
"src/client/*.html", "src/client/*.css", "src/client/images", "src/client/vendor",
BUILD_CLIENT_DIR
);
console.log("Bundling client files with Browserify...");
var b = browserify({ debug: true });
b.require("./src/client/client.js", {expose: "./client.js"} );
b.require("./src/client/html_element.js", {expose: "./html_element.js"} );
b.bundle(function(err, bundle) {
if (err) fail(err);
fs.writeFileSync(BUILD_CLIENT_DIR + "/bundle.js", bundle);
complete();
});
}, {async: true});
//*** DEPLOY
desc("Deploy to Heroku");
task("deploy", function() {
console.log("To deploy to production:");
console.log("1. Integrate ('jake integrate')");
// Correction: Use "git push heroku integration:master" to deploy from integration branch.
// Thanks to Jüri A, http://www.letscodejavascript.com/v3/comments/live/32#comment-798947003 .
console.log("2. 'git push heroku integration:master' (or 'git push staging integration:master')");
console.log("3. 'jake test'");
console.log();
console.log("To deploy latest code to staging server:");
console.log("1. Make sure 'git status' is clean.");
console.log("2. 'git push staging master");
console.log("3. Visit http://wwp-staging.herokuapp.com/");
});
//*** CHECK VERSIONS
// desc("Ensure correct version of Node is present.");
task("nodeVersion", [], function() {
var versionChecker = require("./build/util/version_checker.js");
var deployedVersion = "v" + require("./package.json").engines.node;
versionChecker.check("Node", !process.env.loose, deployedVersion, process.version, fail);
});
//*** INTEGRATE
desc("Integration checklist");
task("integrate", [ "default" ], function() {
console.log("1. Make sure 'git status' is clean.");
console.log("2. Build on the integration box.");
console.log(" a. Walk over to integration box.");
console.log(" b. 'git pull'");
console.log(" c. 'npm rebuild'");
console.log(" d. Check status for files that need to be .gitignore'd");
console.log(" e. 'jake'");
console.log(" f. If jake fails, stop! Try again after fixing the issue.");
console.log("3. 'git checkout integration'");
console.log("4. 'git merge master --no-ff --log'");
console.log("5. 'git checkout master'");
});
desc("End-of-episode checklist");
task("episode", [], function() {
console.log("1. Save recording.");
console.log("2. Double-check sound and framing.");
console.log("3. Commit source code.");
console.log("4. Check Windows compatibility");
console.log(" a. Switch to Windows VM");
console.log(" b. 'git pull'");
console.log(" c. 'npm rebuild'");
console.log(" d. Check status for files that need to be .gitignore'd");
console.log(" e. 'jake'");
console.log("5. Tag episode: 'git tag -a episodeXX -m \"End of episode XX\"'");
});
//*** UTILITY FUNCTIONS
function serverTestFiles() {
return deglob("src/server/**/_*_test.js");
}
function clientFiles() {
return deglob([
"src/client/**/*.js",
"src/client/**/*.html",
"src/client/**/*.css",
]);
}
function serverFiles() {
return deglob("src/server/**/*.js");
}
function smokeTestFiles() {
return deglob("src/_*_test.js");
}
function nodeLintFiles() {
return deglob([
"*.js",
"build/util/*.js",
"src/client/*.js",
"src/server/**/*.js",
"src/*.js"
]);
}
function determineLintDependency(name) {
var result = name.replace(/^generated\/lint\//, "");
return result.replace(/\.lint$/, ".js");
}
function nodeLintDirectories() {
var path = require("path");
var result = [];
nodeLintOutput().forEach(function(lintDependency) {
result.push(path.dirname(lintDependency));
});
return result;
}
function nodeLintOutput() {
return nodeLintFiles().map(function(pathname) {
return "generated/lint/" + pathname.replace(/\.js$/, ".lint");
});
}
function lintOptions() {
return {
bitwise: true,
curly: false,
eqeqeq: true,
forin: true,
immed: true,
latedef: false,
newcap: true,
noarg: true,
noempty: true,
nonew: true,
regexp: true,
undef: true,
strict: true,
trailing: true,
node: true,
browser: true
};
}
function lintGlobals() {
return {
// Browserify
require: false,
module: false,
exports: false,
// Mocha / expect.js
describe: false,
it: false,
expect: false,
dump: false,
beforeEach: false,
afterEach: false,
before: false,
after: false,
// Browser
console: false
};
}
function buildOk() {
var elapsedSeconds = (Date.now() - startTime) / 1000;
console.log("\n\nBUILD OK (" + elapsedSeconds.toFixed(2) + "s)");
}
function createDirectoryDependencies(directories) {
directories.forEach(function(lintDirectory) {
task("lintNode", [lintDirectory]);
directory(lintDirectory);
});
}
// We've factored our require statements into functions so we don't have the overhead of loading
// modules we don't need. At the time this refactoring was done, module loading took about half a
// second, which was 10% of our desired maximum of five seconds for a quick build.
function lint() {
return require("./build/util/lint_runner.js");
}
function nodeunit() {
return require("./build/util/nodeunit_runner.js");
}
function karma() {
return require("./build/util/karma_runner.js");
}
function fs() {
return require("fs");
}
function deglob(patterns) {
var glob = require("glob");
var globPattern = patterns;
if (Array.isArray(patterns)) globPattern = "{" + patterns.join(",") + "}";
return glob.sync(globPattern);
}
}());