Skip to content

Commit

Permalink
feat: try support packageManager
Browse files Browse the repository at this point in the history
  • Loading branch information
Jack-Works committed Feb 22, 2022
1 parent ad2b35a commit 1d51e20
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 45 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
18 changes: 16 additions & 2 deletions src/install-pnpm/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,28 @@ 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')
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}`
}

await remove(dest)
await ensureFile(pkgJson)
Expand Down

0 comments on commit 1d51e20

Please sign in to comment.