Skip to content

Commit

Permalink
feat: verbose mode
Browse files Browse the repository at this point in the history
  • Loading branch information
alvarosabu committed Oct 16, 2024
1 parent f7a4910 commit 0201fad
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 23 deletions.
11 changes: 11 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
}
]
}
5 changes: 3 additions & 2 deletions src/commands/login/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(' | ')}`))
Expand All @@ -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 {
Expand Down Expand Up @@ -123,7 +124,7 @@ export const loginCommand = program
}
}
catch (error) {
handleError(error as Error, true)
handleError(error as Error, verbose)
}
}
})
4 changes: 3 additions & 1 deletion src/commands/logout/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.`)
Expand All @@ -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)
}
})
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})

Expand Down
35 changes: 17 additions & 18 deletions src/utils/error/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down

0 comments on commit 0201fad

Please sign in to comment.