@mikro-orm/cli 7.0.0-dev.76 → 7.0.0-dev.78

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,5 @@
1
- import { ConfigurationLoader, Utils } from '@mikro-orm/core';
1
+ import { Utils } from '@mikro-orm/core';
2
+ import { fs } from '@mikro-orm/core/fs-utils';
2
3
  import yargs from 'yargs';
3
4
  import { ClearCacheCommand } from './commands/ClearCacheCommand.js';
4
5
  import { CreateDatabaseCommand } from './commands/CreateDatabaseCommand.js';
@@ -36,12 +37,12 @@ function createBasicConfig() {
36
37
  .strict();
37
38
  }
38
39
  export async function configure() {
39
- ConfigurationLoader.checkPackageVersion();
40
- const settings = CLIHelper.getSettings();
40
+ await fs.checkPackageVersion();
41
+ const settings = await CLIHelper.getSettings();
41
42
  const version = Utils.getORMVersion();
42
43
  if (settings.preferTs !== false) {
43
44
  const preferTs = await CLIHelper.registerTypeScriptSupport(settings.tsConfigPath, settings.tsLoader);
44
- /* v8 ignore next 3 */
45
+ /* v8 ignore next */
45
46
  if (!preferTs) {
46
47
  process.env.MIKRO_ORM_CLI_PREFER_TS ??= '0';
47
48
  }
package/CLIHelper.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { type EntityManagerType, type EntityManager, MikroORM, Configuration, type IDatabaseDriver, type Options } from '@mikro-orm/core';
1
+ import { Configuration, type EntityManager, type EntityManagerType, type IDatabaseDriver, MikroORM, type Options } from '@mikro-orm/core';
2
2
  /**
3
3
  * @internal
4
4
  */
@@ -7,18 +7,17 @@ export declare class CLIHelper {
7
7
  * Gets a named configuration
8
8
  *
9
9
  * @param contextName Load a config with the given `contextName` value. Used when config file exports array or factory function. Setting it to "default" matches also config objects without `contextName` set.
10
- * @param paths Array of possible paths for a configuration file. Files will be checked in order, and the first existing one will be used. Defaults to the output of {@link ConfigurationLoader.getConfigPaths}.
10
+ * @param paths Array of possible paths for a configuration file. Files will be checked in order, and the first existing one will be used. Defaults to the output of {@link fs.getConfigPaths}.
11
11
  * @param options Additional options to augment the final configuration with.
12
12
  */
13
13
  static getConfiguration<D extends IDatabaseDriver = IDatabaseDriver, EM extends D[typeof EntityManagerType] & EntityManager<D> = D[typeof EntityManagerType] & EntityManager<D>>(contextName?: string, paths?: string[], options?: Partial<Options<D>>): Promise<Configuration<D, EM>>;
14
14
  static getORM<D extends IDatabaseDriver = IDatabaseDriver>(contextName?: string, configPaths?: string[], opts?: Partial<Options<D>>): Promise<MikroORM<D>>;
15
15
  static isDBConnected(config: Configuration, reason?: false): Promise<boolean>;
16
16
  static isDBConnected(config: Configuration, reason: true): Promise<true | string>;
17
- static getNodeVersion(): string;
18
17
  static getDriverDependencies(config: Configuration): string[];
19
18
  static dump(text: string, config?: Configuration): void;
20
- static getSettings(): Settings;
21
- static getConfigPaths(): string[];
19
+ static getSettings(): Promise<Settings>;
20
+ static getConfigPaths(): Promise<string[]>;
22
21
  private static getConfigFile;
23
22
  private static loadEnvironmentVars;
24
23
  static dumpDependencies(): Promise<void>;
@@ -40,7 +39,7 @@ export declare class CLIHelper {
40
39
  * This method is used only in CLI context.
41
40
  */
42
41
  static registerTypeScriptSupport(configPath?: string, tsLoader?: 'swc' | 'tsx' | 'jiti' | 'tsimp' | 'auto'): Promise<boolean>;
43
- static isESM(): boolean;
42
+ static isESM(): Promise<boolean>;
44
43
  static showHelp(): void;
45
44
  }
46
45
  export interface Settings {
package/CLIHelper.js CHANGED
@@ -1,8 +1,8 @@
1
- import { readFile } from 'node:fs/promises';
2
- import { createRequire } from 'node:module';
3
- import { extname, join, resolve } from 'node:path';
1
+ import { extname, join } from 'node:path';
2
+ import { pathToFileURL } from 'node:url';
4
3
  import yargs from 'yargs';
5
- import { colors, ConfigurationLoader, MikroORM, Utils, Configuration, } from '@mikro-orm/core';
4
+ import { colors, Configuration, loadEnvironmentVars, lookupExtensions, MikroORM, Utils, } from '@mikro-orm/core';
5
+ import { fs } from '@mikro-orm/core/fs-utils';
6
6
  /**
7
7
  * @internal
8
8
  */
@@ -11,16 +11,18 @@ export class CLIHelper {
11
11
  * Gets a named configuration
12
12
  *
13
13
  * @param contextName Load a config with the given `contextName` value. Used when config file exports array or factory function. Setting it to "default" matches also config objects without `contextName` set.
14
- * @param paths Array of possible paths for a configuration file. Files will be checked in order, and the first existing one will be used. Defaults to the output of {@link ConfigurationLoader.getConfigPaths}.
14
+ * @param paths Array of possible paths for a configuration file. Files will be checked in order, and the first existing one will be used. Defaults to the output of {@link fs.getConfigPaths}.
15
15
  * @param options Additional options to augment the final configuration with.
16
16
  */
17
- static async getConfiguration(contextName, paths = this.getConfigPaths(), options = {}) {
18
- const deps = ConfigurationLoader.getORMPackages();
17
+ static async getConfiguration(contextName, paths, options = {}) {
18
+ paths ??= await this.getConfigPaths();
19
+ const deps = await fs.getORMPackages();
19
20
  if (!deps.has('@mikro-orm/cli') && !process.env.MIKRO_ORM_ALLOW_GLOBAL_CLI) {
20
21
  throw new Error('@mikro-orm/cli needs to be installed as a local dependency!');
21
22
  }
22
23
  contextName ??= process.env.MIKRO_ORM_CONTEXT_NAME ?? 'default';
23
24
  const env = await this.loadEnvironmentVars();
25
+ await lookupExtensions(options);
24
26
  const configFinder = (cfg) => {
25
27
  return typeof cfg === 'object' && cfg !== null && ('contextName' in cfg ? cfg.contextName === contextName : (contextName === 'default'));
26
28
  };
@@ -78,12 +80,12 @@ export class CLIHelper {
78
80
  }
79
81
  }
80
82
  }
81
- const esmConfigOptions = this.isESM() ? { entityGenerator: { esmImport: true } } : {};
83
+ const esmConfigOptions = await this.isESM() ? { entityGenerator: { esmImport: true } } : {};
82
84
  return new Configuration(Utils.mergeConfig({}, esmConfigOptions, tmp, options, env));
83
85
  }
84
86
  static async getORM(contextName, configPaths, opts = {}) {
85
87
  const options = await this.getConfiguration(contextName, configPaths, opts);
86
- const settings = this.getSettings();
88
+ const settings = await this.getSettings();
87
89
  options.set('allowGlobalContext', true);
88
90
  options.set('debug', !!settings.verbose);
89
91
  options.getLogger().setDebugMode(!!settings.verbose);
@@ -108,9 +110,6 @@ export class CLIHelper {
108
110
  return false;
109
111
  }
110
112
  }
111
- static getNodeVersion() {
112
- return process.versions.node;
113
- }
114
113
  static getDriverDependencies(config) {
115
114
  try {
116
115
  return config.getDriver().getDependencies();
@@ -126,8 +125,8 @@ export class CLIHelper {
126
125
  // eslint-disable-next-line no-console
127
126
  console.log(text);
128
127
  }
129
- static getSettings() {
130
- const config = ConfigurationLoader.getPackageConfig();
128
+ static async getSettings() {
129
+ const config = await fs.getPackageConfig();
131
130
  const settings = { ...config['mikro-orm'] };
132
131
  const bool = (v) => ['true', 't', '1'].includes(v.toLowerCase());
133
132
  settings.preferTs = process.env.MIKRO_ORM_CLI_PREFER_TS != null ? bool(process.env.MIKRO_ORM_CLI_PREFER_TS) : settings.preferTs;
@@ -139,8 +138,8 @@ export class CLIHelper {
139
138
  }
140
139
  return settings;
141
140
  }
142
- static getConfigPaths() {
143
- const settings = this.getSettings();
141
+ static async getConfigPaths() {
142
+ const settings = await this.getSettings();
144
143
  const typeScriptSupport = settings.preferTs ?? Utils.detectTypeScriptSupport();
145
144
  const paths = [];
146
145
  if (process.env.MIKRO_ORM_CLI_CONFIG) {
@@ -151,8 +150,8 @@ export class CLIHelper {
151
150
  paths.push('./src/mikro-orm.config.ts');
152
151
  paths.push('./mikro-orm.config.ts');
153
152
  }
154
- const distDir = Utils.pathExists(process.cwd() + '/dist');
155
- const buildDir = Utils.pathExists(process.cwd() + '/build');
153
+ const distDir = fs.pathExists(process.cwd() + '/dist');
154
+ const buildDir = fs.pathExists(process.cwd() + '/build');
156
155
  /* v8 ignore next */
157
156
  const path = distDir ? 'dist' : (buildDir ? 'build' : 'src');
158
157
  paths.push(`./${path}/mikro-orm.config.js`);
@@ -164,7 +163,7 @@ export class CLIHelper {
164
163
  for (let path of paths) {
165
164
  path = Utils.absolutePath(path);
166
165
  path = Utils.normalizePath(path);
167
- if (Utils.pathExists(path)) {
166
+ if (fs.pathExists(path)) {
168
167
  const config = await Utils.dynamicImport(path);
169
168
  /* v8 ignore next */
170
169
  return [path, await (config.default ?? config)];
@@ -173,21 +172,30 @@ export class CLIHelper {
173
172
  return [];
174
173
  }
175
174
  static async loadEnvironmentVars() {
176
- const ret = ConfigurationLoader.loadEnvironmentVars();
177
- // only to keep some sort of back compatibility with those using env vars only, to support `MIKRO_ORM_TYPE`
178
- const PLATFORMS = {
179
- mongo: { className: 'MongoDriver', module: '@mikro-orm/mongodb' },
180
- mysql: { className: 'MySqlDriver', module: '@mikro-orm/mysql' },
181
- mssql: { className: 'MsSqlDriver', module: '@mikro-orm/mssql' },
182
- mariadb: { className: 'MariaDbDriver', module: '@mikro-orm/mariadb' },
183
- postgresql: { className: 'PostgreSqlDriver', module: '@mikro-orm/postgresql' },
184
- sqlite: { className: 'SqliteDriver', module: '@mikro-orm/sqlite' },
185
- libsql: { className: 'LibSqlDriver', module: '@mikro-orm/libsql' },
186
- };
187
- if (process.env.MIKRO_ORM_TYPE) {
188
- const val = process.env.MIKRO_ORM_TYPE;
189
- const driver = await import(PLATFORMS[val].module);
190
- ret.driver = driver[PLATFORMS[val].className];
175
+ const ret = loadEnvironmentVars();
176
+ /* v8 ignore next */
177
+ switch (process.env.MIKRO_ORM_TYPE) {
178
+ case 'mongo':
179
+ ret.driver ??= await import('@mikro-orm/sqlite').then(m => m.SqliteDriver);
180
+ break;
181
+ case 'mysql':
182
+ ret.driver ??= await import('@mikro-orm/mysql').then(m => m.MySqlDriver);
183
+ break;
184
+ case 'mssql':
185
+ ret.driver ??= await import('@mikro-orm/mssql').then(m => m.MsSqlDriver);
186
+ break;
187
+ case 'mariadb':
188
+ ret.driver ??= await import('@mikro-orm/mariadb').then(m => m.MariaDbDriver);
189
+ break;
190
+ case 'postgresql':
191
+ ret.driver ??= await import('@mikro-orm/postgresql').then(m => m.PostgreSqlDriver);
192
+ break;
193
+ case 'sqlite':
194
+ ret.driver ??= await import('@mikro-orm/sqlite').then(m => m.SqliteDriver);
195
+ break;
196
+ case 'libsql':
197
+ ret.driver ??= await import('@mikro-orm/libsql').then(m => m.LibSqlDriver);
198
+ break;
191
199
  }
192
200
  return ret;
193
201
  }
@@ -195,9 +203,9 @@ export class CLIHelper {
195
203
  const version = Utils.getORMVersion();
196
204
  CLIHelper.dump(' - dependencies:');
197
205
  CLIHelper.dump(` - mikro-orm ${colors.green(version)}`);
198
- CLIHelper.dump(` - node ${colors.green(CLIHelper.getNodeVersion())}`);
199
- if (Utils.pathExists(process.cwd() + '/package.json')) {
200
- /* v8 ignore next 3 */
206
+ CLIHelper.dump(` - node ${colors.green(process.versions.node)}`);
207
+ if (fs.pathExists(process.cwd() + '/package.json')) {
208
+ /* v8 ignore if */
201
209
  if (process.versions.bun) {
202
210
  CLIHelper.dump(` - typescript via bun`);
203
211
  }
@@ -212,18 +220,12 @@ export class CLIHelper {
212
220
  }
213
221
  static async getModuleVersion(name) {
214
222
  try {
215
- const pkg = Utils.requireFrom(`${name}/package.json`);
223
+ const path = `${this.resolveModulePath(name)}/package.json`;
224
+ const pkg = fs.readJSONSync(path);
216
225
  return colors.green(pkg.version);
217
226
  }
218
227
  catch {
219
- try {
220
- const path = `${this.resolveModulePath(name)}/package.json`;
221
- const pkg = await readFile(path, { encoding: 'utf8' });
222
- return colors.green(JSON.parse(pkg).version);
223
- }
224
- catch {
225
- return '';
226
- }
228
+ return '';
227
229
  }
228
230
  }
229
231
  /**
@@ -235,7 +237,7 @@ export class CLIHelper {
235
237
  if (!extname(from)) {
236
238
  from = join(from, '__fake.js');
237
239
  }
238
- const path = Utils.normalizePath(createRequire(resolve(from)).resolve(id));
240
+ const path = Utils.normalizePath(import.meta.resolve(id, pathToFileURL(from)));
239
241
  const parts = path.split('/');
240
242
  const idx = parts.lastIndexOf(id) + 1;
241
243
  parts.splice(idx, parts.length - idx);
@@ -268,16 +270,13 @@ export class CLIHelper {
268
270
  * This method is used only in CLI context.
269
271
  */
270
272
  static async registerTypeScriptSupport(configPath = 'tsconfig.json', tsLoader) {
271
- /* v8 ignore next 3 */
273
+ /* v8 ignore if */
272
274
  if (process.versions.bun) {
273
275
  return true;
274
276
  }
275
277
  process.env.SWC_NODE_PROJECT ??= configPath;
276
278
  process.env.TSIMP_PROJECT ??= configPath;
277
279
  process.env.MIKRO_ORM_CLI_ALWAYS_ALLOW_TS ??= '1';
278
- const isEsm = this.isESM();
279
- /* v8 ignore next */
280
- const importMethod = isEsm ? 'tryImport' : 'tryRequire';
281
280
  const explicitLoader = tsLoader ?? process.env.MIKRO_ORM_CLI_TS_LOADER ?? 'auto';
282
281
  const loaders = {
283
282
  swc: { esm: '@swc-node/register/esm-register', cjs: '@swc-node/register' },
@@ -290,9 +289,10 @@ export class CLIHelper {
290
289
  continue;
291
290
  }
292
291
  const { esm, cjs, cb } = loaders[loader];
292
+ const isEsm = await this.isESM();
293
293
  /* v8 ignore next */
294
294
  const module = isEsm ? esm : cjs;
295
- const mod = await Utils[importMethod]({ module });
295
+ const mod = await Utils.tryImport({ module });
296
296
  if (mod) {
297
297
  cb?.(mod);
298
298
  process.env.MIKRO_ORM_CLI_TS_LOADER = loader;
@@ -303,12 +303,12 @@ export class CLIHelper {
303
303
  console.warn('Neither `swc`, `tsx`, `jiti` nor `tsimp` found in the project dependencies, support for working with TypeScript files might not work. To use `swc`, you need to install both `@swc-node/register` and `@swc/core`.');
304
304
  return false;
305
305
  }
306
- static isESM() {
307
- const config = ConfigurationLoader.getPackageConfig();
306
+ static async isESM() {
307
+ const config = await fs.getPackageConfig();
308
308
  const type = config?.type ?? '';
309
309
  return type === 'module';
310
310
  }
311
- /* v8 ignore next 3 */
311
+ /* v8 ignore next */
312
312
  static showHelp() {
313
313
  yargs(process.argv.slice(2)).showHelp();
314
314
  }
@@ -1,4 +1,5 @@
1
1
  import { colors, Utils } from '@mikro-orm/core';
2
+ import { fs } from '@mikro-orm/core/fs-utils';
2
3
  import { CLIHelper } from '../CLIHelper.js';
3
4
  export class DebugCommand {
4
5
  command = 'debug';
@@ -9,12 +10,12 @@ export class DebugCommand {
9
10
  async handler(args) {
10
11
  CLIHelper.dump(`Current ${colors.cyan('MikroORM')} CLI configuration`);
11
12
  await CLIHelper.dumpDependencies();
12
- const settings = CLIHelper.getSettings();
13
+ const settings = await CLIHelper.getSettings();
13
14
  if (!process.versions.bun && settings.preferTs !== false) {
14
15
  const loader = process.env.MIKRO_ORM_CLI_TS_LOADER ?? 'auto';
15
16
  CLIHelper.dump(' - TypeScript support ' + colors.green(`enabled (${loader})`));
16
17
  }
17
- const configPaths = args.config ?? CLIHelper.getConfigPaths();
18
+ const configPaths = args.config ?? await CLIHelper.getConfigPaths();
18
19
  CLIHelper.dump(' - searched config paths:');
19
20
  await DebugCommand.checkPaths(configPaths, 'yellow');
20
21
  CLIHelper.dump(` - searched for config name: ${colors.green(args.contextName)}`);
@@ -68,7 +69,7 @@ export class DebugCommand {
68
69
  for (let path of paths) {
69
70
  path = Utils.absolutePath(path, baseDir);
70
71
  path = Utils.normalizePath(path);
71
- if (Utils.pathExists(path)) {
72
+ if (fs.pathExists(path)) {
72
73
  CLIHelper.dump(` - ${path} (${colors.green('found')})`);
73
74
  }
74
75
  else {
@@ -1,4 +1,5 @@
1
- import { MetadataDiscovery, MetadataStorage, colors, FileCacheAdapter } from '@mikro-orm/core';
1
+ import { MetadataDiscovery, MetadataStorage, colors } from '@mikro-orm/core';
2
+ import { FileCacheAdapter } from '@mikro-orm/core/fs-utils';
2
3
  import { CLIHelper } from '../CLIHelper.js';
3
4
  export class GenerateCacheCommand {
4
5
  command = 'cache:generate';
@@ -148,7 +148,7 @@ export class MigrationCommandFactory {
148
148
  CLIHelper.dump(colors.green('Creating migration with following queries:'));
149
149
  CLIHelper.dump(colors.green('up:'));
150
150
  CLIHelper.dump(ret.diff.up.map(sql => ' ' + sql).join('\n'), config);
151
- /* v8 ignore next 3 */
151
+ /* v8 ignore if */
152
152
  if (config.getDriver().getPlatform().supportsDownMigrations()) {
153
153
  CLIHelper.dump(colors.green('down:'));
154
154
  CLIHelper.dump(ret.diff.down.map(sql => ' ' + sql).join('\n'), config);
@@ -84,7 +84,7 @@ export class SchemaCommandFactory {
84
84
  if (args.dump) {
85
85
  const m = `get${method.substr(0, 1).toUpperCase()}${method.substr(1)}SchemaSQL`;
86
86
  const dump = await orm.schema[m](params);
87
- /* v8 ignore next 3 */
87
+ /* v8 ignore if */
88
88
  if (dump) {
89
89
  CLIHelper.dump(dump, orm.config);
90
90
  successMessage = '';
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@mikro-orm/cli",
3
3
  "type": "module",
4
- "version": "7.0.0-dev.76",
4
+ "version": "7.0.0-dev.78",
5
5
  "description": "TypeScript ORM for Node.js based on Data Mapper, Unit of Work and Identity Map patterns. Supports MongoDB, MySQL, PostgreSQL and SQLite databases as well as usage with vanilla JavaScript.",
6
6
  "exports": {
7
7
  "./package.json": "./package.json",
@@ -53,9 +53,9 @@
53
53
  "access": "public"
54
54
  },
55
55
  "dependencies": {
56
- "@mikro-orm/core": "7.0.0-dev.76",
57
- "@mikro-orm/knex": "7.0.0-dev.76",
58
- "mikro-orm": "7.0.0-dev.76",
56
+ "@mikro-orm/core": "7.0.0-dev.78",
57
+ "@mikro-orm/knex": "7.0.0-dev.78",
58
+ "mikro-orm": "7.0.0-dev.78",
59
59
  "yargs": "17.7.2"
60
60
  },
61
61
  "devDependencies": {