fix: error types

This commit is contained in:
2023-12-14 11:02:26 +02:00
committed by Chen Asraf
parent cdc9118f6d
commit 8364f1998c
4 changed files with 18 additions and 12 deletions

View File

@@ -13,7 +13,7 @@ import {
Prefixes,
FlagConfig,
} from './option'
import { DeepRequired, setOrPush, deepMerge, getErrorMessage } from './utils'
import { DeepRequired, setOrPush, deepMerge, getErrorMessage, capitalize } from './utils'
import { MassargExample, ExampleConfig } from './example'
import { format } from './style'
@@ -187,7 +187,7 @@ export class MassargCommand<Args extends ArgsObject = ArgsObject> {
flag(config: FlagConfig | MassargFlag): MassargCommand<Args> {
try {
const flag = config instanceof MassargFlag ? config : new MassargFlag(config)
this.assertNotDuplicate(flag)
this.assertNotDuplicate(flag, 'flag')
this.options.push(flag as MassargOption)
return this
} catch (e) {
@@ -227,7 +227,7 @@ export class MassargCommand<Args extends ArgsObject = ArgsObject> {
config instanceof MassargOption
? config
: MassargOption.fromTypedConfig(config as TypedOptionConfig<T, A>)
this.assertNotDuplicate(option)
this.assertNotDuplicate(option, 'option')
this.assertOnlyOneDefault<T, A>(option)
this.options.push(option as MassargOption)
return this
@@ -243,12 +243,15 @@ export class MassargCommand<Args extends ArgsObject = ArgsObject> {
}
}
private assertNotDuplicate<T = string, A extends ArgsObject = Args>(option: MassargOption<T, A>) {
const existingName = this.options.find((c) => c.name === option.name))
private assertNotDuplicate<T = string, A extends ArgsObject = Args>(
option: MassargOption<T, A>,
type: 'option' | 'flag',
) {
const existingName = this.options.find((c) => c.name === option.name)
if (existingName) {
throw new ValidationError({
code: 'duplicate_option_name',
message: `Option name "${existingName.name}" already exists`,
code: `duplicate_${type}_name`,
message: `${capitalize(type)} name "${existingName.name}" already exists`,
path: [this.name, option.name],
})
}

View File

@@ -87,6 +87,11 @@ export function deepMerge<T1, T2>(obj1: T1, obj2: T2): NonNullable<T1> & NonNull
}
return res
}
export function capitalize(str: string): string {
return str[0].toUpperCase() + str.slice(1)
}
/**
* Splits a name into words, using camelCase, PascalCase, snake_case, and kebab-case or
* regular spaced strings.

View File

@@ -99,9 +99,7 @@ describe('prints help from option', () => {
.help({
bindOption: true,
})
const log = jest.spyOn(console, 'log').mockImplementation((...a) => {
console.info(...a)
})
const log = jest.spyOn(console, 'log').mockImplementation(() => {})
command.parse(['--help'])
expect(log).toHaveBeenCalled()
})

View File

@@ -38,7 +38,7 @@ describe('option', () => {
aliases: [],
defaultValue: '',
}),
).toThrow('Option "test2" already exists')
).toThrow('test.test2: Option name "test2" already exists')
})
test('default', () => {
const command = massarg(opts)
@@ -108,7 +108,7 @@ describe('flag', () => {
massarg(opts)
.flag({ name: 'test2', description: 'test2', aliases: [] })
.flag({ name: 'test2', description: 'test2', aliases: [] }),
).toThrow('Flag "test2" already exists')
).toThrow('test.test2: Flag name "test2" already exists')
})
test('validate', () => {
expect(() =>