From a576a70bc64219425cbe4f9598d275aa369a1a20 Mon Sep 17 00:00:00 2001 From: Jack Works Date: Tue, 22 Feb 2022 13:37:35 +0800 Subject: [PATCH] chore: resolve review --- src/install-pnpm/run.ts | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/src/install-pnpm/run.ts b/src/install-pnpm/run.ts index 3b88149..503dcfd 100644 --- a/src/install-pnpm/run.ts +++ b/src/install-pnpm/run.ts @@ -9,21 +9,7 @@ import { Inputs } from '../inputs' export async function runSelfInstaller(inputs: Inputs): Promise { const { version, dest } = inputs const pkgJson = path.join(dest, 'package.json') - let target: string - - if (!version) { - const packageManager = JSON.parse(await readFile(pkgJson, 'utf8')).packageManager - if (packageManager) { - if (!packageManager.startsWith('pnpm@')) { - throw new Error('packageManager field is not pnpm') - } - target = packageManager - } else { - throw new Error('None of packageManager (in package.json) or version (in action config) is defined') - } - } else { - target = `pnpm@${version}` - } + const target = await readTarget(pkgJson, version) await remove(dest) await ensureFile(pkgJson) @@ -50,4 +36,21 @@ export async function runSelfInstaller(inputs: Inputs): Promise { return exitCode } +async function readTarget(packageJsonPath: string, version?: string | undefined) { + if (version) return `pnpm@${version}` + + const { packageManager } = JSON.parse(await readFile(packageJsonPath, 'utf8')) + if (typeof packageManager !== 'string') { + throw new Error(`No pnpm version is specified. +Please specify it by one of the following way: + - 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') + } + return packageManager +} + export default runSelfInstaller