Repository navigation
Expand file tree
/
Copy pathutils.js
More file actions
123 lines (104 loc) · 3.3 KB
/
Copy pathutils.js
File metadata and controls
123 lines (104 loc) · 3.3 KB
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
const fs = require('fs');
const path = require('path');
const ObjectID = require('bson-objectid');
const matter = require('gray-matter');
const {
getMetaData
} = require('../challenge-helper-scripts/helpers/get-project-path-metadata');
const { parseMDSync } = require('../challenge-parser/parser');
const { getProjectMetaPath } = require('./helpers/get-project-meta-path');
const { getProjectPath } = require('./helpers/get-project-path');
const { getStepTemplate } = require('./helpers/get-step-template');
const { padWithLeadingZeros } = require('./helpers/pad-with-leading-zeros');
const createStepFile = ({
projectPath,
stepNum,
challengeSeeds = {},
stepBetween = false
}) => {
const challengeId = ObjectID();
let finalStepNum = padWithLeadingZeros(stepNum);
finalStepNum += stepBetween ? 'a' : '';
const template = getStepTemplate({
challengeId,
challengeSeeds,
stepBetween,
stepNum
});
fs.writeFileSync(`${projectPath}step-${finalStepNum}.md`, template);
return challengeId;
};
const reorderSteps = () => {
const projectPath = getProjectPath();
const projectName = process.env.CALLING_DIR
? process.env.CALLING_DIR.split(path.sep).slice(-1).toString()
: process.cwd().split(path.sep).slice(-1).toString();
const curriculumPath = process.env.CALLING_DIR
? ''
: path.join(__dirname, '../');
const projectMetaPath = getProjectMetaPath(curriculumPath, projectName);
const parsedData = getMetaData(projectMetaPath);
let foundFinal = false;
const filesArr = [];
fs.readdirSync(projectPath).forEach(fileName => {
if (path.extname(fileName).toLowerCase() === '.md') {
if (!fileName.endsWith('final.md')) {
filesArr.push(fileName);
} else {
foundFinal = true;
}
}
});
if (foundFinal) {
filesArr.push('final.md');
}
const filesToReorder = filesArr.map((fileName, i) => {
const newStepNum = i + 1;
const newFileName =
fileName !== 'final.md'
? `step-${padWithLeadingZeros(newStepNum)}.md`
: 'final.md';
return {
oldFileName: fileName,
newFileName,
newStepNum
};
});
const challengeOrder = [];
filesToReorder.forEach(({ oldFileName, newFileName, newStepNum }) => {
fs.renameSync(
`${projectPath}${oldFileName}`,
`${projectPath}${newFileName}.tmp`
);
const filePath = `${projectPath}${newFileName}.tmp`;
const frontMatter = matter.read(filePath);
const challengeID = frontMatter.data.id || ObjectID();
const title =
newFileName === 'final.md' ? 'Final Prototype' : `Step ${newStepNum}`;
const dashedName = `step-${newStepNum}`;
challengeOrder.push(['' + challengeID, title]);
const newData = {
...frontMatter.data,
id: challengeID,
title,
dashedName
};
fs.writeFileSync(filePath, frontMatter.stringify(newData));
});
filesToReorder.forEach(({ newFileName }) => {
fs.renameSync(
`${projectPath}${newFileName}.tmp`,
`${projectPath}${newFileName}`
);
});
const newMeta = { ...parsedData, challengeOrder };
fs.writeFileSync(projectMetaPath, JSON.stringify(newMeta, null, 2));
};
const getChallengeSeeds = challengeFilePath => {
return parseMDSync(challengeFilePath).challengeFiles;
};
module.exports = {
createStepFile,
getChallengeSeeds,
reorderSteps
};