feat(learn): Create tool to delete an existing project step in project-based curriculum (#39786)

* feat: created tool to delete step

* docs: update README.md with delete-step instructions

Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com>
This commit is contained in:
Randell Dawson
2020-10-03 02:54:19 -07:00
committed by GitHub
parent 86a1da59ff
commit 90f6d23639
6 changed files with 66 additions and 22 deletions

View File

@ -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",

View File

@ -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

View File

@ -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) {

View File

@ -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);

View File

@ -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);
}

View File

@ -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
};