Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: packageManager reader #35

Merged
merged 3 commits into from Feb 25, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion action.yml
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.

21 changes: 15 additions & 6 deletions src/install-pnpm/run.ts
Expand Up @@ -2,19 +2,21 @@ import { addPath, exportVariable } from '@actions/core'
import fetch from '@pnpm/fetch'
import { spawn } from 'child_process'
import { remove, ensureFile, writeFile, readFile } from 'fs-extra'
import path from 'path'
import { join } from 'path'
Jack-Works marked this conversation as resolved.
Show resolved Hide resolved
import { execPath } from 'process'
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 = 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 @@ -29,17 +31,24 @@ export async function runSelfInstaller(inputs: Inputs): Promise<number> {
cp.on('close', resolve)
})
if (exitCode === 0) {
const pnpmHome = path.join(dest, 'node_modules/.bin')
const pnpmHome = join(dest, 'node_modules/.bin')
addPath(pnpmHome)
exportVariable('PNPM_HOME', pnpmHome)
}
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 workspace = process.env.GITHUB_WORKSPACE
Jack-Works marked this conversation as resolved.
Show resolved Hide resolved
if (!workspace) {
throw new Error(`No workspace is found.
If you're intended to let this action read pnpm version from the package.json/packageManager field,
please run the actions/checkout before this one, otherwise please specify the version in the action config.`)
Jack-Works marked this conversation as resolved.
Show resolved Hide resolved
}

const { packageManager } = JSON.parse(await readFile(join(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