feat(learn): Added project-based curriculum tools (#39448)
* feat: added project-based curriculum tools * fix: allow script to work /w or /wo npm run * fix: moved console.log to reorder-steps function * fix: integrated bson-objectid library
This commit is contained in:
committed by
Mrugesh Mohapatra
parent
b9c9a95223
commit
7565849d7d
46
curriculum/tools/create-next-step.js
Normal file
46
curriculum/tools/create-next-step.js
Normal file
@@ -0,0 +1,46 @@
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
const { reorderSteps, createStepFile } = require('./utils');
|
||||
|
||||
const getLastStepFileContent = () => {
|
||||
const filesArr = [];
|
||||
fs.readdirSync(projectPath).forEach(fileName => {
|
||||
if (
|
||||
path.extname(fileName).toLowerCase() === '.md' &&
|
||||
!fileName.endsWith('final.md')
|
||||
) {
|
||||
filesArr.push(fileName);
|
||||
}
|
||||
});
|
||||
|
||||
const fileName = filesArr[filesArr.length - 1];
|
||||
let lastStepFileNum = fileName.split('.')[0].split('-')[1];
|
||||
lastStepFileNum = parseInt(lastStepFileNum, 10);
|
||||
if (filesArr.length !== lastStepFileNum) {
|
||||
throw `Error: The last file step is ${lastStepFileNum} and there are ${filesArr.length} files.`;
|
||||
}
|
||||
const fileContent = fs.readFileSync(projectPath + fileName, 'utf8');
|
||||
const matchedSection = fileContent
|
||||
.toString()
|
||||
.match(/<section id='challengeSeed'>(?<challengeSeed>[\s\S]+)<\/section>/);
|
||||
let finalChallengeSeed;
|
||||
if (matchedSection) {
|
||||
let {
|
||||
groups: { challengeSeed }
|
||||
} = matchedSection;
|
||||
finalChallengeSeed = challengeSeed ? challengeSeed : '';
|
||||
}
|
||||
return {
|
||||
nextStepNum: lastStepFileNum + 1,
|
||||
challengeSeed: finalChallengeSeed
|
||||
};
|
||||
};
|
||||
|
||||
const projectPath = (process.env.CALLING_DIR || process.cwd()) + path.sep;
|
||||
|
||||
const { nextStepNum, challengeSeed } = getLastStepFileContent();
|
||||
|
||||
createStepFile({ stepNum: nextStepNum, projectPath, challengeSeed });
|
||||
console.log(`Sucessfully added step #${nextStepNum}`);
|
||||
reorderSteps();
|
Reference in New Issue
Block a user