From 97724801229e0dbef589c62aa9e69aa3fe43b4c8 Mon Sep 17 00:00:00 2001 From: Webber Date: Thu, 23 Jun 2022 23:25:56 +0200 Subject: [PATCH] chore: reinstate exec signature --- .eslintrc.json | 4 +- src/dependencies.ts | 29 ++---------- src/model/platform-setup/setup-mac.ts | 8 ++-- src/model/system.ts | 2 +- src/modules/actions/core.ts | 32 +++++++++++++ src/modules/actions/exec.ts | 46 +++++++++++++++++++ src/modules/actions/index.ts | 6 +++ .../waitUntil.ts => modules/wait-until.ts} | 0 8 files changed, 95 insertions(+), 32 deletions(-) create mode 100644 src/modules/actions/core.ts create mode 100644 src/modules/actions/exec.ts create mode 100644 src/modules/actions/index.ts rename src/{helpers/waitUntil.ts => modules/wait-until.ts} (100%) diff --git a/.eslintrc.json b/.eslintrc.json index f411cde4..c8e3aa28 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -60,6 +60,8 @@ // Allow Array.from(set) mitigate TS2569 which would require '--downlevelIteration' "unicorn/prefer-spread": "off", // Deno has dependencies file, this rule enforces it's named re-exports - "unicorn/prevent-abbreviations": "off" + "unicorn/prevent-abbreviations": "off", + // Allow disabling eslint per file + "eslint-comments/no-use": "off" } } diff --git a/src/dependencies.ts b/src/dependencies.ts index d6245b56..7ab1dffb 100644 --- a/src/dependencies.ts +++ b/src/dependencies.ts @@ -3,7 +3,6 @@ import * as assert from 'https://deno.land/std@0.144.0/testing/asserts.ts'; import * as aws from 'https://deno.land/x/aws_api/client/mod.ts'; import * as base64 from 'https://deno.land/std@0.145.0/encoding/base64.ts'; import * as compress from 'https://deno.land/x/compress@v0.3.3/mod.ts'; -// import * as core from 'https://deno.land/x/deno_actions_core@0.1.3/mod.ts'; import * as fs from 'https://deno.land/std@0.142.0/node/fs/promises.ts'; import * as fsSync from 'https://deno.land/std@0.142.0/fs/mod.ts'; import * as k8s from 'https://deno.land/x/kubernetes_client/mod.ts'; @@ -12,37 +11,15 @@ import * as nanoid from 'https://deno.land/x/nanoid@v3.0.0/mod.ts'; import * as path from 'https://deno.land/std@0.142.0/path/mod.ts'; import * as process from 'https://deno.land/std@0.104.0/node/process.ts'; import * as semver from 'https://deno.land/x/semver@v1.4.0/mod.ts'; -import * as waitUntil from './helpers/waitUntil.ts'; import * as yaml from 'https://deno.land/std@0.145.0/encoding/yaml.ts'; import { crypto } from 'https://deno.land/std@0.142.0/crypto/mod.ts'; import { v4 as uuid } from 'https://deno.land/std@0.142.0/uuid/mod.ts'; import * as http from 'https://deno.land/std@0.145.0/node/http.ts'; import { Command } from 'https://deno.land/x/cmd@v1.2.0/commander/index.ts'; -const core = { - info: console.log, - error: (error) => console.error(error, error.stack), - setFailed: (failure) => console.error('setFailed:', failure), - - // Adapted from: https://github.com/actions/toolkit/blob/9b7bcb1567c9b7f134eb3c2d6bbf409a5106a956/packages/core/src/core.ts#L128 - getInput: (name, options) => { - const val: string = Deno.env.get(`INPUT_${name.replace(/ /g, '_').toUpperCase()}`) || ''; - - if (options?.required && !val) { - throw new Error(`Input required and not supplied: ${name}`); - } - - if (options && options.trimWhitespace === false) { - return val; - } - - return val.trim(); - }, -}; - -const exec = () => { - throw new Error('exec is not implemented'); // @actions/exec' -}; +// Internally managed +import waitUntil from './modules/wait-until.ts'; +import { core, exec } from './modules/actions/index.ts'; const getUnityChangeSet = () => { throw new Error('getUnityChangeSet is not implemented'); // unity-changeset' diff --git a/src/model/platform-setup/setup-mac.ts b/src/model/platform-setup/setup-mac.ts index 155a1302..af721a1c 100644 --- a/src/model/platform-setup/setup-mac.ts +++ b/src/model/platform-setup/setup-mac.ts @@ -21,8 +21,8 @@ class SetupMac { if (!fs.existsSync(this.unityHubPath)) { // Ignoring return code because the log seems to overflow the internal buffer which triggers // a false error - const errorCode = await exec(command, undefined, { silent, ignoreReturnCode: true }); - if (errorCode) { + const { exitCode } = await exec(command, undefined, { silent, ignoreReturnCode: true }); + if (exitCode) { throw new Error(`There was an error installing the Unity Editor. See logs above for details.`); } } @@ -38,8 +38,8 @@ class SetupMac { // Ignoring return code because the log seems to overflow the internal buffer which triggers // a false error - const errorCode = await exec(command, undefined, { silent, ignoreReturnCode: true }); - if (errorCode) { + const { exitCode } = await exec(command, undefined, { silent, ignoreReturnCode: true }); + if (exitCode) { throw new Error(`There was an error installing the Unity Editor. See logs above for details.`); } } diff --git a/src/model/system.ts b/src/model/system.ts index 51719027..56519c54 100644 --- a/src/model/system.ts +++ b/src/model/system.ts @@ -48,7 +48,7 @@ class System { throw new Error(`Failed to execute empty command`); } - const exitCode = await exec(command, arguments_, { silent: true, listeners, ...options }); + const { exitCode } = await exec(command, arguments_, { silent: true, listeners, ...options }); showOutput(); if (exitCode !== 0) { throwContextualError(`Command returned non-zero exit code.\nError: ${error}`); diff --git a/src/modules/actions/core.ts b/src/modules/actions/core.ts new file mode 100644 index 00000000..a676c161 --- /dev/null +++ b/src/modules/actions/core.ts @@ -0,0 +1,32 @@ +/* eslint-disable no-console */ + +export const core = { + info: console.log, + + warning: console.warn, + + error: (error) => { + console.error(error, error.stack); + }, + + setFailed: (failure) => { + console.error('setFailed:', failure); + Deno.exit(1); + }, + + // Adapted from: https://github.com/actions/toolkit/blob/9b7bcb1567c9b7f134eb3c2d6bbf409a5106a956/packages/core/src/core.ts#L128 + getInput: (name, options) => { + const variable = `INPUT_${name.replace(/ /g, '_').toUpperCase()}`; + const value: string = Deno.env.get(variable) || ''; + + if (options?.required && !value) { + throw new Error(`Input required and not supplied: ${name}`); + } + + if (options?.trimWhitespace === false) { + return value; + } + + return value.trim(); + }, +}; diff --git a/src/modules/actions/exec.ts b/src/modules/actions/exec.ts new file mode 100644 index 00000000..9f818482 --- /dev/null +++ b/src/modules/actions/exec.ts @@ -0,0 +1,46 @@ +import { exec as originalExec } from 'https://deno.land/x/exec/mod.ts'; +import { core } from './core.ts'; + +export enum OutputMode { + None = 0, // no output, just run the command + StdOut, // dump the output to stdout + Capture, // capture the output and return it + Tee, // both dump and capture the output +} + +export interface IExecResponse { + code: number; + success: boolean; + output: string; +} + +interface IOptions { + output?: OutputMode; + verbose?: boolean; + continueOnError?: boolean; +} + +// Todo - change signature of exec inside the code instead of adapting the exec method +const exec = async (command, args: string | string[] = [], ghActionsOptions: IOptions = {}): Promise => { + core.info('Running command: ', command, args); + + const options = { + output: OutputMode.Tee, + verbose: false, + continueOnError: false, + }; + + const { silent = false, ignoreReturnCode } = ghActionsOptions; + if (silent) options.output = OutputMode.None; + if (ignoreReturnCode) options.continueOnError = true; + + const result = await originalExec(`${command} ${args.join(' ')}`, options); + core.info('result:', result); + + const { status = {}, output = '' } = result; + const { code: exitCode, success } = status; + + return { exitCode, success, output }; +}; + +export { exec }; diff --git a/src/modules/actions/index.ts b/src/modules/actions/index.ts new file mode 100644 index 00000000..e0202240 --- /dev/null +++ b/src/modules/actions/index.ts @@ -0,0 +1,6 @@ +/** + * The actions/core package from GitHub errors out. + * This substitutes the parts we use in a Deno-compatible way. + */ +export { core } from './core.ts'; +export { exec } from './exec.ts'; diff --git a/src/helpers/waitUntil.ts b/src/modules/wait-until.ts similarity index 100% rename from src/helpers/waitUntil.ts rename to src/modules/wait-until.ts