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
22 changed files with 549 additions and 766 deletions

View File

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

View File

@ -3,7 +3,6 @@ name: Test Action
on:
- push
- pull_request
- workflow_dispatch
jobs:
test_default_inputs:
@ -15,7 +14,7 @@ jobs:
fail-fast: false
matrix:
pnpm:
- 6.17.1
- 4.11.1
os:
- ubuntu-latest
- macos-latest
@ -27,10 +26,10 @@ jobs:
- name: Run the action
uses: ./
with:
version: 6.17.1
version: 4.11.1
- name: 'Test: which'
run: which pnpm
run: which pnpm; which pnpx
- name: 'Test: install'
run: pnpm install
@ -44,7 +43,7 @@ jobs:
fail-fast: false
matrix:
pnpm:
- 6.17.1
- 4.11.1
os:
- ubuntu-latest
- macos-latest
@ -56,67 +55,13 @@ jobs:
- name: Run the action
uses: ./
with:
version: 6.17.1
version: 4.11.1
dest: ~/test/pnpm
bin_dest: ~/test/pnpm/.bin
registry: http://registry.yarnpkg.com/
- name: 'Test: which'
run: which pnpm
- 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:
- 6.17.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@v2
- name: Run the action
uses: ./
with:
version: 6.17.1
run_install: ${{ matrix.run_install.value }}
- name: 'Test: which'
run: which pnpm
run: which pnpm && which pnpx
- name: 'Test: install'
run: pnpm install

1
.gitignore vendored
View File

@ -8,4 +8,3 @@ temp
*.temp
tmp.*
temp.*
.pnpm-store

110
README.md
View File

@ -1,38 +1,28 @@
# Setup pnpm
# Setup PNPM
Install pnpm package manager.
Install PNPM package manager.
## Inputs
### `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`.
**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
@ -42,81 +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.1.0
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.1.0
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:
build:
- uses: actions/checkout@v2
- name: Cache pnpm modules
uses: actions/cache@v2
with:
path: ~/.pnpm-store
key: ${{ runner.os }}-${{ hashFiles('**/pnpm-lock.yaml') }}
restore-keys: |
${{ runner.os }}-
- uses: pnpm/action-setup@v2.1.0
with:
version: 6.0.2
run_install: true
```
**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

@ -11,6 +11,14 @@ inputs:
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

2
dist/index.js vendored

File diff suppressed because one or more lines are too long

View File

@ -7,21 +7,19 @@
"start": "pnpm run build && sh ./run.sh"
},
"dependencies": {
"@actions/core": "^1.6.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",
"ajv": "^6.12.6",
"node-fetch": "^2.6.0",
"expand-tilde": "^2.0.2",
"fs-extra": "^9.1.0",
"js-yaml": "^4.1.0",
"node-fetch": "^2.6.7"
"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.27.0",
"typescript": "^4.5.5"
"@zeit/ncc": "^0.22.1"
}
}

953
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 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

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

View File

@ -1,5 +1,5 @@
import process from 'process'
import { load } from 'js-yaml'
import { safeLoad } from 'js-yaml'
import Ajv from 'ajv'
import { getInput, error, InputOptions } from '@actions/core'
import runInstallSchema from './run-install-input.schema.json'
@ -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}`)
}
}

View File

@ -1,38 +1,26 @@
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 fetch from 'node-fetch'
import { downloadSelfInstaller } from '../self-installer'
import { Inputs } from '../inputs'
export async function runSelfInstaller(inputs: Inputs): Promise<number> {
const { version, dest } = inputs
const target = version ? `@pnpm/exe@${version}` : '@pnpm/exe'
const pkgJson = path.join(dest, 'package.json')
await remove(dest)
await ensureFile(pkgJson)
await writeFile(pkgJson, JSON.stringify({ private: true }))
const cp = spawn(execPath, ['-', '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 response = await fetch('https://get.pnpm.io/v6.16.js')
const response = await downloadSelfInstaller()
response.body.pipe(cp.stdin)
const exitCode = await new Promise<number>((resolve, reject) => {
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
}
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 process from 'process'
import path from 'path'
import { spawnSync } from 'child_process'
import { setFailed, startGroup, endGroup } from '@actions/core'
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 { spawnSync } from 'child_process'
import { warning, startGroup, endGroup } from '@actions/core'
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 process from 'process'
import path from 'path'
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
})