From 0201fad609daf974aac80d715cd0333e6371e459 Mon Sep 17 00:00:00 2001 From: alvarosabu Date: Wed, 16 Oct 2024 16:44:59 +0200 Subject: [PATCH] feat: verbose mode --- .vscode/launch.json | 11 +++++++++++ src/commands/login/index.ts | 5 +++-- src/commands/logout/index.ts | 4 +++- src/index.ts | 4 ++-- src/utils/error/error.ts | 35 +++++++++++++++++------------------ 5 files changed, 36 insertions(+), 23 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index fe0403e..cd95b2d 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -34,6 +34,17 @@ "console": "integratedTerminal", "sourceMaps": true, "outFiles": ["${workspaceFolder}/dist/**/*.js"] + }, + { + "type": "node", + "request": "launch", + "name": "Debug test", + "program": "${workspaceFolder}/dist/index.mjs", + "args": ["test", "--verbose"], + "cwd": "${workspaceFolder}", + "console": "integratedTerminal", + "sourceMaps": true, + "outFiles": ["${workspaceFolder}/dist/**/*.js"] } ] } diff --git a/src/commands/login/index.ts b/src/commands/login/index.ts index c3492c4..491d8bf 100644 --- a/src/commands/login/index.ts +++ b/src/commands/login/index.ts @@ -41,6 +41,7 @@ export const loginCommand = program region: RegionCode }) => { konsola.title(` ${commands.LOGIN} `, '#8556D3') + const verbose = program.opts().verbose const { token, region } = options if (!isRegion(region)) { handleError(new CommandError(`The provided region: ${region} is not valid. Please use one of the following values: ${Object.values(regions).join(' | ')}`)) @@ -64,7 +65,7 @@ export const loginCommand = program konsola.ok(`Successfully logged in with token`) } catch (error) { - handleError(error as Error, true) + handleError(error as Error, verbose) } } else { @@ -123,7 +124,7 @@ export const loginCommand = program } } catch (error) { - handleError(error as Error, true) + handleError(error as Error, verbose) } } }) diff --git a/src/commands/logout/index.ts b/src/commands/logout/index.ts index 6cdc165..405d633 100644 --- a/src/commands/logout/index.ts +++ b/src/commands/logout/index.ts @@ -9,6 +9,8 @@ export const logoutCommand = program .command(commands.LOGOUT) .description('Logout from the Storyblok CLI') .action(async () => { + const verbose = program.opts().verbose + const isAuth = await isAuthorized() if (!isAuth) { konsola.ok(`You are already logged out. If you want to login, please use the login command.`) @@ -20,6 +22,6 @@ export const logoutCommand = program konsola.ok(`Successfully logged out`) } catch (error) { - handleError(error as Error, true) + handleError(error as Error, verbose) } }) diff --git a/src/index.ts b/src/index.ts index 61e6ad4..7a6a7df 100644 --- a/src/index.ts +++ b/src/index.ts @@ -27,13 +27,13 @@ program.on('command:*', () => { program.command('test').action(async () => { konsola.title(`Test`, '#8556D3', 'Attempting a test...') - + const verbose = program.opts().verbose try { // await loginWithEmailAndPassword('aw', 'passwrod', 'eu') await loginWithToken('WYSYDHYASDHSYD', 'eu') } catch (error) { - handleError(error as Error) + handleError(error as Error, verbose) } }) diff --git a/src/utils/error/error.ts b/src/utils/error/error.ts index f8a8b70..93f2259 100644 --- a/src/utils/error/error.ts +++ b/src/utils/error/error.ts @@ -4,7 +4,21 @@ import { CommandError } from './command-error' import { FileSystemError } from './filesystem-error' export function handleError(error: Error, verbose = false): void { - // If verbose flag is true and the error has getInfo method + // Print the message stack if it exists + if ((error as any).messageStack) { + const messageStack = (error as any).messageStack + messageStack.forEach((message: string, index: number) => { + konsola.error(message, null, { + header: index === 0, + margin: false, + }) + }) + } + else { + konsola.error(error.message, null, { + header: true, + }) + } if (verbose && typeof (error as any).getInfo === 'function') { const errorDetails = (error as any).getInfo() if (error instanceof CommandError) { @@ -21,23 +35,8 @@ export function handleError(error: Error, verbose = false): void { } } else { - // Print the message stack if it exists - if ((error as any).messageStack) { - const messageStack = (error as any).messageStack - messageStack.forEach((message: string, index: number) => { - konsola.error(message, null, { - header: index === 0, - margin: false, - }) - }) - konsola.br() - konsola.info('For more information about the error, run the command with the `--verbose` flag') - } - else { - konsola.error(error.message, null, { - header: true, - }) - } + konsola.br() + konsola.info('For more information about the error, run the command with the `--verbose` flag') } if (!process.env.VITEST) {