Skip to content

Commit

Permalink
Merge pull request #33 from pnpm/packageManager
Browse files Browse the repository at this point in the history
feat: try support packageManager
  • Loading branch information
KSXGitHub committed Feb 22, 2022
2 parents ad2b35a + 225f3bb commit 847a737
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 45 deletions.
6 changes: 5 additions & 1 deletion README.md
Expand Up @@ -6,7 +6,11 @@ Install pnpm package manager.

### `version`

**Required** Version of pnpm to install. It supports npm versioning scheme, it could be an exact version (such as `6.24.1`), or a version range (such as `6`, `6.x.x`, `6.24.x`, `^6.24.1`, `*`, etc.), or `latest`.
Version of pnpm to install.

**Optional** when there is a [`packageManager` field in the `package.json`](https://nodejs.org/api/corepack.html).

otherwise, this field is **required** It supports npm versioning scheme, it could be an exact version (such as `6.24.1`), or a version range (such as `6`, `6.x.x`, `6.24.x`, `^6.24.1`, `*`, etc.), or `latest`.

### `dest`

Expand Down
39 changes: 38 additions & 1 deletion dist/index.js

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions package.json
Expand Up @@ -8,21 +8,21 @@
},
"dependencies": {
"@actions/core": "^1.6.0",
"@pnpm/fetch": "^4.2.4",
"@pnpm/fetch": "^4.2.5",
"@pnpm/logger": "^4.0.0",
"@types/expand-tilde": "^2.0.0",
"@types/fs-extra": "^9.0.13",
"@types/js-yaml": "^4.0.5",
"@types/node": "^14.18.10",
"@types/node-fetch": "^2.5.12",
"@types/node-fetch": "^2.6.1",
"ajv": "^6.12.6",
"expand-tilde": "^2.0.2",
"fs-extra": "^9.1.0",
"fs-extra": "^10.0.0",
"js-yaml": "^4.1.0"
},
"devDependencies": {
"@ts-schema-autogen/cli": "^0.1.2",
"@vercel/ncc": "^0.27.0",
"@vercel/ncc": "^0.33.3",
"typescript": "^4.5.5"
}
}
64 changes: 29 additions & 35 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/inputs/index.ts
Expand Up @@ -3,7 +3,7 @@ import expandTilde from 'expand-tilde'
import { RunInstall, parseRunInstall } from './run-install'

export interface Inputs {
readonly version: string
readonly version?: string
readonly dest: string
readonly runInstall: RunInstall[]
}
Expand All @@ -15,7 +15,7 @@ const options: InputOptions = {
const parseInputPath = (name: string) => expandTilde(getInput(name, options))

export const getInputs = (): Inputs => ({
version: getInput('version', options),
version: getInput('version'),
dest: parseInputPath('dest'),
runInstall: parseRunInstall('run_install'),
})
Expand Down
21 changes: 19 additions & 2 deletions src/install-pnpm/run.ts
Expand Up @@ -2,14 +2,14 @@ import { addPath, exportVariable } from '@actions/core'
import { spawn } from 'child_process'
import { execPath } from 'process'
import path from 'path'
import { remove, ensureFile, writeFile } from 'fs-extra'
import { remove, ensureFile, writeFile, readFile } from 'fs-extra'
import fetch from '@pnpm/fetch'
import { Inputs } from '../inputs'

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

await remove(dest)
await ensureFile(pkgJson)
Expand All @@ -36,4 +36,21 @@ export async function runSelfInstaller(inputs: Inputs): Promise<number> {
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 ways:
- 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

0 comments on commit 847a737

Please sign in to comment.