Skip to content

Commit

Permalink
fix: allow arg/flag name to be different from property name
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Aug 27, 2019
1 parent 6dd7890 commit 9adfc4b
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 13 deletions.
2 changes: 2 additions & 0 deletions src/Contracts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export type ArgTypes = 'string' | 'spread'
* The shape of command argument
*/
export type CommandArg = {
propertyName: string,
name: string,
type: ArgTypes,
required: boolean,
Expand All @@ -35,6 +36,7 @@ export type CommandArg = {
* The shape of a command flag
*/
export type CommandFlag = {
propertyName: string,
name: string,
type: FlagTypes,
description?: string,
Expand Down
5 changes: 3 additions & 2 deletions src/Decorators/args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ type DecoratorArg = Partial<Pick<CommandArg, Exclude<keyof CommandArg, 'type'>>>
* type.
*/
function addArg (type: ArgTypes, options: DecoratorArg) {
return function arg (target: any, propertyKey: string) {
return function arg (target: any, propertyName: string) {
const arg: CommandArg = Object.assign({
type,
name: propertyKey,
propertyName,
name: propertyName,
required: true,
}, options)

Expand Down
13 changes: 8 additions & 5 deletions src/Decorators/flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,18 @@ type DecoratorFlag = Partial<Pick<CommandFlag, Exclude<keyof CommandFlag, 'type'
* types.
*/
function addFlag (type: FlagTypes, options: DecoratorFlag) {
return function flag (target: any, propertyKey: string) {
return function flag (target: any, propertyName: string) {
const flag: CommandFlag = Object.assign({
name: propertyName,
propertyName,
type,
}, options)

if (!target.constructor.hasOwnProperty('flags')) {
Object.defineProperty(target.constructor, 'flags', { value: [] })
}

target.constructor.flags.push(Object.assign({
name: propertyKey,
type,
}, options))
target.constructor.flags.push(flag)
}
}

Expand Down
16 changes: 11 additions & 5 deletions src/Kernel/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,15 @@ export class Kernel {
public flag (
name: string,
handler: GlobalFlagHandler,
options: Partial<Pick<CommandFlag, Exclude<keyof CommandFlag, 'name'>>>,
options: Partial<Exclude<CommandFlag, 'name' | 'propertyName'>>,
): this {
this.flags[name] = Object.assign({ name, handler, type: 'boolean' }, options)
this.flags[name] = Object.assign({
name,
propertyName: name,
handler,
type: 'boolean',
}, options)

return this
}

Expand Down Expand Up @@ -197,18 +203,18 @@ export class Kernel {
for (let i = 0; i < command.args.length; i++) {
const arg = command.args[i]
if (arg.type === 'spread') {
commandInstance[arg.name] = parsedOptions._.slice(i)
commandInstance[arg.propertyName] = parsedOptions._.slice(i)
break
} else {
commandInstance[arg.name] = parsedOptions._[i]
commandInstance[arg.propertyName] = parsedOptions._[i]
}
}

/**
* Set flag value on the command instance
*/
command.flags.forEach((flag) => {
commandInstance[flag.name] = parsedOptions[flag.name]
commandInstance[flag.propertyName] = parsedOptions[flag.name]
})

/**
Expand Down
63 changes: 62 additions & 1 deletion test/kernel.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,24 @@ test.group('Kernel | register', () => {
assert.throw(fn, 'spread argument {files} must be at last position')
})

test('return command suggestions for a given string', (assert) => {
test('register command', (assert) => {
const kernel = new Kernel()

class Install extends BaseCommand {
public static commandName = 'install'
public async handle () {}
}

class Greet extends BaseCommand {
public static commandName = 'greet'
public async handle () {}
}

kernel.register([Install, Greet])
assert.deepEqual(kernel.commands, { install: Install, greet: Greet })
})

test('return command name suggestions for a given string', (assert) => {
const kernel = new Kernel()

class Install extends BaseCommand {
Expand Down Expand Up @@ -523,6 +540,50 @@ test.group('Kernel | handle', () => {
const argv = ['greet', 'virk', '--env=production']
await kernel.handle(argv)
})

test('define arg name different from property name', async (assert) => {
assert.plan(2)

class Greet extends BaseCommand {
public static commandName = 'greet'

@args.string({ name: 'theName' })
public name: string

public async handle () {
assert.deepEqual(this.parsed, { _: ['virk'] })
assert.equal(this.name, 'virk')
}
}

const kernel = new Kernel()
kernel.register([Greet])

const argv = ['greet', 'virk']
await kernel.handle(argv)
})

test('define flag name different from property name', async (assert) => {
assert.plan(2)

class Greet extends BaseCommand {
public static commandName = 'greet'

@flags.boolean({ name: 'isAdmin' })
public admin: boolean

public async handle () {
assert.deepEqual(this.parsed, { _: [], isAdmin: true })
assert.isTrue(this.admin)
}
}

const kernel = new Kernel()
kernel.register([Greet])

const argv = ['greet', '--isAdmin']
await kernel.handle(argv)
})
})

test.group('Kernel | runCommand', () => {
Expand Down

0 comments on commit 9adfc4b

Please sign in to comment.