fix(tools): Update createStepFile function in new curriculum helper script (#41693)

* feat: add parseMDSync to parser

* fix: update createStepFile function

Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com>
This commit is contained in:
Randell Dawson
2021-03-31 09:19:46 -06:00
committed by GitHub
parent 40d7e04a61
commit 5bb773fcec
4 changed files with 88 additions and 49 deletions

View File

@ -4,7 +4,7 @@ const path = require('path');
const { const {
reorderSteps, reorderSteps,
createStepFile, createStepFile,
getChallengeSeed, getChallengeSeeds,
getProjectPath getProjectPath
} = require('./utils'); } = require('./utils');
@ -28,14 +28,14 @@ const getLastStepFileContent = () => {
return { return {
nextStepNum: lastStepFileNum + 1, nextStepNum: lastStepFileNum + 1,
challengeSeed: getChallengeSeed(projectPath + fileName) challengeSeeds: getChallengeSeeds(projectPath + fileName)
}; };
}; };
const projectPath = getProjectPath(); const projectPath = getProjectPath();
const { nextStepNum, challengeSeed } = getLastStepFileContent(); const { nextStepNum, challengeSeeds } = getLastStepFileContent();
createStepFile({ stepNum: nextStepNum, projectPath, challengeSeed }); createStepFile({ stepNum: nextStepNum, projectPath, challengeSeeds });
console.log(`Sucessfully added step #${nextStepNum}`); console.log(`Sucessfully added step #${nextStepNum}`);
reorderSteps(); reorderSteps();

View File

