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

chore: assorted createOptionDefaults cleanups #1741

Merged
merged 2 commits into from
Dec 5, 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
25 changes: 4 additions & 21 deletions src/shared/options/createOptionDefaults/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import { tryCatchAsync } from "../../tryCatchAsync.js";
import { tryCatchLazyValueAsync } from "../../tryCatchLazyValueAsync.js";
import { PromptedOptions } from "../../types.js";
import { parsePackageAuthor } from "./parsePackageAuthor.js";
import { readDefaultsFromDevelopment } from "./readDefaultsFromDevelopment.js";
import { readDefaultsFromReadme } from "./readDefaultsFromReadme.js";
import { readGitHubEmail } from "./readGitHubEmail.js";
import { readEmails } from "./readEmails.js";
import { readGuide } from "./readGuide.js";

export function createOptionDefaults(promptedOptions?: PromptedOptions) {
const gitDefaults = tryCatchLazyValueAsync(async () =>
Expand All @@ -34,38 +34,21 @@ export function createOptionDefaults(promptedOptions?: PromptedOptions) {
(await packageAuthor()).author ?? (await npmDefaults())?.name,
bin: async () => (await packageData()).bin,
description: async () => (await packageData()).description,
email: async () => {
const githubEmail =
(await readGitHubEmail()) ??
(await tryCatchAsync(
async () => (await $`git config --get user.email`).stdout,
));
const npmEmail =
(await npmDefaults())?.email ?? (await packageAuthor()).email;

/* eslint-disable @typescript-eslint/no-non-null-assertion */
return githubEmail || npmEmail
? {
github: (githubEmail || npmEmail)!,
npm: (npmEmail || githubEmail)!,
}
: undefined;
/* eslint-enable @typescript-eslint/no-non-null-assertion */
},
email: async () => readEmails(npmDefaults, packageAuthor),
funding: async () =>
await tryCatchAsync(async () =>
(await fs.readFile(".github/FUNDING.yml"))
.toString()
.split(":")[1]
?.trim(),
),
guide: readGuide,
owner: async () =>
(await gitDefaults())?.organization ?? (await packageAuthor()).author,
repository: async () =>
promptedOptions?.repository ??
(await gitDefaults())?.name ??
(await packageData()).name,
...readDefaultsFromDevelopment(),
...readDefaultsFromReadme(),
};
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ export function readDefaultsFromReadme() {
src,
};
},
title: async () =>
(/^<h1\s+align="center">(.+)<\/h1>/.exec(await readme()) ??
/^# (.+)/.exec(await readme()))?.[1],
title: async () => {
const text = await readme();
return (/^<h1\s+align="center">(.+)<\/h1>/.exec(text) ??
/^# (.+)/.exec(text))?.[1];
},
};
}
24 changes: 24 additions & 0 deletions src/shared/options/createOptionDefaults/readEmails.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { $ } from "execa";
import { UserInfo } from "npm-user";

import { tryCatchAsync } from "../../tryCatchAsync.js";
import { readGitHubEmail } from "./readGitHubEmail.js";

export async function readEmails(
npmDefaults: () => Promise<undefined | UserInfo>,
packageAuthor: () => Promise<{
author: string | undefined;
email: string | undefined;
}>,
) {
const github =
(await readGitHubEmail()) ??
(await tryCatchAsync(
async () => (await $`git config --get user.email`).stdout,
));

const npm =
((await npmDefaults())?.email ?? (await packageAuthor()).email) || github;

return npm ? { github: github || npm, npm } : undefined;
}
9 changes: 9 additions & 0 deletions src/shared/options/createOptionDefaults/readFunding.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import fs from "node:fs/promises";

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

export async function readFunding() {
return await tryCatchAsync(async () =>
(await fs.readFile(".github/FUNDING.yml")).toString().split(":")[1]?.trim(),
);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, it, vi } from "vitest";

import { readDefaultsFromDevelopment } from "./readDefaultsFromDevelopment.js";
import { readGuide } from "./readGuide.js";

const mockReadFileSafe = vi.fn();

Expand All @@ -10,12 +10,12 @@ vi.mock("../../readFileSafe.js", () => ({
},
}));

describe("readDefaultsFromDevelopment", () => {
describe("readGuide", () => {
describe("guide", () => {
it("defaults to undefined when .github/DEVELOPMENT.md cannot be found", async () => {
mockReadFileSafe.mockResolvedValue("");

const guide = await readDefaultsFromDevelopment().guide();
const guide = await readGuide();

expect(guide).toBeUndefined();
});
Expand All @@ -27,7 +27,7 @@ describe("readDefaultsFromDevelopment", () => {
> It'll walk you through the common activities you'll need to contribute.
`);

const guide = await readDefaultsFromDevelopment().guide();
const guide = await readGuide();

expect(guide).toEqual({
href: "https://www.joshuakgoldberg.com/blog/contributing-to-a-create-typescript-app-repository",
Expand Down
17 changes: 17 additions & 0 deletions src/shared/options/createOptionDefaults/readGuide.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { readFileSafe } from "../../readFileSafe.js";

export async function readGuide() {
const development = await readFileSafe(".github/DEVELOPMENT.md", "");
const tag = /> .*guided walkthrough, see \[((?!\[).+)\]\((.+)\)/i.exec(
development,
);

if (!tag) {
return undefined;
}

return {
href: tag[2],
title: tag[1],
};
}
Loading