-
-
Notifications
You must be signed in to change notification settings - Fork 481
/
Gruntfile.js
502 lines (466 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
'use strict';
// # Globbing
// for performance reasons we're only matching one level down:
// '<%= config.src %>/templates/pages/{,*/}*.hbs'
// use this if you want to match all subfolders:
// '<%= config.src %>/templates/pages/**/*.hbs'
const yaml = require('js-yaml');
const fs = require('fs').promises;
const fse = require('fs-extra');
const git = require('simple-git');
const pkg = require('./package.json');
const mozjpeg = require('imagemin-mozjpeg');
const pngquant = require('imagemin-pngquant');
module.exports = function(grunt) {
require('time-grunt')(grunt);
require('load-grunt-tasks')(grunt, {
pattern: [
'grunt-*',
'@*/grunt-*',
'!grunt-assemble-permalinks',
'!grunt-assemble-i18n'
]
});
// Project configuration. actual tasks
grunt.initConfig({
banner: '/* p5js.org */',
config: {
src: 'src',
dist: 'dist'
},
exec: {
build_examples: 'node <%= config.src %>/data/examples/build_examples/build.js <%= config.src %>/templates/pages/examples/',
build_libraries: 'node <%= config.src %>/data/libraries/build.js <%= config.src %>/templates/pages/libraries/',
build_learn: 'node <%= config.src %>/data/learn/build.js <%= config.src %>/templates/pages/learn/'
},
watch: {
assemble: {
files: '<%= config.src %>/{content,data,templates}/**/*.{md,hbs,yml,json}',
tasks: ['assemble']
},
css: {
files: '<%= config.src %>/assets/css/*.css',
tasks: ['concat:dist', 'postcss']
},
imagemin: {
files: '<%= config.src %>/assets/img/*.{png,jpg,jpeg,gif,svg,ico}',
tasks: ['newer:imagemin']
},
livereload: {
options: {
livereload: '<%= connect.options.livereload %>'
},
files: [
'<%= config.dist %>/{,*/}*.html',
'<%= config.dist %>/assets/css/*.css',
'<%= config.dist %>/assets/js/*.js',
'<%= config.dist %>/assets/img/*.{png,jpg,jpeg,gif,webp,svg,ico}',
'<%= config.dist %>/assets/p5_featured/{,*/}*.*',
'<%= config.dist %>/assets/learn/{,*/}*.*',
'<%= config.dist %>/assets/contributor-docs/*.*'
]
}
},
// Local server:
connect: {
options: {
port: 9000,
livereload: 35729,
// change this to '0.0.0.0' to access the server from outside
hostname: 'localhost'
},
livereload: {
options: {
open: true,
base: ['<%= config.dist %>']
}
}
},
// HTML:
assemble: {
pages: {
options: {
expand: true,
flatten: true,
assets: '<%= config.dist %>/assets',
helpers: ['helpers/translation.js', 'helpers/cache-busting.js'],
layout: '<%= config.src %>/templates/layouts/default.hbs',
data: [
'<%= config.src %>/data/**/*.{json,yml}',
'!<%= config.src %>/data/reference/*.json'
],
partials: '<%= config.src %>/templates/partials/*.hbs',
plugins: ['grunt-assemble-permalinks', 'grunt-assemble-i18n'],
i18n: {
languages: pkg.languages,
templates: ['<%= config.src %>/templates/pages/**/*.hbs']
},
permalinks: {
structure: ':lang/:slug/:base:ext',
// Official documentation incorrectly states that this property
// should be "patterns" when it should be "replacements"
replacements: [
{
pattern: ':lang',
replacement: function() {
return this.language.toLowerCase() === 'en' ? '' : this.language;
}
},
{
pattern: ':base',
replacement: function() {
var check = this.basename.lastIndexOf(this.language.toLowerCase());
if (check > -1) {
return this.basename.substring(0, check - 1);
} else return this.basename;
}
}
]
}
},
dest: '<%= config.dist %>',
src: '!*.*'
}
},
requirejs: {
yuidoc_theme: {
options: {
baseUrl: '<%= config.src %>/yuidoc-p5-theme-src/scripts/',
mainConfigFile: '<%= config.src %>/yuidoc-p5-theme-src/scripts/config.js',
name: 'main',
out: '<%= config.src %>/assets/js/reference.js',
optimize: 'none',
generateSourceMaps: false,
findNestedDependencies: true,
wrap: true,
paths: {
jquery: 'empty:'
}
}
}
},
// Images:
imagemin: {
images: {
options: {
optimizationLevel: 2,
//plugins for jpeg & png image compression
use: [mozjpeg({ quality: 70 }), pngquant()]
},
files: [
{
expand: true,
cwd: '<%= config.src %>/assets/img',
src: ['**/*.{png,jpg,gif,svg,ico}'],
dest: '<%= config.dist %>/assets/img/'
}
]
}
},
// CSS:
concat: {
options: {
separator: ';'
},
dist: {
src: [
'<%= config.src %>/assets/css/normalize.css',
'<%= config.src %>/assets/css/prism.css',
'<%= config.src %>/assets/css/main.css'
],
dest: '<%= config.dist %>/assets/css/all.css'
}
},
postcss: {
options: {
map: {
inline: false,
annotation: '<%= config.dist %>/assets/css/maps/'
},
processors: [
require('autoprefixer')({
browsers: [
'Android 2.3',
'Android >= 4',
'Chrome >= 20',
'Firefox >= 24',
'Explorer >= 8',
'iOS >= 6',
'Opera >= 12',
'Safari >= 6'
]
}),
require('cssnano')()
]
},
dist: {
src: '<%= config.dist %>/assets/css/all.css'
}
},
// Data handling:
copy: {
js: {
expand: true,
cwd: '<%= config.src %>/assets/js',
src: '**',
dest: '<%= config.dist %>/assets/js/'
},
fonts: {
expand: true,
cwd: '<%= config.src %>/assets/css/fonts',
src: '**',
dest: '<%= config.dist %>/assets/css/fonts'
},
learn: {
expand: true,
cwd: '<%= config.src %>/assets/learn',
src: '**',
dest: '<%= config.dist %>/assets/learn'
},
p5_featured: {
expand: true,
cwd: '<%= config.src %>/assets/p5_featured',
src: '**',
dest: '<%= config.dist %>/assets/p5_featured'
},
examples: {
expand: true,
cwd: '<%= config.src %>/data/examples',
src: ['**', '!build_examples/**'],
dest: '<%= config.dist %>/assets/examples'
},
contributor_docs: {
expand: true,
cwd: '<%= config.src %>/assets/contributor-docs',
src: '**',
dest: '<%= config.dist %>/contributor-docs'
},
reference: {
expand: true,
cwd: '<%= config.src %>/data/reference',
src: ['**'],
dest: '<%= config.dist %>/assets/reference'
},
reference_assets: {
files: (function() {
const cp = [];
pkg.languages.forEach(language => {
if (language === 'en') {
cp.push({
expand: true,
cwd: '<%= config.src %>/templates/pages/reference/',
src: ['**/!(*.hbs)'],
dest: '<%= config.dist %>/reference/'
});
} else {
cp.push({
expand: true,
cwd: '<%= config.src %>/templates/pages/reference/',
src: ['**/!(*.hbs)'],
dest: `<%= config.dist %>/${language}/reference/`
});
}
});
return cp;
})()
},
offlineReference: {
files: [
{
expand: true,
cwd: '<%= config.dist %>/reference',
src: 'assets/**/*',
dest: '<%= config.src %>/offline-reference/'
},
{
expand: true,
cwd: '<%= config.src %>/assets',
src: 'js/**/*',
dest: '<%= config.src %>/offline-reference/'
},
{
expand: true,
cwd: '<%= config.dist %>/assets',
src: 'css/all.css',
dest: '<%= config.src %>/offline-reference/'
},
{
expand: true,
cwd: 'offline-reference/extra',
src: '**',
dest: '<%= config.src %>/offline-reference/'
}
]
}
},
// Before generating any new files,
// remove any previously-created files.
clean: {
assets: [
'<%= config.dist %>/**/*.*',
'!<%= config.dist %>/download/version.json',
'!<%= config.dist %>/git-pull.php',
'!<%= config.dist %>/books/media.zip',
'!<%= config.dist %>/learn/books/media.zip',
'<%= config.src %>/offline-reference/**/*.*'
]
},
file_append: {
default_options: {
files: [
{
prepend: 'referenceData = ',
input: '<%= config.src %>/templates/pages/reference/data.json',
output: '<%= config.src %>/offline-reference/js/data.js'
}
]
}
},
compress: {
main: {
options: {
archive: '<%= config.dist %>/offline-reference/p5-reference.zip'
},
expand: true,
cwd: '<%= config.src %>/offline-reference',
src: ['**/*'],
dest: 'p5-reference/'
}
},
htmlhint: {
html1: {
options: {
'attr-value-double-quotes': false,
'alt-require': true,
'doctype-first': true,
'title-require': true,
'attr-no-duplication': true,
'input-requires-label': true,
'tags-check': true,
'tagname-lowercase': true,
'tagname-specialchars': true,
'empty-tag-not-self-closed': true,
'id-unique': true
},
src: [
'<%= config.dist %>/**/*.html',
'!<%= config.dist %>/**/CHANGES.html',
'!<%= config.dist %>/**/README.html',
'!<%= config.dist %>/**/p5_featured/**/*.html',
'!<%= config.dist %>/**/learn/*.html',
'!<%= config.dist %>/**/examples/*.html',
'!<%= config.dist %>/**/reference/assets/index.html'
]
}
},
shell: {
generate_dataJSON: {
command: `git checkout ${grunt.option('target')} && npm ci && npm run grunt yui build`,
options: {
execOptions: {
cwd: 'tmp/p5.js'
}
}
}
}
});
grunt.registerTask('update-version', function() {
const done = this.async();
const version = grunt.option('target').substring(1);
fs.readFile('./src/data/data.yml')
.then(str => {
const data = yaml.safeLoad(str);
data.version = version;
const dump = yaml.safeDump(data);
return fs.writeFile('./src/data/data.yml', dump);
})
.then(() => {
done();
});
});
// Load modular tasks from the tasks folder
grunt.loadTasks('tasks');
grunt.loadNpmTasks('grunt-exec');
grunt.loadNpmTasks('grunt-assemble');
grunt.loadNpmTasks('grunt-file-append');
grunt.loadNpmTasks('grunt-contrib-compress');
grunt.loadNpmTasks('grunt-contrib-requirejs');
grunt.loadNpmTasks('grunt-htmlhint');
grunt.registerTask('make_tmp_dir', function() {
const tmp_path = 'tmp/p5.js';
fse.mkdirpSync(tmp_path);
});
grunt.registerTask('clone_p5js_repo', async function() {
const done = this.async();
try {
await git().clone('https://github.com/processing/p5.js', 'tmp/p5.js');
done();
} catch (err) {
console.log('Failed to clone p5.js repository.');
throw new Error(err);
}
});
grunt.registerTask('generate_dataJSON', ['shell:generate_dataJSON']);
grunt.registerTask('move_dataJSON', function() {
const dataJSON_p5js = 'tmp/p5.js/docs/reference/data.json';
const dataJSON_p5jswebsite = 'src/templates/pages/reference/data.json';
const dataJSONmin_p5js = 'tmp/p5.js/docs/reference/data.min.json';
const dataJSONmin_p5jswebsite = 'src/templates/pages/reference/data.min.json';
// move the data.json files from the cloned p5.js repository to the p5.js-website repository
fse.moveSync(dataJSON_p5js, dataJSON_p5jswebsite, { overwrite: true });
fse.moveSync(dataJSONmin_p5js, dataJSONmin_p5jswebsite, { overwrite: true });
const p5min_src = 'tmp/p5.js/lib/p5.min.js';
const p5min_dest = 'src/assets/js/p5.min.js';
const p5Soundmin_src = 'tmp/p5.js/lib/addons/p5.sound.min.js';
const p5Soundmin_dest = 'src/assets/js/p5.sound.min.js';
fse.moveSync(p5min_src, p5min_dest, { overwrite: true });
fse.moveSync(p5Soundmin_src, p5Soundmin_dest, { overwrite: true });
// delete the tmp folder that contained the p5.js repository
fse.removeSync('tmp/');
});
grunt.registerTask('generate_enJSON', function() {
const getenJSON = require('./getenJSON.js');
// generate and save the en.json
getenJSON();
});
grunt.registerTask('update-enJSON', [
'make_tmp_dir',
'clone_p5js_repo',
'generate_dataJSON',
'move_dataJSON',
'update-version',
'generate_enJSON'
]);
// multi-tasks: collections of other tasks
grunt.registerTask('server', [
'build',
'connect:livereload',
'watch'
]);
grunt.registerTask('run', [
'connect:livereload',
'watch'
]);
// optimize assets
grunt.registerTask('optimize', [
'newer:imagemin',
'concat:dist',
'postcss'
]);
// runs tasks in order
grunt.registerTask('build', [
// 'update-version',
'exec',
'clean',
'requirejs:yuidoc_theme',
'requirejs',
'copy',
'optimize',
'assemble',
'file_append',
'compress',
'htmlhint'
]);
// runs with just grunt command
grunt.registerTask('default', ['build']);
};