@ -1,7 +1,7 @@
const { const {
reorderSteps, reorderSteps,
createStepFile, createStepFile,
getChallengeSeed, getChallengeSeeds,
padWithLeadingZeros, padWithLeadingZeros,
getExistingStepNums, getExistingStepNums,
getProjectPath, getProjectPath,
@ -35,13 +35,13 @@ if (!allStepsExist(existingSteps, [start, end])) {
throw 'Step not created. At least one of the steps specified does not exist.'; throw 'Step not created. At least one of the steps specified does not exist.';
} }
const challengeSeed = getChallengeSeed( const challengeSeeds = getChallengeSeeds(
`${projectPath}part-${padWithLeadingZeros(start)}.md` `${projectPath}part-${padWithLeadingZeros(start)}.md`
); );
createStepFile({ createStepFile({
stepNum: start, stepNum: start,
projectPath, projectPath,
challengeSeed, challengeSeeds,
stepBetween: true stepBetween: true
}); });
console.log(`Sucessfully added step between step #${start} and step #${end}`); console.log(`Sucessfully added step between step #${start} and step #${end}`);

View File

@ -2,67 +2,106 @@ const fs = require('fs');
const path = require('path'); const path = require('path');
const matter = require('gray-matter'); const matter = require('gray-matter');
const ObjectID = require('bson-objectid'); const ObjectID = require('bson-objectid');
const { parseMDSync } = require('../challenge-parser/parser');
const padWithLeadingZeros = originalNum => { const padWithLeadingZeros = originalNum => {
/* always want file step numbers 3 digits */ /* always want file step numbers 3 digits */
return ('' + originalNum).padStart(3, '0'); return ('' + originalNum).padStart(3, '0');
}; };
const removeErms = seedCode => { const insertErms = (seedCode, erms) => {
const lines = seedCode.split('\n'); const lines = seedCode.split('\n');
return lines if (Number.isInteger(erms[0])) {
.filter(line => !line.includes('--fcc-editable-region--')) lines.splice(erms[0], 0, '--fcc-editable-region--');
.join('\n'); }
if (Number.isInteger(erms[1])) {
lines.splice(erms[1], 0, '--fcc-editable-region--');
}
return lines.join('\n');
}; };
const createStepFile = ({ const createStepFile = ({
projectPath, projectPath,
stepNum, stepNum,
challengeSeed = '', challengeSeeds = {},
stepBetween = false stepBetween = false
}) => { }) => {
if (challengeSeed) { const seedTexts = Object.values(challengeSeeds).map(
challengeSeed = removeErms(challengeSeed); ({ contents, ext, editableRegionBoundaries }) => {
const fullContents = insertErms(contents, editableRegionBoundaries);
return `\`\`\`${ext}
${fullContents}
\`\`\``;
} }
);
const seedHeads = Object.values(challengeSeeds)
.filter(({ head }) => head)
.map(
({ ext, head }) => `\`\`\`${ext}
${head}
\`\`\``
)
.join('\n');
const seedTails = Object.values(challengeSeeds)
.filter(({ tail }) => tail)
.map(
({ ext, tail }) => `\`\`\`${ext}
${tail}
\`\`\``
)
.join('\n');
const descStepNum = stepBetween ? stepNum + 1 : stepNum; const descStepNum = stepBetween ? stepNum + 1 : stepNum;
const stepDescription = `${ const stepDescription = `${
stepBetween ? 'new' : '' stepBetween ? 'new' : ''
} step ${descStepNum} instructions`; } step ${descStepNum} instructions`;
const challengeSeedSection = `<section id='challengeSeed'> const challengeSeedSection = `
# --seed--
${challengeSeed.trim()} ## --seed-contents--
</section>`; ${seedTexts.join('\n')}`;
const template = `--- const seedHeadSection = seedHeads
? `
## --before-user-code--
${seedHeads}`
: '';
const seedTailSection = seedTails
? `
## --after-user-code--
${seedTails}`
: '';
const template =
`---
id: ${ObjectID.generate()} id: ${ObjectID.generate()}
title: Part ${stepNum} title: Part ${stepNum}
challengeType: 0 challengeType: 0
--- ---
## Description # --description--
<section id='description'>
${stepDescription} ${stepDescription}
</section> # --hints--
## Tests Test 1
<section id='tests'>
\`\`\`yml \`\`\`js
tests:
- text: Test 1
testString: ''
\`\`\` \`\`\`
` +
</section> challengeSeedSection +
seedHeadSection +
## Challenge Seed seedTailSection;
${challengeSeedSection}
`;
let finalStepNum = padWithLeadingZeros(stepNum); let finalStepNum = padWithLeadingZeros(stepNum);
finalStepNum += stepBetween ? 'a' : ''; finalStepNum += stepBetween ? 'a' : '';
@ -158,19 +197,8 @@ const reorderSteps = () => {
console.log('Reordered steps'); console.log('Reordered steps');
}; };
const getChallengeSeed = challengeFilePath => { const getChallengeSeeds = challengeFilePath => {
const fileContent = fs.readFileSync(challengeFilePath, 'utf8'); return parseMDSync(challengeFilePath).files;
const matchedSection = fileContent
.toString()
.match(/<section id='challengeSeed'>(?<challengeSeed>[\s\S]+)<\/section>/);
let finalChallengeSeed = '';
if (matchedSection) {
let {
groups: { challengeSeed }
} = matchedSection;
finalChallengeSeed = challengeSeed ? challengeSeed : '';
}
return finalChallengeSeed;
}; };
const getExistingStepNums = projectPath => { const getExistingStepNums = projectPath => {
@ -208,7 +236,7 @@ const getArgValues = argv => {
module.exports = { module.exports = {
createStepFile, createStepFile,
getChallengeSeed, getChallengeSeeds,
padWithLeadingZeros, padWithLeadingZeros,
reorderSteps, reorderSteps,
getExistingStepNums, getExistingStepNums,

View File

@ -53,7 +53,6 @@ exports.parseMD = function parseMD(filename) {
const tree = processor.parse(file); const tree = processor.parse(file);
processor.run(tree, file, function (err, node, file) { processor.run(tree, file, function (err, node, file) {
if (!err) { if (!err) {
delete file.contents;
resolve(file.data); resolve(file.data);
} else { } else {
err.message += ' in file ' + filename; err.message += ' in file ' + filename;
@ -62,3 +61,15 @@ exports.parseMD = function parseMD(filename) {
}); });
}); });
}; };
exports.parseMDSync = function parseMDSync(filename) {
const file = readSync(filename);
const tree = processor.parse(file);
try {
processor.runSync(tree, file);
} catch (err) {
err.message += ' in file ' + filename;
throw err;
}
return file.data;
};