# Modular Mosaic CLI

Mosaic CLI modularity achieved by using Typescript
[Dynamic Import Expressions](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-4.html#dynamic-import-expressions),
that allows to asynchronously request a module at any arbitrary point in the
program.

CLI provides possibility to access commands added in other Mosaic libraries
through `mosaic` script. To load CLI extension Mosaic CLI provides new command

```
mosaic create-extension-config
```

This command scans current project and finds all Mosaic `@axinom/mosaic-*`
packages used in the project. Each package is checked for CLI extension, by
importing method `cliExtension` from the package. If extension for Mosaic CLI is
provided, package will be saved to configuration file `.mosaic` in project root.

```
CLI_EXTENSIONS=@axinom/mosaic-db-common
```

Mosaic CLI automatically imports all commands from packages saved in
configuration file.

## Create extension for Modular CLI

Mosaic packages can provide extension commands for Mosaic CLI. To define
extension commands should be used interface `CommandModule` from package
[`yargs`](https://github.com/yargs/yargs). When creating extension commands for
Mosaic CLI ensure, that the command name is unique across all Mosaic packages.

Extension commands should be exported through method `cliExtension`.

```
export const cliExtension = (): yargs.CommandModule<any, any>[] => [
  {
    command: 'extension-command-start',
    describe: 'Command description...',
    builder: (yargs) =>
      yargs
        .option(...)
        .example(...)
        ...etc,
    handler: commandHandler,
  },
  {
    command: 'extension-command-do',
    describe: 'Command description...',
    builder: (yargs) =>
      yargs
        .option(...)
        .example(...)
        ...etc,
    handler: commandHandler,
  },
];

```
