vilicvane / clime

⌨ The command-line interface framework for TypeScript.
252 stars 10 forks source link

Can I cast a contextExtension parameter? #39

Closed aSapien closed 6 years ago

aSapien commented 6 years ago

Hi @vilic,

Is it possible to cast a ContextExtension parameter?

Example:

  1. Defined a Class with a cast() function that takes Slack user_id and resolves a user email:

    export class SlackUserResolved implements StringCastable<SlackUserResolved> {
    constructor(public email: string){}
    
    async cast(id: string, context: CastingContext<SlackUserResolved>): Promise<SlackUserResolved> {
    const { email } = await getSlackUserProfile(id) as any;
    return new SlackUserResolved(email);
    }
    }
  2. Defined a command that expects a SlackResolvedUserCommandContext (defined below)

    
    @command({
    description: 'This command is listing stuff',
    })
    export default class extends Command {
    @metadata
    execute(context: SlackResolvedUserCommandContext) {
    return listStuffForUser(context.profile);
    }
    }

**Now, here's where I'm having trouble:**

How do I get the user to be resolved before command invocation?
I can't `await` inside the constructor. What do I do? 

2. Defined a `SlackResolvedUserCommandContext` class extending `SlackCommandContext` that I would like to have the user email resolved:
```javascript
export class SlackResolvedUserCommandContext extends SlackCommandContext {
  public profile: SlackUserResolved;
  constructor(options: ContextOptions, contextExt: SlackCommandContext) {
    super(options, contextExt);
  }
}

Appreciate the assistance, Sapien

vilicvane commented 6 years ago

@aSapien Hi, currently it's not possible to have context resolved asynchronously. I guess you have to use work around like let email = await context.getEmail(); for this purpose.

aSapien commented 6 years ago

Got it. Thanks, @vilic!

btw, I would like to offer my greatest gratitude for developing this lib. I think this may be the only CLI node library that isn't tightly coupled to a process and/or stdio.

Chapeau 🎩

vilicvane commented 6 years ago

@aSapien Thanks, it's really nice to know that you enjoy this idea!