Skip to content

Commit

Permalink
Merge pull request #438 from vikejs/cleanup-hooks
Browse files Browse the repository at this point in the history
feat: cleanup README
  • Loading branch information
magne4000 authored Dec 6, 2024
2 parents 7e895eb + 84827e1 commit 5539044
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 7 deletions.
File renamed without changes.
26 changes: 26 additions & 0 deletions boilerplates/shared/hooks/after.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import type { VikeMeta } from "@batijs/core";
import { readFile, rename, 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(/<!--bati:.*-->/g, "")
.replaceAll(/\n\n+/g, "\n\n")
.trimStart(),
"utf-8",
);
}

// 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) {
await cleanupReadme(cwd);
await renameGitIgnore(cwd);
}
8 changes: 4 additions & 4 deletions packages/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(meta);
}
},
});

Expand Down
2 changes: 1 addition & 1 deletion packages/cli/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ export interface ToBeCopied extends BoilerplateDef {
source?: string;
}

export type Hook = (meta: VikeMeta) => Promise<void> | void;
export type Hook = (cwd: string, meta: VikeMeta) => Promise<void> | void;
44 changes: 42 additions & 2 deletions packages/core/tests/markdown/markdown.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,26 +157,64 @@ test("add feature", () => {
<!--bati:start section="features"-->
<!--bati:start category="Hosting" flag="aws"-->
## AWS
<!--bati:end category="Hosting" flag="aws"-->
<!--bati:start category="UI Framework" flag="react"-->
## REACT
<!--bati:end category="UI Framework" flag="react"-->
<!--bati:end section="features"-->
<!--bati:end section="document"-->
`,
);
});

test("add feature with addMarkdownFeature", () => {
const content = parseMarkdown(
`
<!--bati:start section="features"-->
<!--bati:start category="Hosting" flag="aws"-->
## AWS
<!--bati:end category="Hosting" flag="aws"-->
<!--bati:end section="features"-->
`,
);
content.addMarkdownFeature(`## REACT`, "react");

const result = content.finalize();

expect(result).toBe(
`<!--bati:start section="document"-->
<!--bati:start section="features"-->
<!--bati:start category="Hosting" flag="aws"-->
## AWS
<!--bati:end category="Hosting" flag="aws"-->
<!--bati:start category="UI Framework" flag="react"-->
## REACT
<!--bati:end category="UI Framework" flag="react"-->
<!--bati:end section="features"-->
<!--bati:end section="document"-->
`,
);
});

test("add feature with addMarkdownFeature", () => {
test("add feature with addMarkdownFeature (before)", () => {
const content = parseMarkdown(
`
<!--bati:start section="features"-->
Expand All @@ -186,7 +224,9 @@ test("add feature with addMarkdownFeature", () => {
<!--bati:end section="features"-->
`,
);
content.addMarkdownFeature(`## REACT`, "react");
content.addMarkdownFeature(`## REACT`, "react", {
position: "before",
});

const result = content.finalize();

Expand Down

0 comments on commit 5539044

Please sign in to comment.