Skip to content

Commit a76face

Browse files
committed
feat: replace ignore-resolutions with profile
Add four profiles: - `strict` - same as today; all resolutions - `node16` - ignores node10 resolution failures - `esm-only` - ignores CJS resolution failures - `node16-only` - strictly node16 resolutions
1 parent 65d3064 commit a76face

File tree

4 files changed

+46
-12
lines changed

4 files changed

+46
-12
lines changed

.changeset/fuzzy-cats-shed.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
"@arethetypeswrong/core": minor
33
---
44

5-
Add --ignore-resolutions cli option. Example: --ignore-resolutions node10
5+
Add --profile cli option. Example: --profile node16

packages/cli/src/index.ts

+9-4
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ import { readFile, stat, unlink } from "fs/promises";
1010
import { createRequire } from "module";
1111
import path from "path";
1212
import readline from "readline";
13-
import { problemFlags, resolutionKinds } from "./problemUtils.js";
13+
import { problemFlags } from "./problemUtils.js";
1414
import { readConfig } from "./readConfig.js";
1515
import * as render from "./render/index.js";
1616
import { major, minor } from "semver";
1717
import { getExitCode } from "./getExitCode.js";
18+
import { applyProfile, profiles } from "./profiles.js";
1819

1920
const packageJson = createRequire(import.meta.url)("../package.json");
2021
const version = packageJson.version;
@@ -28,6 +29,8 @@ const formats = Object.keys({
2829
} satisfies Record<render.Format, any>) as render.Format[];
2930

3031
interface Opts extends render.RenderOptions {
32+
profile?: keyof typeof profiles;
33+
3134
pack?: boolean;
3235
fromNpm?: boolean;
3336
definitelyTyped?: boolean | string;
@@ -81,9 +84,7 @@ particularly ESM-related module resolution issues.`,
8184
new Option("--ignore-rules <rules...>", "Specify rules to ignore").choices(Object.values(problemFlags)).default([]),
8285
)
8386
.addOption(
84-
new Option("--ignore-resolutions <resolutions...>", "Specify resolutions to ignore")
85-
.choices(Object.keys(resolutionKinds))
86-
.default([]),
87+
new Option("--profile <profile>", "Specify analysis profile").choices(Object.keys(profiles)).default("strict"),
8788
)
8889
.option("--summary, --no-summary", "Whether to print summary information about the different errors")
8990
.option("--emoji, --no-emoji", "Whether to use any emojis")
@@ -93,6 +94,10 @@ particularly ESM-related module resolution issues.`,
9394
const opts = program.opts<Opts>();
9495
await readConfig(program, opts.configPath);
9596

97+
if (opts.profile) {
98+
applyProfile(opts.profile, opts);
99+
}
100+
96101
if (opts.quiet) {
97102
console.log = () => {};
98103
}

packages/cli/src/profiles.ts

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import type { RenderOptions } from "./render/index.js";
2+
3+
type Profile = Pick<Required<RenderOptions>, "ignoreResolutions">;
4+
5+
export const profiles = {
6+
strict: {
7+
ignoreResolutions: [],
8+
},
9+
node16: {
10+
ignoreResolutions: ["node10"],
11+
},
12+
"esm-only": {
13+
ignoreResolutions: ["node10", "node16-cjs"],
14+
},
15+
"node16-only": {
16+
ignoreResolutions: ["node10", "bundler"],
17+
},
18+
} satisfies Record<string, Profile>;
19+
20+
/**
21+
* Merges the profile with the provided options
22+
*
23+
* @param profileKey - name of the profile to apply
24+
* @param opts - options to apply the profile to
25+
*/
26+
export function applyProfile(profileKey: keyof typeof profiles, opts: RenderOptions): void {
27+
const profile = profiles[profileKey];
28+
opts.ignoreResolutions = (opts.ignoreResolutions ?? []).concat(profile.ignoreResolutions);
29+
}

packages/cli/src/readConfig.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Command } from "commander";
22
import { readFile } from "fs/promises";
3-
import { problemFlags, resolutionKinds } from "./problemUtils.js";
3+
import { problemFlags } from "./problemUtils.js";
4+
import { profiles } from "./profiles.js";
45

56
export async function readConfig(program: Command, alternate = ".attw.json") {
67
try {
@@ -25,13 +26,12 @@ export async function readConfig(program: Command, alternate = ".attw.json") {
2526
);
2627
}
2728

28-
if (key === "ignoreResolutions") {
29-
if (!Array.isArray(value)) program.error(`error: config option 'ignoreResolutions' should be an array.`);
30-
const invalid = value.find((resolution) => !Object.keys(resolutionKinds).includes(resolution));
31-
if (invalid)
29+
if (key === "profile") {
30+
if (typeof value !== "string") program.error(`error: config option 'profile' should be a string.`);
31+
if (!(value in profiles))
3232
program.error(
33-
`error: config option 'ignoreResolutions' argument '${invalid}' is invalid. Allowed choices are ${Object.keys(
34-
resolutionKinds,
33+
`error: config option 'profile' argument '${value}' is invalid. Allowed choices are ${Object.keys(
34+
profiles,
3535
).join(", ")}.`,
3636
);
3737
}

0 commit comments

Comments
 (0)