From cca9523c73b9bace5c11cbec7cd6d17e571c9ec1 Mon Sep 17 00:00:00 2001 From: Jonathan Clem Date: Sat, 20 Apr 2019 10:52:56 -0400 Subject: [PATCH] Add the "@actions/exit" package It is useful to have the exit logic separated into its own package --- packages/exit/LICENSE.md | 7 +++++ packages/exit/README.md | 11 +++++++ packages/exit/__tests__/exit.test.ts | 19 ++++++++++++ packages/exit/package-lock.json | 5 ++++ packages/exit/package.json | 34 ++++++++++++++++++++++ packages/exit/src/exit.ts | 43 ++++++++++++++++++++++++++++ packages/exit/tsconfig.json | 11 +++++++ 7 files changed, 130 insertions(+) create mode 100644 packages/exit/LICENSE.md create mode 100644 packages/exit/README.md create mode 100644 packages/exit/__tests__/exit.test.ts create mode 100644 packages/exit/package-lock.json create mode 100644 packages/exit/package.json create mode 100644 packages/exit/src/exit.ts create mode 100644 packages/exit/tsconfig.json diff --git a/packages/exit/LICENSE.md b/packages/exit/LICENSE.md new file mode 100644 index 00000000..e5a73f40 --- /dev/null +++ b/packages/exit/LICENSE.md @@ -0,0 +1,7 @@ +Copyright 2019 GitHub + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/packages/exit/README.md b/packages/exit/README.md new file mode 100644 index 00000000..c54d04da --- /dev/null +++ b/packages/exit/README.md @@ -0,0 +1,11 @@ +# `@actions/exit` + +> TODO: description + +## Usage + +``` +const exit = require('@actions/exit'); + +// TODO: DEMONSTRATE API +``` diff --git a/packages/exit/__tests__/exit.test.ts b/packages/exit/__tests__/exit.test.ts new file mode 100644 index 00000000..31ae074f --- /dev/null +++ b/packages/exit/__tests__/exit.test.ts @@ -0,0 +1,19 @@ +import * as exit from '../src/exit' + +it('exits successfully', () => { + jest.spyOn(process, 'exit').mockImplementation() + exit.success() + expect(process.exit).toHaveBeenCalledWith(0) +}) + +it('exits as a failure', () => { + jest.spyOn(process, 'exit').mockImplementation() + exit.failure() + expect(process.exit).toHaveBeenCalledWith(1) +}) + +it('exits neutrally', () => { + jest.spyOn(process, 'exit').mockImplementation() + exit.neutral() + expect(process.exit).toHaveBeenCalledWith(78) +}) diff --git a/packages/exit/package-lock.json b/packages/exit/package-lock.json new file mode 100644 index 00000000..6d1e6837 --- /dev/null +++ b/packages/exit/package-lock.json @@ -0,0 +1,5 @@ +{ + "name": "@actions/exit", + "version": "0.0.0", + "lockfileVersion": 1 +} diff --git a/packages/exit/package.json b/packages/exit/package.json new file mode 100644 index 00000000..a0ae51e1 --- /dev/null +++ b/packages/exit/package.json @@ -0,0 +1,34 @@ +{ + "name": "@actions/exit", + "version": "0.0.0", + "description": "Functions for safely exiting from GitHub Actions", + "main": "lib/exit.js", + "keywords": [ + "github", + "actions", + "toolkit" + ], + "homepage": "https://github.com/actions/toolkit/tree/master/packages/exit", + "license": "MIT", + "directories": { + "lib": "lib", + "test": "__tests__" + }, + "files": [ + "lib" + ], + "publishConfig": { + "access": "public" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/actions/toolkit.git" + }, + "scripts": { + "test": "echo \"Error: run tests from root\" && exit 1", + "tsc": "tsc" + }, + "bugs": { + "url": "https://github.com/actions/toolkit/issues" + } +} diff --git a/packages/exit/src/exit.ts b/packages/exit/src/exit.ts new file mode 100644 index 00000000..ecea2961 --- /dev/null +++ b/packages/exit/src/exit.ts @@ -0,0 +1,43 @@ +/** + * The code to exit an action + */ +export enum ExitCode { + /** + * A code indicating that the action was successful + */ + Success = 0, + + /** + * A code indicating that the action was a failure + */ + Failure = 1, + + /** + * A code indicating that the action is complete, but neither succeeded nor failed + */ + Neutral = 78 +} + +// TODO: These exit codes may not behave as expected on the new runtime, due to +// complexities of async logging and sync exiting. + +/** + * Exit the action as a success. + */ +export function success() { + process.exit(ExitCode.Success) +} + +/** + * Exit the action as a failure. + */ +export function failure() { + process.exit(ExitCode.Failure) +} + +/** + * Exit the action neither a success or a failure + */ +export function neutral() { + process.exit(ExitCode.Neutral) +} diff --git a/packages/exit/tsconfig.json b/packages/exit/tsconfig.json new file mode 100644 index 00000000..a8b812a6 --- /dev/null +++ b/packages/exit/tsconfig.json @@ -0,0 +1,11 @@ +{ + "extends": "../../tsconfig.json", + "compilerOptions": { + "baseUrl": "./", + "outDir": "./lib", + "rootDir": "./src" + }, + "include": [ + "./src" + ] +} \ No newline at end of file