Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: don't infer --description if it's the source default #1803

Merged
merged 4 commits into from
Dec 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions script/__snapshots__/migrate-test-e2e.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,14 @@ exports[`expected file changes > .prettierignore 1`] = `
exports[`expected file changes > README.md 1`] = `
"--- a/README.md
+++ b/README.md
@@ ... @@
<h1 align="center">Create TypeScript App</h1>

-<p align="center">Quickstart-friendly TypeScript template with comprehensive, configurable, opinionated tooling. ❤️‍🔥</p>
+<p align="center">A very lovely package. Hooray!</p>

<p align="center">
<!-- prettier-ignore-start -->
@@ ... @@ Thanks! 💖

<!-- ALL-CONTRIBUTORS-LIST:END -->
Expand Down Expand Up @@ -225,6 +233,15 @@ exports[`expected file changes > knip.json 1`] = `
exports[`expected file changes > package.json 1`] = `
"--- a/package.json
+++ b/package.json
@@ ... @@
{
"name": "create-typescript-app",
"version": "1.78.0",
- "description": "Quickstart-friendly TypeScript template with comprehensive, configurable, opinionated tooling. ❤️‍🔥",
+ "description": "A very lovely package. Hooray!",
"repository": {
"type": "git",
"url": "https://github.com/JoshuaKGoldberg/create-typescript-app"
@@ ... @@
"lint-staged": "15.2.11",
"markdownlint": "0.37.2",
Expand Down
3 changes: 2 additions & 1 deletion 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 { readDescription } from "./readDescription.js";
import { readDocumentation } from "./readDocumentation.js";
import { swallowError } from "./utils/swallowError.js";

Expand Down Expand Up @@ -193,7 +194,7 @@ export const base = createBase({
author,
bin: async () => (await packageData()).bin,
contributors: allContributors,
description: async () => (await packageData()).description,
description: async () => await readDescription(packageData),
documentation,
email: async () => readEmails(npmDefaults, packageAuthor),
funding: readFunding,
Expand Down
10 changes: 2 additions & 8 deletions src/next/blocks/packageData.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
import { createRequire } from "node:module";

const require = createRequire(import.meta.url);

const packageData =
// Importing from above src/ would expand the TS build rootDir
require("../../../package.json") as typeof import("../../../package.json");
import { sourcePackageJson } from "./sourcePackageJson.js";

export function getPackageDependencies(...names: string[]) {
return Object.fromEntries(
Expand Down Expand Up @@ -32,7 +26,7 @@ function getPackageInner(
key: "dependencies" | "devDependencies",
name: string,
) {
const inner = packageData[key];
const inner = sourcePackageJson[key];

return inner[name as keyof typeof inner] as string | undefined;
}
7 changes: 7 additions & 0 deletions src/next/blocks/sourcePackageJson.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { createRequire } from "node:module";

const require = createRequire(import.meta.url);

export const sourcePackageJson =
// Importing from above src/ would expand the TS build rootDir
require("../../../package.json") as typeof import("../../../package.json");
45 changes: 45 additions & 0 deletions src/next/readDescription.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { describe, expect, it, vi } from "vitest";

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

const mockSourcePackageJsonDescription = vi.fn<() => string>();

vi.mock("./blocks/sourcePackageJson", () => ({
sourcePackageJson: {
get description() {
return mockSourcePackageJsonDescription();
},
},
}));

describe("finalize", () => {
it("returns undefined when the description matches the current package.json description", async () => {
const existing = "Same description.";

mockSourcePackageJsonDescription.mockReturnValueOnce(existing);

const documentation = await readDescription(() =>
Promise.resolve({
description: existing,
}),
);

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

it("filters known headings when .github/DEVELOPMENT.md exists", async () => {
const updated = "Updated description.";

mockSourcePackageJsonDescription.mockReturnValueOnce(
"Existing description",
);

const documentation = await readDescription(() =>
Promise.resolve({
description: updated,
}),
);

expect(documentation).toBe(updated);
});
});
11 changes: 11 additions & 0 deletions src/next/readDescription.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { PartialPackageData } from "../shared/types.js";
import { sourcePackageJson } from "./blocks/sourcePackageJson.js";

export async function readDescription(
getPackageData: () => Promise<PartialPackageData>,
) {
const { description: inferred } = await getPackageData();
const { description: existing } = sourcePackageJson;

return existing === inferred ? undefined : inferred;
}
3 changes: 2 additions & 1 deletion src/shared/options/createOptionDefaults/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import lazyValue from "lazy-value";
import * as fs from "node:fs/promises";
import npmUser from "npm-user";

import { readDescription } from "../../../next/readDescription.js";
import { readPackageData } from "../../packages.js";
import { tryCatchAsync } from "../../tryCatchAsync.js";
import { tryCatchLazyValueAsync } from "../../tryCatchLazyValueAsync.js";
Expand Down Expand Up @@ -33,7 +34,7 @@ export function createOptionDefaults(promptedOptions?: PromptedOptions) {
author: async () =>
(await packageAuthor()).author ?? (await npmDefaults())?.name,
bin: async () => (await packageData()).bin,
description: async () => (await packageData()).description,
description: async () => await readDescription(packageData),
email: async () => readEmails(npmDefaults, packageAuthor),
funding: async () =>
await tryCatchAsync(async () =>
Expand Down
Loading