Support using a file

This commit is contained in:
Danny van der Jagt 2020-04-21 21:59:00 +02:00
parent 648bc46b8a
commit 2dbe5367a7
3 changed files with 1754 additions and 1581 deletions

View file

@ -7,7 +7,8 @@ branding:
inputs:
script:
description: The script to run
required: true
file:
description: The javascript file to run
github-token:
description: The GitHub token used to create an authenticated client
default: ${{ github.token }}

3308
dist/index.js vendored

File diff suppressed because it is too large Load diff

View file

@ -1,6 +1,7 @@
import * as core from '@actions/core'
import {context, GitHub} from '@actions/github'
import {callAsyncFunction} from './async-function'
import { existsSync, readFileSync } from 'fs'
process.on('unhandledRejection', handleError)
main().catch(handleError)
@ -15,8 +16,8 @@ async function main() {
if (userAgent != null) opts.userAgent = userAgent
if (previews != null) opts.previews = previews.split(',')
const github = new GitHub(token, opts)
const script = core.getInput('script', {required: true})
const script = loadScript()
// Using property/value shorthand on `require` (e.g. `{require}`) causes compilatin errors.
const result = await callAsyncFunction(
{require: require, github, context, core},
@ -42,6 +43,25 @@ async function main() {
core.setOutput('result', output)
}
function loadScript(){
const file = core.getInput('file')
let script = core.getInput('script')
if(!script && !file){
throw new Error('either "script" or "file" must be a "string')
}
if(file){
if(!existsSync(file)){
throw new Error('file can\'t be found: ' + file)
}
script = readFileSync(file, 'utf-8')
}
return script
}
function handleError(err: any) {
console.error(err)