Skip to content

Commit

Permalink
chore: correct options.development sections parsing (#1798)
Browse files Browse the repository at this point in the history
## PR Checklist

- [x] Addresses an existing open issue: fixes #1797
- [x] That issue was marked as [`status: accepting
prs`](https://github.com/JoshuaKGoldberg/create-typescript-app/issues?q=is%3Aopen+is%3Aissue+label%3A%22status%3A+accepting+prs%22)
- [x] Steps in
[CONTRIBUTING.md](https://github.com/JoshuaKGoldberg/create-typescript-app/blob/main/.github/CONTRIBUTING.md)
were taken

## Overview

Splits it out into a separate `readDocumentation` function.

💖
  • Loading branch information
JoshuaKGoldberg authored Dec 24, 2024
1 parent 6e43554 commit de7a4d4
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 7 deletions.
9 changes: 2 additions & 7 deletions src/next/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { readGuide } from "../shared/options/createOptionDefaults/readGuide.js";
import { readPackageData } from "../shared/packages.js";
import { tryCatchLazyValueAsync } from "../shared/tryCatchLazyValueAsync.js";
import { AllContributorsData } from "../shared/types.js";
import { readDocumentation } from "./readDocumentation.js";
import { swallowError } from "./utils/swallowError.js";

export const base = createBase({
Expand Down Expand Up @@ -135,13 +136,7 @@ export const base = createBase({
return contributions.contributors;
});

const documentation = lazyValue(async () =>
swallowError(
await take(inputFromFile, {
filePath: ".github/DEVELOPMENT.md",
}),
),
);
const documentation = lazyValue(async () => readDocumentation(take));

const nvmrc = lazyValue(
async () =>
Expand Down
26 changes: 26 additions & 0 deletions src/next/readDocumentation.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { describe, expect, it } from "vitest";

import { readDocumentation } from "./readDocumentation.js";

describe("finalize", () => {
it("returns undefined when no .github/DEVELOPMENT.md exists", async () => {
const documentation = await readDocumentation(() =>
Promise.resolve(undefined),
);

expect(documentation).toBeUndefined();
});

it("filters known headings when .github/DEVELOPMENT.md exists", async () => {
const documentation = await readDocumentation(() =>
Promise.resolve(`# Development\nremoved\n\n## Unknown\n\nKept.\n`),
);

expect(documentation).toMatchInlineSnapshot(`
"## Unknown
Kept.
"
`);
});
});
37 changes: 37 additions & 0 deletions src/next/readDocumentation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { TakeInput } from "create";
import { inputFromFile } from "input-from-file";

import { swallowError } from "./utils/swallowError.js";

const knownHeadings = new Set([
"building",
"development",
"formatting",
"linting",
"testing",
"type checking",
]);

export async function readDocumentation(take: TakeInput) {
const existing = swallowError(
await take(inputFromFile, {
filePath: ".github/DEVELOPMENT.md",
}),
);
if (!existing) {
return undefined;
}

return existing
.split(/\n\n(?=##\s)/)
.filter((section) => !knownHeadings.has(parseHeading(section)))
.join("\n\n");
}

function parseHeading(section: string) {
return section
.split("\n")[0]
.replace(/^#+\s+/, "")
.trim()
.toLowerCase();
}

0 comments on commit de7a4d4

Please sign in to comment.