* refactor: light tweaks for readability * refactor: simplify metadata functions * fix: most tests * test: fix utils tests * test: simplify mocks * WIP: update get-last-step-file-content * feat: finish create-next-step * fix: type error * test: provide mock meta.json for test * refactor: get meta path from project path * refactor: get project name from path * refactor: simplify getProjectMetaPath further Also removes some excessive mocking * refactor: remove more mocks, always clear .env * feat: update create-next-step * feat: update create-empty steps Also refactors slightly, so it's easier to insert steps into the meta * docs: update challenge-helper-script docs * feat: create-step-between * refactor: allow metadata parse errors to propagate * fix: convert reorderSteps to renameSteps * refactor: create-step-between -> insert-step * feat: update delete-step * refactor: consolidate commands into commands.ts * refactor: clean up and consolidation * refactor: more cleanup * fix: make cli args consistent Everything accepts a single integer and nothing else * refactor: renameSteps -> updateStepTitles * docs: update with the names and args * feat: add step validating meta + files are synced
74 lines
2.0 KiB
TypeScript
74 lines
2.0 KiB
TypeScript
import fs from 'fs';
|
|
import { getProjectPath } from './helpers/get-project-info';
|
|
import { getMetaData } from './helpers/project-metadata';
|
|
import {
|
|
createStepFile,
|
|
deleteStepFromMeta,
|
|
getChallengeSeeds,
|
|
insertStepIntoMeta,
|
|
updateStepTitles
|
|
} from './utils';
|
|
|
|
function deleteStep(stepNum: number) {
|
|
if (stepNum < 1) {
|
|
throw 'Step not deleted. Step num must be a number greater than 0.';
|
|
}
|
|
|
|
const challengeOrder = getMetaData().challengeOrder;
|
|
|
|
if (stepNum > challengeOrder.length)
|
|
throw `Step # ${stepNum} not deleted. Largest step number is ${challengeOrder.length}.`;
|
|
|
|
const stepId = challengeOrder[stepNum - 1][0];
|
|
|
|
fs.unlinkSync(`${getProjectPath()}${stepId}.md`);
|
|
deleteStepFromMeta({ stepNum });
|
|
updateStepTitles();
|
|
|
|
console.log(`Sucessfully deleted step #${stepNum}`);
|
|
}
|
|
|
|
function insertStep(stepNum: number) {
|
|
if (stepNum < 1) {
|
|
throw 'Step not inserted. New step number must be greater than 0.';
|
|
}
|
|
const challengeOrder = getMetaData().challengeOrder;
|
|
|
|
if (stepNum > challengeOrder.length + 1)
|
|
throw `Step not inserted. New step number must be less than ${
|
|
challengeOrder.length + 2
|
|
}.`;
|
|
|
|
const challengeSeeds =
|
|
stepNum > 1
|
|
? getChallengeSeeds(
|
|
`${getProjectPath()}${challengeOrder[stepNum - 2][0]}.md`
|
|
)
|
|
: {};
|
|
|
|
const stepId = createStepFile({
|
|
stepNum,
|
|
challengeSeeds
|
|
});
|
|
|
|
insertStepIntoMeta({ stepNum, stepId });
|
|
updateStepTitles();
|
|
console.log(`Sucessfully inserted new step #${stepNum}`);
|
|
}
|
|
|
|
function createEmptySteps(num: number) {
|
|
if (num < 1 || num > 1000) {
|
|
throw `No steps created. arg 'num' must be between 1 and 1000 inclusive`;
|
|
}
|
|
|
|
const nextStepNum = getMetaData().challengeOrder.length + 1;
|
|
|
|
for (let stepNum = nextStepNum; stepNum < nextStepNum + num; stepNum++) {
|
|
const stepId = createStepFile({ stepNum });
|
|
insertStepIntoMeta({ stepNum, stepId });
|
|
}
|
|
console.log(`Sucessfully added ${num} steps`);
|
|
}
|
|
|
|
export { deleteStep, insertStep, createEmptySteps };
|