Skip to content

Commit

Permalink
feat: replace ignore-resolutions with profile
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jason-ha committed Sep 20, 2024
1 parent 65d3064 commit a76face
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .changeset/fuzzy-cats-shed.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
"@arethetypeswrong/core": minor
---

Add --ignore-resolutions cli option. Example: --ignore-resolutions node10
Add --profile cli option. Example: --profile node16
13 changes: 9 additions & 4 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ import { readFile, stat, unlink } from "fs/promises";
import { createRequire } from "module";
import path from "path";
import readline from "readline";
import { problemFlags, resolutionKinds } from "./problemUtils.js";
import { problemFlags } from "./problemUtils.js";
import { readConfig } from "./readConfig.js";
import * as render from "./render/index.js";
import { major, minor } from "semver";
import { getExitCode } from "./getExitCode.js";
import { applyProfile, profiles } from "./profiles.js";

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

interface Opts extends render.RenderOptions {
profile?: keyof typeof profiles;

pack?: boolean;
fromNpm?: boolean;
definitelyTyped?: boolean | string;
Expand Down Expand Up @@ -81,9 +84,7 @@ particularly ESM-related module resolution issues.`,
new Option("--ignore-rules <rules...>", "Specify rules to ignore").choices(Object.values(problemFlags)).default([]),
)
.addOption(
new Option("--ignore-resolutions <resolutions...>", "Specify resolutions to ignore")
.choices(Object.keys(resolutionKinds))
.default([]),
new Option("--profile <profile>", "Specify analysis profile").choices(Object.keys(profiles)).default("strict"),
)
.option("--summary, --no-summary", "Whether to print summary information about the different errors")
.option("--emoji, --no-emoji", "Whether to use any emojis")
Expand All @@ -93,6 +94,10 @@ particularly ESM-related module resolution issues.`,
const opts = program.opts<Opts>();
await readConfig(program, opts.configPath);

if (opts.profile) {
applyProfile(opts.profile, opts);
}

if (opts.quiet) {
console.log = () => {};
}
Expand Down
29 changes: 29 additions & 0 deletions packages/cli/src/profiles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import type { RenderOptions } from "./render/index.js";

type Profile = Pick<Required<RenderOptions>, "ignoreResolutions">;

export const profiles = {
strict: {
ignoreResolutions: [],
},
node16: {
ignoreResolutions: ["node10"],
},
"esm-only": {
ignoreResolutions: ["node10", "node16-cjs"],
},
"node16-only": {
ignoreResolutions: ["node10", "bundler"],
},
} satisfies Record<string, Profile>;

/**
* Merges the profile with the provided options
*
* @param profileKey - name of the profile to apply
* @param opts - options to apply the profile to
*/
export function applyProfile(profileKey: keyof typeof profiles, opts: RenderOptions): void {
const profile = profiles[profileKey];
opts.ignoreResolutions = (opts.ignoreResolutions ?? []).concat(profile.ignoreResolutions);
}
14 changes: 7 additions & 7 deletions packages/cli/src/readConfig.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Command } from "commander";
import { readFile } from "fs/promises";
import { problemFlags, resolutionKinds } from "./problemUtils.js";
import { problemFlags } from "./problemUtils.js";
import { profiles } from "./profiles.js";

export async function readConfig(program: Command, alternate = ".attw.json") {
try {
Expand All @@ -25,13 +26,12 @@ export async function readConfig(program: Command, alternate = ".attw.json") {
);
}

if (key === "ignoreResolutions") {
if (!Array.isArray(value)) program.error(`error: config option 'ignoreResolutions' should be an array.`);
const invalid = value.find((resolution) => !Object.keys(resolutionKinds).includes(resolution));
if (invalid)
if (key === "profile") {
if (typeof value !== "string") program.error(`error: config option 'profile' should be a string.`);
if (!(value in profiles))
program.error(
`error: config option 'ignoreResolutions' argument '${invalid}' is invalid. Allowed choices are ${Object.keys(
resolutionKinds,
`error: config option 'profile' argument '${value}' is invalid. Allowed choices are ${Object.keys(
profiles,
).join(", ")}.`,
);
}
Expand Down

0 comments on commit a76face

Please sign in to comment.