67 lines
2.3 KiB
TypeScript
Raw Normal View History

2022-02-08 14:44:50 +02:00
import { addPath, exportVariable } from '@actions/core'
2020-05-08 13:06:16 +07:00
import { spawn } from 'child_process'
2022-02-22 12:26:05 +08:00
import { remove, ensureFile, writeFile, readFile } from 'fs-extra'
2022-02-23 10:07:15 +07:00
import path from 'path'
import { execPath } from 'process'
2020-05-08 11:29:39 +07:00
import { Inputs } from '../inputs'
2020-05-08 21:34:25 +07:00
export async function runSelfInstaller(inputs: Inputs): Promise<number> {
const { version, dest, nodeJsBundled } = inputs
2021-03-23 12:42:43 +07:00
// prepare self install
2021-03-23 12:42:43 +07:00
await remove(dest)
const pkgJson = path.join(dest, 'package.json')
2021-03-23 12:42:43 +07:00
await ensureFile(pkgJson)
await writeFile(pkgJson, JSON.stringify({ private: true }))
// prepare target pnpm
const target = await readTarget(nodeJsBundled, version)
const cp = spawn(execPath, [path.join(__dirname, 'pnpm.js'), 'install', target, '--no-lockfile'], {
2021-03-23 12:48:54 +07:00
cwd: dest,
2020-05-08 13:06:16 +07:00
stdio: ['pipe', 'inherit', 'inherit'],
})
const exitCode = await new Promise<number>((resolve, reject) => {
2020-05-08 13:06:16 +07:00
cp.on('error', reject)
cp.on('close', resolve)
2020-05-08 11:29:39 +07:00
})
if (exitCode === 0) {
2022-02-08 14:44:50 +02:00
const pnpmHome = path.join(dest, 'node_modules/.bin')
addPath(pnpmHome)
exportVariable('PNPM_HOME', pnpmHome)
}
return exitCode
2020-05-08 11:29:39 +07:00
}
async function readTarget(nodeJsBundled: boolean, version?: string | undefined) {
if (version) return `${ nodeJsBundled ? '@pnpm/exe' : 'pnpm' }@${version}`
2022-02-22 13:37:35 +08:00
const { GITHUB_WORKSPACE } = process.env
if (!GITHUB_WORKSPACE) {
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,
please run the actions/checkout before pnpm/action-setup.
Otherwise, please specify the pnpm version in the action configuration.`)
}
const { packageManager } = JSON.parse(await readFile(path.join(GITHUB_WORKSPACE, 'package.json'), 'utf8'))
2022-02-22 13:37:35 +08:00
if (typeof packageManager !== 'string') {
throw new Error(`No pnpm version is specified.
2022-02-22 11:32:59 +02:00
Please specify it by one of the following ways:
2022-02-22 13:37:35 +08:00
- in the GitHub Action config with the key "version"
- in the package.json with the key "packageManager" (See https://nodejs.org/api/corepack.html)`)
}
if (!packageManager.startsWith('pnpm@')) {
throw new Error('Invalid packageManager field in package.json')
}
if(nodeJsBundled){
return packageManager.replace('pnpm@', '@pnpm/exe@')
}
2022-02-22 13:37:35 +08:00
return packageManager
}
2020-05-08 11:29:39 +07:00
export default runSelfInstaller