mirror of
https://github.com/actions/github-script.git
synced 2026-04-08 07:00:05 +00:00
Run function with async+eval
This commit is contained in:
parent
83107c8695
commit
04219251e3
3 changed files with 97 additions and 144 deletions
|
|
@ -1,22 +1,14 @@
|
|||
import * as core from '@actions/core'
|
||||
import {Context} from '@actions/github/lib/context'
|
||||
import {GitHub} from '@actions/github/lib/utils'
|
||||
import * as io from '@actions/io'
|
||||
type AsyncFunction<A, R> = (args: A) => Promise<R>
|
||||
|
||||
const AsyncFunction = Object.getPrototypeOf(async () => null).constructor
|
||||
|
||||
type AsyncFunctionArguments = {
|
||||
context: Context
|
||||
core: typeof core
|
||||
github: InstanceType<typeof GitHub>
|
||||
io: typeof io
|
||||
require: NodeRequire
|
||||
}
|
||||
|
||||
export function callAsyncFunction<T>(
|
||||
args: AsyncFunctionArguments,
|
||||
export async function callAsyncFunction<A = {}, R = unknown>(
|
||||
args: A,
|
||||
source: string
|
||||
): Promise<T> {
|
||||
const fn = new AsyncFunction(...Object.keys(args), source)
|
||||
return fn(...Object.values(args))
|
||||
): Promise<R> {
|
||||
const argsKeys = Object.keys(args).join(',')
|
||||
|
||||
const wrappedFunction: AsyncFunction<A, R> = eval(`async({${argsKeys}}) => {
|
||||
${source}
|
||||
}`)
|
||||
|
||||
return wrappedFunction(args)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -25,12 +25,7 @@ async function main(): Promise<void> {
|
|||
|
||||
const github = getOctokit(token, opts)
|
||||
const script = core.getInput('script', {required: true})
|
||||
|
||||
// Using property/value shorthand on `require` (e.g. `{require}`) causes compilation errors.
|
||||
const result = await callAsyncFunction(
|
||||
{require: require, github, context, core, io},
|
||||
script
|
||||
)
|
||||
const result = await callAsyncFunction({context, core, github, io}, script)
|
||||
|
||||
let encoding = core.getInput('result-encoding')
|
||||
encoding = encoding ? encoding : 'json'
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue