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

feat: support multiple package managers #238

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
5 changes: 5 additions & 0 deletions .changeset/green-waves-beam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@arethetypeswrong/cli": minor
---

Add support for multiple package managers like [pnpm](https://pnpm.io/) and [yarn](https://yarnpkg.com/)
4 changes: 3 additions & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"@types/marked-terminal": "^3.1.3",
"@types/node": "^22.5.0",
"@types/semver": "^7.5.3",
"@types/which-pm-runs": "^1.0.2",
"ts-expose-internals": "5.6.1-rc"
},
"dependencies": {
Expand All @@ -58,7 +59,8 @@
"commander": "^10.0.1",
"marked": "^9.1.2",
"marked-terminal": "^7.1.0",
"semver": "^7.5.4"
"semver": "^7.5.4",
"which-pm-runs": "^1.1.0"
},
"engines": {
"node": ">=18"
Expand Down
38 changes: 35 additions & 3 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { getExitCode } from "./getExitCode.js";
import { applyProfile, profiles } from "./profiles.js";
import { write } from "./write.js";
import { Writable } from "stream";
import detectPackageManager from "which-pm-runs";

const packageJson = createRequire(import.meta.url)("../package.json");
const version = packageJson.version;
Expand All @@ -43,6 +44,8 @@ interface Opts extends render.RenderOptions {
includeEntrypoints?: string[];
excludeEntrypoints?: string[];
entrypointsLegacy?: boolean;

pm?: string;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should i change this to a list of the choices?

}

program
Expand Down Expand Up @@ -92,6 +95,11 @@ particularly ESM-related module resolution issues.`,
.option("--emoji, --no-emoji", "Whether to use any emojis")
.option("--color, --no-color", "Whether to use any colors (the FORCE_COLOR env variable is also available)")
.option("--config-path <path>", "Path to config file (default: ./.attw.json)")
.addOption(
new Option("--pm [package manager]", "Specify the package manager to use for --pack (default: auto)")
.choices(["pnpm", "yarn-classic", "yarn-modern", "npm", "auto"])
.default("auto"),
)
.action(async (fileOrDirectory = ".") => {
const opts = program.opts<Opts>();
await readConfig(program, opts.configPath);
Expand Down Expand Up @@ -176,15 +184,35 @@ particularly ESM-related module resolution issues.`,
);
}

let packageManager = "npm";

switch (opts.pm) {
case "auto":
const pm = detectPackageManager();
if (pm) {
packageManager = pm.name;
}
break;
case "pnpm": {
packageManager = "pnpm";
break;
}
case "yarn-modern":
case "yarn-classic": {
packageManager = "yarn";
break;
}
}

