2
0
mirror of https://github.com/pnpm/action-setup.git synced 2025-03-13 22:24:34 +08:00

39 lines
1.1 KiB
TypeScript
Raw Normal View History

import * as core from '@actions/core'
2020-05-08 13:06:16 +07:00
import { spawn } from 'child_process'
2020-05-08 13:47:46 +07:00
import { execPath } from 'process'
2021-03-23 12:42:43 +07:00
import { join } from 'path'
import { remove, ensureFile, writeFile } from 'fs-extra'
import fetch from 'node-fetch'
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> {
2021-03-23 12:42:43 +07:00
const { version, dest } = inputs
const target = version ? `pnpm@${version}` : 'pnpm'
const pkgJson = join(dest, 'package.json')
await remove(dest)
await ensureFile(pkgJson)
await writeFile(pkgJson, JSON.stringify({ private: true }))
const cp = spawn(execPath, ['-', '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'],
})
2022-02-08 00:39:20 +02:00
const response = await fetch('https://pnpm.io/pnpm.js')
2020-05-08 21:34:25 +07:00
response.body.pipe(cp.stdin)
2020-05-08 13:06:16 +07:00
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) {
const pnpmHome = join(dest, 'node_modules/.bin')
core.addPath(pnpmHome)
2022-02-08 00:21:53 +02:00
core.exportVariable('PNPM_HOME', pnpmHome)
}
return exitCode
2020-05-08 11:29:39 +07:00
}
export default runSelfInstaller