From b7d6f66e7e20a6cc45d0ed075aa3d575eb24fc18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=ABl=20Charles?= Date: Fri, 6 Dec 2024 16:00:38 +0100 Subject: [PATCH 1/3] feat: cleanup README --- boilerplates/shared/hooks/after.ts | 21 +++++++++++++++++++++ packages/cli/index.ts | 2 +- packages/cli/types.ts | 2 +- 3 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 boilerplates/shared/hooks/after.ts diff --git a/boilerplates/shared/hooks/after.ts b/boilerplates/shared/hooks/after.ts new file mode 100644 index 000000000..af3a9f0fa --- /dev/null +++ b/boilerplates/shared/hooks/after.ts @@ -0,0 +1,21 @@ +import type { VikeMeta } from "@batijs/core"; +import { readFile, writeFile } from "node:fs/promises"; +import { join } from "node:path"; + +async function cleanupReadme(cwd: string) { + const content = await readFile(join(cwd, "README.md"), "utf8"); + await writeFile( + join(cwd, "README.md"), + content + .replaceAll(//g, "") + .replaceAll(/\n\n+/g, "\n\n") + .trimStart(), + "utf-8", + ); +} + +function renameGitIgnore() {} + +export default async function onafter(cwd: string, meta: VikeMeta) { + await cleanupReadme(cwd); +} diff --git a/packages/cli/index.ts b/packages/cli/index.ts index d16e1d816..31b5e40b5 100644 --- a/packages/cli/index.ts +++ b/packages/cli/index.ts @@ -420,7 +420,7 @@ async function run() { printOK(args.project, flags); for (const onafter of hooksMap.get("after") ?? []) { - await onafter(meta); + await onafter(args.project, meta); } }, }); diff --git a/packages/cli/types.ts b/packages/cli/types.ts index 2029a061e..3216d5ef3 100644 --- a/packages/cli/types.ts +++ b/packages/cli/types.ts @@ -15,4 +15,4 @@ export interface ToBeCopied extends BoilerplateDef { source?: string; } -export type Hook = (meta: VikeMeta) => Promise | void; +export type Hook = (cwd: string, meta: VikeMeta) => Promise | void; From 52e4261e45b23fc40772ab2f8d3c076bb7110776 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=ABl=20Charles?= Date: Fri, 6 Dec 2024 16:07:02 +0100 Subject: [PATCH 2/3] fix: npm .gitignore renaming --- boilerplates/shared/files/{.gitignore => gitignore} | 0 boilerplates/shared/hooks/after.ts | 11 ++++++++--- packages/cli/index.ts | 8 ++++---- 3 files changed, 12 insertions(+), 7 deletions(-) rename boilerplates/shared/files/{.gitignore => gitignore} (100%) diff --git a/boilerplates/shared/files/.gitignore b/boilerplates/shared/files/gitignore similarity index 100% rename from boilerplates/shared/files/.gitignore rename to boilerplates/shared/files/gitignore diff --git a/boilerplates/shared/hooks/after.ts b/boilerplates/shared/hooks/after.ts index af3a9f0fa..ac3b5753b 100644 --- a/boilerplates/shared/hooks/after.ts +++ b/boilerplates/shared/hooks/after.ts @@ -1,5 +1,5 @@ import type { VikeMeta } from "@batijs/core"; -import { readFile, writeFile } from "node:fs/promises"; +import { readFile, rename, writeFile } from "node:fs/promises"; import { join } from "node:path"; async function cleanupReadme(cwd: string) { @@ -14,8 +14,13 @@ async function cleanupReadme(cwd: string) { ); } -function renameGitIgnore() {} +// Rename gitignore after the fact to prevent npm from renaming it to .npmignore +// See: https://github.com/npm/npm/issues/1862 +async function renameGitIgnore(cwd: string) { + await rename(join(cwd, "gitignore"), join(cwd, ".gitignore")); +} -export default async function onafter(cwd: string, meta: VikeMeta) { +export default async function onafter(cwd: string, _meta: VikeMeta) { await cleanupReadme(cwd); + await renameGitIgnore(cwd); } diff --git a/packages/cli/index.ts b/packages/cli/index.ts index 31b5e40b5..b29c1b711 100644 --- a/packages/cli/index.ts +++ b/packages/cli/index.ts @@ -413,15 +413,15 @@ async function run() { meta, ); + for (const onafter of hooksMap.get("after") ?? []) { + await onafter(args.project, meta); + } + if (!args["skip-git"]) { gitInit(args.project); } printOK(args.project, flags); - - for (const onafter of hooksMap.get("after") ?? []) { - await onafter(args.project, meta); - } }, }); From 84827e1c6c99c12a16d5839076ec95c94e10f126 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=ABl=20Charles?= Date: Fri, 6 Dec 2024 16:09:28 +0100 Subject: [PATCH 3/3] test: update markdown unit tests --- packages/core/tests/markdown/markdown.spec.ts | 44 ++++++++++++++++++- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/packages/core/tests/markdown/markdown.spec.ts b/packages/core/tests/markdown/markdown.spec.ts index 258719a53..3cc65ae28 100644 --- a/packages/core/tests/markdown/markdown.spec.ts +++ b/packages/core/tests/markdown/markdown.spec.ts @@ -157,18 +157,56 @@ test("add feature", () => { + + +## AWS + + + ## REACT + + + +`, + ); +}); + +test("add feature with addMarkdownFeature", () => { + const content = parseMarkdown( + ` + + +## AWS + + +`, + ); + content.addMarkdownFeature(`## REACT`, "react"); + + const result = content.finalize(); + + expect(result).toBe( + ` + + + ## AWS + + +## REACT + + + @@ -176,7 +214,7 @@ test("add feature", () => { ); }); -test("add feature with addMarkdownFeature", () => { +test("add feature with addMarkdownFeature (before)", () => { const content = parseMarkdown( ` @@ -186,7 +224,9 @@ test("add feature with addMarkdownFeature", () => { `, ); - content.addMarkdownFeature(`## REACT`, "react"); + content.addMarkdownFeature(`## REACT`, "react", { + position: "before", + }); const result = content.finalize();