forked from rwaldron/johnny-five
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gruntfile.js
510 lines (421 loc) · 14 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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
require("es6-shim");
require("copy-paste");
var fs = require("fs");
var exec = require("child_process").exec;
var shell = require("shelljs");
process.env.IS_TEST_MODE = true;
module.exports = function(grunt) {
var task = grunt.task;
var file = grunt.file;
var log = grunt.log;
var fail = grunt.fail;
var verbose = grunt.verbose;
var _ = grunt.util._;
var templates = {
changelog: _.template(file.read("tpl/.changelog.md")),
eg: _.template(file.read("tpl/.eg.md")),
img: _.template(file.read("tpl/.img.md")),
breadboard: _.template(file.read("tpl/.breadboard.md")),
eglink: _.template(file.read("tpl/.readme.eglink.md")),
readme: _.template(file.read("tpl/.readme.md")),
embeds: {
youtube: _.template(file.read("tpl/.embed-youtube.html")),
},
program: _.template(file.read("tpl/.eg-program-template.js")),
};
var noedit = file.read("tpl/.noedit.md");
var programsJson = JSON.parse(file.read("tpl/programs.json"));
var programsList = programsJson.reduce(function(paccum, topics) {
return paccum.concat(
topics.examples.reduce(function(faccum, example) {
return faccum.concat(["eg/" + example.file]);
}, [])
);
}, []);
var primaryFiles = [
"Gruntfile.js",
"lib/**/!(johnny-five)*.js",
"test/**/*.js",
].concat(programsList);
// Project configuration.
grunt.initConfig({
pkg: grunt.file.readJSON("package.json"),
examples: {
files: ["tpl/programs.json"]
},
nodeunit: {
tests: [
"test/bootstrap/*.js",
"test/*.js"
]
},
jshint: {
options: {
jshintrc: true
},
files: {
src: primaryFiles,
}
},
jscs: {
src: primaryFiles,
options: {
config: ".jscsrc"
}
},
jsbeautifier: {
files: ["lib/**/*.js", "eg/**/*.js", "test/**/*.js"],
options: {
js: {
braceStyle: "collapse",
breakChainedMethods: false,
e4x: false,
evalCode: false,
indentChar: " ",
indentLevel: 0,
indentSize: 2,
indentWithTabs: false,
jslintHappy: false,
keepArrayIndentation: false,
keepFunctionIndentation: false,
maxPreserveNewlines: 10,
preserveNewlines: true,
spaceBeforeConditional: true,
spaceInParen: false,
unescapeStrings: false,
wrapLineLength: 0
}
}
},
watch: {
src: {
files: [
"Gruntfile.js",
"lib/**/!(johnny-five)*.js",
"test/**/*.js",
"eg/**/*.js"
],
tasks: ["default"],
options: {
interrupt: true,
},
}
}
});
grunt.registerTask("example", "Create an example program, usage: \"grunt expample:<file-name>[.js]\"", function(fileName) {
if (!fileName.endsWith(".js")) {
fileName += ".js";
}
var pathAndFile = "eg/" + fileName;
if (file.exists(pathAndFile)) {
fail.warn(pathAndFile + " exists!");
} else {
file.write(pathAndFile, templates.program());
log.writeln("Example created: %s", pathAndFile);
}
});
// Support running a single test suite:
// grunt nodeunit:just:motor for example
grunt.registerTask("nodeunit:just", "Run a single or limited set of tests specified by a target; usage: 'grunt nodeunit:just:test-file' or 'grunt nodeunit:just:[test-file-a,test-file-b]'", function(file) {
var config = [
"test/bootstrap/*.js",
];
if (file) {
var files = [file];
//
// grunt nodeunit:just:[test-file-a,test-file-b]
//
if (file[0] === "[" && file[file.length - 1] === "]") {
files = file.match(/(\w+)/g);
}
if (files) {
files.forEach(function(file) {
config.push("test/" + file + ".js");
});
grunt.config("nodeunit.tests", config);
}
}
grunt.task.run("nodeunit");
});
//
//
// grunt beautify:file-a.js
// grunt beautify:[file-a.js,file-b.js]
//
// grunt beautify:file-a
// grunt beautify:[file-a,file-b]
//
grunt.registerTask("beautify", "Cleanup a single or limited set of files; usage: 'grunt beautify:file.js' or 'grunt beautify:[file-a.js,file-b.js]' (extension optional)", function(file) {
var files;
if (file) {
files = [file];
//
// grunt beautify:[test-file-a,test-file-b]
//
if (file[0] === "[" && file[file.length - 1] === "]") {
files = file.match(/(\w+)/g);
}
if (files) {
for (var i = 0; i < files.length; i++) {
if (!files[i].endsWith(".js")) {
files[i] += ".js";
}
}
grunt.config("jsbeautifier.files", files);
}
}
grunt.task.run("jsbeautifier");
});
// Support running a complete set of tests with
// extended (possibly-slow) tests included.
grunt.registerTask("nodeunit:complete", function() {
var testConfig = grunt.config("nodeunit.tests");
testConfig.push("test/extended/*.js");
grunt.config("nodeunit.tests", testConfig);
grunt.task.run("nodeunit");
});
grunt.loadNpmTasks("grunt-contrib-watch");
grunt.loadNpmTasks("grunt-contrib-nodeunit");
grunt.loadNpmTasks("grunt-contrib-jshint");
grunt.loadNpmTasks("grunt-jsbeautifier");
grunt.loadNpmTasks("grunt-jscs");
// grunt.registerTask("beautify", ["jsbeautifier"]);
grunt.registerTask("default", ["jshint", "jscs", "nodeunit"]);
// Explicit test task runs complete set of tests
grunt.registerTask("test", ["jshint", "jscs", "nodeunit:complete"]);
grunt.registerMultiTask("examples", "Generate examples", function() {
// Concat specified files.
var entries = JSON.parse(file.read(file.expand(this.data)));
var readme = [];
entries.forEach(function(entry) {
var topic = entry.topic;
log.writeln("Processing examples for: " + entry.topic);
readme.push("\n### " + topic + "\n");
entry.examples.sort(function(a, b) {
if (a.title < b.title) {
return -1;
}
if (a.title > b.title) {
return 1;
}
return 0;
}).forEach(function(example) {
var markdown, filepath, eg, md, inMarkdown,
images, alternates, breadboards, embeds, name, imgMarkdown, values, primary;
markdown = [];
filepath = "eg/" + example.file;
if ( !example.file || !fs.existsSync(filepath) ) {
grunt.fail.fatal("Specified example file doesn't exist: " + filepath);
}
eg = file.read(filepath);
name = (example.name || example.file).replace(".js", "");
md = "docs/" + name + ".md";
inMarkdown = false;
if (!example.title) {
example.title = null;
}
// Modify code in example to appear as it would if installed via npm
eg = eg.replace(/\.\.\/lib\/|\.js/g, "").split("\n").filter(function(line) {
if (/@markdown/.test(line)) {
inMarkdown = !inMarkdown;
return false;
}
if (inMarkdown) {
line = line.trim();
if (line) {
markdown.push(
line.replace(/^\/\//, "").trim()
);
}
// Filter out the markdown lines
// from the main content.
return false;
}
return true;
}).join("\n");
markdown = markdown.join("\n");
// If there are photo images to include
images = example.images || [];
// Get list of breadboards diagrams to include (Default: same as file name)
breadboards = example.breadboards || [{
"name": name,
"title": "Breadboard for \"" + example.title + "\"",
"auto": true,
}];
embeds = (example.embeds || []).map(function(embed) {
return templates.embeds[embed.type]({ src: embed.src });
});
// We'll combine markdown for images and breadboards
imgMarkdown = "";
primary = breadboards.shift();
images.forEach(function(img) {
if (!img.title || !img.file) {
grunt.fail.fatal("Invalid image: title and file required");
}
img.filepath = "docs/images/" + img.file;
var hasImg = fs.existsSync(img.filepath);
if (hasImg) {
imgMarkdown += templates.img({ img: img });
} else {
// If it's specified but doesn't exist, we'll consider it an error
grunt.fail.fatal("Invalid image: " + img.file);
}
});
breadboards.forEach(function(breadboard) {
imgMarkdown += breadboardMarkdown(breadboard);
});
if (example.alternates) {
alternates = example.alternates.map(function(alternate) {
return {
description: alternate.description || "",
source: file.read("eg/" + alternate.file).replace("../", "johnny-five"),
title: alternate.title || "",
};
});
}
values = {
command: "node " + filepath,
description: example.description,
embeds: embeds,
example: eg,
alternates: alternates || [],
externals: example.externals || [],
file: md,
images: imgMarkdown,
markdown: markdown,
primary: primary ? breadboardMarkdown(primary) : "",
title: example.title,
theYear: (new Date()).getUTCFullYear()
};
// Write the file to /docs/*
file.write(md, templates.eg(values));
// Push a rendered markdown link into the readme "index"
readme.push(templates.eglink(values));
});
});
// Write the readme with doc link index
file.write("README.md",
templates.readme({
noedit: noedit,
eglinks: readme.join("")
})
);
log.writeln("Examples created.");
});
function breadboardMarkdown(breadboard) {
if (!breadboard.name) {
grunt.fail.fatal("Invalid breadboard: name required");
}
breadboard.png = "docs/breadboard/" + breadboard.name + ".png";
breadboard.fzz = "docs/breadboard/" + breadboard.name + ".fzz";
breadboard.hasPng = fs.existsSync(breadboard.png);
breadboard.hasFzz = fs.existsSync(breadboard.fzz);
if (!breadboard.hasPng) {
if (breadboard.auto) {
// i.e. we tried to guess at a name but still doesn't exist
// We can just ignore and no breadboard shown
return;
} else {
// A breadboard was specified but doesn't exist - error
grunt.fail.fatal("Specified breadboard doesn't exist: " + breadboard.png);
}
}
// FZZ is optional, but we'll warn at verbose
if (!breadboard.hasFzz) {
verbose.writeln("Missing FZZ: " + breadboard.fzz);
}
return templates.breadboard({ breadboard: breadboard });
}
// run the examples task and fail if there are uncommitted changes to the docs directory
task.registerTask("test-examples", "Guard against out of date examples", ["examples", "fail-if-uncommitted-examples"]);
task.registerTask("fail-if-uncommitted-examples", function() {
task.requires("examples");
if (shell.exec("git diff --exit-code --name-status ./docs").code !== 0) {
grunt.fail.fatal("The generated examples don't match the committed examples. Please ensure you've run `grunt examples` before committing.");
}
});
grunt.registerTask("bump", "Bump the version", function(version) {
// THIS IS SLIGHTLY INSANE.
//
//
//
// I don't want the whole package.json file reformatted,
// (because it makes the contributors section look insane)
// so we're going to look at lines and update the version
// line with either the next version of the specified version.
//
// It's either this or the whole contributors section
// changes from 1 line per contributor to 3 lines per.
//
var pkg = grunt.file.read("package.json").split(/\n/).map(function(line) {
var replacement, minor, data;
if (/version/.test(line)) {
data = line.replace(/"|,/g, "").split(":")[1].split(".");
if (version) {
replacement = version;
} else {
minor = +data[2];
data[2] = ++minor;
replacement = data.join(".").trim();
}
copy(replacement);
return " \"version\": \"" + replacement + "\",";
}
return line;
});
grunt.file.write("package.json", pkg.join("\n"));
// TODO:
//
// - git commit with "vX.X.X" for commit message
// - npm publish
//
//
});
grunt.registerTask("changelog", "Generate a changelog. Range: changelog:v0.0.0--v0.0.2; Current: changelog:v0.0.2", function(version) {
var done = this.async();
var temp = "";
var previous = "";
var thisPatch;
var lastPatch;
if (!version) {
version = grunt.config("pkg.version");
}
if (version.includes("--")) {
/*
Example:
grunt changelog:v0.8.71--v0.8.73
*/
temp = version.split("--");
previous = temp[0];
version = temp[1];
} else {
/*
Example:
grunt changelog
grunt changelog:v0.8.73
*/
thisPatch = version.replace(/^v/, "").split(".").pop();
lastPatch = Number(thisPatch) - 1;
previous = version.replace(thisPatch, lastPatch);
version = "HEAD";
}
exec("git log --format='%H|%h|%an|%s' " + previous + ".." + version, function(error, result) {
if (error) {
console.log(error.message);
return;
}
var commits = result.split("\n")
.filter(function(cmt) { return cmt.trim() !== ""; })
.map(function(cmt) { return cmt.split("|"); });
var rows = commits.reduce(function(accum, commit) {
if (commit[3].indexOf("Merge") === 0) {
return accum;
}
accum += "| https://github.com/rwaldron/johnny-five/commit/" + commit[0] + " | " + commit[3] + " |\n";
return accum;
}, "");
log.writeln(templates.changelog({ rows: rows }));
done();
});
});
};