Skip to content
This repository was archived by the owner on Sep 25, 2019. It is now read-only.

Commit ffe7634

Browse files
feat(scripts): stop caching challenges and improve build scripts
Avoid caching challenges so that changes can be hotloaded into Gatsby. Add scripts to watch challenges and atomically copy them to the dist directory, without minification. ISSUES CLOSED: #68
1 parent e41f078 commit ffe7634

File tree

4 files changed

+2933
-2406
lines changed

4 files changed

+2933
-2406
lines changed

getChallenges.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const fs = require('fs');
44
const path = require('path');
55
const omit = require('lodash/omit');
66

7-
const hiddenFile = /(^(\.|\/\.))|(.md$)/g;
7+
const hiddenFile = /(^(\.|\/\.))|(.md$)|(~$)/g;
88

99
function getFilesFor(dir) {
1010
let targetDir = path.join(__dirname, dir);
@@ -47,7 +47,12 @@ module.exports = function getChallenges(challengesDir) {
4747
challengesDir = 'challenges';
4848
}
4949
return getFilesFor(challengesDir).map(function(data) {
50-
const challengeSpec = require('./' + challengesDir + '/' + data.file);
50+
// During development, challenge files are often read multiple times.
51+
// As a result, rather than clear the require cache we read them directly.
52+
const rawChallenge = fs.readFileSync(
53+
path.join(__dirname, challengesDir, data.file)
54+
);
55+
const challengeSpec = JSON.parse(rawChallenge);
5156
let superInfo = superblockInfo(data.superBlock);
5257
challengeSpec.fileName = data.file;
5358
challengeSpec.superBlock = superInfo.name;

gulpfile.js

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,56 @@ const util = require('gulp-util');
33
const jsonMinify = require('gulp-json-minify');
44
const babel = require('gulp-babel');
55
const rename = require('gulp-rename');
6+
const path = require('path');
7+
const fs = require('fs');
68

7-
gulp.task('json:minify', function() {
8-
return gulp.src('./challenges/**/*.json')
9-
.pipe(jsonMinify())
10-
.pipe(gulp.dest('dist/challenges/'))
11-
.on('error', util.log);
9+
gulp.task('json:minify', () =>
10+
gulp
11+
.src('./challenges/**/*.json')
12+
.pipe(jsonMinify())
13+
.pipe(gulp.dest('dist/challenges/'))
14+
.on('error', util.log)
15+
);
16+
17+
gulp.task('json:copy', () =>
18+
gulp
19+
.src('./challenges/**/*.json')
20+
.pipe(gulp.dest('dist/challenges/'))
21+
.on('error', util.log)
22+
);
23+
24+
gulp.task('json:watch', ['json:copy'], () => {
25+
gulp.watch('./challenges/**/*.json', event => {
26+
// Copying via streams can result in dist files being updated incrementally
27+
// this means that, if it is watching, Gatsby might try to read an
28+
// unfinished file.
29+
util.log('Updating', event.path);
30+
const src = event.path;
31+
const tail = src.split(path.sep).slice(-2);
32+
const dest = path.join(__dirname, 'dist', 'challenges', ...tail);
33+
fs.copyFileSync(src, dest);
34+
});
1235
});
1336

1437
gulp.task('babel-getChallenges', () =>
15-
gulp.src('./getChallenges.js')
16-
.pipe(babel({
17-
presets: ['env']
18-
}))
38+
gulp
39+
.src('./getChallenges.js')
40+
.pipe(
41+
babel({
42+
presets: ['env']
43+
})
44+
)
1945
.pipe(gulp.dest('dist'))
2046
);
2147

2248
gulp.task('babel', ['babel-getChallenges'], () =>
23-
gulp.src('./package-entry.js')
24-
.pipe(rename('./index.js'))
25-
.pipe(babel({
26-
presets: ['env']
27-
}))
28-
.pipe(gulp.dest('dist/'))
49+
gulp
50+
.src('./package-entry.js')
51+
.pipe(rename('./index.js'))
52+
.pipe(
53+
babel({
54+
presets: ['env']
55+
})
56+
)
57+
.pipe(gulp.dest('dist/'))
2958
);

0 commit comments

Comments
 (0)