diff --git a/curriculum/package.json b/curriculum/package.json index d819615751..a6af835376 100644 --- a/curriculum/package.json +++ b/curriculum/package.json @@ -17,6 +17,7 @@ "create-empty-steps": "cross-env CALLING_DIR=$INIT_CWD node ../tools/challenge-helper-scripts/create-empty-steps", "create-next-step": "cross-env CALLING_DIR=$INIT_CWD node ../tools/challenge-helper-scripts/create-next-step", "create-step-between": "cross-env CALLING_DIR=$INIT_CWD node ../tools/challenge-helper-scripts/create-step-between", + "delete-step": "cross-env CALLING_DIR=$INIT_CWD node ../tools/challenge-helper-scripts/delete-step", "reorder-steps": "cross-env CALLING_DIR=$INIT_CWD node ../tools/challenge-helper-scripts/reorder-steps", "develop": "gulp", "lint": "gulp lint", diff --git a/tools/challenge-helper-scripts/README.md b/tools/challenge-helper-scripts/README.md index b427b17802..6bf02c4420 100644 --- a/tools/challenge-helper-scripts/README.md +++ b/tools/challenge-helper-scripts/README.md @@ -37,6 +37,17 @@ A one-off script that automatically adds a new step between two existing consecu ```bash npm run create-step-between start=X end=Y # where X is the starting step number and Y is the following step number. ``` + +## [delete-step.js](delete-step.js) +A one-off script that deletes an existing step and then reorders the remaining step files in the project's folder as well as updates the `challengeOrder` property array in the project's `meta.json` with the new order of the steps. + +### How to run script +1. Change to the directory of the project. +2. Run the following npm command: + ```bash + npm run delete-step num=x # where x is the step number to be deleted. + ``` + ## [reorder-steps.js](reorder-steps.js) A one-off script that automatically reorders the step files in a project's markdown files based on the filename. It also updates the `challengeOrder` property array in the project's `meta.json` with the new order of the steps. @@ -77,7 +88,7 @@ part-006.md renames to part-007.md and title changes to "Part 7" ``` Along with the above changes, the `challengeOrder` key in the project's `meta.json` file needs to reflect the new step order. This is needed because each step below a step deletion and/or step addtion changes the `title` assoiciated with each of the affected step's challenge `id`. -### Solution Steps +### How to run script 1. Change to the directory of the project. 2. Run the following npm command: ```bash diff --git a/tools/challenge-helper-scripts/create-empty-steps.js b/tools/challenge-helper-scripts/create-empty-steps.js index 97ea955d9d..fb1d4a3a05 100644 --- a/tools/challenge-helper-scripts/create-empty-steps.js +++ b/tools/challenge-helper-scripts/create-empty-steps.js @@ -2,22 +2,15 @@ const { reorderSteps, createStepFile, getExistingStepNums, - getProjectPath + getProjectPath, + getArgValues } = require('./utils'); const anyStepExists = (steps, stepsToFind) => stepsToFind.some(num => steps.includes(num)); const projectPath = getProjectPath(); -const argValuePairs = process.argv.slice(2); - -const args = argValuePairs.reduce((argsObj, arg) => { - const [argument, value] = arg.replace(/\s/g, '').split('='); - if (!argument || !value) { - throw `Invalid argument/value specified: ${arg}`; - } - return { ...argsObj, [argument]: value }; -}, {}); +const args = getArgValues(process.argv); let { num, start } = args; if (!start) { diff --git a/tools/challenge-helper-scripts/create-step-between.js b/tools/challenge-helper-scripts/create-step-between.js index 128c739abe..0d49afda43 100644 --- a/tools/challenge-helper-scripts/create-step-between.js +++ b/tools/challenge-helper-scripts/create-step-between.js @@ -4,22 +4,15 @@ const { getChallengeSeed, padWithLeadingZeros, getExistingStepNums, - getProjectPath + getProjectPath, + getArgValues } = require('./utils'); const allStepsExist = (steps, stepsToFind) => stepsToFind.every(num => steps.includes(num)); const projectPath = getProjectPath(); -const argValuePairs = process.argv.slice(2); - -const args = argValuePairs.reduce((argsObj, arg) => { - const [argument, value] = arg.replace(/\s/g, '').split('='); - if (!argument || !value) { - throw `Invalid argument/value specified: ${arg}`; - } - return { ...argsObj, [argument]: value }; -}, {}); +const args = getArgValues(process.argv); let { start, end } = args; start = parseInt(start, 10); diff --git a/tools/challenge-helper-scripts/delete-step.js b/tools/challenge-helper-scripts/delete-step.js new file mode 100644 index 0000000000..db92137cbc --- /dev/null +++ b/tools/challenge-helper-scripts/delete-step.js @@ -0,0 +1,35 @@ +const fs = require('fs'); + +const { + reorderSteps, + padWithLeadingZeros, + getExistingStepNums, + getProjectPath, + getArgValues +} = require('./utils'); + +const stepExists = (steps, stepToFind) => steps.includes(stepToFind); + +const projectPath = getProjectPath(); +const args = getArgValues(process.argv); + +let { num } = args; +num = parseInt(num, 10); + +if (!Number.isInteger(num) || num < 1) { + throw 'Step not deleted. Step num must be a number greater than 0.'; +} + +const existingSteps = getExistingStepNums(projectPath); +if (!stepExists(existingSteps, num)) { + throw `Step # ${num} not deleted because it does not exist.`; +} + +const stepFileToDelete = `${projectPath}part-${padWithLeadingZeros(num)}.md`; +try { + fs.unlinkSync(stepFileToDelete); + console.log(`Sucessfully deleted step #${num}`); + reorderSteps(); +} catch (err) { + console.error(err); +} diff --git a/tools/challenge-helper-scripts/utils.js b/tools/challenge-helper-scripts/utils.js index 246519b0cc..d31d58e8c4 100644 --- a/tools/challenge-helper-scripts/utils.js +++ b/tools/challenge-helper-scripts/utils.js @@ -201,11 +201,22 @@ const getExistingStepNums = projectPath => { const getProjectPath = () => (process.env.CALLING_DIR || process.cwd()) + path.sep; +const getArgValues = argv => { + return argv.slice(2).reduce((argsObj, arg) => { + const [argument, value] = arg.replace(/\s/g, '').split('='); + if (!argument || !value) { + throw `Invalid argument/value specified: ${arg}`; + } + return { ...argsObj, [argument]: value }; + }, {}); +}; + module.exports = { createStepFile, getChallengeSeed, padWithLeadingZeros, reorderSteps, getExistingStepNums, - getProjectPath + getProjectPath, + getArgValues };