Skip to content

Commit 4a77ba7

Browse files
alexchraisedadead
authored andcommitted
feat(seed): add dasherized titles to unpacked filenames
1 parent 966b138 commit 4a77ba7

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
lines changed

repack.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@ import fs from 'fs-extra';
33
import path from 'path';
44
import {UnpackedChallenge, ChallengeFile} from './unpackedChallenge';
55

6-
const jsdiff = require('diff');
7-
86
// Repack all challenges from all
9-
// seed/unpacked/00-foo/bar/000-id.html files
7+
// seed/unpacked/00-foo/bar/000-xxx-id.html files
108
// into
119
// seed/challenges/00-foo/bar.json files
1210

unpack.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ import {UnpackedChallenge, ChallengeFile} from './unpackedChallenge';
1515
// todo: figure out embedded images etc. served from elsewhere in the project
1616
// todo: prettier/clearer CSS
1717

18+
let unpackedDir = path.join(__dirname, 'unpacked');
19+
1820
// bundle up the test-running JS
1921
function createUnpackedBundle() {
20-
let unpackedDir = path.join(__dirname, 'unpacked');
2122
fs.mkdirp(unpackedDir, (err) => {
2223
if (err && err.code !== 'EEXIST') {
2324
console.log(err);
@@ -44,20 +45,34 @@ function createUnpackedBundle() {
4445

4546
let currentlyUnpackingDir = null;
4647

48+
async function cleanUnpackedDir(unpackedChallengeBlockDir) {
49+
let promiseToDelete = function(filePath) {
50+
filePath = path.join(unpackedChallengeBlockDir, filePath);
51+
return new Promise(() => fs.unlink(filePath));
52+
};
53+
let promises = fs.readdirSync(unpackedChallengeBlockDir)
54+
.filter(filePath => (/\.html$/i).test(filePath))
55+
.map(promiseToDelete);
56+
await Promise.all(promises);
57+
}
58+
4759
function unpackChallengeBlock(challengeBlock) {
4860
let challengeBlockPath = path.parse(challengeBlock.fileName);
4961
let unpackedChallengeBlockDir = path.join(
50-
__dirname,
51-
'unpacked',
62+
unpackedDir,
5263
challengeBlockPath.dir,
5364
challengeBlockPath.name
5465
);
66+
5567
fs.mkdirp(unpackedChallengeBlockDir, (err) => {
5668
if (err && err.code !== 'EEXIST') {
5769
console.log(err);
5870
throw err;
5971
}
6072

73+
// remove existing unpacked files
74+
cleanUnpackedDir(unpackedChallengeBlockDir);
75+
6176
if (currentlyUnpackingDir !== challengeBlockPath.dir) {
6277
currentlyUnpackingDir = challengeBlockPath.dir;
6378
console.log(`Unpacking into ${currentlyUnpackingDir}:`);

unpackedChallenge.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import fs from 'fs-extra';
33
import path from 'path';
44
import _ from 'lodash';
5+
import { dasherize } from '../common/utils';
56

67
const jsonLinePrefix = '//--JSON:';
78
const paragraphBreak = '<!--break-->';
@@ -28,7 +29,6 @@ class ChallengeFile {
2829
});
2930
}
3031

31-
3232
readChunks() {
3333
// todo: make this work async
3434
// todo: make sure it works with encodings
@@ -142,7 +142,7 @@ class UnpackedChallenge {
142142
// eslint-disable-next-line no-nested-ternary
143143
let prefix = ((this.index < 10) ? '00' : (this.index < 100) ? '0' : '')
144144
+ this.index;
145-
return `${prefix}-${this.challenge.id}`;
145+
return `${prefix}-${dasherize(this.challenge.title)}-${this.challenge.id}`;
146146
}
147147

148148
expandedDescription() {

0 commit comments

Comments
 (0)