5 Commits

Author SHA1 Message Date
271a9df5fb Try different ways 2020-05-09 20:57:00 +07:00
3b5e8836ec Update dist/index.js 2020-05-09 20:52:54 +07:00
0fa2b7d3a5 Use console.log 2020-05-09 20:52:27 +07:00
c3e0d9f20f Update dist/index.js 2020-05-09 20:50:21 +07:00
d2316507fd Inspect isPost 2020-05-09 20:47:42 +07:00
24 changed files with 564 additions and 268861 deletions

View File

@ -1,7 +0,0 @@
version: 2
updates:
- package-ecosystem: github-actions
directory: "/"
schedule:
interval: weekly
open-pull-requests-limit: 10

View File

@ -3,7 +3,6 @@ name: Test Action
on:
- push
- pull_request
- workflow_dispatch
jobs:
test_default_inputs:
@ -22,7 +21,7 @@ jobs:
- windows-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v2
- name: Run the action
uses: ./
@ -51,72 +50,18 @@ jobs:
- windows-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v2
- name: Run the action
uses: ./
with:
version: 4.11.1
dest: ~/test/pnpm
bin_dest: ~/test/pnpm/.bin
registry: http://registry.yarnpkg.com/
- name: 'Test: which'
run: which pnpm && which pnpx
- name: 'Test: install'
run: pnpm install
test_run_install:
name: 'Test with run_install (${{ matrix.run_install.name }}, ${{ matrix.os }})'
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
pnpm:
- 4.11.1
os:
- ubuntu-latest
- macos-latest
- windows-latest
run_install:
- name: 'null'
value: 'null'
- name: 'empty object'
value: '{}'
- name: 'recursive'
value: |
recursive: true
- name: 'global'
value: |
args:
- --global
- --global-dir=./pnpm-global
- npm
- yarn
- pnpm
- name: 'array'
value: |
- {}
- recursive: true
- args:
- --global
- --global-dir=./pnpm-global
- npm
- yarn
- pnpm
steps:
- uses: actions/checkout@v3
- name: Run the action
uses: ./
with:
version: 4.11.1
run_install: ${{ matrix.run_install.value }}
- name: 'Test: which'
run: which pnpm; which pnpx
- name: 'Test: install'
run: pnpm install

2
.gitignore vendored
View File

