From ab72e3e12e52f8bcd8a3bf3af31083d6bba31ae9 Mon Sep 17 00:00:00 2001 From: Alexander Hornung Date: Wed, 4 Feb 2026 20:48:16 +1100 Subject: [PATCH 1/4] fix: check if pnpm already installed (#176) --- dist/index.js | Bin 1508866 -> 1508038 bytes src/install-pnpm/index.ts | 29 ++++++--- src/install-pnpm/run.ts | 123 ++++++++++++++++++++++---------------- 3 files changed, 91 insertions(+), 61 deletions(-) diff --git a/dist/index.js b/dist/index.js index c315d0680ca5e97b4e228b0c5737a27ababa3d9c..6c8085fba1a00ca2ee05e269ab72a69c4fa91708 100644 GIT binary patch delta 533 zcmYMxK~EDw6ae5_CadsA1mxcsF zJiv)LaPFyBlE(NCylUdje?a1o;B9N;;Y()Tym|ZH?CIsskMo_I{_OSdO=sa+>G=)& zq5RDDpI4i&n?caRA(bLAUep#S2pa3bIH1~*|HYTPdyE>CYerL@@B_9H*`H;%6xqLJ z_kEE;v?duUR*BJEd_)-gQW-7<=^)%4my3txQUP(aU1W>21LrHna#zX@BLa46xF3~N zHg=Xv++H~^T(q_#eMNM_FfuqaI?G3GvvzEMIiDfzO{Fy#`R`=(R%@wo4p+FnsJw)u zLFJF_R~sjzD+(s9H|p!n|E)owwf3q$4rFb(9Q#VkeV*_*!$|q;h*2DpBoow&Q$dRX zJq|BNU<`)yq9t-jxbZTyq=ee{R#y)gJed?rIMmFfdP;ODG4xa3wN8x=j%(jalL8I@ z!XRASnwWvFZvLC{tml61xg~H9+y@ij0eA=|!6PsQrojw&44!~l@D$8}c~AxpsDSGC Io?9cgcOM?K^8f$< delta 581 zcmY+=L2DC16aZj0jft(A-K0%1R4`UsXUMWdq-*0kw+0a@A|48+9=4g;xU$Twv$H8{ z=%pfFyv-pe&*DueQoVZcT(2UYy!Zz^dGKx12p#xf_Pw`nX5QCV+xu^}KfJ3Pe(4!2 zg#sRp>Cf3uWyw@3d2?(OjqXDtcKLmlC5U00lD0?ha3^AXpq_fP)xu$bc#l50LdY^@ zfuBZd8HY&O%+ABu6Zsx{CoR?99vXl9TwWR<9T}JPpUTxvOgLKWz`|Z%@I*BOLJ>#n zw_LY_r?Cu*TzBn;I}Hp;rXQH~Zsxpjc7%wS&k=S+tU|&42w}UwOK<1NM9Gj3PUn0% zNRE@flzxUX{oZWe+#q>*%Ao=3t7>ua_3OUjWc zf5f8T9?Zj!A|?rSPNu8h)|T|ITE8@z=)v9jkH%d1e_#hQ^{t}0Ijx@D>LqKuW(@-r ozzisYGoS>@;4GL0bKo2}4=#WTFo6ZCpa$l_0;nId#-jiGA2gM|UH||9 diff --git a/src/install-pnpm/index.ts b/src/install-pnpm/index.ts index 23b2695..55ed739 100644 --- a/src/install-pnpm/index.ts +++ b/src/install-pnpm/index.ts @@ -1,16 +1,27 @@ -import { setFailed, startGroup, endGroup } from '@actions/core' -import { Inputs } from '../inputs' -import runSelfInstaller from './run' +import { setFailed, startGroup, endGroup } from "@actions/core"; +import { Inputs } from "../inputs"; +import runSelfInstaller from "./run"; +import { exec } from "child_process"; +import { promisify } from "util"; -export { runSelfInstaller } +const execAsync = promisify(exec); export async function install(inputs: Inputs) { - startGroup('Running self-installer...') - const status = await runSelfInstaller(inputs) - endGroup() + try { + await execAsync("pnpm --version"); + // pnpm already installed + return; + } catch { + // pnpm not installed, continue + } + + startGroup("Running self-installer..."); + const status = await runSelfInstaller(inputs); + endGroup(); + if (status) { - return setFailed(`Something went wrong, self-installer exits with code ${status}`) + setFailed(`Something went wrong, self-installer exits with code ${status}`); } } -export default install +export default install; diff --git a/src/install-pnpm/run.ts b/src/install-pnpm/run.ts index 005c500..9ff583f 100644 --- a/src/install-pnpm/run.ts +++ b/src/install-pnpm/run.ts @@ -1,115 +1,134 @@ -import { addPath, exportVariable } from '@actions/core' -import { spawn } from 'child_process' -import { rm, writeFile, mkdir, copyFile } from 'fs/promises' -import { readFileSync } from 'fs' -import path from 'path' -import { execPath } from 'process' -import util from 'util' -import { Inputs } from '../inputs' -import { parse as parseYaml } from 'yaml' +import { addPath, exportVariable } from "@actions/core"; +import { spawn } from "child_process"; +import { rm, writeFile, mkdir, copyFile } from "fs/promises"; +import { readFileSync } from "fs"; +import path from "path"; +import { execPath } from "process"; +import util from "util"; +import { Inputs } from "../inputs"; +import { parse as parseYaml } from "yaml"; export async function runSelfInstaller(inputs: Inputs): Promise { - const { version, dest, packageJsonFile, standalone } = inputs - const { GITHUB_WORKSPACE } = process.env + const { version, dest, packageJsonFile, standalone } = inputs; + const { GITHUB_WORKSPACE } = process.env; // prepare self install - await rm(dest, { recursive: true, force: true }) + await rm(dest, { recursive: true, force: true }); // create dest directory after removal - await mkdir(dest, { recursive: true }) - const pkgJson = path.join(dest, 'package.json') + await mkdir(dest, { recursive: true }); + const pkgJson = path.join(dest, "package.json"); // 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')) + 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 + if ( + !util.types.isNativeError(error) || + !("code" in error) || + error.code !== "ENOENT" + ) + throw error; } } // prepare target pnpm - const target = await readTarget({ version, packageJsonFile, standalone }) - const cp = spawn(execPath, [path.join(__dirname, 'pnpm.cjs'), 'install', target, '--no-lockfile'], { - cwd: dest, - stdio: ['pipe', 'inherit', 'inherit'], - }) + const target = await readTarget({ version, packageJsonFile, standalone }); + const cp = spawn( + execPath, + [path.join(__dirname, "pnpm.cjs"), "install", target, "--no-lockfile"], + { + cwd: dest, + stdio: ["pipe", "inherit", "inherit"], + }, + ); const exitCode = await new Promise((resolve, reject) => { - cp.on('error', reject) - cp.on('close', resolve) - }) + cp.on("error", reject); + cp.on("close", resolve); + }); if (exitCode === 0) { - const pnpmHome = path.join(dest, 'node_modules/.bin') - addPath(pnpmHome) - exportVariable('PNPM_HOME', pnpmHome) + const pnpmHome = path.join(dest, "node_modules/.bin"); + addPath(pnpmHome); + exportVariable("PNPM_HOME", pnpmHome); } - return exitCode + return exitCode; } async function readTarget(opts: { - readonly version?: string | undefined - readonly packageJsonFile: string - readonly standalone: boolean + readonly version?: string | undefined; + readonly packageJsonFile: string; + readonly standalone: boolean; }) { - const { version, packageJsonFile, standalone } = opts - const { GITHUB_WORKSPACE } = process.env + const { version, packageJsonFile, standalone } = opts; + const { GITHUB_WORKSPACE } = process.env; - let packageManager + let packageManager; if (GITHUB_WORKSPACE) { try { - const content = readFileSync(path.join(GITHUB_WORKSPACE, packageJsonFile), 'utf8'); + const content = readFileSync( + path.join(GITHUB_WORKSPACE, packageJsonFile), + "utf8", + ); ({ packageManager } = packageJsonFile.endsWith(".yaml") ? parseYaml(content, { merge: true }) - : JSON.parse(content) - ) + : JSON.parse(content)); } catch (error: unknown) { // Swallow error if package.json doesn't exist in root - if (!util.types.isNativeError(error) || !('code' in error) || error.code !== 'ENOENT') throw error + if ( + !util.types.isNativeError(error) || + !("code" in error) || + error.code !== "ENOENT" + ) + throw error; } } if (version) { if ( - typeof packageManager === 'string' && - packageManager.startsWith('pnpm@') && - packageManager.replace('pnpm@', '') !== version + typeof packageManager === "string" && + packageManager.startsWith("pnpm@") && + 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`) +Remove one of these versions to avoid version mismatch errors like ERR_PNPM_BAD_PM_VERSION`); } - return `${ standalone ? '@pnpm/exe' : 'pnpm' }@${version}` + return `${standalone ? "@pnpm/exe" : "pnpm"}@${version}`; } if (!GITHUB_WORKSPACE) { throw new Error(`No workspace is found. If you've intended to let pnpm/action-setup read preferred pnpm version from the "packageManager" field in the package.json file, 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.`); } - if (typeof packageManager !== 'string') { + if (typeof packageManager !== "string") { throw new Error(`No pnpm version is specified. Please specify it by one of the following ways: - 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@')) { - throw new Error('Invalid packageManager field in package.json') + if (!packageManager.startsWith("pnpm@")) { + throw new Error("Invalid packageManager field in package.json"); } if (standalone) { - return packageManager.replace('pnpm@', '@pnpm/exe@') + return packageManager.replace("pnpm@", "@pnpm/exe@"); } - return packageManager + return packageManager; } -export default runSelfInstaller +export default runSelfInstaller; From b2ad3f504b04d782162d7d5d3bf6a9e00cfddef9 Mon Sep 17 00:00:00 2001 From: Alexander Hornung Date: Thu, 5 Feb 2026 13:09:28 +1100 Subject: [PATCH 2/4] Remove unnecessary comments --- src/install-pnpm/index.ts | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/install-pnpm/index.ts b/src/install-pnpm/index.ts index 55ed739..aa1a3a6 100644 --- a/src/install-pnpm/index.ts +++ b/src/install-pnpm/index.ts @@ -9,11 +9,8 @@ const execAsync = promisify(exec); export async function install(inputs: Inputs) { try { await execAsync("pnpm --version"); - // pnpm already installed return; - } catch { - // pnpm not installed, continue - } + } catch {} startGroup("Running self-installer..."); const status = await runSelfInstaller(inputs); From 4a8f7ada16ee27f9d20f967be591f230f7d53a3e Mon Sep 17 00:00:00 2001 From: Alexander Hornung Date: Thu, 5 Feb 2026 13:21:54 +1100 Subject: [PATCH 3/4] Revert old styling --- src/install-pnpm/index.ts | 5 +- src/install-pnpm/run.ts | 123 ++++++++++++++++---------------------- 2 files changed, 56 insertions(+), 72 deletions(-) diff --git a/src/install-pnpm/index.ts b/src/install-pnpm/index.ts index aa1a3a6..ac74224 100644 --- a/src/install-pnpm/index.ts +++ b/src/install-pnpm/index.ts @@ -4,6 +4,7 @@ import runSelfInstaller from "./run"; import { exec } from "child_process"; import { promisify } from "util"; +export { runSelfInstaller }; const execAsync = promisify(exec); export async function install(inputs: Inputs) { @@ -17,7 +18,9 @@ export async function install(inputs: Inputs) { endGroup(); if (status) { - setFailed(`Something went wrong, self-installer exits with code ${status}`); + return setFailed( + `Something went wrong, self-installer exits with code ${status}`, + ); } } diff --git a/src/install-pnpm/run.ts b/src/install-pnpm/run.ts index 9ff583f..005c500 100644 --- a/src/install-pnpm/run.ts +++ b/src/install-pnpm/run.ts @@ -1,134 +1,115 @@ -import { addPath, exportVariable } from "@actions/core"; -import { spawn } from "child_process"; -import { rm, writeFile, mkdir, copyFile } from "fs/promises"; -import { readFileSync } from "fs"; -import path from "path"; -import { execPath } from "process"; -import util from "util"; -import { Inputs } from "../inputs"; -import { parse as parseYaml } from "yaml"; +import { addPath, exportVariable } from '@actions/core' +import { spawn } from 'child_process' +import { rm, writeFile, mkdir, copyFile } from 'fs/promises' +import { readFileSync } from 'fs' +import path from 'path' +import { execPath } from 'process' +import util from 'util' +import { Inputs } from '../inputs' +import { parse as parseYaml } from 'yaml' export async function runSelfInstaller(inputs: Inputs): Promise { - const { version, dest, packageJsonFile, standalone } = inputs; - const { GITHUB_WORKSPACE } = process.env; + const { version, dest, packageJsonFile, standalone } = inputs + const { GITHUB_WORKSPACE } = process.env // prepare self install - await rm(dest, { recursive: true, force: true }); + await rm(dest, { recursive: true, force: true }) // create dest directory after removal - await mkdir(dest, { recursive: true }); - const pkgJson = path.join(dest, "package.json"); + await mkdir(dest, { recursive: true }) + const pkgJson = path.join(dest, 'package.json') // 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"), - ); + 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; + if (!util.types.isNativeError(error) || !('code' in error) || error.code !== 'ENOENT') throw error } } // prepare target pnpm - const target = await readTarget({ version, packageJsonFile, standalone }); - const cp = spawn( - execPath, - [path.join(__dirname, "pnpm.cjs"), "install", target, "--no-lockfile"], - { - cwd: dest, - stdio: ["pipe", "inherit", "inherit"], - }, - ); + const target = await readTarget({ version, packageJsonFile, standalone }) + const cp = spawn(execPath, [path.join(__dirname, 'pnpm.cjs'), 'install', target, '--no-lockfile'], { + cwd: dest, + stdio: ['pipe', 'inherit', 'inherit'], + }) const exitCode = await new Promise((resolve, reject) => { - cp.on("error", reject); - cp.on("close", resolve); - }); + cp.on('error', reject) + cp.on('close', resolve) + }) if (exitCode === 0) { - const pnpmHome = path.join(dest, "node_modules/.bin"); - addPath(pnpmHome); - exportVariable("PNPM_HOME", pnpmHome); + const pnpmHome = path.join(dest, 'node_modules/.bin') + addPath(pnpmHome) + exportVariable('PNPM_HOME', pnpmHome) } - return exitCode; + return exitCode } async function readTarget(opts: { - readonly version?: string | undefined; - readonly packageJsonFile: string; - readonly standalone: boolean; + readonly version?: string | undefined + readonly packageJsonFile: string + readonly standalone: boolean }) { - const { version, packageJsonFile, standalone } = opts; - const { GITHUB_WORKSPACE } = process.env; + const { version, packageJsonFile, standalone } = opts + const { GITHUB_WORKSPACE } = process.env - let packageManager; + let packageManager if (GITHUB_WORKSPACE) { try { - const content = readFileSync( - path.join(GITHUB_WORKSPACE, packageJsonFile), - "utf8", - ); + const content = readFileSync(path.join(GITHUB_WORKSPACE, packageJsonFile), 'utf8'); ({ packageManager } = packageJsonFile.endsWith(".yaml") ? parseYaml(content, { merge: true }) - : JSON.parse(content)); + : JSON.parse(content) + ) } catch (error: unknown) { // Swallow error if package.json doesn't exist in root - if ( - !util.types.isNativeError(error) || - !("code" in error) || - error.code !== "ENOENT" - ) - throw error; + if (!util.types.isNativeError(error) || !('code' in error) || error.code !== 'ENOENT') throw error } } if (version) { if ( - typeof packageManager === "string" && - packageManager.startsWith("pnpm@") && - packageManager.replace("pnpm@", "") !== version + typeof packageManager === 'string' && + packageManager.startsWith('pnpm@') && + 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`); +Remove one of these versions to avoid version mismatch errors like ERR_PNPM_BAD_PM_VERSION`) } - return `${standalone ? "@pnpm/exe" : "pnpm"}@${version}`; + return `${ standalone ? '@pnpm/exe' : 'pnpm' }@${version}` } if (!GITHUB_WORKSPACE) { throw new Error(`No workspace is found. If you've intended to let pnpm/action-setup read preferred pnpm version from the "packageManager" field in the package.json file, 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.`) } - if (typeof packageManager !== "string") { + if (typeof packageManager !== 'string') { throw new Error(`No pnpm version is specified. Please specify it by one of the following ways: - 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@")) { - throw new Error("Invalid packageManager field in package.json"); + if (!packageManager.startsWith('pnpm@')) { + throw new Error('Invalid packageManager field in package.json') } if (standalone) { - return packageManager.replace("pnpm@", "@pnpm/exe@"); + return packageManager.replace('pnpm@', '@pnpm/exe@') } - return packageManager; + return packageManager } -export default runSelfInstaller; +export default runSelfInstaller From 29985b169f4aba9576528f009098e136e36c56da Mon Sep 17 00:00:00 2001 From: Alexander Hornung Date: Thu, 5 Feb 2026 13:24:45 +1100 Subject: [PATCH 4/4] Build --- dist/index.js | Bin 1508038 -> 1508099 bytes 1 file changed, 0 insertions(+), 0 deletions(-) diff --git a/dist/index.js b/dist/index.js index 6c8085fba1a00ca2ee05e269ab72a69c4fa91708..1ab06d7ebaec3015d718b4a0fee4aee562880841 100644 GIT binary patch delta 157 zcmX@s8q?esvq4>Ga=3^fTUmZ)ih{vp6(I|m61}3*yx`QFG|#-^lEj>x)FRsyy_D3n z#L}FS$