Skip to content

Commit 5978879

Browse files
fix: account for base when suggesting rerun cmd (#992)
<!-- 👋 Hi, thanks for sending a PR to create-typescript-app! 💖. Please fill out all fields below and make sure each item is true and [x] checked. Otherwise we may not be able to review your PR. --> ## PR Checklist - [x] Addresses an existing open issue: fixes #905 - [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 <!-- Description of what is changed and how the code change does that. --> Add additional filter logic to check if an options entry was excluded by the base, and filter it out of the suggested rerun flags, add new test, update failing test with suggestion from jest (>^u^)><(^u^)><(^u^<) --------- Co-authored-by: Josh Goldberg <[email protected]>
1 parent 157f930 commit 5978879

File tree

4 files changed

+68
-23
lines changed

4 files changed

+68
-23
lines changed

src/create/createRerunSuggestion.test.ts

+25
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { describe, expect, it } from "vitest";
22

3+
import { getExclusions } from "../shared/options/augmentOptionsWithExcludes.js";
34
import { Options } from "../shared/types.js";
45
import { createRerunSuggestion } from "./createRerunSuggestion.js";
56

@@ -93,4 +94,28 @@ describe("createRerunSuggestion", () => {
9394
'"npx create-typescript-app --mode initialize --base everything --access public --author TestAuthor --description \\"Test description.\\" --directory . --email-github [email protected] --email-npm [email protected] --exclude-all-contributors true --exclude-compliance true --exclude-lint-jsdoc true --exclude-lint-json true --exclude-lint-knip true --exclude-lint-md true --exclude-lint-package-json true --exclude-lint-perfectionist true --exclude-lint-spelling true --keywords \\"abc def ghi jkl mno pqr\\" --mode initialize --owner TestOwner --repository test-repository --skip-github-api true --skip-install true --skip-removal true --title \\"Test Title\\""',
9495
);
9596
});
97+
98+
it("does not list all excludes when using common base", () => {
99+
const common = createRerunSuggestion({
100+
base: "common",
101+
...getExclusions(options, "common"),
102+
excludeLintKnip: undefined,
103+
});
104+
105+
expect(common).toMatchInlineSnapshot(
106+
'"npx create-typescript-app --mode undefined --base common"',
107+
);
108+
});
109+
110+
it("does not list all excludes when using minimum base", () => {
111+
const minimum = createRerunSuggestion({
112+
base: "minimum",
113+
...getExclusions(options, "minimum"),
114+
excludeLintKnip: undefined,
115+
});
116+
117+
expect(minimum).toMatchInlineSnapshot(
118+
'"npx create-typescript-app --mode undefined --base minimum"',
119+
);
120+
});
96121
});

src/create/createRerunSuggestion.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
import { allArgOptions } from "../shared/options/args.js";
2+
import {
3+
ExclusionKey,
4+
getExclusions,
5+
} from "../shared/options/augmentOptionsWithExcludes.js";
26
import { Options } from "../shared/types.js";
37

48
function getFirstMatchingArg(key: string) {
@@ -30,10 +34,16 @@ export function createRerunSuggestion(options: Partial<Options>): string {
3034
};
3135

3236
const args = Object.entries(optionsNormalized)
37+
// Sort so the base is first, then the rest are sorted alphabetically
3338
.sort(([a], [b]) =>
3439
a === "base" ? -1 : b === "base" ? 1 : a.localeCompare(b),
3540
)
36-
.filter(([, value]) => !!value)
41+
// Filter out all entries that have a key in the excluded object or have a falsy value
42+
.filter(
43+
([key, value]) =>
44+
getExclusions(options, optionsNormalized.base)[key as ExclusionKey] ==
45+
undefined && !!value,
46+
)
3747
.map(([key, value]) => {
3848
return `--${getFirstMatchingArg(key)} ${stringifyValue(value)}`;
3949
})

src/shared/options/augmentOptionsWithExcludes.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,6 @@ describe("augmentOptionsWithExcludes", () => {
125125

126126
const actual = await augmentOptionsWithExcludes(options);
127127

128-
expect(actual).toBe(options);
128+
expect(actual).toStrictEqual(options);
129129
});
130130
});

src/shared/options/augmentOptionsWithExcludes.ts

+31-21
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ interface ExclusionDescription {
1010
uncommon?: true;
1111
}
1212

13-
type ExclusionKey = keyof Options & `exclude${string}`;
13+
export type ExclusionKey = keyof Options & `exclude${string}`;
1414

1515
const exclusionDescriptions: Record<ExclusionKey, ExclusionDescription> = {
1616
excludeAllContributors: {
@@ -119,6 +119,34 @@ const exclusionDescriptions: Record<ExclusionKey, ExclusionDescription> = {
119119

120120
const exclusionKeys = Object.keys(exclusionDescriptions) as ExclusionKey[];
121121

122+
export function getExclusions(
123+
options: Partial<Options>,
124+
base?: OptionsBase,
125+
): Partial<Options> {
126+
switch (base) {
127+
case "common":
128+
return {
129+
...Object.fromEntries(
130+
exclusionKeys
131+
.filter((exclusion) => exclusionDescriptions[exclusion].uncommon)
132+
.map((exclusion) => [exclusion, options[exclusion] ?? true]),
133+
),
134+
};
135+
case "minimum":
136+
return {
137+
...Object.fromEntries(
138+
exclusionKeys.map((exclusion) => [
139+
exclusion,
140+
options[exclusion] ?? true,
141+
]),
142+
),
143+
};
144+
// We only really care about exclusions on the common and minimum bases
145+
default:
146+
return {};
147+
}
148+
}
149+
122150
export async function augmentOptionsWithExcludes(
123151
options: Options,
124152
): Promise<Options | undefined> {
@@ -172,31 +200,13 @@ export async function augmentOptionsWithExcludes(
172200
switch (base) {
173201
case undefined:
174202
return undefined;
175-
176203
case "common":
177-
return {
178-
...options,
179-
...Object.fromEntries(
180-
exclusionKeys
181-
.filter((exclusion) => exclusionDescriptions[exclusion].uncommon)
182-
.map((exclusion) => [exclusion, options[exclusion] ?? true]),
183-
),
184-
};
185-
186-
case "everything":
187-
return options;
188-
189204
case "minimum":
205+
case "everything":
190206
return {
191207
...options,
192-
...Object.fromEntries(
193-
exclusionKeys.map((exclusion) => [
194-
exclusion,
195-
options[exclusion] ?? true,
196-
]),
197-
),
208+
...getExclusions(options, base),
198209
};
199-
200210
case "prompt":
201211
const exclusionsNotEnabled = new Set(
202212
filterPromptCancel(

0 commit comments

Comments
 (0)