Skip to content

Commit

Permalink
feat(core): allow overriding ORM config path via --config (#3924)
Browse files Browse the repository at this point in the history
Allows to override the config path via `--config` option:

```sh
$ npx mikro-orm debug --config ./my-config.ts
```

This option will be respected also when you run your app, not just when
you use the CLI.
  • Loading branch information
B4nan committed Nov 5, 2023
1 parent 0c9fb07 commit 2c929e0
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 1 deletion.
10 changes: 9 additions & 1 deletion docs/docs/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,13 +188,21 @@ For CLI to be able to access our database, we will need to create `mikro-orm.con
TypeScript is also supported, just enable `useTsNode` flag in our `package.json` file. By default, when `useTsNode` is not enabled, CLI will ignore `.ts` files, so if you want to out-out of this behaviour, enable the `alwaysAllowTs` option. This would be useful if you want to use MikroORM with [Bun](https://bun.sh), which has TypeScript support out of the box. There we can also set up array of possible paths to `mikro-orm.config` file, as well as use different file name. The `package.json` file can be located in the current working directory, or in one of its parent folders.

We can use these environment variables to override CLI settings:
You can use these environment variables to override the CLI settings:

- `MIKRO_ORM_CLI`: the path to ORM config file
- `MIKRO_ORM_CLI_USE_TS_NODE`: register ts-node
- `MIKRO_ORM_CLI_TS_CONFIG_PATH`: path to the tsconfig.json (for ts-node)
- `MIKRO_ORM_CLI_ALWAYS_ALLOW_TS`: enable `.ts` files to use without ts-node

Alternatively, you can also specify the config path via `--config` option:

```sh
$ npx mikro-orm debug --config ./my-config.ts
```

This option will be respected also when you run your app, not just when you use the CLI.

> Do not forget to install `ts-node` when enabling `useTsNode` flag.
> The `useTsNode` is used only when executing the CLI, it is not respected when running our app.
Expand Down
4 changes: 4 additions & 0 deletions packages/cli/src/CLIConfigurator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ export class CLIConfigurator {
.version(version)
.usage('Usage: $0 <command> [options]')
.example('$0 schema:update --run', 'Runs schema synchronization')
.option('config', {
type: 'string',
desc: `Set path to the ORM configuration file`,
})
.alias('v', 'version')
.alias('h', 'help')
.command(new ClearCacheCommand())
Expand Down
6 changes: 6 additions & 0 deletions packages/core/src/utils/ConfigurationLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ export class ConfigurationLoader {
}

static async getConfigPaths(): Promise<string[]> {
const options = Utils.parseArgs();

if (options.config) {
return [options.config];
}

const paths: string[] = [];
const settings = await ConfigurationLoader.getSettings();

Expand Down
21 changes: 21 additions & 0 deletions packages/core/src/utils/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1167,4 +1167,25 @@ export class Utils {
return str.replace(/^(?:\.\.\/|\.\/)+/, '/');
}

/**
* simple process.argv parser, supports only properties with long names, prefixed with `--`
*/
static parseArgs<T extends Dictionary = Dictionary>(): T {
let lastKey: string | undefined;

return process.argv.slice(2).reduce((args, arg) => {
if (arg.includes('=')) {
const [key, value] = arg.split('=');
args[key.substring(2)] = value;
} else if (lastKey) {
args[lastKey] = arg;
lastKey = undefined;
} else if (arg.startsWith('--')) {
lastKey = arg.substring(2);
}

return args;
}, {} as Dictionary) as T;
}

}
7 changes: 7 additions & 0 deletions tests/features/cli/CLIHelper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,13 @@ Maybe you want to check, or regenerate your yarn.lock or package-lock.json file?
});

test('getConfigPaths', async () => {
(global as any).process.argv = ['node', 'start.js', '--config', './override1/orm-config.ts'];
await expect(CLIHelper.getConfigPaths()).resolves.toEqual(['./override1/orm-config.ts']);
(global as any).process.argv = ['node', 'start.js', '--config=./override2/orm-config.ts'];
await expect(CLIHelper.getConfigPaths()).resolves.toEqual(['./override2/orm-config.ts']);
(global as any).process.argv = ['npx', 'mikro-orm', 'debug', '--config', './override3/orm-config.ts'];
await expect(CLIHelper.getConfigPaths()).resolves.toEqual(['./override3/orm-config.ts']);
(global as any).process.argv = ['node', 'start.js'];
(global as any).process.env.MIKRO_ORM_CLI = './override/orm-config.ts';
await expect(CLIHelper.getConfigPaths()).resolves.toEqual(['./override/orm-config.ts', './src/mikro-orm.config.ts', './mikro-orm.config.ts', './src/mikro-orm.config.js', './mikro-orm.config.js']);
delete (global as any).process.env.MIKRO_ORM_CLI;
Expand Down

0 comments on commit 2c929e0

Please sign in to comment.