From cc896f340e2f7a664b1aae13141c0422a71bdb14 Mon Sep 17 00:00:00 2001 From: thomas Date: Tue, 3 Oct 2023 11:53:06 +0200 Subject: [PATCH] feat(generator): add the management of new badge --- ...allengeNumber__-__projectName__.md__tmpl__ | 1 + .../cli/src/generators/challenge/generator.ts | 40 ++++++++++++++++++- 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/libs/cli/src/generators/challenge/files/docs/__challengeNumber__-__projectName__.md__tmpl__ b/libs/cli/src/generators/challenge/files/docs/__challengeNumber__-__projectName__.md__tmpl__ index 2d422eb..561ed05 100644 --- a/libs/cli/src/generators/challenge/files/docs/__challengeNumber__-__projectName__.md__tmpl__ +++ b/libs/cli/src/generators/challenge/files/docs/__challengeNumber__-__projectName__.md__tmpl__ @@ -3,6 +3,7 @@ title: <%= difficulty %> <%= title %> description: Challenge <%= challengeNumber %> is about ... sidebar: order: <%= order %> + badge: New --- :::note diff --git a/libs/cli/src/generators/challenge/generator.ts b/libs/cli/src/generators/challenge/generator.ts index b319f17..f829662 100644 --- a/libs/cli/src/generators/challenge/generator.ts +++ b/libs/cli/src/generators/challenge/generator.ts @@ -16,6 +16,30 @@ import { join } from 'path'; import { getProjectDir } from '../../utils/normalize'; import { Schema } from './schema'; +function findPreviousChallengeFilePath(tree, path, number) { + if (tree.isFile(path) && path.startsWith(`${number}-`)) { + return path; + } + + const matchingChild = tree + .children(path) + .find((child) => child.startsWith(`${number}-`)); + + if (matchingChild) { + const fullPath = path + '/' + matchingChild; + return fullPath; + } + + for (const child of tree.children(path)) { + const childPath = path + '/' + child; + const result = findPreviousChallengeFilePath(tree, childPath, number); + if (result) { + return result; + } + } + return null; +} + export async function challengeGenerator(tree: Tree, options: Schema) { const { appDirectory } = getProjectDir(options.name, options.directory); @@ -51,7 +75,7 @@ export async function challengeGenerator(tree: Tree, options: Schema) { tmpl: '', projectName: names(options.name).name, title: options.title, - challengeNumber: challengeNumber + 1, + challengeNumber, docRepository: options.docRepository, }); @@ -92,6 +116,20 @@ export async function challengeGenerator(tree: Tree, options: Schema) { tree.write('./docs/src/content/docs/index.mdx', replaced); + const previousChallengeFilePath = findPreviousChallengeFilePath( + tree, + `./docs/src/content/docs/challenges`, + String(challengeNumber - 1) + ); + console.log(`restul`, previousChallengeFilePath); + + const previousChallenge = tree.read(previousChallengeFilePath).toString(); + + tree.write( + previousChallengeFilePath, + previousChallenge.replace(`badge: New`, ``) + ); + updateJson(tree, challengeNumberPath, (json) => { json.total += 1; json[difficulty] += 1;