From f2b1224b00a2f6cda954aa71c56a4fb1b054f344 Mon Sep 17 00:00:00 2001 From: CrazyMax Date: Tue, 21 Feb 2023 08:12:26 +0100 Subject: [PATCH] run function to handle GitHub Action main and post runs Signed-off-by: CrazyMax --- README.md | 2 +- jest.config.ts | 2 +- package.json | 4 ++-- src/index.ts | 42 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 46 insertions(+), 4 deletions(-) create mode 100644 src/index.ts diff --git a/README.md b/README.md index 066f2d0..c3e8a55 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,7 @@ $ npm install @docker/actions-toolkit ## Usage ```js -const { Toolkit } = require('@docker/actions-toolkit') +const { Toolkit } = require('@docker/actions-toolkit/lib/toolkit') const toolkit = new Toolkit() ``` diff --git a/jest.config.ts b/jest.config.ts index 49ba111..4e28d8a 100644 --- a/jest.config.ts +++ b/jest.config.ts @@ -41,7 +41,7 @@ module.exports = { moduleNameMapper: { '^csv-parse/sync': '/node_modules/csv-parse/dist/cjs/sync.cjs' }, - collectCoverageFrom: ['src/**/{!(toolkit.ts),}.ts'], + collectCoverageFrom: ['src/**/{!(index.ts),}.ts'], coveragePathIgnorePatterns: ['lib/', 'node_modules/', '__mocks__/', '__tests__/'], verbose: true }; diff --git a/package.json b/package.json index 26af6e8..ceb4e3d 100644 --- a/package.json +++ b/package.json @@ -28,8 +28,8 @@ "author": "Docker Inc.", "license": "Apache-2.0", "packageManager": "yarn@3.3.1", - "main": "lib/toolkit.js", - "types": "lib/toolkit.d.ts", + "main": "lib/index.js", + "types": "lib/index.d.ts", "directories": { "lib": "lib", "test": "__tests__" diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 0000000..8d02199 --- /dev/null +++ b/src/index.ts @@ -0,0 +1,42 @@ +/** + * Copyright 2023 actions-toolkit authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import * as core from '@actions/core'; + +const isPost = !!process.env['STATE_isPost']; +if (!isPost) { + core.saveState('isPost', 'true'); +} + +/** + * Runs a GitHub Action. + * Output will be streamed to the live console. + * + * @param main runs the defined function. + * @param post runs the defined function at the end of the job if set. + * @returns Promise + */ +export async function run(main: () => Promise, post?: () => Promise): Promise { + if (!isPost) { + try { + await main(); + } catch (e) { + core.setFailed(e.message); + } + } else if (post) { + await post(); + } +}