Skip to content

Commit

Permalink
feat(tools): repair-meta script (#50475)
Browse files Browse the repository at this point in the history
  • Loading branch information
naomi-lgbt committed Jun 1, 2023
1 parent b9be06a commit 5cf2228
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
1 change: 1 addition & 0 deletions curriculum/package.json
Expand Up @@ -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"
Expand Down
12 changes: 12 additions & 0 deletions docs/how-to-work-on-practice-projects.md
Expand Up @@ -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).
30 changes: 30 additions & 0 deletions tools/challenge-helper-scripts/repair-meta.ts
@@ -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())();

0 comments on commit 5cf2228

Please sign in to comment.