Skip to content

Commit 847a737

Browse files
authored
Merge pull request #33 from pnpm/packageManager
feat: try support packageManager
2 parents ad2b35a + 225f3bb commit 847a737

File tree

6 files changed

+97
-45
lines changed

6 files changed

+97
-45
lines changed

README.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ Install pnpm package manager.
66

77
### `version`
88

9-
**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`.
9+
Version of pnpm to install.
10+
11+
**Optional** when there is a [`packageManager` field in the `package.json`](https://nodejs.org/api/corepack.html).
12+
13+
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`.
1014

1115
### `dest`
1216

dist/index.js

+38-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,21 @@
88
},
99
"dependencies": {
1010
"@actions/core": "^1.6.0",
11-
"@pnpm/fetch": "^4.2.4",
11+
"@pnpm/fetch": "^4.2.5",
1212
"@pnpm/logger": "^4.0.0",
1313
"@types/expand-tilde": "^2.0.0",
1414
"@types/fs-extra": "^9.0.13",
1515
"@types/js-yaml": "^4.0.5",
1616
"@types/node": "^14.18.10",
17-
"@types/node-fetch": "^2.5.12",
17+
"@types/node-fetch": "^2.6.1",
1818
"ajv": "^6.12.6",
1919
"expand-tilde": "^2.0.2",
20-
"fs-extra": "^9.1.0",
20+
"fs-extra": "^10.0.0",
2121
"js-yaml": "^4.1.0"
2222
},
2323
"devDependencies": {
2424
"@ts-schema-autogen/cli": "^0.1.2",
25-
"@vercel/ncc": "^0.27.0",
25+
"@vercel/ncc": "^0.33.3",
2626
"typescript": "^4.5.5"
2727
}
2828
}

pnpm-lock.yaml

+29-35
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/inputs/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import expandTilde from 'expand-tilde'
33
import { RunInstall, parseRunInstall } from './run-install'
44

55
export interface Inputs {
6-
readonly version: string
6+
readonly version?: string
77
readonly dest: string
88
readonly runInstall: RunInstall[]
99
}
@@ -15,7 +15,7 @@ const options: InputOptions = {
1515
const parseInputPath = (name: string) => expandTilde(getInput(name, options))
1616

1717
export const getInputs = (): Inputs => ({
18-
version: getInput('version', options),
18+
version: getInput('version'),
1919
dest: parseInputPath('dest'),
2020
runInstall: parseRunInstall('run_install'),
2121
})

src/install-pnpm/run.ts

+19-2
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ import { addPath, exportVariable } from '@actions/core'
22
import { spawn } from 'child_process'
33
import { execPath } from 'process'
44
import path from 'path'
5-
import { remove, ensureFile, writeFile } from 'fs-extra'
5+
import { remove, ensureFile, writeFile, readFile } from 'fs-extra'
66
import fetch from '@pnpm/fetch'
77
import { Inputs } from '../inputs'
88

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

1414
await remove(dest)
1515
await ensureFile(pkgJson)
@@ -36,4 +36,21 @@ export async function runSelfInstaller(inputs: Inputs): Promise<number> {
3636
return exitCode
3737
}
3838

39+
async function readTarget(packageJsonPath: string, version?: string | undefined) {
40+
if (version) return `pnpm@${version}`
41+
42+
const { packageManager } = JSON.parse(await readFile(packageJsonPath, 'utf8'))
43+
if (typeof packageManager !== 'string') {
44+
throw new Error(`No pnpm version is specified.
45+
Please specify it by one of the following ways:
46+
- in the GitHub Action config with the key "version"
47+
- in the package.json with the key "packageManager" (See https://nodejs.org/api/corepack.html)`)
48+
}
49+
50+
if (!packageManager.startsWith('pnpm@')) {
51+
throw new Error('Invalid packageManager field in package.json')
52+
}
53+
return packageManager
54+
}
55+
3956
export default runSelfInstaller

0 commit comments

Comments
 (0)