feat(generator): add the management of new badge

This commit is contained in:
thomas
2023-10-03 11:53:06 +02:00
parent c062613a8e
commit cc896f340e
2 changed files with 40 additions and 1 deletions

View File

@@ -3,6 +3,7 @@ title: <%= difficulty %> <%= title %>
description: Challenge <%= challengeNumber %> is about ...
sidebar:
order: <%= order %>
badge: New
---
:::note

View File

@@ -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;