2022-01-25 11:34:16 +01:00
|
|
|
import fs from 'fs';
|
|
|
|
import path from 'path';
|
|
|
|
import ObjectID from 'bson-objectid';
|
|
|
|
import * as matter from 'gray-matter';
|
|
|
|
import { parseMDSync } from '../challenge-parser/parser';
|
2022-03-02 16:12:20 +01:00
|
|
|
import { getMetaData, updateMetaData } from './helpers/project-metadata';
|
|
|
|
import { getProjectPath } from './helpers/get-project-info';
|
2022-01-25 11:34:16 +01:00
|
|
|
import { ChallengeSeed, getStepTemplate } from './helpers/get-step-template';
|
|
|
|
|
|
|
|
interface Options {
|
|
|
|
stepNum: number;
|
2022-03-02 16:12:20 +01:00
|
|
|
projectPath?: string;
|
2022-01-25 11:34:16 +01:00
|
|
|
challengeSeeds?: Record<string, ChallengeSeed>;
|
|
|
|
}
|
2020-08-25 02:35:46 -07:00
|
|
|
|
2020-08-27 18:57:51 -07:00
|
|
|
const createStepFile = ({
|
|
|
|
stepNum,
|
2022-03-02 16:12:20 +01:00
|
|
|
projectPath = getProjectPath(),
|
|
|
|
challengeSeeds = {}
|
2022-01-25 11:34:16 +01:00
|
|
|
}: Options) => {
|
|
|
|
const challengeId = new ObjectID();
|
2021-03-31 09:19:46 -06:00
|
|
|
|
2021-07-06 19:22:12 -05:00
|
|
|
const template = getStepTemplate({
|
|
|
|
challengeId,
|
|
|
|
challengeSeeds,
|
|
|
|
stepNum
|
|
|
|
});
|
|
|
|
|
2022-03-02 16:12:20 +01:00
|
|
|
fs.writeFileSync(`${projectPath}${challengeId.toString()}.md`, template);
|
2021-07-06 19:22:12 -05:00
|
|
|
|
2021-06-08 21:27:45 +02:00
|
|
|
return challengeId;
|
2020-08-25 02:35:46 -07:00
|
|
|
};
|
|
|
|
|
2022-03-02 16:12:20 +01:00
|
|
|
interface InsertOptions {
|
|
|
|
stepNum: number;
|
|
|
|
stepId: ObjectID;
|
|
|
|
}
|
2020-08-25 02:35:46 -07:00
|
|
|
|
2022-03-02 16:12:20 +01:00
|
|
|
function insertStepIntoMeta({ stepNum, stepId }: InsertOptions) {
|
|
|
|
const existingMeta = getMetaData();
|
|
|
|
const oldOrder = [...existingMeta.challengeOrder];
|
|
|
|
oldOrder.splice(stepNum - 1, 0, [stepId.toString()]);
|
|
|
|
// rename all the files in challengeOrder
|
|
|
|
const challengeOrder = oldOrder.map(([id], index) => [
|
|
|
|
id,
|
|
|
|
`Step ${index + 1}`
|
|
|
|
]);
|
|
|
|
|
|
|
|
updateMetaData({ ...existingMeta, challengeOrder });
|
|
|
|
}
|
2020-08-25 02:35:46 -07:00
|
|
|
|
2022-03-02 16:12:20 +01:00
|
|
|
function deleteStepFromMeta({ stepNum }: { stepNum: number }) {
|
|
|
|
const existingMeta = getMetaData();
|
|
|
|
const oldOrder = [...existingMeta.challengeOrder];
|
|
|
|
oldOrder.splice(stepNum - 1, 1);
|
|
|
|
// rename all the files in challengeOrder
|
|
|
|
const challengeOrder = oldOrder.map(([id], index) => [
|
|
|
|
id,
|
|
|
|
`Step ${index + 1}`
|
|
|
|
]);
|
|
|
|
|
|
|
|
updateMetaData({ ...existingMeta, challengeOrder });
|
|
|
|
}
|
2021-07-06 19:22:12 -05:00
|
|
|
|
2022-03-02 16:12:20 +01:00
|
|
|
const updateStepTitles = () => {
|
|
|
|
const meta = getMetaData();
|
2020-08-25 02:35:46 -07:00
|
|
|
|
2022-03-02 16:12:20 +01:00
|
|
|
const fileNames: string[] = [];
|
|
|
|
fs.readdirSync(getProjectPath()).forEach(fileName => {
|
2020-08-25 02:35:46 -07:00
|
|
|
if (path.extname(fileName).toLowerCase() === '.md') {
|
2022-03-02 16:12:20 +01:00
|
|
|
fileNames.push(fileName);
|
2020-08-25 02:35:46 -07:00
|
|
|
}
|
|
|
|
});
|
2020-08-27 18:57:51 -07:00
|
|
|
|
2022-03-02 16:12:20 +01:00
|
|
|
fileNames.forEach(fileName => {
|
|
|
|
const filePath = `${getProjectPath()}${fileName}`;
|
2020-08-25 02:35:46 -07:00
|
|
|
const frontMatter = matter.read(filePath);
|
2022-03-02 16:12:20 +01:00
|
|
|
const newStepNum =
|
|
|
|
meta.challengeOrder.findIndex(elem => elem[0] === frontMatter.data.id) +
|
|
|
|
1;
|
|
|
|
const title = `Step ${newStepNum}`;
|
2021-10-21 10:07:52 -07:00
|
|
|
const dashedName = `step-${newStepNum}`;
|
2020-08-25 02:35:46 -07:00
|
|
|
const newData = {
|
|
|
|
...frontMatter.data,
|
2021-04-27 20:29:49 +02:00
|
|
|
title,
|
|
|
|
dashedName
|
2020-08-25 02:35:46 -07:00
|
|
|
};
|
2022-01-25 11:34:16 +01:00
|
|
|
fs.writeFileSync(filePath, matter.stringify(frontMatter.content, newData));
|
2020-08-25 02:35:46 -07:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-01-25 11:34:16 +01:00
|
|
|
const getChallengeSeeds = (
|
|
|
|
challengeFilePath: string
|
|
|
|
): Record<string, ChallengeSeed> => {
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-member-access
|
2021-09-07 15:54:11 +01:00
|
|
|
return parseMDSync(challengeFilePath).challengeFiles;
|
2020-08-27 18:57:51 -07:00
|
|
|
};
|
|
|
|
|
2022-03-02 16:12:20 +01:00
|
|
|
export {
|
|
|
|
createStepFile,
|
|
|
|
updateStepTitles,
|
|
|
|
getChallengeSeeds,
|
|
|
|
insertStepIntoMeta,
|
|
|
|
deleteStepFromMeta
|
|
|
|
};
|