forked from freeCodeCamp/freeCodeCamp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unpack.js
110 lines (97 loc) · 3.37 KB
/
unpack.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
/* eslint-disable no-eval, no-process-exit */
import fs from 'fs-extra';
import path from 'path';
import browserify from 'browserify';
import getChallenges from './getChallenges';
import {UnpackedChallenge, ChallengeFile} from './unpackedChallenge';
// Unpack all challenges
// from all seed/challenges/00-foo/bar.json files
// into seed/unpacked/00-foo/bar/000-id.html files
//
// todo: unpack translations too
// todo: use common/app/routes/Challenges/utils/index.js:15 maps
// to determine format/style for non-JS tests
// todo: figure out embedded images etc. served from elsewhere in the project
// todo: prettier/clearer CSS
let unpackedDir = path.join(__dirname, 'unpacked');
// bundle up the test-running JS
function createUnpackedBundle() {
fs.mkdirp(unpackedDir, (err) => {
if (err && err.code !== 'EEXIST') {
console.log(err);
throw err;
}
let unpackedFile = path.join(__dirname, 'unpacked.js');
let b = browserify(unpackedFile).bundle();
b.on('error', console.error);
let unpackedBundleFile =
path.join(unpackedDir, 'unpacked-bundle.js');
const bundleFileStream = fs.createWriteStream(unpackedBundleFile);
bundleFileStream.on('finish', () => {
console.log('Wrote bundled JS into ' + unpackedBundleFile);
});
bundleFileStream.on('pipe', () => {
console.log('Writing bundled JS...');
});
bundleFileStream.on('error', console.error);
b.pipe(bundleFileStream);
// bundleFileStream.end(); // do not do this prematurely!
});
}
let currentlyUnpackingDir = null;
async function cleanUnpackedDir(unpackedChallengeBlockDir) {
let promiseToDelete = function(filePath) {
filePath = path.join(unpackedChallengeBlockDir, filePath);
return new Promise(() => fs.unlink(filePath));
};
let promises = fs.readdirSync(unpackedChallengeBlockDir)
.filter(filePath => (/\.html$/i).test(filePath))
.map(promiseToDelete);
await Promise.all(promises);
}
function unpackChallengeBlock(challengeBlock) {
let challengeBlockPath = path.parse(challengeBlock.fileName);
let unpackedChallengeBlockDir = path.join(
unpackedDir,
challengeBlockPath.dir,
challengeBlockPath.name
);
fs.mkdirp(unpackedChallengeBlockDir, (err) => {
if (err && err.code !== 'EEXIST') {
console.log(err);
throw err;
}
// remove existing unpacked files
cleanUnpackedDir(unpackedChallengeBlockDir);
if (currentlyUnpackingDir !== challengeBlockPath.dir) {
currentlyUnpackingDir = challengeBlockPath.dir;
console.log(`Unpacking into ${currentlyUnpackingDir}:`);
}
console.log(` ${challengeBlock.name}`);
// write a copy of the challenge block into unpacked dir
delete challengeBlock.fileName;
delete challengeBlock.superBlock;
delete challengeBlock.superOrder;
let challengeBlockCopy =
new ChallengeFile(
unpackedChallengeBlockDir,
challengeBlockPath.name,
'.json');
challengeBlockCopy.write(JSON.stringify(challengeBlock, null, 2));
// unpack each challenge into an HTML file
let index = 0;
challengeBlock.challenges.forEach(challenge => {
new UnpackedChallenge(
unpackedChallengeBlockDir,
challenge,
index
).unpack();
index += 1;
});
});
}
createUnpackedBundle();
let challenges = getChallenges();
challenges.forEach(challengeBlock => {
unpackChallengeBlock(challengeBlock);
});