Skip to content

Commit

Permalink
fix: generator
Browse files Browse the repository at this point in the history
  • Loading branch information
tomalaforge committed Jan 28, 2024
1 parent 18bb6e5 commit 7dd2154
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 24 deletions.
2 changes: 1 addition & 1 deletion docs/src/content/docs/fr/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ hero:
icon: right-arrow
variant: primary
- text: Aller au dernier Challenge
link: /challenges/angular/43-signal-input/
link: /fr/challenges/angular/43-signal-input/
icon: rocket
- text: Donne une étoile
link: https://github.com/tomalaforge/angular-challenges
Expand Down
2 changes: 1 addition & 1 deletion docs/src/content/docs/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import MyIcon from '../../components/MyIcon.astro';

<CardGrid>
<Card title="43 Challenges">
This repository gathers 43 Challenges related to <b>Angular</b>, <b>Nx</b>, <b>RxJS</b>, <b>Ngrx</b> and <b>Typescript</b>.
This repository gathers 43 challenges related to <b>Angular</b>, <b>Nx</b>, <b>RxJS</b>, <b>Ngrx</b> and <b>Typescript</b>.
These challenges resolve around real-life issues or specific features to elevate your skills.
</Card>

Expand Down
7 changes: 7 additions & 0 deletions libs/cli/src/generators/challenge/files/lang-mapper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const langMapper = {
en: 'Challenges',
fr: 'Défis',
es: 'Desafíos',
pt: 'Desafios',
ru: 'Испытания',
};
58 changes: 38 additions & 20 deletions libs/cli/src/generators/challenge/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
import { Linter } from '@nx/eslint';
import { join } from 'path';
import { getProjectDir } from '../../utils/normalize';
import { langMapper } from './files/lang-mapper';
import { Schema } from './schema';

function findPreviousChallengeFilePath(tree, path, number) {
Expand All @@ -25,8 +26,7 @@ function findPreviousChallengeFilePath(tree, path, number) {
.find((child) => child.startsWith(`${number}-`));

if (matchingChild) {
const fullPath = path + '/' + matchingChild;
return fullPath;
return path + '/' + matchingChild;
}

for (const child of tree.children(path)) {
Expand All @@ -49,7 +49,8 @@ export async function challengeGenerator(tree: Tree, options: Schema) {

await applicationGenerator(tree, {
...options,
directory: `apps/${options.category}`,
name: `${options.category}-${options.name}`,
directory: `apps/${options.category}/${options.name}`,
style: 'scss',
routing: false,
inlineStyle: true,
Expand All @@ -61,14 +62,15 @@ export async function challengeGenerator(tree: Tree, options: Schema) {
addTailwind: true,
standalone: true,
skipTests: true,
projectNameAndRootFormat: 'as-provided',
});

const challengeNumberPath = 'challenge-number.json';
const challangeNumberJson = JSON.parse(
const challengeNumberJson = JSON.parse(
tree.read(challengeNumberPath).toString(),
);
const challengeNumber = challangeNumberJson.total + 1;
const order = challangeNumberJson[difficulty] + 1;
const challengeNumber = challengeNumberJson.total + 1;
const order = challengeNumberJson[difficulty] + 1;

generateFiles(tree, join(__dirname, 'files', 'app'), appDirectory, {
tmpl: '',
Expand Down Expand Up @@ -116,20 +118,36 @@ export async function challengeGenerator(tree: Tree, options: Schema) {

tree.write('./README.md', readmeReplace);

const docs = tree.read('./docs/src/content/docs/index.mdx').toString();

const regex = new RegExp(`${challengeNumber - 1} Challenges`, 'gi');
const replaced = docs.replace(regex, `${challengeNumber} Challenges`);

const linkRegex = new RegExp(`link: \\/challenges\\/(.*?)\n`, 'gi');
const replacedLink = replaced.replace(
linkRegex,
`link: /challenges/${options.category}/${challengeNumber}-${
names(options.name).name
}/\n`,
);

tree.write('./docs/src/content/docs/index.mdx', replacedLink);
for (const lang of ['en', 'es', 'fr', 'pt', 'ru']) {
const docs = tree
.read(`./docs/src/content/docs/${lang === 'en' ? '' : lang}/index.mdx`)
.toString();

const regex = new RegExp(
`${challengeNumber - 1} ${langMapper[lang]}`,
'gi',
);
const replaced = docs.replace(
regex,
`${challengeNumber} ${langMapper[lang]}`,
);

const linkRegex = new RegExp(
`link: \\/${lang}\\/challenges\\/(.*?)\n`,
'gi',
);
const replacedLink = replaced.replace(
linkRegex,
`link: /${lang}/challenges/${options.category}/${challengeNumber}-${
names(options.name).name
}/\n`,
);

tree.write(
`./docs/src/content/docs/${lang === 'en' ? '' : lang}/index.mdx`,
replacedLink,
);
}

const previousChallengeFilePath = findPreviousChallengeFilePath(
tree,
Expand Down
7 changes: 5 additions & 2 deletions libs/cli/src/generators/challenge/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,16 @@
"$source": "argv",
"index": 1
},
"x-priority": "important"
"x-priority": "important",
"x-prompt": "What should be the title of your challenge?",
"pattern": "^[a-zA-Z].*$"
},
"author": {
"description": "Your full name",
"type": "string",
"maxLength": "25",
"x-priority": "important"
"x-priority": "important",
"x-prompt": "Author?"
},
"challengeDifficulty": {
"description": "The difficulty of the challenge.",
Expand Down

0 comments on commit 7dd2154

Please sign in to comment.