Skip to content

Commit

Permalink
[New] add --entrypoints-legacy CLI command
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Feb 27, 2024
1 parent 8d66a58 commit ab6cd95
Show file tree
Hide file tree
Showing 9 changed files with 160 additions and 3 deletions.
6 changes: 6 additions & 0 deletions .changeset/violet-bananas-own.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@arethetypeswrong/cli": minor
"@arethetypeswrong/core": minor
---

add `--entrypoints-legacy` option
3 changes: 2 additions & 1 deletion packages/cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,13 @@ In the config file, `format` can be a string value.

#### Entrypoints

`attw` automatically discovers package entrypoints by looking at package.json `exports` and subdirectories with additional package.json files. This automatic discovery process can be overridden with the `--entrypoints` option, or altered with the `--include-entrypoints` and `--exclude-entrypoints` options:
`attw` automatically discovers package entrypoints by looking at package.json `exports` and subdirectories with additional package.json files. In a package lacking `exports`, providing the `--entrypoints-legacy` option will include all published code files. This automatic discovery process can be overridden with the `--entrypoints` option, or altered with the `--include-entrypoints` and `--exclude-entrypoints` options:

```shell
attw --pack . --entrypoints . one two three # Just ".", "./one", "./two", "./three"
attw --pack . --include-entrypoints added # Auto-discovered entyrpoints plus "./added"
attw --pack . --exclude-entrypoints styles.css # Auto-discovered entrypoints except "./styles.css"
attw --pack . --entrypoints-legacy # All published code files
```

#### Ignore Rules
Expand Down
8 changes: 8 additions & 0 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ interface Opts extends render.RenderOptions {
entrypoints?: string[];
includeEntrypoints?: string[];
excludeEntrypoints?: string[];
entrypointsLegacy?: boolean;
}

