@mikro-orm/cli 7.0.15 → 7.0.16-dev.1
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.
- package/CLIConfigurator.d.ts +6 -7
- package/CLIConfigurator.js +51 -51
- package/CLIHelper.d.ts +45 -58
- package/CLIHelper.js +315 -324
- package/README.md +1 -1
- package/cli.js +1 -1
- package/commands/ClearCacheCommand.d.ts +6 -6
- package/commands/ClearCacheCommand.js +14 -16
- package/commands/CompileCommand.d.ts +13 -16
- package/commands/CompileCommand.js +83 -88
- package/commands/CreateDatabaseCommand.d.ts +6 -6
- package/commands/CreateDatabaseCommand.js +10 -10
- package/commands/CreateSeederCommand.d.ts +12 -12
- package/commands/CreateSeederCommand.js +27 -28
- package/commands/DatabaseSeedCommand.d.ts +8 -8
- package/commands/DatabaseSeedCommand.js +20 -20
- package/commands/DebugCommand.d.ts +7 -7
- package/commands/DebugCommand.js +71 -74
- package/commands/GenerateCacheCommand.d.ts +9 -9
- package/commands/GenerateCacheCommand.js +29 -33
- package/commands/GenerateEntitiesCommand.d.ts +14 -14
- package/commands/GenerateEntitiesCommand.js +43 -43
- package/commands/ImportCommand.d.ts +7 -7
- package/commands/ImportCommand.js +12 -12
- package/commands/MigrationCommandFactory.d.ts +53 -58
- package/commands/MigrationCommandFactory.js +191 -192
- package/commands/SchemaCommandFactory.d.ts +32 -42
- package/commands/SchemaCommandFactory.js +100 -97
- package/package.json +3 -3
package/CLIHelper.js
CHANGED
|
@@ -2,357 +2,348 @@ import { extname, join } from 'node:path';
|
|
|
2
2
|
import { createRequire } from 'node:module';
|
|
3
3
|
import { fileURLToPath, pathToFileURL } from 'node:url';
|
|
4
4
|
import yargs from 'yargs';
|
|
5
|
-
import { colors, Configuration, loadEnvironmentVars, loadOptionalDependencies, MikroORM, Utils } from '@mikro-orm/core';
|
|
5
|
+
import { colors, Configuration, loadEnvironmentVars, loadOptionalDependencies, MikroORM, Utils, } from '@mikro-orm/core';
|
|
6
6
|
import { fs } from '@mikro-orm/core/fs-utils';
|
|
7
7
|
/**
|
|
8
8
|
* @internal
|
|
9
9
|
*/
|
|
10
10
|
export class CLIHelper {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
11
|
+
/**
|
|
12
|
+
* Gets a named configuration
|
|
13
|
+
*
|
|
14
|
+
* @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.
|
|
15
|
+
* @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}.
|
|
16
|
+
* @param options Additional options to augment the final configuration with.
|
|
17
|
+
*/
|
|
18
|
+
static async getConfiguration(contextName, paths, options = {}) {
|
|
19
|
+
this.commonJSCompat(options);
|
|
20
|
+
paths ??= await this.getConfigPaths();
|
|
21
|
+
const deps = fs.getORMPackages();
|
|
22
|
+
if (!deps.has('@mikro-orm/cli') && !process.env.MIKRO_ORM_ALLOW_GLOBAL_CLI) {
|
|
23
|
+
throw new Error('@mikro-orm/cli needs to be installed as a local dependency!');
|
|
24
|
+
}
|
|
25
|
+
contextName ??= process.env.MIKRO_ORM_CONTEXT_NAME ?? 'default';
|
|
26
|
+
const env = await this.loadEnvironmentVars();
|
|
27
|
+
// oxfmt-ignore
|
|
28
|
+
const configFinder = (cfg) => {
|
|
29
29
|
return typeof cfg === 'object' && cfg !== null && ('contextName' in cfg ? cfg.contextName === contextName : contextName === 'default');
|
|
30
30
|
};
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
await loadOptionalDependencies(merged);
|
|
43
|
-
return new Configuration(merged);
|
|
44
|
-
}
|
|
45
|
-
throw new Error(`MikroORM config file not found in ['${paths.join(`', '`)}']`);
|
|
46
|
-
}
|
|
47
|
-
const path = result[0];
|
|
48
|
-
let tmp = result[1];
|
|
49
|
-
if (Array.isArray(tmp)) {
|
|
50
|
-
const tmpFirstIndex = tmp.findIndex(configFinder);
|
|
51
|
-
if (tmpFirstIndex === -1) {
|
|
52
|
-
// Static config not found. Try factory functions
|
|
53
|
-
let configCandidate;
|
|
54
|
-
for (let i = 0, l = tmp.length; i < l; ++i) {
|
|
55
|
-
const f = tmp[i];
|
|
56
|
-
if (typeof f !== 'function') {
|
|
57
|
-
continue;
|
|
58
|
-
}
|
|
59
|
-
configCandidate = await f(contextName);
|
|
60
|
-
if (!isValidConfigFactoryResult(configCandidate)) {
|
|
61
|
-
continue;
|
|
62
|
-
}
|
|
63
|
-
tmp = configCandidate;
|
|
64
|
-
break;
|
|
31
|
+
const isValidConfigFactoryResult = (cfg) => {
|
|
32
|
+
return typeof cfg === 'object' && cfg !== null && (!('contextName' in cfg) || cfg.contextName === contextName);
|
|
33
|
+
};
|
|
34
|
+
const result = await this.getConfigFile(paths);
|
|
35
|
+
if (!result[0]) {
|
|
36
|
+
if (Utils.hasObjectKeys(env)) {
|
|
37
|
+
const merged = Utils.mergeConfig({ contextName }, options.preferEnvVars ? options : env, options.preferEnvVars ? env : options);
|
|
38
|
+
await loadOptionalDependencies(merged);
|
|
39
|
+
return new Configuration(merged);
|
|
40
|
+
}
|
|
41
|
+
throw new Error(`MikroORM config file not found in ['${paths.join(`', '`)}']`);
|
|
65
42
|
}
|
|
43
|
+
const path = result[0];
|
|
44
|
+
let tmp = result[1];
|
|
66
45
|
if (Array.isArray(tmp)) {
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
46
|
+
const tmpFirstIndex = tmp.findIndex(configFinder);
|
|
47
|
+
if (tmpFirstIndex === -1) {
|
|
48
|
+
// Static config not found. Try factory functions
|
|
49
|
+
let configCandidate;
|
|
50
|
+
for (let i = 0, l = tmp.length; i < l; ++i) {
|
|
51
|
+
const f = tmp[i];
|
|
52
|
+
if (typeof f !== 'function') {
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
configCandidate = await f(contextName);
|
|
56
|
+
if (!isValidConfigFactoryResult(configCandidate)) {
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
tmp = configCandidate;
|
|
60
|
+
break;
|
|
61
|
+
}
|
|
62
|
+
if (Array.isArray(tmp)) {
|
|
63
|
+
throw new Error(`MikroORM config '${contextName}' was not found within the config file '${path}'. Either add a config with this name to the array, or add a function that when given this name will return a configuration object without a name, or with name set to this name.`);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
const tmpLastIndex = tmp.findLastIndex(configFinder);
|
|
68
|
+
if (tmpLastIndex !== tmpFirstIndex) {
|
|
69
|
+
throw new Error(`MikroORM config '${contextName}' is not unique within the array exported by '${path}' (first occurrence index: ${tmpFirstIndex}; last occurrence index: ${tmpLastIndex})`);
|
|
70
|
+
}
|
|
71
|
+
tmp = tmp[tmpFirstIndex];
|
|
72
|
+
}
|
|
77
73
|
}
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
74
|
+
else {
|
|
75
|
+
if (tmp instanceof Function) {
|
|
76
|
+
tmp = await tmp(contextName);
|
|
77
|
+
if (!isValidConfigFactoryResult(tmp)) {
|
|
78
|
+
throw new Error(`MikroORM config '${contextName}' was not what the function exported from '${path}' provided. Ensure it returns a config object with no name, or name matching the requested one.`);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
if (!configFinder(tmp)) {
|
|
83
|
+
throw new Error(`MikroORM config '${contextName}' was not what the default export from '${path}' provided.`);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
87
86
|
}
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
const esmConfigOptions = this.isESM() ? { entityGenerator: { esmImport: true } } : {};
|
|
95
|
-
await loadOptionalDependencies(tmp);
|
|
96
|
-
const preferEnvVars = options.preferEnvVars ?? tmp.preferEnvVars;
|
|
97
|
-
return new Configuration(
|
|
98
|
-
Utils.mergeConfig({}, esmConfigOptions, tmp, preferEnvVars ? options : env, preferEnvVars ? env : options),
|
|
99
|
-
);
|
|
100
|
-
}
|
|
101
|
-
static commonJSCompat(options) {
|
|
102
|
-
if (this.isESM()) {
|
|
103
|
-
return;
|
|
87
|
+
const esmConfigOptions = this.isESM() ? { entityGenerator: { esmImport: true } } : {};
|
|
88
|
+
await loadOptionalDependencies(tmp);
|
|
89
|
+
const preferEnvVars = options.preferEnvVars ?? tmp.preferEnvVars;
|
|
90
|
+
return new Configuration(Utils.mergeConfig({}, esmConfigOptions, tmp, preferEnvVars ? options : env, preferEnvVars ? env : options));
|
|
104
91
|
}
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
options.set('allowGlobalContext', true);
|
|
117
|
-
options.set('debug', !!settings.verbose);
|
|
118
|
-
options.getLogger().setDebugMode(!!settings.verbose);
|
|
119
|
-
if (settings.preferTs !== false) {
|
|
120
|
-
options.set('preferTs', true);
|
|
92
|
+
static commonJSCompat(options) {
|
|
93
|
+
if (this.isESM()) {
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
/* v8 ignore next */
|
|
97
|
+
options.dynamicImportProvider ??=
|
|
98
|
+
globalThis.dynamicImportProvider ??
|
|
99
|
+
((id) => {
|
|
100
|
+
return createRequire(process.cwd())(fileURLToPath(id));
|
|
101
|
+
});
|
|
102
|
+
globalThis.dynamicImportProvider = options.dynamicImportProvider;
|
|
121
103
|
}
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
104
|
+
static async getORM(contextName, configPaths, opts = {}) {
|
|
105
|
+
const options = await this.getConfiguration(contextName, configPaths, opts);
|
|
106
|
+
const settings = this.getSettings();
|
|
107
|
+
options.set('allowGlobalContext', true);
|
|
108
|
+
options.set('debug', !!settings.verbose);
|
|
109
|
+
options.getLogger().setDebugMode(!!settings.verbose);
|
|
110
|
+
if (settings.preferTs !== false) {
|
|
111
|
+
options.set('preferTs', true);
|
|
112
|
+
}
|
|
113
|
+
// The only times when we don't care to have a warning about no entities is also the time when we ignore entities.
|
|
114
|
+
if (opts.discovery?.warnWhenNoEntities === false) {
|
|
115
|
+
options.set('entities', []);
|
|
116
|
+
options.set('entitiesTs', []);
|
|
117
|
+
}
|
|
118
|
+
return MikroORM.init(options.getAll());
|
|
126
119
|
}
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
120
|
+
static async isDBConnected(config, reason = false) {
|
|
121
|
+
try {
|
|
122
|
+
await config.getDriver().connect();
|
|
123
|
+
const isConnected = await config.getDriver().getConnection().checkConnection();
|
|
124
|
+
await config.getDriver().close();
|
|
125
|
+
return isConnected.ok || (reason ? isConnected.reason : false);
|
|
126
|
+
}
|
|
127
|
+
catch {
|
|
128
|
+
return false;
|
|
129
|
+
}
|
|
137
130
|
}
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
131
|
+
static getDriverDependencies(config) {
|
|
132
|
+
try {
|
|
133
|
+
return config.getDriver().getDependencies();
|
|
134
|
+
}
|
|
135
|
+
catch {
|
|
136
|
+
return [];
|
|
137
|
+
}
|
|
144
138
|
}
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
139
|
+
static dump(text, config) {
|
|
140
|
+
if (config?.get('highlighter')) {
|
|
141
|
+
text = config.get('highlighter').highlight(text);
|
|
142
|
+
}
|
|
143
|
+
// eslint-disable-next-line no-console
|
|
144
|
+
console.log(text);
|
|
149
145
|
}
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
settings.preferTs = true;
|
|
146
|
+
static getSettings() {
|
|
147
|
+
const config = fs.getPackageConfig();
|
|
148
|
+
const settings = { ...config['mikro-orm'] };
|
|
149
|
+
const bool = (v) => ['true', 't', '1'].includes(v.toLowerCase());
|
|
150
|
+
settings.preferTs =
|
|
151
|
+
process.env.MIKRO_ORM_CLI_PREFER_TS != null ? bool(process.env.MIKRO_ORM_CLI_PREFER_TS) : settings.preferTs;
|
|
152
|
+
settings.tsLoader = process.env.MIKRO_ORM_CLI_TS_LOADER ?? settings.tsLoader;
|
|
153
|
+
settings.tsConfigPath = process.env.MIKRO_ORM_CLI_TS_CONFIG_PATH ?? settings.tsConfigPath;
|
|
154
|
+
settings.verbose =
|
|
155
|
+
process.env.MIKRO_ORM_CLI_VERBOSE != null ? bool(process.env.MIKRO_ORM_CLI_VERBOSE) : settings.verbose;
|
|
156
|
+
if (process.env.MIKRO_ORM_CLI_CONFIG?.endsWith('.ts')) {
|
|
157
|
+
settings.preferTs = true;
|
|
158
|
+
}
|
|
159
|
+
return settings;
|
|
165
160
|
}
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
161
|
+
static async getConfigPaths() {
|
|
162
|
+
const settings = this.getSettings();
|
|
163
|
+
const typeScriptSupport = settings.preferTs ?? Utils.detectTypeScriptSupport();
|
|
164
|
+
const paths = [];
|
|
165
|
+
if (process.env.MIKRO_ORM_CLI_CONFIG) {
|
|
166
|
+
paths.push(process.env.MIKRO_ORM_CLI_CONFIG);
|
|
167
|
+
}
|
|
168
|
+
paths.push(...(settings.configPaths || []));
|
|
169
|
+
if (typeScriptSupport) {
|
|
170
|
+
paths.push('./src/mikro-orm.config.ts');
|
|
171
|
+
paths.push('./mikro-orm.config.ts');
|
|
172
|
+
}
|
|
173
|
+
const distDir = fs.pathExists(process.cwd() + '/dist');
|
|
174
|
+
const buildDir = fs.pathExists(process.cwd() + '/build');
|
|
175
|
+
/* v8 ignore next */
|
|
176
|
+
const path = distDir ? 'dist' : buildDir ? 'build' : 'src';
|
|
177
|
+
paths.push(`./${path}/mikro-orm.config.js`);
|
|
178
|
+
paths.push('./mikro-orm.config.js');
|
|
179
|
+
/* v8 ignore next */
|
|
180
|
+
return Utils.unique(paths).filter(p => !/\.[mc]?ts$/.exec(p) || typeScriptSupport);
|
|
174
181
|
}
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
182
|
+
static async getConfigFile(paths) {
|
|
183
|
+
for (let path of paths) {
|
|
184
|
+
path = fs.absolutePath(path);
|
|
185
|
+
path = fs.normalizePath(path);
|
|
186
|
+
if (fs.pathExists(path)) {
|
|
187
|
+
const config = await fs.dynamicImport(path);
|
|
188
|
+
/* v8 ignore next */
|
|
189
|
+
return [path, await (config.default ?? config)];
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
return [];
|
|
179
193
|
}
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
/* v8 ignore next */
|
|
183
|
-
const path = distDir ? 'dist' : buildDir ? 'build' : 'src';
|
|
184
|
-
paths.push(`./${path}/mikro-orm.config.js`);
|
|
185
|
-
paths.push('./mikro-orm.config.js');
|
|
186
|
-
/* v8 ignore next */
|
|
187
|
-
return Utils.unique(paths).filter(p => !/\.[mc]?ts$/.exec(p) || typeScriptSupport);
|
|
188
|
-
}
|
|
189
|
-
static async getConfigFile(paths) {
|
|
190
|
-
for (let path of paths) {
|
|
191
|
-
path = fs.absolutePath(path);
|
|
192
|
-
path = fs.normalizePath(path);
|
|
193
|
-
if (fs.pathExists(path)) {
|
|
194
|
-
const config = await fs.dynamicImport(path);
|
|
194
|
+
static async loadEnvironmentVars() {
|
|
195
|
+
const ret = loadEnvironmentVars();
|
|
195
196
|
/* v8 ignore next */
|
|
196
|
-
|
|
197
|
-
|
|
197
|
+
switch (process.env.MIKRO_ORM_TYPE) {
|
|
198
|
+
case 'mongo':
|
|
199
|
+
ret.driver ??= await import('@mikro-orm/sqlite').then(m => m.SqliteDriver);
|
|
200
|
+
break;
|
|
201
|
+
case 'mysql':
|
|
202
|
+
ret.driver ??= await import('@mikro-orm/mysql').then(m => m.MySqlDriver);
|
|
203
|
+
break;
|
|
204
|
+
case 'mssql':
|
|
205
|
+
ret.driver ??= await import('@mikro-orm/mssql').then(m => m.MsSqlDriver);
|
|
206
|
+
break;
|
|
207
|
+
case 'mariadb':
|
|
208
|
+
ret.driver ??= await import('@mikro-orm/mariadb').then(m => m.MariaDbDriver);
|
|
209
|
+
break;
|
|
210
|
+
case 'postgresql':
|
|
211
|
+
ret.driver ??= await import('@mikro-orm/postgresql').then(m => m.PostgreSqlDriver);
|
|
212
|
+
break;
|
|
213
|
+
case 'sqlite':
|
|
214
|
+
ret.driver ??= await import('@mikro-orm/sqlite').then(m => m.SqliteDriver);
|
|
215
|
+
break;
|
|
216
|
+
case 'libsql':
|
|
217
|
+
ret.driver ??= await import('@mikro-orm/libsql').then(m => m.LibSqlDriver);
|
|
218
|
+
break;
|
|
219
|
+
case 'oracledb':
|
|
220
|
+
ret.driver ??= await import('@mikro-orm/oracledb').then(m => m.OracleDriver);
|
|
221
|
+
break;
|
|
222
|
+
}
|
|
223
|
+
return ret;
|
|
198
224
|
}
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
case 'postgresql':
|
|
218
|
-
ret.driver ??= await import('@mikro-orm/postgresql').then(m => m.PostgreSqlDriver);
|
|
219
|
-
break;
|
|
220
|
-
case 'sqlite':
|
|
221
|
-
ret.driver ??= await import('@mikro-orm/sqlite').then(m => m.SqliteDriver);
|
|
222
|
-
break;
|
|
223
|
-
case 'libsql':
|
|
224
|
-
ret.driver ??= await import('@mikro-orm/libsql').then(m => m.LibSqlDriver);
|
|
225
|
-
break;
|
|
226
|
-
case 'oracledb':
|
|
227
|
-
ret.driver ??= await import('@mikro-orm/oracledb').then(m => m.OracleDriver);
|
|
228
|
-
break;
|
|
225
|
+
static dumpDependencies() {
|
|
226
|
+
const version = Utils.getORMVersion();
|
|
227
|
+
CLIHelper.dump(' - dependencies:');
|
|
228
|
+
CLIHelper.dump(` - mikro-orm ${colors.green(version)}`);
|
|
229
|
+
CLIHelper.dump(` - node ${colors.green(process.versions.node)}`);
|
|
230
|
+
if (fs.pathExists(process.cwd() + '/package.json')) {
|
|
231
|
+
/* v8 ignore if */
|
|
232
|
+
if (process.versions.bun) {
|
|
233
|
+
CLIHelper.dump(` - typescript via bun`);
|
|
234
|
+
}
|
|
235
|
+
else {
|
|
236
|
+
CLIHelper.dump(` - typescript ${CLIHelper.getModuleVersion('typescript')}`);
|
|
237
|
+
}
|
|
238
|
+
CLIHelper.dump(' - package.json ' + colors.green('found'));
|
|
239
|
+
}
|
|
240
|
+
else {
|
|
241
|
+
CLIHelper.dump(' - package.json ' + colors.red('not found'));
|
|
242
|
+
}
|
|
229
243
|
}
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
if (process.versions.bun) {
|
|
240
|
-
CLIHelper.dump(` - typescript via bun`);
|
|
241
|
-
} else {
|
|
242
|
-
CLIHelper.dump(` - typescript ${CLIHelper.getModuleVersion('typescript')}`);
|
|
243
|
-
}
|
|
244
|
-
CLIHelper.dump(' - package.json ' + colors.green('found'));
|
|
245
|
-
} else {
|
|
246
|
-
CLIHelper.dump(' - package.json ' + colors.red('not found'));
|
|
244
|
+
static getModuleVersion(name) {
|
|
245
|
+
try {
|
|
246
|
+
const path = `${this.resolveModulePath(name)}/package.json`;
|
|
247
|
+
const pkg = fs.readJSONSync(path);
|
|
248
|
+
return colors.green(pkg.version);
|
|
249
|
+
}
|
|
250
|
+
catch {
|
|
251
|
+
return '';
|
|
252
|
+
}
|
|
247
253
|
}
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
254
|
+
/**
|
|
255
|
+
* Resolve path to a module.
|
|
256
|
+
* @param id The module to require
|
|
257
|
+
* @param [from] Location to start the node resolution
|
|
258
|
+
*/
|
|
259
|
+
static resolveModulePath(id, from = process.cwd()) {
|
|
260
|
+
if (!extname(from)) {
|
|
261
|
+
from = join(from, '__fake.js');
|
|
262
|
+
}
|
|
263
|
+
const path = fs.normalizePath(import.meta.resolve(id, pathToFileURL(from)));
|
|
264
|
+
const parts = path.split('/');
|
|
265
|
+
const idx = parts.lastIndexOf(id) + 1;
|
|
266
|
+
parts.splice(idx, parts.length - idx);
|
|
267
|
+
return parts.join('/');
|
|
256
268
|
}
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
269
|
+
static dumpTable(options) {
|
|
270
|
+
if (options.rows.length === 0) {
|
|
271
|
+
return CLIHelper.dump(options.empty);
|
|
272
|
+
}
|
|
273
|
+
const data = [options.columns, ...options.rows];
|
|
274
|
+
const lengths = options.columns.map(() => 0);
|
|
275
|
+
data.forEach(row => {
|
|
276
|
+
row.forEach((cell, idx) => {
|
|
277
|
+
lengths[idx] = Math.max(lengths[idx], cell.length + 2);
|
|
278
|
+
});
|
|
279
|
+
});
|
|
280
|
+
let ret = '';
|
|
281
|
+
ret += colors.grey('┌' + lengths.map(length => '─'.repeat(length)).join('┬') + '┐\n');
|
|
282
|
+
ret +=
|
|
283
|
+
colors.grey('│') +
|
|
284
|
+
lengths
|
|
285
|
+
.map((length, idx) => ' ' + colors.red(options.columns[idx]) + ' '.repeat(length - options.columns[idx].length - 1))
|
|
286
|
+
.join(colors.grey('│')) +
|
|
287
|
+
colors.grey('│\n');
|
|
288
|
+
ret += colors.grey('├' + lengths.map(length => '─'.repeat(length)).join('┼') + '┤\n');
|
|
289
|
+
options.rows.forEach(row => {
|
|
290
|
+
ret +=
|
|
291
|
+
colors.grey('│') +
|
|
292
|
+
lengths.map((length, idx) => ' ' + row[idx] + ' '.repeat(length - row[idx].length - 1)).join(colors.grey('│')) +
|
|
293
|
+
colors.grey('│\n');
|
|
294
|
+
});
|
|
295
|
+
ret += colors.grey('└' + lengths.map(length => '─'.repeat(length)).join('┴') + '┘');
|
|
296
|
+
CLIHelper.dump(ret);
|
|
266
297
|
}
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
298
|
+
/**
|
|
299
|
+
* Tries to register TS support in the following order: oxc, swc, tsx, jiti, tsimp
|
|
300
|
+
* Use `MIKRO_ORM_CLI_TS_LOADER` env var to set the loader explicitly.
|
|
301
|
+
* This method is used only in CLI context.
|
|
302
|
+
*/
|
|
303
|
+
static async registerTypeScriptSupport(configPath = 'tsconfig.json', tsLoader) {
|
|
304
|
+
/* v8 ignore if */
|
|
305
|
+
if (process.versions.bun) {
|
|
306
|
+
return true;
|
|
307
|
+
}
|
|
308
|
+
process.env.SWC_NODE_PROJECT ??= configPath;
|
|
309
|
+
process.env.TSIMP_PROJECT ??= configPath;
|
|
310
|
+
process.env.MIKRO_ORM_CLI_ALWAYS_ALLOW_TS ??= '1';
|
|
311
|
+
const explicitLoader = tsLoader ?? process.env.MIKRO_ORM_CLI_TS_LOADER ?? 'auto';
|
|
312
|
+
const setEsmImportProvider = () => {
|
|
313
|
+
return (globalThis.dynamicImportProvider = (id) => import(id).then(mod => mod?.default ?? mod));
|
|
314
|
+
};
|
|
315
|
+
const loaders = {
|
|
316
|
+
oxc: { esm: '@oxc-node/core/register', cjs: '@oxc-node/core/register' },
|
|
317
|
+
swc: { esm: '@swc-node/register/esm-register', cjs: '@swc-node/register' },
|
|
318
|
+
tsx: { esm: 'tsx/esm/api', cjs: 'tsx/cjs/api', cb: (tsx) => tsx.register({ tsconfig: configPath }) },
|
|
319
|
+
jiti: { cjs: 'jiti/register', cb: setEsmImportProvider },
|
|
320
|
+
tsimp: { cjs: 'tsimp/import', cb: setEsmImportProvider },
|
|
321
|
+
};
|
|
322
|
+
for (const loader of Utils.keys(loaders)) {
|
|
323
|
+
if (explicitLoader !== 'auto' && loader !== explicitLoader) {
|
|
324
|
+
continue;
|
|
325
|
+
}
|
|
326
|
+
const { esm, cjs, cb } = loaders[loader];
|
|
327
|
+
const isEsm = this.isESM();
|
|
328
|
+
const module = isEsm && esm ? esm : cjs;
|
|
329
|
+
const mod = await Utils.tryImport({ module });
|
|
330
|
+
if (mod) {
|
|
331
|
+
cb?.(mod);
|
|
332
|
+
process.env.MIKRO_ORM_CLI_TS_LOADER = loader;
|
|
333
|
+
return true;
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
// eslint-disable-next-line no-console
|
|
337
|
+
console.warn('Neither `oxc`, `swc`, `tsx`, `jiti` nor `tsimp` found in the project dependencies, support for working with TypeScript files might not work. To use `oxc`, install `@oxc-node/core`. To use `swc`, install both `@swc-node/register` and `@swc/core`.');
|
|
338
|
+
return false;
|
|
276
339
|
}
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
lengths[idx] = Math.max(lengths[idx], cell.length + 2);
|
|
282
|
-
});
|
|
283
|
-
});
|
|
284
|
-
let ret = '';
|
|
285
|
-
ret += colors.grey('┌' + lengths.map(length => '─'.repeat(length)).join('┬') + '┐\n');
|
|
286
|
-
ret +=
|
|
287
|
-
colors.grey('│') +
|
|
288
|
-
lengths
|
|
289
|
-
.map(
|
|
290
|
-
(length, idx) =>
|
|
291
|
-
' ' + colors.red(options.columns[idx]) + ' '.repeat(length - options.columns[idx].length - 1),
|
|
292
|
-
)
|
|
293
|
-
.join(colors.grey('│')) +
|
|
294
|
-
colors.grey('│\n');
|
|
295
|
-
ret += colors.grey('├' + lengths.map(length => '─'.repeat(length)).join('┼') + '┤\n');
|
|
296
|
-
options.rows.forEach(row => {
|
|
297
|
-
ret +=
|
|
298
|
-
colors.grey('│') +
|
|
299
|
-
lengths.map((length, idx) => ' ' + row[idx] + ' '.repeat(length - row[idx].length - 1)).join(colors.grey('│')) +
|
|
300
|
-
colors.grey('│\n');
|
|
301
|
-
});
|
|
302
|
-
ret += colors.grey('└' + lengths.map(length => '─'.repeat(length)).join('┴') + '┘');
|
|
303
|
-
CLIHelper.dump(ret);
|
|
304
|
-
}
|
|
305
|
-
/**
|
|
306
|
-
* Tries to register TS support in the following order: oxc, swc, tsx, jiti, tsimp
|
|
307
|
-
* Use `MIKRO_ORM_CLI_TS_LOADER` env var to set the loader explicitly.
|
|
308
|
-
* This method is used only in CLI context.
|
|
309
|
-
*/
|
|
310
|
-
static async registerTypeScriptSupport(configPath = 'tsconfig.json', tsLoader) {
|
|
311
|
-
/* v8 ignore if */
|
|
312
|
-
if (process.versions.bun) {
|
|
313
|
-
return true;
|
|
340
|
+
static isESM() {
|
|
341
|
+
const config = fs.getPackageConfig();
|
|
342
|
+
const type = config?.type ?? '';
|
|
343
|
+
return type === 'module';
|
|
314
344
|
}
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
const explicitLoader = tsLoader ?? process.env.MIKRO_ORM_CLI_TS_LOADER ?? 'auto';
|
|
319
|
-
const setEsmImportProvider = () => {
|
|
320
|
-
return (globalThis.dynamicImportProvider = id => import(id).then(mod => mod?.default ?? mod));
|
|
321
|
-
};
|
|
322
|
-
const loaders = {
|
|
323
|
-
oxc: { esm: '@oxc-node/core/register', cjs: '@oxc-node/core/register' },
|
|
324
|
-
swc: { esm: '@swc-node/register/esm-register', cjs: '@swc-node/register' },
|
|
325
|
-
tsx: { esm: 'tsx/esm/api', cjs: 'tsx/cjs/api', cb: tsx => tsx.register({ tsconfig: configPath }) },
|
|
326
|
-
jiti: { cjs: 'jiti/register', cb: setEsmImportProvider },
|
|
327
|
-
tsimp: { cjs: 'tsimp/import', cb: setEsmImportProvider },
|
|
328
|
-
};
|
|
329
|
-
for (const loader of Utils.keys(loaders)) {
|
|
330
|
-
if (explicitLoader !== 'auto' && loader !== explicitLoader) {
|
|
331
|
-
continue;
|
|
332
|
-
}
|
|
333
|
-
const { esm, cjs, cb } = loaders[loader];
|
|
334
|
-
const isEsm = this.isESM();
|
|
335
|
-
const module = isEsm && esm ? esm : cjs;
|
|
336
|
-
const mod = await Utils.tryImport({ module });
|
|
337
|
-
if (mod) {
|
|
338
|
-
cb?.(mod);
|
|
339
|
-
process.env.MIKRO_ORM_CLI_TS_LOADER = loader;
|
|
340
|
-
return true;
|
|
341
|
-
}
|
|
345
|
+
/* v8 ignore next */
|
|
346
|
+
static showHelp() {
|
|
347
|
+
yargs(process.argv.slice(2)).showHelp();
|
|
342
348
|
}
|
|
343
|
-
// eslint-disable-next-line no-console
|
|
344
|
-
console.warn(
|
|
345
|
-
'Neither `oxc`, `swc`, `tsx`, `jiti` nor `tsimp` found in the project dependencies, support for working with TypeScript files might not work. To use `oxc`, install `@oxc-node/core`. To use `swc`, install both `@swc-node/register` and `@swc/core`.',
|
|
346
|
-
);
|
|
347
|
-
return false;
|
|
348
|
-
}
|
|
349
|
-
static isESM() {
|
|
350
|
-
const config = fs.getPackageConfig();
|
|
351
|
-
const type = config?.type ?? '';
|
|
352
|
-
return type === 'module';
|
|
353
|
-
}
|
|
354
|
-
/* v8 ignore next */
|
|
355
|
-
static showHelp() {
|
|
356
|
-
yargs(process.argv.slice(2)).showHelp();
|
|
357
|
-
}
|
|
358
349
|
}
|