-
-
Couldn't load subscription status.
- Fork 28
feat: improve shareable config #208
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
Changes from 8 commits
ecdb5fa
fecf3f3
c2f6259
22d042c
bd29368
896baf7
bfbff1c
6970f86
122699f
cde4824
73bea01
f7fedc3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,6 +12,7 @@ import fs from "node:fs"; | |
| import spawn from "cross-spawn"; | ||
| import path from "node:path"; | ||
| import * as log from "./logging.js"; | ||
| import semverMaxSatisfying from "semver/ranges/max-satisfying.js"; | ||
|
|
||
| //------------------------------------------------------------------------------ | ||
| // Helpers | ||
|
|
@@ -86,52 +87,32 @@ function parsePackageName(packageName) { | |
| * @returns {Object} Gotten peerDependencies. Returns null if npm was not found. | ||
| */ | ||
| async function fetchPeerDependencies(packageName) { | ||
| const npmProcess = spawn.sync( | ||
| "npm", | ||
| ["show", "--json", packageName, "peerDependencies"], | ||
| { encoding: "utf8" } | ||
| ); | ||
| const { name, version } = parsePackageName(packageName); | ||
|
|
||
| const error = npmProcess.error; | ||
| try { | ||
| // eslint-disable-next-line n/no-unsupported-features/node-builtins -- Fallback using built-in fetch | ||
| const response = await fetch(`https://registry.npmjs.org/${name}`); | ||
| const data = await response.json(); | ||
|
|
||
| if (error && error.code === "ENOENT") { | ||
| if (data.error) { | ||
| throw new Error(data.error); | ||
aladdin-add marked this conversation as resolved.
Show resolved
Hide resolved
aladdin-add marked this conversation as resolved.
Show resolved
Hide resolved
aladdin-add marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| // Fallback to using the npm registry API directly when npm is not available. | ||
| const { name, version } = parsePackageName(packageName); | ||
|
|
||
| try { | ||
| // eslint-disable-next-line n/no-unsupported-features/node-builtins -- Fallback using built-in fetch | ||
| const response = await fetch(`https://registry.npmjs.org/${name}`); | ||
| const data = await response.json(); | ||
|
|
||
| const resolvedVersion = | ||
| version === "latest" ? data["dist-tags"]?.latest : version; | ||
| const packageVersion = data.versions[resolvedVersion]; | ||
|
|
||
| if (!packageVersion) { | ||
| throw new Error( | ||
| `Version "${version}" not found for package "${name}".` | ||
| ); | ||
| } | ||
| return Object.entries(packageVersion.peerDependencies).map( | ||
| ([pkgName, pkgVersion]) => `${pkgName}@${pkgVersion}` | ||
| ); | ||
| } catch { | ||
| const resolvedVersion = semverMaxSatisfying(Object.keys(data.versions), data["dist-tags"]?.[version] ?? version); | ||
aladdin-add marked this conversation as resolved.
Show resolved
Hide resolved
aladdin-add marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const packageVersion = data.versions[resolvedVersion]; | ||
|
|
||
| // TODO: should throw an error instead of returning null | ||
| return null; | ||
| if (!packageVersion) { | ||
| throw new Error( | ||
| `Version "${version}" not found for package "${name}".` | ||
| ); | ||
| } | ||
| return Object.entries(Object(packageVersion.peerDependencies)).map( | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why |
||
| ([pkgName, pkgVersion]) => `${pkgName}@${pkgVersion}` | ||
| ); | ||
| } catch (err) { | ||
| // eslint-disable-next-line preserve-caught-error -- Throw error again :) | ||
| throw new Error(`Cannot fetch "${name}@${version}" with error: ${err.message || err}`); | ||
| } | ||
| const fetchedText = npmProcess.stdout.trim(); | ||
|
|
||
| const peers = JSON.parse(fetchedText || "{}"); | ||
| const dependencies = []; | ||
|
|
||
| Object.keys(peers).forEach(pkgName => { | ||
| dependencies.push(`${pkgName}@${peers[pkgName]}`); | ||
| }); | ||
|
|
||
| return dependencies; | ||
| } | ||
|
|
||
| /** | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,7 +18,7 @@ import { | |
| parsePackageName | ||
| } from "../../lib/utils/npm-utils.js"; | ||
| import { defineInMemoryFs } from "../_utils/in-memory-fs.js"; | ||
| import { assert, describe, afterEach, it } from "vitest"; | ||
| import { assert, describe, afterEach, it, expect } from "vitest"; | ||
| import fs from "node:fs"; | ||
| import process from "node:process"; | ||
|
|
||
|
|
@@ -241,20 +241,10 @@ describe("npmUtils", () => { | |
| }); | ||
|
|
||
| describe("fetchPeerDependencies()", () => { | ||
| it("should execute 'npm show --json <packageName> peerDependencies' command", async () => { | ||
| const stub = sinon.stub(spawn, "sync").returns({ stdout: "" }); | ||
|
|
||
| await fetchPeerDependencies("desired-package"); | ||
| assert(stub.calledOnce); | ||
| assert.strictEqual(stub.firstCall.args[0], "npm"); | ||
| assert.deepStrictEqual(stub.firstCall.args[1], ["show", "--json", "desired-package", "peerDependencies"]); | ||
| stub.restore(); | ||
| }); | ||
|
|
||
| // Skip on Node.js v21 due to a bug where fetch cannot be stubbed | ||
| // See: https://github.com/sinonjs/sinon/issues/2590 | ||
| it.skipIf(process.version.startsWith("v21"))("should fetch peer dependencies from npm registry when npm is not available", async () => { | ||
| const npmStub = sinon.stub(spawn, "sync").returns({ error: { code: "ENOENT" } }); | ||
| it.skipIf(process.version.startsWith("v21"))("should fetch peer dependencies from npm registry", async () => { | ||
| const fetchStub = sinon.stub(globalThis, "fetch"); | ||
|
|
||
| const mockResponse = { | ||
|
|
@@ -277,16 +267,21 @@ describe("npmUtils", () => { | |
| assert(fetchStub.calledOnceWith("https://registry.npmjs.org/desired-package")); | ||
| assert.deepStrictEqual(result, ["[email protected]"]); | ||
|
|
||
| npmStub.restore(); | ||
| fetchStub.restore(); | ||
| }); | ||
|
|
||
| it("should return null if an error is thrown", async () => { | ||
| const stub = sinon.stub(spawn, "sync").returns({ error: { code: "ENOENT" } }); | ||
| it("should throw if an error is thrown", async () => { | ||
| const stub = sinon.stub(globalThis, "fetch"); | ||
|
|
||
| const mockResponse = { | ||
| json: sinon.stub().resolves({ error: "Not found" }), | ||
| ok: false, | ||
| status: 404 | ||
| }; | ||
|
|
||
| const peerDependencies = await fetchPeerDependencies("desired-package"); | ||
| stub.resolves(mockResponse); | ||
|
|
||
| assert.isNull(peerDependencies); | ||
| expect(async () => await fetchPeerDependencies("desired-package")).rejects.toThrowError(); | ||
aladdin-add marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| stub.restore(); | ||
| }); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the comment needs an update. :)