forked from rwaldron/johnny-five
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGruntfile.js
290 lines (247 loc) · 7.31 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
var inspect = require("util").inspect,
fs = require("fs");
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 = {
doc: _.template( file.read("tpl/.docs.md") ),
img: _.template( file.read("tpl/.img.md") ),
fritzing: _.template( file.read("tpl/.fritzing.md") ),
doclink: _.template( file.read("tpl/.readme.doclink.md") ),
readme: _.template( file.read("tpl/.readme.md") )
};
// Project configuration.
grunt.initConfig({
pkg: grunt.file.readJSON("package.json"),
docs: {
files: ["programs.json"]
},
nodeunit: {
tests: [
"test/board.js",
"test/options.js",
"test/board.pins.js",
"test/capabilities.js",
// ------------------
"test/accelerometer.js",
"test/button.js",
"test/esc.js",
"test/distance.js",
"test/gyro.js",
"test/lcd.js",
"test/led.js",
"test/pin.js",
"test/piezo.js",
"test/ping.js",
"test/pir.js",
"test/relay.js",
"test/sensor.js",
"test/servo.js",
"test/sonar.js",
"test/stepper.js",
"test/switch.js",
"test/shiftregister.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
}
},
files: {
src: [
"Gruntfile.js",
"lib/**/!(johnny-five)*.js",
"test/**/*.js",
"eg/**/*.js"
]
}
},
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.loadNpmTasks("grunt-contrib-watch");
grunt.loadNpmTasks("grunt-contrib-nodeunit");
grunt.loadNpmTasks("grunt-contrib-jshint");
grunt.loadNpmTasks("grunt-jsbeautifier");
grunt.registerTask("default", ["jshint", "nodeunit"]);
grunt.registerMultiTask("docs", "generate simple docs from examples", function() {
// Concat specified files.
var entries, readme;
entries = JSON.parse(file.read(file.expand( this.data )));
readme = [];
entries.forEach(function( entry ) {
var values, markdown, eg, md, png, fzz, title,
hasPng, hasFzz, inMarkdown, filepath, fritzfile, fritzpath;
if ( Array.isArray(entry) ) {
// Produces:
// "### Heading\n"
readme.push( "\n### " + entry[0] + "\n" );
// TODO: figure out a way to have tiered subheadings
// readme.push(
// entry.reduce(function( prev, val, k ) {
// // Produces:
// // "### Board\n"
// return prev + (Array(k + 4).join("#")) + " " + val + "\n";
// }, "")
// );
}
else {
filepath = "eg/" + entry;
eg = file.read( filepath );
md = "docs/" + entry.replace(".js", ".md");
png = "docs/breadboard/" + entry.replace(".js", ".png");
fzz = "docs/breadboard/" + entry.replace(".js", ".fzz");
title = entry;
markdown = [];
// Generate a title string from the file name
[ [ /^.+\//, "" ],
[ /\.js/, "" ],
[ /\-/g, " " ]
].forEach(function( args ) {
title = "".replace.apply( title, args );
});
fritzpath = fzz.split("/");
fritzfile = fritzpath[ fritzpath.length - 1 ];
inMarkdown = false;
// Modify code in example to appear as it would if installed via npm
eg = eg.replace("../lib/johnny-five.js", "johnny-five")
.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");
hasPng = fs.existsSync(png);
hasFzz = fs.existsSync(fzz);
// console.log( markdown );
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.doc(values) );
// Push a rendered markdown link into the readme "index"
readme.push( templates.doclink(values) );
}
});
// Write the readme with doc link index
file.write( "README.md", templates.readme({ doclinks: readme.join("") }) );
log.writeln("Docs created.");
});
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();
}
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
//
//
});
};