Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(tools): repair-meta script (#50475)
- Loading branch information
1 parent
b9be06a
commit 5cf2228
Showing
3 changed files
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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())(); |