Skip to content

Commit

Permalink
fix: packageManager reader (#35)
Browse files Browse the repository at this point in the history
* fix: packageManager reader

* chore: resolve review

* chore: run build
  • Loading branch information
Jack-Works authored Feb 25, 2022
1 parent 11ba342 commit 35ab426
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
1 change: 0 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ branding:
inputs:
version:
description: Version of PNPM to install
required: true
dest:
description: Where to store PNPM files
required: false
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

18 changes: 14 additions & 4 deletions src/install-pnpm/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ import { Inputs } from '../inputs'

export async function runSelfInstaller(inputs: Inputs): Promise<number> {
const { version, dest } = inputs
const pkgJson = path.join(dest, 'package.json')
const target = await readTarget(pkgJson, version)

// prepare self install
await remove(dest)
const pkgJson = path.join(dest, 'package.json')
await ensureFile(pkgJson)
await writeFile(pkgJson, JSON.stringify({ private: true }))

// prepare target pnpm
const target = await readTarget(version)
const cp = spawn(execPath, ['-', 'install', target, '--no-lockfile'], {
cwd: dest,
stdio: ['pipe', 'inherit', 'inherit'],
Expand All @@ -36,10 +38,18 @@ export async function runSelfInstaller(inputs: Inputs): Promise<number> {
return exitCode
}

async function readTarget(packageJsonPath: string, version?: string | undefined) {
async function readTarget(version?: string | undefined) {
if (version) return `pnpm@${version}`

const { packageManager } = JSON.parse(await readFile(packageJsonPath, 'utf8'))
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'))
if (typeof packageManager !== 'string') {
throw new Error(`No pnpm version is specified.
Please specify it by one of the following ways:
Expand Down

0 comments on commit 35ab426

Please sign in to comment.