refactor: success and error functions

This commit is contained in:
Amin Yahyaabadi 2021-09-18 08:21:22 -05:00
parent 9bef371233
commit bfd6260fca
4 changed files with 26 additions and 14 deletions

2
dist/setup_cpp.js vendored

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -20,6 +20,7 @@ import semverValid from "semver/functions/valid"
import { getVersion } from "./default_versions" import { getVersion } from "./default_versions"
import { setupGcc } from "./gcc/gcc" import { setupGcc } from "./gcc/gcc"
import { InstallationInfo } from "./utils/setup/setupBin" import { InstallationInfo } from "./utils/setup/setupBin"
import { error, success } from "./utils/io/io"
/** The setup functions */ /** The setup functions */
const setups = { const setups = {
@ -121,7 +122,7 @@ export async function main(args: string[]): Promise<number> {
} }
} catch (e) { } catch (e) {
// push error message to the logger // push error message to the logger
core.error(e as string | Error) error(e as string | Error)
errorMessages.push(`${tool} failed to install`) errorMessages.push(`${tool} failed to install`)
} }
} }
@ -140,7 +141,7 @@ export async function main(args: string[]): Promise<number> {
if (semverValid(maybeVersion) !== null) { if (semverValid(maybeVersion) !== null) {
version = maybeVersion version = maybeVersion
} else { } else {
core.error(`Invalid version ${maybeVersion} used for the compiler. Using the default version...`) error(`Invalid version ${maybeVersion} used for the compiler. Using the default version...`)
} }
} }
@ -175,13 +176,13 @@ export async function main(args: string[]): Promise<number> {
} }
} }
} catch (e) { } catch (e) {
core.error(e as string | Error) error(e as string | Error)
errorMessages.push(`Failed to install the ${maybeCompiler}`) errorMessages.push(`Failed to install the ${maybeCompiler}`)
} }
// report the messages in the end // report the messages in the end
successMessages.forEach((tool) => (isCI ? core.info(tool) : console.log(`\x1b[32m${tool}\x1b[0m`))) successMessages.forEach((tool) => success(tool))
errorMessages.forEach((tool) => (isCI ? core.error(tool) : console.log(`\x1b[31m${tool}\x1b[0m`))) errorMessages.forEach((tool) => error(tool))
core.info("setup_cpp finished") core.info("setup_cpp finished")
return errorMessages.length === 0 ? 0 : 1 // exit with non-zero if any error message return errorMessages.length === 0 ? 0 : 1 // exit with non-zero if any error message
@ -191,9 +192,9 @@ main(process.argv)
.then((ret) => { .then((ret) => {
process.exitCode = ret process.exitCode = ret
}) })
.catch((error) => { .catch((err) => {
core.error("main() panicked!") error("main() panicked!")
core.error(error as string | Error) error(err as string | Error)
process.exitCode = 1 process.exitCode = 1
}) })
@ -238,12 +239,12 @@ function maybeGetInput(key: string) {
} }
function getSuccessMessage(tool: string, installationInfo: InstallationInfo) { function getSuccessMessage(tool: string, installationInfo: InstallationInfo) {
let success = `${tool} was successfully installed` let msg = `${tool} was successfully installed`
if ("installDir" in installationInfo) { if ("installDir" in installationInfo) {
success += `\nThe installation direcotry is ${installationInfo.installDir}` msg += `\nThe installation direcotry is ${installationInfo.installDir}`
} }
if (installationInfo.binDir !== "") { if (installationInfo.binDir !== "") {
success += `\nThe binary direcotry is ${installationInfo.binDir}` msg += `\nThe binary direcotry is ${installationInfo.binDir}`
} }
return success return msg
} }

11
src/utils/io/io.ts Normal file
View File

@ -0,0 +1,11 @@
import * as core from "@actions/core"
const isCI = process.env.CI === undefined || process.env.CI === "" || process.env.CI === "false"
export function error(err: string | Error) {
return isCI ? core.error(err) : console.log(`\x1b[31m${err}\x1b[0m`)
}
export function success(msg: string) {
return isCI ? core.info(msg) : console.log(`\x1b[32m${msg}\x1b[0m`)
}