Skip to content

Commit

Permalink
Merge pull request #574 from magieno/improve-the-cli-module
Browse files Browse the repository at this point in the history
  • Loading branch information
etiennenoel authored Sep 8, 2023
2 parents 62a1213 + a6c3cbc commit b6a4c05
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 2 deletions.
6 changes: 5 additions & 1 deletion packages/cli/src/event-handlers/cli.event-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ export class CliEventHandler implements EventHandlerInterface<any, any>{
throw new CommandNotFoundError(event.payload.name)
}

const mappedArguments = plainToInstance(command.optionsType, event.payload.arguments);
let mappedArguments;

if(event.payload.arguments !== undefined) {
mappedArguments = plainToInstance(command.optionsType, event.payload.arguments);
}

// Validates if all the conditions are respected in the expected type.
const errors = await this.validator.validate(mappedArguments);
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/interfaces/command.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ export interface CommandInterface<ArgumentsOptionsType> {

optionsType: ArgumentsOptionsType;

run(args: {new(...args : any[]): ArgumentsOptionsType ;}): Promise<ExitCodeEnum | number>;
run(args: ArgumentsOptionsType ): Promise<ExitCodeEnum | number>;
}
1 change: 1 addition & 0 deletions packages/cli/src/mappers/command-event.mapper.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ describe('Command Event Mapper', function () {

const consoleArguments = [
["--parameter", "value"],
["--parameter=value"],
["-parameter", "value"],
];

Expand Down
7 changes: 7 additions & 0 deletions packages/cli/src/mappers/command-event.mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ export class CommandEventMapper implements EventMapperInterface<CommandEventPayl

const argumentName = arg.slice(numberOfStartingDashes);

// If there's an equal sign in the name, that's the value. ex: --parameter=value
const indexOfEqualSign = argumentName.indexOf("=");
if(numberOfStartingDashes === 2 && indexOfEqualSign != -1) {
const actualArgumentName = argumentName.slice(0, indexOfEqualSign);
command.arguments[actualArgumentName] = argumentName.slice(indexOfEqualSign+1);
continue;
}

// If there are no more passed arguments or the next one also starts with '-' or '--', then simply assign true.
if( (i+1) >= passedArguments.length || passedArguments[i+1].startsWith('-')) {
Expand Down

0 comments on commit b6a4c05

Please sign in to comment.