if (!opts.pack) {
if (!process.stdout.isTTY) {
program.error(
"Specifying a directory requires the --pack option to confirm that running `npm pack` is ok.",
`Specifying a directory requires the --pack option to confirm that running \`${packageManager} pack\` is ok.`,
);
}
const rl = readline.createInterface(process.stdin, process.stdout);
const answer = await new Promise<string>((resolve) => {
rl.question(`Run \`npm pack\`? (Pass -P/--pack to skip) (Y/n) `, resolve);
rl.question(`Run \`${packageManager} pack\`? (Pass -P/--pack to skip) (Y/n) `, resolve);
});
rl.close();
if (answer.trim() && !answer.trim().toLowerCase().startsWith("y")) {
Expand All @@ -198,7 +226,11 @@ particularly ESM-related module resolution issues.`,
// https://github.com/npm/cli/blob/f875caa86900122819311dd77cde01c700fd1817/lib/utils/tar.js#L123-L125
`${manifest.name.replace("@", "").replace("/", "-")}-${manifest.version}.tgz`,
);
execSync("npm pack", { cwd: fileOrDirectory, encoding: "utf8", stdio: "ignore" });
execSync(`${packageManager} pack` + (opts.pm === "yarn-classic" ? ` --filename ` + fileName : opts.pm === "yarn-modern" ? ` --out ` + fileName :""), {
cwd: fileOrDirectory,
encoding: "utf8",
stdio: "ignore",
});
}
const file = await readFile(fileName);
const data = new Uint8Array(file);
Expand Down
6 changes: 6 additions & 0 deletions packages/cli/test/snapshots.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ const tests = [
["@fluid-experimental__presence@2.3.0.tgz", "--profile node16 -f table-flipped"],
// Profile ignoring node10 and CJS resolution mixed with specific entrypoint - exit code 0
["@fluid-experimental__presence@2.3.0.tgz", "--profile esm-only -f json --entrypoints ."],

// package manager test cases
["axios@1.4.0.tgz", "--pm"], // auto
["axios@1.4.0.tgz", "--pm npm"],
["axios@1.4.0.tgz", "--pm pnpm"],
["axios@1.4.0.tgz", "--pm yarn-classic"],
];

const defaultOpts = "-f table-flipped";
Expand Down
57 changes: 57 additions & 0 deletions packages/cli/test/snapshots/axios@1.4.0.tgz --pm npm.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# axios@1.4.0.tgz --pm npm

```
$ attw axios@1.4.0.tgz --pm npm


axios v1.4.0

Build tools:
- typescript@^4.8.4
- rollup@^2.67.0

💀 Import failed to resolve to type declarations or JavaScript files. https://github.com/arethetypeswrong/arethetypeswrong.github.io/blob/main/docs/problems/NoResolution.md

❌ Import resolved to JavaScript files, but no type declarations were found. https://github.com/arethetypeswrong/arethetypeswrong.github.io/blob/main/docs/problems/UntypedResolution.md

⚠️ A require call resolved to an ESM JavaScript file, which is an error in Node and some bundlers. CommonJS consumers will need to use a dynamic import. https://github.com/arethetypeswrong/arethetypeswrong.github.io/blob/main/docs/problems/CJSResolvesToESM.md


┌─────────────────────────────────────────┬──────────────────────┬──────────────────────────────┬───────────────────┬─────────────┐
│ │ node10 │ node16 (from CJS) │ node16 (from ESM) │ bundler │
├─────────────────────────────────────────┼──────────────────────┼──────────────────────────────┼───────────────────┼─────────────┤
│ "axios" │ 🟢 │ 🟢 (CJS) │ 🟢 (ESM) │ 🟢 │
├─────────────────────────────────────────┼──────────────────────┼──────────────────────────────┼───────────────────┼─────────────┤
│ "axios/unsafe/*" │ (wildcard) │ (wildcard) │ (wildcard) │ (wildcard) │
├─────────────────────────────────────────┼──────────────────────┼──────────────────────────────┼───────────────────┼─────────────┤
│ "axios/unsafe/core/settle.js" │ 💀 Resolution failed │ ❌ No types │ ❌ No types │ ❌ No types │
│ │ │ ⚠️ ESM (dynamic import only) │ │ │
├─────────────────────────────────────────┼──────────────────────┼──────────────────────────────┼───────────────────┼─────────────┤
│ "axios/unsafe/core/buildFullPath.js" │ 💀 Resolution failed │ ❌ No types │ ❌ No types │ ❌ No types │
│ │ │ ⚠️ ESM (dynamic import only) │ │ │
├─────────────────────────────────────────┼──────────────────────┼──────────────────────────────┼───────────────────┼─────────────┤
│ "axios/unsafe/helpers/isAbsoluteURL.js" │ 💀 Resolution failed │ ❌ No types │ ❌ No types │ ❌ No types │
│ │ │ ⚠️ ESM (dynamic import only) │ │ │
├─────────────────────────────────────────┼──────────────────────┼──────────────────────────────┼───────────────────┼─────────────┤
│ "axios/unsafe/helpers/buildURL.js" │ 💀 Resolution failed │ ❌ No types │ ❌ No types │ ❌ No types │
│ │ │ ⚠️ ESM (dynamic import only) │ │ │
├─────────────────────────────────────────┼──────────────────────┼──────────────────────────────┼───────────────────┼─────────────┤
│ "axios/unsafe/helpers/combineURLs.js" │ 💀 Resolution failed │ ❌ No types │ ❌ No types │ ❌ No types │
│ │ │ ⚠️ ESM (dynamic import only) │ │ │
├─────────────────────────────────────────┼──────────────────────┼──────────────────────────────┼───────────────────┼─────────────┤
│ "axios/unsafe/adapters/http.js" │ 💀 Resolution failed │ ❌ No types │ ❌ No types │ ❌ No types │
│ │ │ ⚠️ ESM (dynamic import only) │ │ │
├─────────────────────────────────────────┼──────────────────────┼──────────────────────────────┼───────────────────┼─────────────┤
│ "axios/unsafe/adapters/xhr.js" │ 💀 Resolution failed │ ❌ No types │ ❌ No types │ ❌ No types │
│ │ │ ⚠️ ESM (dynamic import only) │ │ │
├─────────────────────────────────────────┼──────────────────────┼──────────────────────────────┼───────────────────┼─────────────┤
│ "axios/unsafe/utils.js" │ 💀 Resolution failed │ ❌ No types │ ❌ No types │ ❌ No types │
│ │ │ ⚠️ ESM (dynamic import only) │ │ │
├─────────────────────────────────────────┼──────────────────────┼──────────────────────────────┼───────────────────┼─────────────┤
│ "axios/package.json" │ 🟢 (JSON) │ 🟢 (JSON) │ 🟢 (JSON) │ 🟢 (JSON) │
└─────────────────────────────────────────┴──────────────────────┴──────────────────────────────┴───────────────────┴─────────────┘


```

Exit code: 1
Loading