Skip to content

Commit

Permalink
refactor(command): refactor env() method
Browse files Browse the repository at this point in the history
  • Loading branch information
c4spar committed Mar 22, 2020
1 parent b2c4f91 commit a1a3364
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
29 changes: 18 additions & 11 deletions packages/command/lib/base-command.ts
Expand Up @@ -336,24 +336,31 @@ export class BaseCommand {
*/
public env( name: string, description: string ): this {

const names: string[] = name.split( /, */g );
const result = this.splitArguments( name );

const parts: string[] = names[ names.length - 1 ].split( /[ =]+/g );
names[ names.length - 1 ] = parts.shift() as string;
const type: OptionType = parts.pop() as OptionType || OptionType.BOOLEAN;

if ( parts.length ) {
throw this.error( new Error( `Invalid environment variable: ${ name }` ) );
if ( !result.typeDefinition ) {
result.typeDefinition = '<value:boolean>';
}

if ( names.some( envName => this.cmd.hasEnvVar( envName ) ) ) {
if ( result.args.some( envName => this.cmd.hasEnvVar( envName ) ) ) {
throw this.error( new Error( `Environment variable already exists: ${ name }` ) );
}

const details = this.parseArgsDefinition( result.typeDefinition );

if ( details.length > 1 ) {
throw this.error( new Error( `An environment variable can only have one value but got: ${ name }` ) );
} else if ( details.length && details[ 0 ].optionalValue ) {
throw this.error( new Error( `An environment variable can not have an optional value but '${ name }' is defined as optional.` ) );
} else if ( details.length && details[ 0 ].variadic ) {
throw this.error( new Error( `An environment variable can not have an variadic value but '${ name }' is defined as variadic.` ) );
}

this.cmd.envVars.push( {
names,
names: result.args,
description,
type
type: result.typeDefinition,
details: details.shift() as IArgumentDetails
} );

return this;
Expand Down Expand Up @@ -518,7 +525,7 @@ export class BaseCommand {
*/
protected splitArguments( args: string ) {

const parts = args.trim().split( /[, ] */g );
const parts = args.trim().split( /[, =] */g );
const typeParts = [];

while ( parts[ parts.length - 1 ] && parts[ parts.length - 1 ].match( /^[<\[].+[\]>]$/ ) ) {
Expand Down
3 changes: 2 additions & 1 deletion packages/command/lib/types.ts
Expand Up @@ -59,7 +59,8 @@ export interface IOption extends IFlagOptions {
export interface IEnvVariable {
names: string[];
description: string;
type: OptionType;
type: string;
details: IArgumentDetails;
}

/**
Expand Down

0 comments on commit a1a3364

Please sign in to comment.