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 538 additions and 752 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: on:
- push - push
- pull_request - pull_request
- workflow_dispatch
jobs: jobs:
test_default_inputs: test_default_inputs:
@ -58,65 +57,11 @@ jobs:
with: with:
version: 4.11.1 version: 4.11.1
dest: ~/test/pnpm dest: ~/test/pnpm
bin_dest: ~/test/pnpm/.bin
registry: http://registry.yarnpkg.com/
- name: 'Test: which' - name: 'Test: which'
run: which pnpm && which pnpx run: which pnpm && which pnpx
- name: 'Test: install' - name: 'Test: install'
run: pnpm 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@v2
- 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

1
.gitignore vendored
View File

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

View File

@ -1,38 +1,28 @@
# Setup pnpm # Setup PNPM
Install pnpm package manager. Install PNPM package manager.
## Inputs ## Inputs
### `version` ### `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` ### `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` ### `run_install`
**Optional** (_default:_ `null`) If specified, run `pnpm install`. **Optional** 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]`.
## Outputs ## Outputs
@ -42,28 +32,10 @@ Expanded path of inputs#dest.
### `bin_dest` ### `bin_dest`
Location of `pnpm` and `pnpx` command. Expanded path of inputs#bin_dest.
## Usage example ## Usage example
### Just install pnpm
```yaml
on:
- push
- pull_request
jobs:
runs-on: ubuntu-latest
steps:
- uses: pnpm/action-setup@v2.0.1
with:
version: 6.0.2
```
### Install pnpm and a few npm packages
```yaml ```yaml
on: on:
- push - push
@ -75,45 +47,14 @@ jobs:
steps: steps:
- uses: actions/checkout@v2 - uses: actions/checkout@v2
- uses: pnpm/action-setup@v2.0.1 - uses: pnpm/action-setup@v1.1.0
with: with:
version: 6.0.2 version: 4.11.1
run_install: |
- recursive: true - name: Install dependencies
args: [--frozen-lockfile, --strict-peer-dependencies] run: pnpm install
- args: [--global, gulp, prettier, typescript]
``` ```
### Use cache to reduce installation time
```yaml
on:
- push
- pull_request
jobs:
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.0.1
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 ## Notes
This action does not setup Node.js for you, use [actions/setup-node](https://github.com/actions/setup-node) yourself. 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 description: Where to store PNPM files
required: false required: false
default: ~/setup-pnpm 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: run_install:
description: If specified, run `pnpm install` description: If specified, run `pnpm install`
required: false 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" "start": "pnpm run build && sh ./run.sh"
}, },
"dependencies": { "dependencies": {
"@actions/core": "^1.6.0", "node-fetch": "^2.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",
"expand-tilde": "^2.0.2", "expand-tilde": "^2.0.2",
"fs-extra": "^9.1.0", "js-yaml": "^3.13.1",
"js-yaml": "^4.1.0", "ajv": "^6.12.0",
"node-fetch": "^2.6.7" "@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": { "devDependencies": {
"typescript": "^3.8.3",
"@ts-schema-autogen/cli": "^0.1.2", "@ts-schema-autogen/cli": "^0.1.2",
"@vercel/ncc": "^0.27.0", "@zeit/ncc": "^0.22.1"
"typescript": "^4.5.5"
} }
} }

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, "pinVersions": false,
"extends": [ "extends": [
"config:base" "config:base"
],
"ignoreDeps": [
"ajv"
] ]
} }

2
run.sh
View File

