Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow passing commands directly when creating a CLI #43

Open
MartinJohns opened this issue Mar 23, 2018 · 3 comments
Open

Allow passing commands directly when creating a CLI #43

MartinJohns opened this issue Mar 23, 2018 · 3 comments

Comments

@MartinJohns
Copy link

Hey ho,

I like your library, but something I'd like over the file-structured based hierarchy is if I could define the architecture myself when creating an instance of CLI. This would also be a workaround for #41 as there's no need for a pre-compiled file.

I'd propose an overload for CLI or a new type like InMemoryCLI that accepts the following data structure as the second argument:

interface CommandMap { readonly [commandName: string]: CommandClass | CommandMap; }

To give an example, here's your structure from the "subcommands" section in the README:

command

command foo
command foo biu
command foo yo

command bar
command bar bia
command bar pia

//

- commands
  - default.ts
  - foo.ts
  - foo
    - biu.ts
    - yo.ts
  - bar
    - default.ts
    - bia.ts
    - pia.ts

And with my proposed functionality:

import { DefaultCommand } from './commands/default';
import { FooDefaultCommand } from './commands/foo/default';
import { FooBiuCommand } from './commands/foo/biu';
import { FooYoCommand } from './commands/foo/yo';
import { BarDefaultCommand } from './commands/bar/default';
import { BarBiaCommand } from './commands/bar/bia';
import { BarPiaCommand } from './commands/bar/pia';

const cli = new CLI(
    'name',
    {
        default: DefaultCommand,
        foo: {
            default: FooDefaultCommand,
            biu: FooBiuCommand,
            yo: FooYoCommand
        },
        bar: {
            default: BarDefaultCommand,
            bia: BarBiaCommand,
            pia: BarPiaCommand
        }
    });

Or alternatively, you could create an index.ts in ever sub-commands folder, e.g. for commands/foo/index.ts:

import { DefaultCommand } from './default';
import { BiuCommand } from './biu';
import { YoCommand } from './yo';

export default {
    default: DefaultCommand,
    biu: BiuCommand,
    yo: YoCommand
};

And when importing:

import FooCommands from './commands/foo';

new CLI('name', { foo: FooCommands });
@vilicvane
Copy link
Owner

Hi, thanks for this suggestion. There is some performance concern over large command bundle, but I think there would be easy fixes like dynamic import.

@dejitaiza
Copy link

Hey, I am interested to implement this feature, as I am needing it myself.
Would a PR be welcome @vilic ?

@vilicvane
Copy link
Owner

@dejitaiza Hi, absolutely yes. But please also add related tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants