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

Compare commits

...

4 Commits

Author SHA1 Message Date
Ben McCann
a376caa22a
Merge d5bd7e8c9a into 41ff726559 2025-10-08 07:32:57 -06:00
Adrian Riedel
41ff726559
feat: support installation from custom NPM registry (#179)
copy .npmrc from GitHub workspace if it exists so that PNPM respects custom
registry configurations when self-installing
2025-10-08 10:48:14 +02:00
Ben McCann
d5bd7e8c9a
Update src/install-pnpm/run.ts 2024-06-12 08:37:26 -07:00
Ben McCann
44def7846a feat: detect pnpm version from lockfile 2024-05-28 12:08:13 -07:00
2 changed files with 40 additions and 3 deletions

2
dist/index.js vendored

File diff suppressed because one or more lines are too long

View File

@ -1,15 +1,17 @@
import { addPath, exportVariable } from '@actions/core' import { addPath, exportVariable } from '@actions/core'
import { spawn } from 'child_process' import { spawn } from 'child_process'
import { rm, writeFile, mkdir } from 'fs/promises' import { rm, writeFile, mkdir, copyFile } from 'fs/promises'
import { readFileSync } from 'fs' import { readFileSync } from 'fs'
import path from 'path' import path from 'path'
import { execPath } from 'process' import { execPath } from 'process'
import util from 'util' import util from 'util'
import * as yaml from 'yaml'
import { Inputs } from '../inputs' import { Inputs } from '../inputs'
import YAML from 'yaml' import YAML from 'yaml'
export async function runSelfInstaller(inputs: Inputs): Promise<number> { export async function runSelfInstaller(inputs: Inputs): Promise<number> {
const { version, dest, packageJsonFile, standalone } = inputs const { version, dest, packageJsonFile, standalone } = inputs
const { GITHUB_WORKSPACE } = process.env
// prepare self install // prepare self install
await rm(dest, { recursive: true, force: true }) await rm(dest, { recursive: true, force: true })
@ -19,6 +21,16 @@ export async function runSelfInstaller(inputs: Inputs): Promise<number> {
// we have ensured the dest directory exists, we can write the file directly // we have ensured the dest directory exists, we can write the file directly
await writeFile(pkgJson, JSON.stringify({ private: true })) await writeFile(pkgJson, JSON.stringify({ private: true }))
// copy .npmrc if it exists to install from custom registry
if (GITHUB_WORKSPACE) {
try {
await copyFile(path.join(GITHUB_WORKSPACE, '.npmrc'), path.join(dest, '.npmrc'))
} catch (error) {
// Swallow error if .npmrc doesn't exist
if (!util.types.isNativeError(error) || !('code' in error) || error.code !== 'ENOENT') throw error
}
}
// prepare target pnpm // prepare target pnpm
const target = await readTarget({ version, packageJsonFile, standalone }) const target = await readTarget({ version, packageJsonFile, standalone })
const cp = spawn(execPath, [path.join(__dirname, 'pnpm.cjs'), 'install', target, '--no-lockfile'], { const cp = spawn(execPath, [path.join(__dirname, 'pnpm.cjs'), 'install', target, '--no-lockfile'], {
@ -84,11 +96,19 @@ Otherwise, please specify the pnpm version in the action configuration.`)
} }
if (typeof packageManager !== 'string') { if (typeof packageManager !== 'string') {
if (GITHUB_WORKSPACE) {
try {
const { lockfileVersion } = yaml.parse(readFileSync(path.join(GITHUB_WORKSPACE, 'pnpm-lock.yaml'), 'utf8'))
const version = getPnpmVersionFromLockfile(lockfileVersion);
return `${ standalone ? '@pnpm/exe' : 'pnpm' }@${version}`
} catch (error: unknown) {
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:
- in the GitHub Action config with the key "version" - in the GitHub Action config with the key "version"
- in the package.json with the key "packageManager"`) - in the package.json with the key "packageManager"`)
} }
}
}
if (!packageManager.startsWith('pnpm@')) { if (!packageManager.startsWith('pnpm@')) {
throw new Error('Invalid packageManager field in package.json') throw new Error('Invalid packageManager field in package.json')
@ -101,4 +121,21 @@ Please specify it by one of the following ways:
return packageManager return packageManager
} }
function getPnpmVersionFromLockfile(
lockfileVersion: string | undefined
): string | undefined {
switch (true) {
case lockfileVersion === '5.3':
return 'latest-6';
case lockfileVersion === '5.4':
return 'latest-7';
case lockfileVersion === '6.0' || lockfileVersion === '6.1':
return 'latest-8';
case lockfileVersion === '7.0' || lockfileVersion === '9.0':
return 'latest-9';
default:
return undefined;
}
}
export default runSelfInstaller export default runSelfInstaller