@ -3,5 +3,7 @@
export HOME="$(pwd)" export HOME="$(pwd)"
export INPUT_VERSION=4.11.1 export INPUT_VERSION=4.11.1
export INPUT_DEST='~/pnpm.temp' export INPUT_DEST='~/pnpm.temp'
export INPUT_BIN_DEST='~/pnpm.temp/.bin'
export INPUT_REGISTRY=https://registry.npmjs.com
export INPUT_RUN_INSTALL=null export INPUT_RUN_INSTALL=null
exec node dist/index.js 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 getInputs from './inputs'
import setOutputs from './outputs' import setOutputs from './outputs'
import installPnpm from './install-pnpm' import installPnpm from './install-pnpm'
import pnpmInstall from './pnpm-install' import pnpmInstall from './pnpm-install'
import pruneStore from './pnpm-store-prune'
async function main() { 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 inputs = getInputs()
const isPost = getState('is_post')
if (isPost === 'true') return pruneStore(inputs)
saveState('is_post', 'true')
await installPnpm(inputs) await installPnpm(inputs)
console.log('Installation Completed!') console.log('Installation Completed!')
setOutputs(inputs) setOutputs(inputs)

View File

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

View File

@ -1,5 +1,5 @@
import process from 'process' import process from 'process'
import { load } from 'js-yaml' import { safeLoad } from 'js-yaml'
import Ajv from 'ajv' import Ajv from 'ajv'
import { getInput, error, InputOptions } from '@actions/core' import { getInput, error, InputOptions } from '@actions/core'
import runInstallSchema from './run-install-input.schema.json' import runInstallSchema from './run-install-input.schema.json'
@ -21,9 +21,10 @@ const options: InputOptions = {
} }
export function parseRunInstall(name: string): RunInstall[] { 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({ const ajv = new Ajv({
allErrors: true, allErrors: true,
async: false,
}) })
const validate = ajv.compile(runInstallSchema) const validate = ajv.compile(runInstallSchema)
if (!validate(result)) { 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 { Inputs } from '../inputs'
import runSelfInstaller from './run' import runSelfInstaller from './run'
export { runSelfInstaller } export { runSelfInstaller }
export async function install(inputs: Inputs) { export async function install(inputs: Inputs) {
startGroup('Running self-installer...')
const status = await runSelfInstaller(inputs) const status = await runSelfInstaller(inputs)
endGroup()
if (status) { 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 * as core from '@actions/core'
import { spawn } from 'child_process' import { spawn } from 'child_process'
import { execPath } from 'process' import { execPath } from 'process'
import { join } from 'path' import { downloadSelfInstaller } from '../self-installer'
import { remove, ensureFile, writeFile } from 'fs-extra'
import fetch from 'node-fetch'
import { Inputs } from '../inputs' import { Inputs } from '../inputs'
export async function runSelfInstaller(inputs: Inputs): Promise<number> { export async function runSelfInstaller(inputs: Inputs): Promise<number> {
const { version, dest } = inputs const cp = spawn(execPath, {
const target = version ? `pnpm@${version}` : 'pnpm' env: {
const pkgJson = join(dest, 'package.json') PNPM_VERSION: inputs.version,
PNPM_DEST: inputs.dest,
await remove(dest) PNPM_BIN_DEST: inputs.binDest,
await ensureFile(pkgJson) PNPM_REGISTRY: inputs.registry,
await writeFile(pkgJson, JSON.stringify({ private: true })) },
const cp = spawn(execPath, ['-', 'install', target, '--no-lockfile'], {
cwd: dest,
stdio: ['pipe', 'inherit', 'inherit'], stdio: ['pipe', 'inherit', 'inherit'],
}) })
const response = await fetch('https://pnpm.js.org/pnpm.js') const response = await downloadSelfInstaller()
response.body.pipe(cp.stdin) response.body.pipe(cp.stdin)
const result = await new Promise<number>((resolve, reject) => { return new Promise((resolve, reject) => {
cp.on('error', reject) cp.on('error', reject)
cp.on('close', resolve) cp.on('close', resolve)
}) })
if (result === 0) {
const pnpmHome = join(dest, 'node_modules/.bin')
core.addPath(pnpmHome)
core.exportVariable('PNPM_HOME_PATH', pnpmHome)
}
return result
} }
export default runSelfInstaller export default runSelfInstaller

View File

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

View File

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