run function to handle GitHub Action main and post runs

Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax 2023-02-21 08:12:26 +01:00
parent 1383a2bcaf
commit f2b1224b00
No known key found for this signature in database
GPG Key ID: 3248E46B6BB8C7F7
4 changed files with 46 additions and 4 deletions

View File

@ -39,7 +39,7 @@ $ npm install @docker/actions-toolkit
## Usage ## Usage
```js ```js
const { Toolkit } = require('@docker/actions-toolkit') const { Toolkit } = require('@docker/actions-toolkit/lib/toolkit')
const toolkit = new Toolkit() const toolkit = new Toolkit()
``` ```

View File

@ -41,7 +41,7 @@ module.exports = {
moduleNameMapper: { moduleNameMapper: {
'^csv-parse/sync': '<rootDir>/node_modules/csv-parse/dist/cjs/sync.cjs' '^csv-parse/sync': '<rootDir>/node_modules/csv-parse/dist/cjs/sync.cjs'
}, },
collectCoverageFrom: ['src/**/{!(toolkit.ts),}.ts'], collectCoverageFrom: ['src/**/{!(index.ts),}.ts'],
coveragePathIgnorePatterns: ['lib/', 'node_modules/', '__mocks__/', '__tests__/'], coveragePathIgnorePatterns: ['lib/', 'node_modules/', '__mocks__/', '__tests__/'],
verbose: true verbose: true
}; };

View File

@ -28,8 +28,8 @@
"author": "Docker Inc.", "author": "Docker Inc.",
"license": "Apache-2.0", "license": "Apache-2.0",
"packageManager": "yarn@3.3.1", "packageManager": "yarn@3.3.1",
"main": "lib/toolkit.js", "main": "lib/index.js",
"types": "lib/toolkit.d.ts", "types": "lib/index.d.ts",
"directories": { "directories": {
"lib": "lib", "lib": "lib",
"test": "__tests__" "test": "__tests__"

42
src/index.ts Normal file
View File

@ -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<void>
*/
export async function run(main: () => Promise<void>, post?: () => Promise<void>): Promise<void> {
if (!isPost) {
try {
await main();
} catch (e) {
core.setFailed(e.message);
}
} else if (post) {
await post();
}
}