Connect, collaborate, and grow with a community of TypeScript developers
1174 mdwiltfong update validate.ts (#1457)
Michael Wiltfong340340e4d217
diff --git a/challenges/aot/metadata.schema.json b/challenges/aot/metadata.schema.jsonnew file mode 100644index 00000000..55ba02fb--- /dev/null+++ b/challenges/aot/metadata.schema.json@@ -0,0 +1,38 @@+{+ "$schema": "http://json-schema.org/draft-07/schema#",+ "type": "object",+ "properties": {+ "id": {+ "description": "the day of the advent month following the format `day-number` ",+ "maxLength": 6,+ "type": "string"+ },+ "label": {+ "description": "The day of the advent month the challenge is for. Follows `Day <number>`",+ "maxLength": 30,+ "type": "string"+ },+ "description": {+ "description": "Short summary of the challenge",+ "maxLength": 40,+ "type": "string"+ },+ "difficulty": {+ "description": "Since AOT is an annual event, the challenge may not have a specific difficutly. It will always be `event`",+ "maxLength": 5,+ "type": "string"+ },+ "prerequisites": {+ "description": "Recommended challenges to do before the respective AOT challenge",+ "maxLength": 30,+ "items": {+ "type": "string"+ }+ },+ "author": {+ "description": "The person who wrote the challenge",+ "maxLength": 15,+ "type": "string"+ }+ }+}diff --git a/challenges/validate.ts b/challenges/validate.tsindex 6267ac0a..49df22ff 100644--- a/challenges/validate.ts+++ b/challenges/validate.ts@@ -70,18 +70,37 @@ Finally, as an almost entirely different task: this script runs each solution anconst repoRoot = join(__dirname, '..');const challengesDir = `${repoRoot}/challenges/`;+const aotChallengesDir = `${challengesDir}aot`;export const getChallengeIds = () => {- return readdirSync(challengesDir)+ const challengeIds = readdirSync(challengesDir).filter((id) => id !== 'blank').filter((id) => id !== 'aot').filter((id) => statSync(join(challengesDir, id)).isDirectory()).filter((id) => statSync(join(challengesDir, id, 'metadata.json')).isFile()).filter((id) => statSync(join(challengesDir, id, 'tsconfig.json')).isFile());+ const aotChallengeIds = {};++ const aotYears = readdirSync(aotChallengesDir).filter((file) => file !== 'metadata.schema.json');+ aotYears.forEach((year) => {+ aotChallengeIds[year] = readdirSync(join(aotChallengesDir, year))+ .filter((id) => statSync(join(aotChallengesDir, year, id)).isDirectory())+ .filter((id) => statSync(join(aotChallengesDir, year, id, 'metadata.json')).isFile())+ .filter((id) => statSync(join(aotChallengesDir, year, id, 'tsconfig.json')).isFile());+ });++ return {+ challengeIds,+ aotChallengeIds,+ };};-const getMetadata = (id: string) => {- const metadataFilePath = join(challengesDir, id, 'metadata.json');+const getMetadata = (id: string, isAot = false, year: string | undefined = undefined) => {+ const metadataFilePath = join(+ isAot ? aotChallengesDir + `/${year}` : challengesDir,+ id,+ 'metadata.json',+ );const metadataFile = readFileSync(metadataFilePath, 'utf8');const metadata = JSON.parse(metadataFile) as {id: string;@@ -93,15 +112,21 @@ const getMetadata = (id: string) => {};};-const validateMetadataSchema = (ids: string[]) => {+const validateMetadataSchema = (+ ids: string[],+ isAot = false,+ year: string | undefined = undefined,+) => {const ajv = new Ajv();- const schema = JSON.parse(readFileSync(join(challengesDir, 'metadata.schema.json'), 'utf8'));+ const schema = JSON.parse(+ readFileSync(join(isAot ? aotChallengesDir : challengesDir, 'metadata.schema.json'), 'utf8'),+ );const validate = ajv.compile(schema);ids.forEach((id) => {- const { metadata, metadataFilePath } = getMetadata(id);+ const { metadata, metadataFilePath } = getMetadata(id, isAot, year);if (!validate(metadata)) {console.error(@@ -142,15 +167,18 @@ const validatePrerequisiteIds = (id: string, _: number, ids: string[]) => {};const validateMetadataFiles = () => {- const challengeIds = getChallengeIds();-+ const { challengeIds, aotChallengeIds } = getChallengeIds();+ for (const year in aotChallengeIds) {+ validateMetadataSchema(aotChallengeIds[year], true, year);+ }validateMetadataSchema(challengeIds);challengeIds.forEach(ensureChallengeIdMatchesDirectory);challengeIds.forEach(validatePrerequisiteIds);};const validateTests = () => {- getChallengeIds()+ const { challengeIds, aotChallengeIds } = getChallengeIds();+ challengeIds.filter((id) => {const path = join(challengesDir, id, 'solutions');statSync(path).isDirectory();