1
0
mirror of https://github.com/pnpm/action-setup.git synced 2026-07-14 18:03:44 +08:00

Compare commits

...

5 Commits

Author SHA1 Message Date
Karl Horky
7ce3debe10 Match versions 2024-04-19 16:45:24 +02:00
Karl Horky
b036c0dcb9 Swallow error on ENOENT 2024-04-19 16:43:36 +02:00
Khải
327b709af8 fix: fmt 2024-04-19 17:57:05 +07:00
Khải
4504319403 fix: fmt 2024-04-19 17:55:58 +07:00
Karl Horky
1ecf7e8d5f Throw error when multiple versions specified 2024-04-19 12:29:23 +02:00

View File

@@ -41,10 +41,33 @@ async function readTarget(opts: {
readonly standalone: boolean readonly standalone: boolean
}) { }) {
const { version, packageJsonFile, standalone } = opts const { version, packageJsonFile, standalone } = opts
if (version) return `${ standalone ? '@pnpm/exe' : 'pnpm' }@${version}`
const { GITHUB_WORKSPACE } = process.env const { GITHUB_WORKSPACE } = process.env
let packageManager
if (GITHUB_WORKSPACE) {
try {
({ packageManager } = JSON.parse(await readFile(path.join(GITHUB_WORKSPACE, packageJsonFile), 'utf8')))
} catch (error) {
// Swallow error if package.json doesn't exist in root
if (error.code !== 'ENOENT') throw error
}
}
if (version) {
if (
typeof packageManager === 'string' &&
packageManager.replace('pnpm@', '') !== version
) {
throw new Error(`Multiple versions of pnpm specified:
- version ${version} in the GitHub Action config with the key "version"
- version ${packageManager} in the package.json with the key "packageManager"
Remove one of these versions to avoid version mismatch errors like ERR_PNPM_BAD_PM_VERSION`)
}
return `${ standalone ? '@pnpm/exe' : 'pnpm' }@${version}`
}
if (!GITHUB_WORKSPACE) { if (!GITHUB_WORKSPACE) {
throw new Error(`No workspace is found. throw new Error(`No workspace is found.
If you're intended to let pnpm/action-setup read preferred pnpm version from the "packageManager" field in the package.json file, If you're intended to let pnpm/action-setup read preferred pnpm version from the "packageManager" field in the package.json file,
@@ -52,7 +75,6 @@ please run the actions/checkout before pnpm/action-setup.
Otherwise, please specify the pnpm version in the action configuration.`) Otherwise, please specify the pnpm version in the action configuration.`)
} }
const { packageManager } = JSON.parse(await readFile(path.join(GITHUB_WORKSPACE, packageJsonFile), 'utf8'))
if (typeof packageManager !== 'string') { if (typeof packageManager !== 'string') {
throw new Error(`No pnpm version is specified. throw new Error(`No pnpm version is specified.
Please specify it by one of the following ways: Please specify it by one of the following ways:
@@ -64,7 +86,7 @@ Please specify it by one of the following ways:
throw new Error('Invalid packageManager field in package.json') throw new Error('Invalid packageManager field in package.json')
} }
if(standalone){ if (standalone) {
return packageManager.replace('pnpm@', '@pnpm/exe@') return packageManager.replace('pnpm@', '@pnpm/exe@')
} }