Files
setup-pnpm/src/inputs/index.ts

62 lines
1.6 KiB
TypeScript
Raw Normal View History

2020-05-09 19:45:31 +07:00
import process from 'process'
import { getInput, error, InputOptions } from '@actions/core'
2020-05-08 14:24:25 +07:00
import expandTilde from 'expand-tilde'
2020-05-09 19:13:46 +07:00
import { safeLoad } from 'js-yaml'
2020-05-09 19:45:31 +07:00
import Ajv from 'ajv'
import runInstallSchema from './run-install-input.schema.json'
2020-05-09 19:13:46 +07:00
interface RunInstall {
readonly recursive?: boolean
readonly cwd?: string
readonly args?: readonly string[]
}
2020-05-09 19:24:07 +07:00
export type RunInstallInput =
2020-05-09 19:13:46 +07:00
| null
| boolean
| RunInstall
| RunInstall[]
2020-05-08 11:29:39 +07:00
export interface Inputs {
readonly version: string
readonly dest: string
readonly binDest: string
readonly registry: string
2020-05-09 19:13:46 +07:00
readonly runInstall: RunInstall[]
2020-05-08 11:29:39 +07:00
}
const options: InputOptions = {
required: true,
}
2020-05-08 14:24:25 +07:00
const parseInputPath = (name: string) => expandTilde(getInput(name, options))
2020-05-08 14:12:16 +07:00
2020-05-09 19:13:46 +07:00
function parseRunInstall(name: string): RunInstall[] {
const result: RunInstallInput = safeLoad(getInput(name, options))
2020-05-09 19:45:31 +07:00
const ajv = new Ajv({
allErrors: true,
async: false,
})
const validate = ajv.compile(runInstallSchema)
if (!validate(result)) {
for (const errorItem of validate.errors!) {
2020-05-09 19:52:48 +07:00
error(`with.run_install${errorItem.dataPath}: ${errorItem.message}`)
2020-05-09 19:45:31 +07:00
}
return process.exit(1)
}
2020-05-09 19:13:46 +07:00
if (!result) return []
if (result === true) return [{ recursive: true }]
if (Array.isArray(result)) return result
return [result]
}
2020-05-08 14:24:25 +07:00
export const getInputs = (): Inputs => ({
2020-05-08 11:29:39 +07:00
version: getInput('version', options),
2020-05-08 14:24:25 +07:00
dest: parseInputPath('dest'),
binDest: parseInputPath('bin_dest'),
2020-05-08 11:29:39 +07:00
registry: getInput('registry', options),
2020-05-09 19:13:46 +07:00
runInstall: parseRunInstall('run_install'),
2020-05-08 11:29:39 +07:00
})
export default getInputs