action-setup/src/install-pnpm/run.ts

31 lines
902 B
TypeScript
Raw Normal View History

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'], {
2020-05-08 13:06:16 +07:00
stdio: ['pipe', 'inherit', 'inherit'],
})
2021-03-23 12:42:43 +07:00
const response = await fetch('https://pnpm.js.org/pnpm.js')
2020-05-08 21:34:25 +07:00
response.body.pipe(cp.stdin)
2020-05-08 13:06:16 +07:00
return new Promise((resolve, reject) => {
cp.on('error', reject)
cp.on('close', resolve)
2020-05-08 11:29:39 +07:00
})
}
export default runSelfInstaller