diff --git a/.github/workflows/integration.yml b/.github/workflows/integration.yml index 7c41682..440827b 100644 --- a/.github/workflows/integration.yml +++ b/.github/workflows/integration.yml @@ -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 diff --git a/dist/index.js b/dist/index.js index 02c4f48..4c0da40 100644 --- a/dist/index.js +++ b/dist/index.js @@ -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); diff --git a/src/main.ts b/src/main.ts index becbc56..2abdcac 100644 --- a/src/main.ts +++ b/src/main.ts @@ -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 { 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 { 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)