forked from ryan-roemer/node-sunny
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jakefile
314 lines (277 loc) · 7.99 KB
/
Jakefile
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
/**
* Jakefile.
*/
/*global namespace: true, desc: true, task: true, complete: true, fail: true */
///////////////////////////////////////////////////////////////////////////////
// Requires
var fs = require('fs'),
path = require('path'),
exec = require('child_process').exec,
spawn = require('child_process').spawn,
findit = require('findit');
///////////////////////////////////////////////////////////////////////////////
// Constants
var FILES_RE = {
EXCLUDE: new RegExp("(^\\./(\\.git|node_modules|docs.*|local))"),
ALL_JS: new RegExp("(Jakefile|\\.js$)"),
LIB_JS: new RegExp("(\\.js$)")
};
var NODE_UNIT_BIN = "./node_modules/nodeunit/bin/nodeunit";
var TESTS = {
CORE: {
path: "test/core.test.js"
},
LIVE: {
path: "test/live.test.js",
config: "local/live-test-config.js"
}
};
var JSLINT_BIN = "./node_modules/jslint/bin/jslint.js";
var JSLINT_OPTIONS = [
"--goodparts",
"--indent=2",
"--maxlen=80",
"--nomen=false",
"--sub"
];
var CRUFT_RE = [
"require\\(.*\\.js",
"console\\.",
].join("|");
var DOCS_OUT = "./docs_html";
var DOCS_CSS_SRC = "./docs/css";
var DOCS_CSS_OUT = "./docs_html/css";
var STYLUS_BIN = "./node_modules/stylus/bin/stylus";
var STYLUS_OPTIONS = [
// "--compress",
"--out " + DOCS_CSS_OUT
];
var JEKYLL_BIN = "jekyll";
var JEKYLL_SRC = "./docs";
var JSDOC_BIN = "jsdoc";
var JSDOC_CFG = "./docs_api/jsdoc-conf.js";
var JSDOC_OUT = DOCS_OUT + "/api";
var JSDOC_OPTIONS = [
"--conf=" + JSDOC_CFG,
"--directory=" + JSDOC_OUT,
"lib",
"test"
];
var BUILD_FILES = [
DOCS_OUT
];
///////////////////////////////////////////////////////////////////////////////
// Helpers
/**
* Run process and dump output to stdout, stderr.
*
* Arguments are same as for child_process.spawn.
*/
var runProcess = function (command, args, options) {
var psDesc = command + (args ? " " + args.join(" ") : ""),
print = function (msg) { console.log(String(msg).replace(/\n$/, '')); },
failOnErr = options.failOnErr !== false,
stderr = [],
stdout = [],
both = [],
ps,
pid;
options = options || {};
// Create process and bind handlers.
ps = spawn(command, args);
ps.stdout.on('data', function (msg) {
msg = String(msg);
both.push(msg);
stdout.push(msg);
if (options.stdoutFn) {
options.stdoutFn(msg);
} else {
print(msg);
}
});
ps.stderr.on('data', function (msg) {
msg = String(msg);
both.push(msg);
stderr.push(msg);
if (options.stderrFn) {
options.stderrFn(msg);
} else {
print(msg);
}
});
ps.on('exit', function (code) {
var msg = "Process: \"" + command + "\" (" + pid +
") exited w/ code: " + code;
if (options.endFn) {
options.endFn(code, { both: both, stdout: stdout, stderr: stderr });
}
if (code !== 0 && failOnErr) {
fail(msg);
} else {
console.log(msg);
}
});
pid = ps.pid;
console.log("Starting process (%s): \"%s\"", pid, psDesc);
};
var findFiles = function (options, callback) {
options = options || {};
var walker = findit.find(options.root || "."),
files = [];
walker.on('file', function (file) {
if ((!options.exclude || !options.exclude.test(file)) &&
(!options.include || options.include.test(file))) {
files.push(file);
}
});
walker.on('end', function () {
callback(files);
});
};
///////////////////////////////////////////////////////////////////////////////
// Targets
namespace('dev', function () {
desc("Check for debugging snippets and bad code style.");
task('cruft', function () {
findFiles({
root: ".",
exclude: FILES_RE.EXCLUDE,
include: FILES_RE.LIB_JS
}, function (files) {
runProcess("egrep", [].concat(["-n"], CRUFT_RE, files), {
endFn: function (code, output) {
if (code !== 0) {
fail("Cruft found!");
}
complete();
}
});
});
}, true);
desc("Run style checks.");
task('jslint', function () {
findFiles({
root: ".",
exclude: FILES_RE.EXCLUDE,
include: FILES_RE.ALL_JS
}, function (files) {
runProcess(JSLINT_BIN, [].concat(JSLINT_OPTIONS, files), {
stdoutFn: function () {},
stderrFn: function () {},
endFn: function (code, output) {
// Count up number of files, vs. number of "No errors found."
var numSuccess = 0,
successRe = new RegExp("^\\s*No errors found\\.\\s*$"),
removeRe = new RegExp("^\\s*$|^/\\*jslint|^No errors found.$"),
finalOut = [];
output.stdout.forEach(function (lines) {
lines.split("\n").forEach(function (line) {
// Success counter.
if (successRe.test(line)) {
numSuccess += 1;
}
// Remove unneeded final output lines.
if (!removeRe.test(line)) {
finalOut.push(line);
}
});
});
// Check for success.
console.log("\n" + finalOut.join("\n") + "\n");
if (output.stderr.length > 0 || files.length !== numSuccess) {
console.warn("JsLint failed!");
} else {
console.log("JsLint succeeded.");
}
complete();
}
});
});
}, true);
});
namespace('test', function () {
desc("Run all tests.");
task('all', ['test:core', 'test:live']);
desc("Run core module tests.");
task('core', function (config) {
runProcess(NODE_UNIT_BIN, [TESTS.CORE.path], {
endFn: complete
});
}, true);
desc("Run unit tests on real datastores (requires configuration(s)).");
task('live', function (config) {
config = config || process.env.config || TESTS.LIVE.config || null;
console.log("Using configuration: " + config);
// Resolve path to absolute (if not already);
if (new RegExp("^[^/]").test(config)) {
config = path.join(__dirname, config);
}
// Patch path into environment.
process.env.SUNNY_LIVE_TEST_CONFIG = config;
runProcess(NODE_UNIT_BIN, [TESTS.LIVE.path], {
endFn: complete
});
}, true);
});
namespace('build', function () {
desc("Clean all build files.");
task('clean', function () {
runProcess("rm", ["-rf"].concat(BUILD_FILES), {
endFn: complete
});
}, true);
desc("Compile styles.");
task('css', function () {
var mkdir = "mkdir -p " + DOCS_CSS_OUT + " && ",
opts = STYLUS_OPTIONS.join(" "),
stylus = [mkdir, STYLUS_BIN, opts, DOCS_CSS_SRC].join(" ");
exec(stylus, function (error, stdout, stderr) {
console.log(stdout);
if (error !== null) {
console.log("STDERR: " + stderr);
console.log("ERROR: " + error);
} else {
console.log("Built CSS at: " + DOCS_CSS_OUT);
}
complete();
});
}, true);
desc("Build all documentation.");
task('docs', ['build:siteDocs', 'build:apiDocs']);
desc("Build Site documentation.");
task('siteDocs', ['build:_siteDocsRaw', 'build:_CNAME', 'build:css']);
desc("Copy CNAME file (internal).");
task('_CNAME', function () {
runProcess("cp", ["CNAME", DOCS_OUT + "/CNAME"], {
endFn: complete
});
}, true);
desc("Build Site (internal).");
task('_siteDocsRaw', function () {
var jekyll = "cd " + JEKYLL_SRC + " && " + JEKYLL_BIN;
exec(jekyll, function (error, stdout, stderr) {
console.log(stdout);
if (error !== null) {
console.log("STDERR: " + stderr);
console.log("ERROR: " + error);
} else {
console.log("Built Site docs at: " + DOCS_OUT);
}
complete();
});
}, true);
desc("Build API documentation.");
task('apiDocs', ['build:css'], function () {
var jsdoc = [JSDOC_BIN].concat(JSDOC_OPTIONS).join(" ");
exec(jsdoc, function (error, stdout, stderr) {
console.log(stdout);
if (error !== null) {
console.log("STDERR: " + stderr);
console.log("ERROR: " + error);
} else {
console.log("Built API docs at: " + JSDOC_OUT);
}
complete();
});
}, true);
});