-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor to use npm as a fallback if no
packageManager
specified...
and no lockfile is present. Fixes #180
- Loading branch information
Showing
8 changed files
with
163 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"wrangler-action": patch | ||
--- | ||
|
||
Fixed action failure when no `packageManager` specified and no lockfile is found. The action now falls back to using npm. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { describe, expect, test } from "vitest"; | ||
import { getPackageManager } from "./packageManagers"; | ||
|
||
describe("getPackageManager", () => { | ||
test("should use provided value instead of inferring from lockfile", () => { | ||
expect(getPackageManager("npm", { workingDirectory: "test/npm" })) | ||
.toMatchInlineSnapshot(` | ||
{ | ||
"exec": "npx", | ||
"install": "npm i", | ||
} | ||
`); | ||
|
||
expect(getPackageManager("yarn", { workingDirectory: "test/npm" })) | ||
.toMatchInlineSnapshot(` | ||
{ | ||
"exec": "yarn", | ||
"install": "yarn add", | ||
} | ||
`); | ||
|
||
expect(getPackageManager("pnpm", { workingDirectory: "test/npm" })) | ||
.toMatchInlineSnapshot(` | ||
{ | ||
"exec": "pnpm exec", | ||
"install": "pnpm add", | ||
} | ||
`); | ||
}); | ||
|
||
test("should use npm if no value provided and package-lock.json exists", () => { | ||
expect(getPackageManager("", { workingDirectory: "test/npm" })) | ||
.toMatchInlineSnapshot(` | ||
{ | ||
"exec": "npx", | ||
"install": "npm i", | ||
} | ||
`); | ||
}); | ||
|
||
test("should use yarn if no value provided and yarn.lock exists", () => { | ||
expect(getPackageManager("", { workingDirectory: "test/yarn" })) | ||
.toMatchInlineSnapshot(` | ||
{ | ||
"exec": "yarn", | ||
"install": "yarn add", | ||
} | ||
`); | ||
}); | ||
|
||
test("should use pnpm if no value provided and pnpm-lock.yaml exists", () => { | ||
expect(getPackageManager("", { workingDirectory: "test/pnpm" })) | ||
.toMatchInlineSnapshot(` | ||
{ | ||
"exec": "pnpm exec", | ||
"install": "pnpm add", | ||
} | ||
`); | ||
}); | ||
|
||
test("should use npm if no value provided and no lockfile is present", () => { | ||
expect(getPackageManager("", { workingDirectory: "test/empty" })) | ||
.toMatchInlineSnapshot(` | ||
{ | ||
"exec": "npx", | ||
"install": "npm i", | ||
} | ||
`); | ||
}); | ||
|
||
test("should throw if an invalid value is provided", () => { | ||
expect(() => | ||
getPackageManager("cargo", { workingDirectory: "test/npm" }), | ||
).toThrowError(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { existsSync } from "node:fs"; | ||
import * as path from "node:path"; | ||
|
||
interface PackageManager { | ||
install: string; | ||
exec: string; | ||
} | ||
|
||
const PACKAGE_MANAGERS = { | ||
npm: { | ||
install: "npm i", | ||
exec: "npx", | ||
}, | ||
yarn: { | ||
install: "yarn add", | ||
exec: "yarn", | ||
}, | ||
pnpm: { | ||
install: "pnpm add", | ||
exec: "pnpm exec", | ||
}, | ||
} as const satisfies Readonly<Record<string, PackageManager>>; | ||
|
||
type PackageManagerValue = keyof typeof PACKAGE_MANAGERS; | ||
|
||
function detectPackageManager( | ||
workingDirectory = ".", | ||
): PackageManagerValue | null { | ||
if (existsSync(path.join(workingDirectory, "package-lock.json"))) { | ||
return "npm"; | ||
} | ||
if (existsSync(path.join(workingDirectory, "yarn.lock"))) { | ||
return "yarn"; | ||
} | ||
if (existsSync(path.join(workingDirectory, "pnpm-lock.yaml"))) { | ||
return "pnpm"; | ||
} | ||
return null; | ||
} | ||
|
||
function assertValidPackageManagerValue( | ||
name: string, | ||
): asserts name is PackageManagerValue | "" { | ||
if (name && !Object.keys(PACKAGE_MANAGERS).includes(name)) { | ||
throw new TypeError( | ||
`invalid value provided for "packageManager": ${name} | ||
Value must be one of: [${Object.keys(PACKAGE_MANAGERS).join(", ")}]`, | ||
); | ||
} | ||
} | ||
|
||
export function getPackageManager( | ||
name: string, | ||
{ workingDirectory = "." }: { workingDirectory?: string } = {}, | ||
) { | ||
assertValidPackageManagerValue(name); | ||
|
||
return PACKAGE_MANAGERS[ | ||
name || detectPackageManager(workingDirectory) || "npm" | ||
]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters