forked from freeCodeCamp/freeCodeCamp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproject-metadata.ts
72 lines (63 loc) · 1.96 KB
/
project-metadata.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import fs from 'fs';
import path from 'path';
import glob from 'glob';
import { getProjectName, getProjectPath } from './get-project-info';
export type Meta = {
name: string;
isUpcomingChange: boolean;
dashedName: string;
helpCategory: string;
order: number;
time: string;
template: string;
required: string[];
superBlock: string;
challengeOrder: { id: string; title: string }[];
};
function getMetaData(): Meta {
const metaData = fs.readFileSync(getProjectMetaPath(), 'utf-8');
return JSON.parse(metaData) as Meta;
}
function updateMetaData(newMetaData: Record<string, unknown>): void {
fs.writeFileSync(getProjectMetaPath(), JSON.stringify(newMetaData, null, 2));
}
function getProjectMetaPath(): string {
return path.join(
getProjectPath(),
'../../..',
'_meta',
getProjectName(),
'meta.json'
);
}
// This (and everything else) should be async, but it's fast enough
// for the moment.
function validateMetaData(): void {
const { challengeOrder } = getMetaData();
// each step in the challengeOrder should correspond to a file
challengeOrder.forEach(({ id }) => {
const filePath = `${getProjectPath()}${id}.md`;
try {
fs.accessSync(filePath);
} catch (_e) {
throw new Error(
`The file
${filePath}
does not exist, but is required by the challengeOrder of
${getProjectMetaPath()}
To fix this, you can rename the file containing id: ${id} to ${id}.md
If there is no file for this id, then either the challengeOrder needs to be updated, or the file needs to be created.
`
);
}
});
// each file should have a corresponding step in the challengeOrder
glob.sync(`${getProjectPath()}/*.md`).forEach(file => {
const id = path.basename(file, '.md');
if (!challengeOrder.find(({ id: stepId }) => stepId === id))
throw new Error(
`File ${file} should be in the meta.json's challengeOrder`
);
});
}
export { getMetaData, updateMetaData, getProjectMetaPath, validateMetaData };