forked from rwaldron/johnny-five
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGruntfile.js
355 lines (316 loc) · 9.3 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
require("copy-paste");
var inspect = require("util").inspect;
var fs = require("fs");
var shell = require("shelljs");
module.exports = function(grunt) {
var task = grunt.task;
var file = grunt.file;
var log = grunt.log;
var verbose = grunt.verbose;
var fail = grunt.fail;
var option = grunt.option;
var config = grunt.config;
var template = grunt.template;
var _ = grunt.util._;
var templates = {
eg: _.template(file.read("tpl/.eg.md")),
img: _.template(file.read("tpl/.img.md")),
fritzing: _.template(file.read("tpl/.fritzing.md")),
eglink: _.template(file.read("tpl/.readme.eglink.md")),
readme: _.template(file.read("tpl/.readme.md")),
noedit: _.template(file.read("tpl/.noedit.md")),
plugin: _.template(file.read("tpl/.plugin.md")),
};
// Project configuration.
grunt.initConfig({
pkg: grunt.file.readJSON("package.json"),
examples: {
files: ["programs.json"]
},
nodeunit: {
tests: [
"test/bootstrap.js",
"test/board.js",
"test/board-connection.js",
"test/compass.js",
"test/options.js",
"test/board.pins.js",
"test/board.component.js",
"test/capabilities.js",
// ------------------
"test/accelerometer.js",
"test/animation.js",
"test/button.js",
"test/distance.js",
"test/esc.js",
"test/fn.js",
"test/gyro.js",
"test/imu.js",
"test/lcd.js",
"test/led.js",
"test/ledcontrol.js",
"test/motor.js",
"test/pin.js",
"test/piezo.js",
"test/ping.js",
"test/pir.js",
"test/reflectancearray.js",
"test/relay.js",
"test/repl.js",
"test/sensor.js",
"test/servo.js",
"test/shiftregister.js",
"test/sonar.js",
"test/stepper.js",
"test/temperature.js",
"test/switch.js",
"test/wii.js"
]
},
jshint: {
options: {
curly: true,
eqeqeq: true,
immed: true,
latedef: false,
newcap: false,
noarg: true,
sub: true,
undef: true,
boss: true,
eqnull: true,
node: true,
strict: false,
esnext: true,
globals: {
exports: true,
document: true,
$: true,
Radar: true,
WeakMap: true,
window: true,
copy: true
}
},
files: {
src: [
"Gruntfile.js",
"lib/**/!(johnny-five)*.js",
"test/**/*.js",
"eg/**/*.js",
"wip/autobot-2.js"
]
}
},
jscs: {
files: {
src: [
"Gruntfile.js",
"lib/**/!(johnny-five)*.js",
"test/**/*.js",
"eg/**/*.js",
]
},
options: {
config: ".jscsrc",
requireCurlyBraces: [
"if",
"else",
"for",
"while",
"do",
"try",
"catch",
],
disallowNewlineBeforeBlockStatements: true,
requireSpaceBeforeBlockStatements: true,
requireParenthesesAroundIIFE: true,
requireSpacesInConditionalExpression: true,
// requireSpaceBeforeKeywords: true,
requireSpaceAfterKeywords: [
"if", "else",
"switch", "case",
"try", "catch",
"do", "while", "for",
"return", "typeof", "void",
],
validateQuoteMarks: {
mark: "\"",
escape: true
}
}
},
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,
},
}
}
});
// Support running a single test suite:
// grunt nodeunit:just:motor for example
grunt.registerTask("nodeunit:just", function(file) {
if (file) {
grunt.config("nodeunit.tests", [
"test/bootstrap.js",
"test/" + file + ".js",
]);
}
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("default", ["jshint", "jscs", "nodeunit"]);
grunt.registerMultiTask("examples", "Generate examples", function() {
// Concat specified files.
var entries = JSON.parse(file.read(file.expand(this.data)));
var readme = [];
var tplType = "eg";
entries.forEach(function(entry) {
var topic = entry.topic;
var tplType = entry.tplType || "eg";
readme.push("\n### " + topic + "\n");
entry.files.forEach(function(value) {
var markdown = [];
var filepath = "eg/" + value;
var eg = file.read(filepath);
var md = "docs/" + value.replace(".js", ".md");
var png = "docs/breadboard/" + value.replace(".js", ".png");
var fzz = "docs/breadboard/" + value.replace(".js", ".fzz");
var title = value;
// Generate a title string from the file name
[
[/^.+\//, ""],
[/\.js/, ""],
[/\-/g, " "]
].forEach(function(args) {
title = "".replace.apply(title, args);
});
var fritzpath = fzz.split("/");
var fritzfile = fritzpath[fritzpath.length - 1];
var inMarkdown = false;
// 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");
var hasPng = fs.existsSync(png);
var hasFzz = fs.existsSync(fzz);
// console.log( markdown );
var values = {
title: _.titleize(title),
command: "node " + filepath,
example: eg,
file: md,
markdown: markdown.join("\n"),
breadboard: hasPng ? templates.img({ png: png }) : "",
fritzing: hasFzz ? templates.fritzing({ fzz: fzz }) : ""
};
// Write the file to /docs/*
file.write(md, templates[tplType](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.noedit() +
templates.readme({ eglinks: readme.join("") })
);
log.writeln("Examples created.");
});
// 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
//
//
});
};