@ -2,11 +2,9 @@ node_modules
*.log
/dist/*
!/dist/index.js
!/dist/pnpm.js
tmp
temp
*.tmp
*.temp
tmp.*
temp.*
.pnpm-store

129
README.md
View File

@ -1,42 +1,28 @@
# Setup pnpm
# Setup PNPM
Install pnpm package manager.
Install PNPM package manager.
## Inputs
### `version`
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`.
**Required** Version of PNPM to install.
### `dest`
**Optional** Where to store pnpm files.
**Optional** Where to store PNPM files.
### `bin_dest`
**Optional** Where to store executables (`pnpm` and `pnpx` commands).
### `registry`
**Optional** Registry to download PNPM from.
### `run_install`
**Optional** (_default:_ `null`) If specified, run `pnpm install`.
If `run_install` is either `null` or `false`, pnpm will not install any npm package.
If `run_install` is `true`, pnpm will install dependencies recursively.
If `run_install` is a YAML string representation of either an object or an array, pnpm will execute every install commands.
#### `run_install.recursive`
**Optional** (_type:_ `boolean`, _default:_ `false`) Whether to use `pnpm recursive install`.
#### `run_install.cwd`
**Optional** (_type:_ `string`) Working directory when run `pnpm [recursive] install`.
#### `run_install.args`
**Optional** (_type:_ `string[]`) Additional arguments after `pnpm [recursive] install`, e.g. `[--frozen-lockfile, --strict-peer-dependencies]`.
**Optional** If specified, run `pnpm install`.
## Outputs
@ -46,96 +32,29 @@ Expanded path of inputs#dest.
### `bin_dest`
Location of `pnpm` and `pnpx` command.
Expanded path of inputs#bin_dest.
## Usage example
### Just install pnpm
```yaml
on:
- push
- pull_request
jobs:
install:
runs-on: ubuntu-latest
runs-on: ubuntu-latest
steps:
- uses: pnpm/action-setup@v2.2.4
with:
version: 6.0.2
steps:
- uses: actions/checkout@v2
- uses: pnpm/action-setup@v1.1.0
with:
version: 4.11.1
- name: Install dependencies
run: pnpm install
```
### Install pnpm and a few npm packages
```yaml
on:
- push
- pull_request
jobs:
install:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: pnpm/action-setup@v2.2.4
with:
version: 6.0.2
run_install: |
- recursive: true
args: [--frozen-lockfile, --strict-peer-dependencies]
- args: [--global, gulp, prettier, typescript]
```
### Use cache to reduce installation time
```yaml
on:
- push
- pull_request
jobs:
cache-and-install:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Install Node.js
uses: actions/setup-node@v3
with:
node-version: 16
- uses: pnpm/action-setup@v2.0.1
name: Install pnpm
id: pnpm-install
with:
version: 7
run_install: false
- name: Get pnpm store directory
id: pnpm-cache
run: |
echo "STORE_PATH=$(pnpm store path)" >> $GITHUB_OUTPUT
- uses: actions/cache@v3
name: Setup pnpm cache
with:
path: ${{ steps.pnpm-cache.outputs.STORE_PATH }}
key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
restore-keys: |
${{ runner.os }}-pnpm-store-
- name: Install dependencies
run: pnpm install
```
**Note:** You don't need to run `pnpm store prune` at the end; post-action has already taken care of that.
## Notes
This action does not setup Node.js for you, use [actions/setup-node](https://github.com/actions/setup-node) yourself.

View File

@ -1,20 +1,29 @@
name: Setup pnpm
description: Install pnpm package manager
name: Setup PNPM
description: Install PNPM package manager
branding:
icon: package
color: orange
inputs:
version:
description: Version of pnpm to install
description: Version of PNPM to install
required: true
dest:
description: Where to store pnpm files
description: Where to store PNPM files
required: false
default: ~/setup-pnpm
bin_dest:
description: Where to store executables (pnpm and pnpx commands)
required: false
default: ~/setup-pnpm/.bin
registry:
description: Registry to download PNPM from
required: false
default: https://registry.npmjs.com
run_install:
description: If specified, run `pnpm install`
required: false
default: 'null'
runs:
using: node16
using: node12
main: dist/index.js
post: dist/index.js

11
dist/index.js vendored

File diff suppressed because one or more lines are too long

134031
dist/pnpm.js vendored

File diff suppressed because one or more lines are too long

View File

@ -3,24 +3,23 @@
"scripts": {
"build:schemas": "ts-schema-autogen generate",
"build:ncc": "ncc build --minify --no-source-map-register --no-cache dist/tsc/index.js --out dist/",
"build": "pnpm run build:schemas && tsc && pnpm run build:ncc && cp src/install-pnpm/pnpm.js dist/pnpm.js",
"build": "pnpm run build:schemas && tsc && pnpm run build:ncc",
"start": "pnpm run build && sh ./run.sh"
},
"dependencies": {
"@actions/core": "^1.10.0",
"@types/expand-tilde": "^2.0.0",
"@types/fs-extra": "^9.0.13",
"@types/js-yaml": "^4.0.5",
"@types/node": "^14.18.32",
"@types/node-fetch": "^2.6.2",
"ajv": "^6.12.6",
"node-fetch": "^2.6.0",
"expand-tilde": "^2.0.2",
"fs-extra": "^10.1.0",
"js-yaml": "^4.1.0"
"js-yaml": "^3.13.1",
"ajv": "^6.12.0",
"@actions/core": "^1.2.4",
"@types/expand-tilde": "^2.0.0",
"@types/node-fetch": "^2.5.7",
"@types/js-yaml": "^3.12.3",
"@types/node": "^13.13.5"
},
"devDependencies": {
"typescript": "^3.8.3",
"@ts-schema-autogen/cli": "^0.1.2",
"@vercel/ncc": "^0.33.4",
"typescript": "^4.8.4"
"@zeit/ncc": "^0.22.1"
}
}

934
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff

View File

@ -1,10 +1,6 @@
{
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
"pinVersions": false,
"extends": [
"config:base"
],
"ignoreDeps": [
"ajv"
]
}

2
run.sh
View File

@ -3,5 +3,7 @@
export HOME="$(pwd)"
export INPUT_VERSION=4.11.1
export INPUT_DEST='~/pnpm.temp'
export INPUT_BIN_DEST='~/pnpm.temp/.bin'
export INPUT_REGISTRY=https://registry.npmjs.com
export INPUT_RUN_INSTALL=null
exec node dist/index.js

View File

@ -1,15 +1,20 @@
import { setFailed, saveState, getState } from '@actions/core'
import { setFailed, getState } from '@actions/core'
import getInputs from './inputs'
import installPnpm from './install-pnpm'
import setOutputs from './outputs'
import installPnpm from './install-pnpm'
import pnpmInstall from './pnpm-install'
import pruneStore from './pnpm-store-prune'
async function main() {
const isPost = getState('isPost')
console.log({
is_post: getState('is_post'),
isPost: getState('isPost'),
STATE_isPost: process.env['STATE_isPost'],
})
if (isPost) {
return
}
const inputs = getInputs()
const isPost = getState('is_post')
if (isPost === 'true') return pruneStore(inputs)
saveState('is_post', 'true')
await installPnpm(inputs)
console.log('Installation Completed!')
setOutputs(inputs)

View File

@ -3,8 +3,10 @@ import expandTilde from 'expand-tilde'
import { RunInstall, parseRunInstall } from './run-install'
export interface Inputs {
readonly version?: string
readonly version: string
readonly dest: string
readonly binDest: string
readonly registry: string
readonly runInstall: RunInstall[]
}
@ -15,8 +17,10 @@ const options: InputOptions = {
const parseInputPath = (name: string) => expandTilde(getInput(name, options))
export const getInputs = (): Inputs => ({
version: getInput('version'),
version: getInput('version', options),
dest: parseInputPath('dest'),
binDest: parseInputPath('bin_dest'),
registry: getInput('registry', options),
runInstall: parseRunInstall('run_install'),
})

View File

@ -1,7 +1,7 @@
import { getInput, error, InputOptions } from '@actions/core'
import Ajv from 'ajv'
import { load } from 'js-yaml'
import process from 'process'
import { safeLoad } from 'js-yaml'
import Ajv from 'ajv'
import { getInput, error, InputOptions } from '@actions/core'
import runInstallSchema from './run-install-input.schema.json'
export interface RunInstall {
@ -21,9 +21,10 @@ const options: InputOptions = {
}
export function parseRunInstall(name: string): RunInstall[] {
const result: RunInstallInput = load(getInput(name, options)) as any
const result: RunInstallInput = safeLoad(getInput(name, options))
const ajv = new Ajv({
allErrors: true,
async: false,
})
const validate = ajv.compile(runInstallSchema)
if (!validate(result)) {

View File

@ -1,15 +1,13 @@
import { setFailed, startGroup, endGroup } from '@actions/core'
import { setFailed } from '@actions/core'
import { Inputs } from '../inputs'
import runSelfInstaller from './run'
export { runSelfInstaller }
export async function install(inputs: Inputs) {
startGroup('Running self-installer...')
const status = await runSelfInstaller(inputs)
endGroup()
if (status) {
return setFailed(`Something went wrong, self-installer exits with code ${status}`)
return setFailed(`Something does wrong, self-installer exits with code ${status}`)
}
}

File diff suppressed because one or more lines are too long

View File

@ -1,61 +1,26 @@
import { addPath, exportVariable } from '@actions/core'
import { spawn } from 'child_process'
import { remove, ensureFile, writeFile, readFile } from 'fs-extra'
import path from 'path'
import { execPath } from 'process'
import { downloadSelfInstaller } from '../self-installer'
import { Inputs } from '../inputs'
export async function runSelfInstaller(inputs: Inputs): Promise<number> {
const { version, dest } = inputs
// 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, [path.join(__dirname, 'pnpm.js'), 'install', target, '--no-lockfile'], {
cwd: dest,
const cp = spawn(execPath, {
env: {
PNPM_VERSION: inputs.version,
PNPM_DEST: inputs.dest,
PNPM_BIN_DEST: inputs.binDest,
PNPM_REGISTRY: inputs.registry,
},
stdio: ['pipe', 'inherit', 'inherit'],
})
const exitCode = await new Promise<number>((resolve, reject) => {
const response = await downloadSelfInstaller()
response.body.pipe(cp.stdin)
return new Promise((resolve, reject) => {
cp.on('error', reject)
cp.on('close', resolve)
})
if (exitCode === 0) {
const pnpmHome = path.join(dest, 'node_modules/.bin')
addPath(pnpmHome)
exportVariable('PNPM_HOME', pnpmHome)
}
return exitCode
}
async function readTarget(version?: string | undefined) {
if (version) return `pnpm@${version}`
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:
- 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

View File

@ -1,12 +1,10 @@
import { setOutput, addPath } from '@actions/core'
import { Inputs } from '../inputs'
import { getBinDest } from '../utils'
export function setOutputs(inputs: Inputs) {
const binDest = getBinDest(inputs)
addPath(binDest)
addPath(inputs.binDest)
setOutput('dest', inputs.dest)
setOutput('bin_dest', binDest)
setOutput('bin_dest', inputs.binDest)
}
export default setOutputs

View File

@ -1,10 +1,14 @@
import { setFailed, startGroup, endGroup } from '@actions/core'
import process from 'process'
import path from 'path'
import { spawnSync } from 'child_process'
import { setFailed } from '@actions/core'
import { Inputs } from '../inputs'
import { patchPnpmEnv } from '../utils'
export function runPnpmInstall(inputs: Inputs) {
const env = patchPnpmEnv(inputs)
const env = {
...process.env,
PATH: inputs.binDest + path.delimiter + process.env.PATH
}
for (const options of inputs.runInstall) {
const args = ['install']
@ -12,7 +16,7 @@ export function runPnpmInstall(inputs: Inputs) {
if (options.args) args.push(...options.args)
const cmdStr = ['pnpm', ...args].join(' ')
startGroup(`Running ${cmdStr}...`)
console.log('Running', cmdStr)
const { error, status } = spawnSync('pnpm', args, {
stdio: 'inherit',
@ -21,8 +25,6 @@ export function runPnpmInstall(inputs: Inputs) {
env,
})
endGroup()
if (error) {
setFailed(error)
continue

View File

@ -1,31 +0,0 @@
import { warning, startGroup, endGroup } from '@actions/core'
import { spawnSync } from 'child_process'
import { Inputs } from '../inputs'
import { patchPnpmEnv } from '../utils'
export function pruneStore(inputs: Inputs) {
if (inputs.runInstall.length === 0) {
console.log('Pruning is unnecessary.')
return
}
startGroup('Running pnpm store prune...')
const { error, status } = spawnSync('pnpm', ['store', 'prune'], {
stdio: 'inherit',
shell: true,
env: patchPnpmEnv(inputs),
})
endGroup()
if (error) {
warning(error)
return
}
if (status) {
warning(`command pnpm store prune exits with code ${status}`)
return
}
}
export default pruneStore

View File

@ -0,0 +1,4 @@
import fetch from 'node-fetch'
import url from './url'
export const downloadSelfInstaller = () => fetch(url)
export default downloadSelfInstaller

View File

@ -0,0 +1,2 @@
export * from './url'
export * from './download'

View File

@ -0,0 +1,3 @@
export const ref = '301414cec74a2b6b63c95b42f2ad1790ccb980ed'
export const url = `https://raw.githubusercontent.com/pnpm/self-installer/${ref}/install.js`
export default url

View File

@ -1,10 +0,0 @@
import path from 'path'
import process from 'process'
import { Inputs } from '../inputs'
export const getBinDest = (inputs: Inputs): string => path.join(inputs.dest, 'node_modules', '.bin')
export const patchPnpmEnv = (inputs: Inputs): NodeJS.ProcessEnv => ({
...process.env,
PATH: getBinDest(inputs) + path.delimiter + process.env.PATH,
})