feat(tools): repair-meta script (#50475)

This commit is contained in:
Naomi Carrigan
2023-06-01 10:42:24 -07:00
committed by GitHub
parent b9be06aa0a
commit 5cf2228e84
3 changed files with 43 additions and 0 deletions
+1
View File
@@ -24,6 +24,7 @@
"insert-step": "cross-env CALLING_DIR=$INIT_CWD ts-node --project ../tsconfig.json ../tools/challenge-helper-scripts/insert-step",
"delete-step": "cross-env CALLING_DIR=$INIT_CWD ts-node --project ../tsconfig.json ../tools/challenge-helper-scripts/delete-step",
"lint": "ts-node --project ../tsconfig.json lint-localized",
"repair-meta": "cross-env CALLING_DIR=$INIT_CWD ts-node --project ../tsconfig.json ../tools/challenge-helper-scripts/repair-meta",
"update-step-titles": "cross-env CALLING_DIR=$INIT_CWD ts-node --project ../tsconfig.json ../tools/challenge-helper-scripts/update-step-titles",
"test": "ts-node ./node_modules/mocha/bin/mocha.js --delay --exit --reporter progress --bail",
"test:full-output": "cross-env FULL_OUTPUT=true ts-node ./node_modules/mocha/bin/mocha.js --delay --reporter progress"
+12
View File
@@ -135,6 +135,18 @@ A one-off script that automatically updates the frontmatter in a project's markd
pnpm run update-step-titles
```
### repair-meta
One-off script to parse the step names from the project and update the meta.json order to reflect those steps. Useful if you've accidentally lost the changes to the meta.json file when adding/removing steps.
#### How to Run the Script
1. Change to the directory of the project.
2. Run the following command:
```bash
pnpm run repair-meta
```
## Proposing a Pull Request (PR)
After you've committed your changes, check here for [how to open a Pull Request](how-to-open-a-pull-request.md).
@@ -0,0 +1,30 @@
import { readdir } from 'fs/promises';
import { join } from 'path';
import * as matter from 'gray-matter';
import { getProjectPath } from './helpers/get-project-info';
import { getMetaData, updateMetaData } from './helpers/project-metadata';
const sortByStepNum = (a: string, b: string) =>
parseInt(a.split('-')[1]) - parseInt(b.split('-')[1]);
const repairMeta = async () => {
const path = getProjectPath();
const fileList = await readdir(path);
// [id, title]
const challengeOrder = fileList
.map(file => matter.read(join(path, file)))
.sort((a, b) => sortByStepNum(a.data.dashedName, b.data.dashedName))
.map(({ data }) => [data.id, data.title] as [string, string]);
if (!challengeOrder.every(([, step]) => /Step \d+/.test(step))) {
throw new Error(
'You can only run this command on project-based blocks with step files.'
);
}
const meta = getMetaData();
meta.challengeOrder = challengeOrder;
updateMetaData(meta);
};
void (async () => await repairMeta())();