Run from a script or a file

This commit is contained in:
Jonathan Clem 2020-05-18 11:50:54 -04:00
parent 64af053a78
commit 35288c3418
No known key found for this signature in database
GPG key ID: B3662C4A8F843179
3 changed files with 61 additions and 4 deletions

View file

@ -16,13 +16,23 @@ jobs:
if [[ "${{steps.output-set.outputs.result}}" != "output" ]]; then
exit 1
fi
- id: file-output-set
- id: file-relative
uses: actions/github-script@run-file
with:
file: __test__/test-output.js
result-encoding: string
input-value: output
- run: |
if [[ "${{steps.file-output-set.outputs.result}}" != "output" ]]; then
if [[ "${{steps.file-relative.outputs.result}}" != "output" ]]; then
exit 1
fi
- id: file-absolute
uses: actions/github-script@run-file
with:
file: ${{github.workspace}}/__test__/test-output.js
result-encoding: string
input-value: output
- run: |
if [[ "${{steps.file-absolute.outputs.result}}" != "output" ]]; then
exit 1
fi

26
dist/index.js vendored
View file

@ -9273,6 +9273,12 @@ var core = __webpack_require__(470);
// EXTERNAL MODULE: ./node_modules/@actions/github/lib/github.js
var lib_github = __webpack_require__(469);
// EXTERNAL MODULE: external "fs"
var external_fs_ = __webpack_require__(747);
// EXTERNAL MODULE: external "path"
var external_path_ = __webpack_require__(622);
// CONCATENATED MODULE: ./src/async-function.ts
const AsyncFunction = Object.getPrototypeOf(async () => null).constructor;
function callAsyncFunction(args, source) {
@ -9284,6 +9290,8 @@ function callAsyncFunction(args, source) {
process.on('unhandledRejection', handleError);
main().catch(handleError);
async function main() {
@ -9299,7 +9307,7 @@ async function main() {
if (previews != null)
opts.previews = previews.split(',');
const github = new lib_github.GitHub(token, opts);
const script = Object(core.getInput)('script', { required: true });
const script = getScript();
// Using property/value shorthand on `require` (e.g. `{require}`) causes compilation errors.
const result = await callAsyncFunction({ require: __webpack_require__(875), github, context: lib_github.context, core: core }, script);
let encoding = Object(core.getInput)('result-encoding');
@ -9317,6 +9325,22 @@ async function main() {
}
Object(core.setOutput)('result', output);
}
function getScript() {
const script = Object(core.getInput)('script');
const filePath = Object(core.getInput)('file');
if (script && filePath) {
Object(core.setFailed)('A script and a file were provided; only one is allowed');
process.exit(1);
}
if (!(script || filePath)) {
Object(core.setFailed)('Neither a script nor a file were provided');
process.exit(1);
}
if (filePath) {
return Object(external_fs_.readFileSync)(Object(external_path_.resolve)(filePath)).toString('utf-8');
}
return script;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function handleError(err) {
console.error(err);

View file

@ -1,5 +1,7 @@
import * as core from '@actions/core'
import {context, GitHub} from '@actions/github'
import * as fs from 'fs'
import * as path from 'path'
import {callAsyncFunction} from './async-function'
process.on('unhandledRejection', handleError)
@ -23,7 +25,7 @@ async function main(): Promise<void> {
if (previews != null) opts.previews = previews.split(',')
const github = new GitHub(token, opts)
const script = core.getInput('script', {required: true})
const script = getScript()
// Using property/value shorthand on `require` (e.g. `{require}`) causes compilation errors.
const result = await callAsyncFunction(
@ -50,6 +52,27 @@ async function main(): Promise<void> {
core.setOutput('result', output)
}
function getScript(): string {
const script = core.getInput('script')
const filePath = core.getInput('file')
if (script && filePath) {
core.setFailed('A script and a file were provided; only one is allowed')
process.exit(1)
}
if (!(script || filePath)) {
core.setFailed('Neither a script nor a file were provided')
process.exit(1)
}
if (filePath) {
return fs.readFileSync(path.resolve(filePath)).toString('utf-8')
}
return script
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function handleError(err: any): void {
console.error(err)