forked from google/blockly-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
431 lines (394 loc) · 13 KB
/
gulpfile.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
/**
* @license
* Copyright 2020 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @fileoverview Gulp tasks.
*/
const execSync = require('child_process').execSync;
const fs = require('fs');
const ghpages = require('gh-pages');
const gulp = require('gulp');
const jsgl = require('js-green-licenses');
const path = require('path');
const rimraf = require('rimraf');
const yaml = require('json-to-pretty-yaml');
gulp.header = require('gulp-header');
const appDirectory = fs.realpathSync(process.cwd());
const resolveApp = (relativePath) => path.resolve(appDirectory, relativePath);
/**
* Run the license checker for all packages.
* @return {Promise} A promise.
*/
function checkLicenses() {
const checker = new jsgl.LicenseChecker({
// dev: true,
// verbose: false,
});
checker.setDefaultHandlers();
const pluginsDir = 'plugins';
const promises = [];
// Check root package.json.
promises.push(checker.checkLocalDirectory('.'));
fs.readdirSync(pluginsDir)
.filter((file) => {
return fs.statSync(path.join(pluginsDir, file)).isDirectory();
})
.forEach((plugin) => {
const pluginDir = path.join(pluginsDir, plugin);
// Check each plugin package.json.
promises.push(checker.checkLocalDirectory(pluginDir));
});
return Promise.all(promises);
}
/**
* Prepare for publishing. Must be run before any manual publish command.
*
* Clones blockly-samples, runs build and tests, logs into npm publish service.
* @param {Function} done Completed callback.
*/
function prepareForPublish(done) {
const releaseDir = 'dist';
// Delete the release directory if it exists.
if (fs.existsSync(releaseDir)) {
console.log('Removing previous `dist/` directory.');
rimraf.sync(releaseDir);
}
// Clone a fresh copy of blockly-samples.
console.log(`Checking out a fresh copy of blockly-samples under ` +
`${path.resolve(releaseDir)}`);
execSync(
`git clone https://github.com/google/blockly-samples ${releaseDir}`,
{stdio: 'pipe'});
// Run npm ci.
console.log('Running npm ci to install.');
execSync(`npm ci`, {cwd: releaseDir, stdio: 'inherit'});
// Build all plugins.
console.log('Building all plugins.');
execSync('npm run build', {cwd: releaseDir, stdio: 'inherit'});
// Test all plugins.
console.log('Testing all plugins.');
execSync('npm run test', {cwd: releaseDir, stdio: 'inherit'});
// Login to npm.
console.log('Logging in to npm.');
execSync(
`npm login --registry https://wombat-dressing-room.appspot.com`,
{stdio: 'inherit'});
done();
}
/**
* Exit early if the release directory does not exist.
* @param {string} releaseDir release directory to check
* @param {Function} done Gulp callback.
*/
function exitIfNoReleaseDir(releaseDir, done) {
// Check that release directory exists.
if (!fs.existsSync(releaseDir)) {
console.error(
`No release directory ${releaseDir} exists. ` +
`Did you run 'npm run publish:prepare'?`);
done();
process.exit(1);
}
}
/**
* This script does not log into the npm publish service. If you haven't run
* the prepare script recently, publishing will fail for that reason.
* @param {boolean=} force True for forcing all plugins to publish, even ones
* that have not changed.
* @return {Function} Gulp task.
*/
function publish(force) {
return (done) => {
const releaseDir = 'dist';
exitIfNoReleaseDir(releaseDir, done);
// Run lerna publish. Uses conventional commits for versioning
// creates the release on GitHub.
console.log(`Publishing ${force ? 'all' : 'changed'} plugins.`);
execSync(
`lerna publish --conventional-commits --create-release github` +
`${force ? ' --force-publish=*' : ''}`,
{cwd: releaseDir, stdio: 'inherit'});
done();
};
}
/**
* Publish all plugins that have changed since the last release.
* Run `npm run publish:prepare` before running this script.
* @param {Function} done Completed callback.
* @return {Function} Gulp task.
*/
function publishManual(done) {
return publish(false)(done);
}
/**
* Forces all plugins to publish.
* Uses the built in lerna option --force-publish=*.
* @param {Function} done Completed callback.
* @return {Function} Gulp task.
*/
function forcePublish(done) {
return publish(true)(done);
}
/**
* Publishes plugins that haven't previously been uploaded to npm.
* Useful if a previous run of publishing failed after versions were
* uploaded to github but not all packages were placed uploaded to npm.
* You must run `npm run publish:prepare` first.
* @param {Function} done Completed callback.
*/
function publishFromPackage(done) {
const releaseDir = 'dist';
exitIfNoReleaseDir(releaseDir, done);
// Run lerna publish. Will not update versions.
console.log(`Publishing plugins from package.json versions.`);
execSync(
`lerna publish --from-package`,
{cwd: releaseDir, stdio: 'inherit'});
done();
}
/**
* Runs lerna version to check which version numbers would be updated.
* The version numbers will not be pushed and no tags or releases will
* be created, even if you answer 'yes' to the prompt.
* @param {Function} done Completed callback.
*/
function checkVersions(done) {
const releaseDir = 'dist';
exitIfNoReleaseDir(releaseDir, done);
// Check version numbers that would be created.
console.log('Running lerna version.',
'These version numbers will not be pushed and no tags will be created,',
'even if you answer yes to the prompt.');
execSync(
`lerna version --conventional-commits --no-git-tag-version --no-push`,
{cwd: releaseDir, stdio: 'inherit'});
done();
}
/**
* Convert json to front matter YAML config.
* @param {!Object} json The json config.
* @return {string} The front matter YAML config.
*/
function buildFrontMatter(json) {
return `---
${yaml.stringify(json)}
---
`;
}
/**
* Copy over the test page (index.html and bundled js) and the readme for
* this plugin. Add variables as needed for Jekyll.
* The resulting code lives in gh-pages/plugins/<pluginName>.
* @param {string} pluginDir The subdirectory (inside plugins/) for this plugin.
* @return {Function} Gulp task.
*/
function preparePlugin(pluginDir) {
const packageJson = require(resolveApp(`plugins/${pluginDir}/package.json`));
const files = [
`plugins/${pluginDir}/test/index.html`,
`plugins/${pluginDir}/README.md`,
];
console.log(`Preparing ${pluginDir} plugin for deployment.`);
return gulp
.src(files, {base: 'plugins/', allowEmpty: true})
// Add front matter tags to index and readme pages for Jekyll processing.
.pipe(gulp.header(buildFrontMatter({
title: `${packageJson.name} Demo`,
packageName: packageJson.name,
description: packageJson.description,
version: packageJson.version,
pageRoot: `plugins/${pluginDir}`,
pages: [
{
label: 'Playground',
link: 'test/index',
},
{
label: 'README',
link: 'README',
},
],
})))
.pipe(gulp.src(
[
'./plugins/' + pluginDir + '/build/test_bundle.js',
],
{base: './plugins/', allowEmpty: true}))
.pipe(gulp.dest('./gh-pages/plugins/'));
}
/**
* Prepare plugins for deployment to gh-pages.
*
* For each plugin, copy relevant files to the gh-pages directory.
* @param {Function} done Completed callback.
* @return {Function} Gulp task.
*/
function prepareToDeployPlugins(done) {
const dir = 'plugins';
const folders = fs.readdirSync(dir).filter(function(file) {
return fs.statSync(path.join(dir, file)).isDirectory() &&
fs.existsSync(path.join(dir, file, 'package.json'));
});
return gulp.parallel(folders.map(function(folder) {
return function preDeployPlugin() {
return preparePlugin(folder);
};
}))(done);
}
/**
* Copy over files listed in the blocklyDemoConfig.files section of the
* package.json. Add variables needed for Jekyll processing.
* The resulting code lives in gh-pages/examples/<exampleName>.
* @param {string} baseDir The base directory to use, eg: ./examples.
* @param {string} exampleDir The subdirectory (inside examples/) for this
* example.
* @param {Function} done Completed callback.
* @return {Function} Gulp task.
*/
function prepareExample(baseDir, exampleDir, done) {
const packageJson =
require(resolveApp(path.join(baseDir, exampleDir, 'package.json')));
const {blocklyDemoConfig} = packageJson;
if (!blocklyDemoConfig) {
done();
return;
}
console.log(`Preparing ${exampleDir} example for deployment.`);
blocklyDemoConfig.pageRoot = `${baseDir}/${exampleDir}`;
const pageRegex = /.*\.(html|htm|md)$/i;
const pages = blocklyDemoConfig.files.filter((f) => pageRegex.test(f));
const assets = blocklyDemoConfig.files.filter((f) => !pageRegex.test(f));
let stream = gulp.src(
pages.map((f) => path.join(baseDir, exampleDir, f)),
{base: baseDir, allowEmpty: true})
.pipe(gulp.header(buildFrontMatter(blocklyDemoConfig)));
if (assets.length) {
stream = stream.pipe(gulp.src(
assets.map((f) => path.join(baseDir, exampleDir, f)),
{base: baseDir, allowEmpty: true}));
}
return stream.pipe(gulp.dest('./gh-pages/examples/'));
}
/**
* Prepare examples/demos for deployment to gh-pages.
*
* For each examples, read the demo config, and copy relevant files to the
* gh-pages directory.
* @param {Function} done Completed callback.
* @return {Function} Gulp task.
*/
function prepareToDeployExamples(done) {
const dir = 'examples';
const folders = fs.readdirSync(dir).filter((file) => {
return fs.statSync(path.join(dir, file)).isDirectory() &&
fs.existsSync(path.join(dir, file, 'package.json'));
});
return gulp.parallel(folders.map(function(folder) {
return function preDeployExample(done) {
return prepareExample(dir, folder, done);
};
}))(done);
}
/**
* Deploy all plugins to gh-pages.
* @param {string=} repo The repo to deploy to.
* @return {Function} Gulp task.
*/
function deployToGhPages(repo) {
return (done) => {
const d = new Date();
const m = `Deploying ${d.getMonth() + 1}-${d.getDate()}-${d.getFullYear()}`;
ghpages.publish(
'gh-pages', {
message: m,
repo,
},
done);
};
}
/**
* Prepares plugins to be tested locally.
* @param {boolean} isBeta True if we want to test gh pages with the beta
* version of Blockly.
* @return {Function} Gulp task.
*/
function preparePluginsForLocal(isBeta) {
return (done) => {
if (isBeta) {
execSync(`lerna add blockly@beta --dev`, {stdio: 'inherit'});
}
execSync(`npm run boot`, {stdio: 'inherit'});
// Bundles all the plugins.
execSync(`npm run deploy:prepare:plugins`, {stdio: 'inherit'});
done();
};
}
/**
* Prepares examples to be tested locally.
* @param {boolean} isBeta True if we want to test gh pages with the beta
* version of Blockly.
* @return {Function} Gulp task.
*/
function prepareExamplesForLocal(isBeta) {
return (done) => {
const examplesDirectory = 'examples';
if (isBeta) {
execSync(
`lerna add blockly@beta`, {cwd: examplesDirectory, stdio: 'inherit'});
}
execSync(`npm run boot`, {cwd: examplesDirectory, stdio: 'inherit'});
// Bundles any examples that define a predeploy script (ex. blockly-react).
execSync(`npm run deploy:prepare:examples`, {stdio: 'inherit'});
done();
};
}
/**
* Does all the necessary tasks to run github pages locally.
* @param {boolean} isBeta True if we want to test gh pages with the beta
* version of Blockly. This is particularly helpful for testing before we
* release core.
* @return {Function} Gulp task.
*/
function testGhPagesLocally(isBeta) {
return gulp.series(
gulp.parallel(
preparePluginsForLocal(isBeta), prepareExamplesForLocal(isBeta)),
gulp.parallel(prepareToDeployPlugins, prepareToDeployExamples),
function(done) {
console.log('Starting server using "bundle exec jekyll serve"');
execSync(
`bundle exec jekyll serve`, {cwd: 'gh-pages', stdio: 'inherit'});
done();
});
}
/**
* Deploy all plugins to gh-pages on origin.
* @param {Function} done Completed callback.
* @return {Function} Gulp task.
*/
function deployToGhPagesOrigin(done) {
return deployToGhPages()(done);
}
/**
* Deploy all plugins to gh-pages on upstream.
* @param {Function} done Completed callback.
* @return {Function} Gulp task.
*/
function deployToGhPagesUpstream(done) {
return deployToGhPages('https://github.com/google/blockly-samples.git')(done);
}
module.exports = {
checkLicenses: checkLicenses,
deploy: deployToGhPagesOrigin,
deployUpstream: deployToGhPagesUpstream,
predeploy: gulp.parallel(prepareToDeployPlugins, prepareToDeployExamples),
prepareForPublish: prepareForPublish,
publishManual: publishManual,
forcePublish: forcePublish,
publishFromPackage: publishFromPackage,
checkVersions: checkVersions,
testGhPagesBeta: testGhPagesLocally(true),
testGhPages: testGhPagesLocally(false),
};