Skip to content

Commit

Permalink
fix: don't infer --description if it's the source default (#1803)
Browse files Browse the repository at this point in the history
## PR Checklist

- [x] Addresses an existing open issue: fixes #1363
- [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

Applies both in the legacy mode and new create mode.

💖
  • Loading branch information
JoshuaKGoldberg authored Dec 24, 2024
1 parent d64e3ab commit 442cb3b
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 10 deletions.
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

0 comments on commit 442cb3b

Please sign in to comment.