program
Expand Down Expand Up @@ -71,6 +72,11 @@ particularly ESM-related module resolution issues.`,
"Specify entrypoints to check in addition to automatically discovered ones.",
)
.option("--exclude-entrypoints <entrypoints...>", "Specify entrypoints to exclude from checking.")
.option(
"--entrypoints-legacy",
'In packages without the `exports` field, every file is an entry point. Specifying this option ' +
'only takes effect when no entrypoints are automatically detected, or explicitly provided with other options.'
)
.addOption(
new Option("--ignore-rules <rules...>", "Specify rules to ignore").choices(Object.values(problemFlags)).default([]),
)
Expand Down Expand Up @@ -127,6 +133,7 @@ particularly ESM-related module resolution issues.`,
entrypoints: opts.entrypoints,
includeEntrypoints: opts.includeEntrypoints,
excludeEntrypoints: opts.excludeEntrypoints,
entrypointsLegacy: opts.entrypointsLegacy,
});
}
} catch (error) {
Expand Down Expand Up @@ -189,6 +196,7 @@ particularly ESM-related module resolution issues.`,
entrypoints: opts.entrypoints,
includeEntrypoints: opts.includeEntrypoints,
excludeEntrypoints: opts.excludeEntrypoints,
entrypointsLegacy: opts.entrypointsLegacy,
});
} catch (error) {
handleError(error, "checking file");
Expand Down
2 changes: 2 additions & 0 deletions packages/cli/test/snapshots.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ const tests = [
"react@18.2.0.tgz",
`--definitely-typed ${new URL("../../../core/test/fixtures/@types__react@18.2.21.tgz", import.meta.url).pathname}`,
],

["eslint-module-utils@2.8.1.tgz", "--entrypoints-legacy --ignore-rules=cjs-only-exports-default"],
];

const defaultOpts = "-f table-flipped";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# eslint-module-utils@2.8.1.tgz --entrypoints-legacy --ignore-rules=cjs-only-exports-default

```
$ attw eslint-module-utils@2.8.1.tgz --entrypoints-legacy --ignore-rules=cjs-only-exports-default
eslint-module-utils v2.8.1
Build tools:
- typescript@next
(ignoring rules: 'cjs-only-exports-default')
No problems found 🌟
┌─────────────────────────────────────────┬────────┬───────────────────┬───────────────────┬─────────┐
│ │ node10 │ node16 (from CJS) │ node16 (from ESM) │ bundler │
├─────────────────────────────────────────┼────────┼───────────────────┼───────────────────┼─────────┤
│ "eslint-module-utils/declaredScope.js" │ 🟢 │ 🟢 (CJS) │ 🟢 (CJS) │ 🟢 │
├─────────────────────────────────────────┼────────┼───────────────────┼───────────────────┼─────────┤
│ "eslint-module-utils/hash.js" │ 🟢 │ 🟢 (CJS) │ 🟢 (CJS) │ 🟢 │
├─────────────────────────────────────────┼────────┼───────────────────┼───────────────────┼─────────┤
│ "eslint-module-utils/ignore.js" │ 🟢 │ 🟢 (CJS) │ 🟢 (CJS) │ 🟢 │
├─────────────────────────────────────────┼────────┼───────────────────┼───────────────────┼─────────┤
│ "eslint-module-utils/module-require.js" │ 🟢 │ 🟢 (CJS) │ 🟢 (CJS) │ 🟢 │
├─────────────────────────────────────────┼────────┼───────────────────┼───────────────────┼─────────┤
│ "eslint-module-utils/ModuleCache.js" │ 🟢 │ 🟢 (CJS) │ 🟢 (CJS) │ 🟢 │
├─────────────────────────────────────────┼────────┼───────────────────┼───────────────────┼─────────┤
│ "eslint-module-utils/moduleVisitor.js" │ 🟢 │ 🟢 (CJS) │ 🟢 (CJS) │ 🟢 │
├─────────────────────────────────────────┼────────┼───────────────────┼───────────────────┼─────────┤
│ "eslint-module-utils/parse.js" │ 🟢 │ 🟢 (CJS) │ 🟢 (CJS) │ 🟢 │
├─────────────────────────────────────────┼────────┼───────────────────┼───────────────────┼─────────┤
│ "eslint-module-utils/pkgDir.js" │ 🟢 │ 🟢 (CJS) │ 🟢 (CJS) │ 🟢 │
├─────────────────────────────────────────┼────────┼───────────────────┼───────────────────┼─────────┤
│ "eslint-module-utils/pkgUp.js" │ 🟢 │ 🟢 (CJS) │ 🟢 (CJS) │ 🟢 │
├─────────────────────────────────────────┼────────┼───────────────────┼───────────────────┼─────────┤
│ "eslint-module-utils/readPkgUp.js" │ 🟢 │ 🟢 (CJS) │ 🟢 (CJS) │ 🟢 │
├─────────────────────────────────────────┼────────┼───────────────────┼───────────────────┼─────────┤
│ "eslint-module-utils/resolve.js" │ 🟢 │ 🟢 (CJS) │ 🟢 (CJS) │ 🟢 │
├─────────────────────────────────────────┼────────┼───────────────────┼───────────────────┼─────────┤
│ "eslint-module-utils/unambiguous.js" │ 🟢 │ 🟢 (CJS) │ 🟢 (CJS) │ 🟢 │
├─────────────────────────────────────────┼────────┼───────────────────┼───────────────────┼─────────┤
│ "eslint-module-utils/visit.js" │ 🟢 │ 🟢 (CJS) │ 🟢 (CJS) │ 🟢 │
└─────────────────────────────────────────┴────────┴───────────────────┴───────────────────┴─────────┘
```

Exit code: 0
6 changes: 6 additions & 0 deletions packages/core/src/checkPackage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ export interface CheckPackageOptions {
* Entrypoints to exclude from checking.
*/
excludeEntrypoints?: (string | RegExp)[];

/**
* Whether to automatically consider all published files as entrypoints
* in the absence of any other detected or configured entrypoints.
*/
entrypointsLegacy?: boolean;
}

export async function checkPackage(pkg: Package, options?: CheckPackageOptions): Promise<CheckResult> {
Expand Down
12 changes: 10 additions & 2 deletions packages/core/src/internal/getEntrypointInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,21 @@ import type {
import { allBuildTools, getResolutionKinds } from "../utils.js";
import type { CheckPackageOptions } from "../checkPackage.js";

function getEntrypoints(fs: Package, exportsObject: any, options: CheckPackageOptions | undefined): string[] {
const extensions = new Set([".jsx", ".tsx", ".js", ".ts", ".mjs", ".cjs", ".mts", ".cjs"]);

function getEntrypoints(fs: Package, exportsObject: unknown, options: CheckPackageOptions | undefined): string[] {
if (options?.entrypoints) {
return options.entrypoints.map((e) => formatEntrypointString(e, fs.packageName));
}
if (exportsObject === undefined && fs) {
const proxies = getProxyDirectories(`/node_modules/${fs.packageName}`, fs);
const rootDir = `/node_modules/${fs.packageName}`;
const proxies = getProxyDirectories(rootDir, fs);
if (proxies.length === 0) {
if (options?.entrypointsLegacy) {
return fs.listFiles()
.filter(f => !ts.isDeclarationFileName(f) && extensions.has(f.slice(f.lastIndexOf("."))))
.map(f => "." + f.slice(rootDir.length));
}
return ["."];
}
return proxies;
Expand Down
Binary file not shown.
76 changes: 76 additions & 0 deletions packages/core/test/snapshots/eslint-module-utils@2.8.1.tgz.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
{
"packageName": "eslint-module-utils",
"packageVersion": "2.8.1",
"types": {
"kind": "included"
},
"buildTools": {
"typescript": "next"
},
"entrypoints": {
".": {
"subpath": ".",
"resolutions": {
"node10": {
"name": ".",
"resolutionKind": "node10",
"visibleProblems": [
0
]
},
"node16-cjs": {
"name": ".",
"resolutionKind": "node16-cjs",
"visibleProblems": [
1
]
},
"node16-esm": {
"name": ".",
"resolutionKind": "node16-esm",
"visibleProblems": [
2
]
},
"bundler": {
"name": ".",
"resolutionKind": "bundler",
"visibleProblems": [
3
]
}
},
"hasTypes": false,
"isWildcard": false
}
},
"programInfo": {
"node10": {},
"node16": {
"moduleKinds": {}
},
"bundler": {}
},
"problems": [
{
"kind": "NoResolution",
"entrypoint": ".",
"resolutionKind": "node10"
},
{
"kind": "NoResolution",
"entrypoint": ".",
"resolutionKind": "node16-cjs"
},
{
"kind": "NoResolution",
"entrypoint": ".",
"resolutionKind": "node16-esm"
},
{
"kind": "NoResolution",
"entrypoint": ".",
"resolutionKind": "bundler"
}
]
}

0 comments on commit ab6cd95

Please sign in to comment.