diff --git a/src/main.ts b/src/main.ts index d0ee6d394..36788801a 100644 --- a/src/main.ts +++ b/src/main.ts @@ -8,20 +8,30 @@ import { wait } from './wait.js' */ export async function run(): Promise { try { - const ms: string = core.getInput('milliseconds') + const msInput = core.getInput('milliseconds') + const ms = parseInt(msInput, 10) + + if (Number.isNaN(ms)) { + throw new Error("Input 'milliseconds' must be a valid number.") + } // Debug logs are only output if the `ACTIONS_STEP_DEBUG` secret is true - core.debug(`Waiting ${ms} milliseconds ...`) + core.debug(`Waiting for ${ms} milliseconds...`) + + const before = new Date().toTimeString() + core.debug(`Start time: ${before}`) + + await wait(ms) - // Log the current timestamp, wait, then log the new timestamp - core.debug(new Date().toTimeString()) - await wait(parseInt(ms, 10)) - core.debug(new Date().toTimeString()) + const after = new Date().toTimeString() + core.debug(`End time: ${after}`) // Set outputs for other workflow steps to use - core.setOutput('time', new Date().toTimeString()) + core.setOutput('time', after) } catch (error) { // Fail the workflow run if an error occurs - if (error instanceof Error) core.setFailed(error.message) + if (error instanceof Error) { + core.setFailed(error.message) + } } } diff --git a/src/wait.ts b/src/wait.ts index c79f72112..ef9ec1e8e 100644 --- a/src/wait.ts +++ b/src/wait.ts @@ -1,13 +1,15 @@ /** * Waits for a number of milliseconds. * - * @param milliseconds The number of milliseconds to wait. + * @param milliseconds Number of milliseconds to wait. * @returns Resolves with 'done!' after the wait is over. */ -export async function wait(milliseconds: number): Promise { - return new Promise((resolve) => { - if (isNaN(milliseconds)) throw new Error('milliseconds is not a number') +export function wait(milliseconds: number): Promise { + if (Number.isNaN(milliseconds)) { + throw new Error("The 'milliseconds' parameter must be a valid number.") + } + return new Promise((resolve) => { setTimeout(() => resolve('done!'), milliseconds) }) }