fragment-ts 1.1.7 → 1.1.9
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/dist/cli/commands/init.command.js +1 -1
- package/dist/cli/commands/migrate.command.d.ts +1 -24
- package/dist/cli/commands/migrate.command.d.ts.map +1 -1
- package/dist/cli/commands/migrate.command.js +126 -220
- package/dist/cli/commands/migrate.command.js.map +1 -1
- package/dist/cli/index.js +3 -4
- package/dist/cli/index.js.map +1 -1
- package/dist/shared/config.utils.d.ts +0 -1
- package/dist/shared/config.utils.d.ts.map +1 -1
- package/dist/shared/config.utils.js +7 -55
- package/dist/shared/config.utils.js.map +1 -1
- package/dist/shared/runtime.resolver.d.ts +26 -0
- package/dist/shared/runtime.resolver.d.ts.map +1 -0
- package/dist/shared/runtime.resolver.js +178 -0
- package/dist/shared/runtime.resolver.js.map +1 -0
- package/dist/shared/tsconfig.utils.d.ts +6 -117
- package/dist/shared/tsconfig.utils.d.ts.map +1 -1
- package/dist/shared/tsconfig.utils.js +31 -249
- package/dist/shared/tsconfig.utils.js.map +1 -1
- package/package.json +1 -1
- package/src/cli/commands/init.command.ts +1 -1
- package/src/cli/commands/migrate.command.ts +176 -275
- package/src/cli/index.ts +8 -12
- package/src/shared/config.utils.ts +10 -72
- package/src/shared/env.utils.ts +1 -1
- package/src/shared/runtime.resolver.ts +175 -0
- package/src/shared/tsconfig.utils.ts +38 -325
|
@@ -1,30 +1,6 @@
|
|
|
1
1
|
import { Command } from "commander";
|
|
2
2
|
export declare class MigrateCommand {
|
|
3
|
-
private static tsNodeAvailable;
|
|
4
3
|
static register(program: Command): void;
|
|
5
|
-
/**
|
|
6
|
-
* Detect if we should use TypeScript or JavaScript
|
|
7
|
-
*/
|
|
8
|
-
private static setupEnvironment;
|
|
9
|
-
/**
|
|
10
|
-
* Use TypeScript only if:
|
|
11
|
-
* - NODE_ENV !== 'production'
|
|
12
|
-
* - tsconfig.json exists with decorators enabled
|
|
13
|
-
* - ts-node is available
|
|
14
|
-
* - TypeScript files exist
|
|
15
|
-
*/
|
|
16
|
-
private static shouldUseTypeScript;
|
|
17
|
-
private static isTsNodeAvailable;
|
|
18
|
-
private static registerTsNodeIfNeeded;
|
|
19
|
-
/**
|
|
20
|
-
* Get entity and migration paths based on current mode
|
|
21
|
-
* Uses fragment.json config when available, otherwise infers from tsconfig
|
|
22
|
-
*/
|
|
23
|
-
private static getPaths;
|
|
24
|
-
/**
|
|
25
|
-
* Extract the actual directory path from a glob pattern for file creation
|
|
26
|
-
*/
|
|
27
|
-
private static extractMigrationDirectory;
|
|
28
4
|
private static generateMigration;
|
|
29
5
|
private static runMigrations;
|
|
30
6
|
private static createMigration;
|
|
@@ -36,6 +12,7 @@ export declare class MigrateCommand {
|
|
|
36
12
|
private static runSeeds;
|
|
37
13
|
private static createSeed;
|
|
38
14
|
private static verifyEntities;
|
|
15
|
+
private static extractMigrationDirectory;
|
|
39
16
|
private static buildQueryStatement;
|
|
40
17
|
private static buildMigrationTemplate;
|
|
41
18
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"migrate.command.d.ts","sourceRoot":"","sources":["../../../src/cli/commands/migrate.command.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;
|
|
1
|
+
{"version":3,"file":"migrate.command.d.ts","sourceRoot":"","sources":["../../../src/cli/commands/migrate.command.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAUpC,qBAAa,cAAc;IACzB,MAAM,CAAC,QAAQ,CAAC,OAAO,EAAE,OAAO,GAAG,IAAI;mBAgFlB,iBAAiB;mBA2GjB,aAAa;mBAyCb,eAAe;mBAqCf,eAAe;mBAyCf,iBAAiB;mBA0CjB,UAAU;mBAqEV,UAAU;mBAiCV,UAAU;mBAiCV,QAAQ;mBA+CR,UAAU;IAgC/B,OAAO,CAAC,MAAM,CAAC,cAAc;IAY7B,OAAO,CAAC,MAAM,CAAC,yBAAyB;IAcxC,OAAO,CAAC,MAAM,CAAC,mBAAmB;IAoBlC,OAAO,CAAC,MAAM,CAAC,sBAAsB;CA0BtC"}
|
|
@@ -43,9 +43,7 @@ const chalk_1 = __importDefault(require("chalk"));
|
|
|
43
43
|
const ora_1 = __importDefault(require("ora"));
|
|
44
44
|
const typeorm_module_1 = require("../../typeorm/typeorm-module");
|
|
45
45
|
const config_utils_1 = require("../../shared/config.utils");
|
|
46
|
-
const
|
|
47
|
-
const tsconfig_utils_1 = require("../../shared/tsconfig.utils");
|
|
48
|
-
const glob_1 = require("glob");
|
|
46
|
+
const runtime_resolver_1 = require("../../shared/runtime.resolver");
|
|
49
47
|
class MigrateCommand {
|
|
50
48
|
static register(program) {
|
|
51
49
|
program
|
|
@@ -116,164 +114,37 @@ class MigrateCommand {
|
|
|
116
114
|
await this.createSeed(name);
|
|
117
115
|
});
|
|
118
116
|
}
|
|
119
|
-
/**
|
|
120
|
-
* Detect if we should use TypeScript or JavaScript
|
|
121
|
-
*/
|
|
122
|
-
static setupEnvironment() {
|
|
123
|
-
return this.shouldUseTypeScript();
|
|
124
|
-
}
|
|
125
|
-
/**
|
|
126
|
-
* Use TypeScript only if:
|
|
127
|
-
* - NODE_ENV !== 'production'
|
|
128
|
-
* - tsconfig.json exists with decorators enabled
|
|
129
|
-
* - ts-node is available
|
|
130
|
-
* - TypeScript files exist
|
|
131
|
-
*/
|
|
132
|
-
static shouldUseTypeScript() {
|
|
133
|
-
// Never use TS in production
|
|
134
|
-
if (env_utils_1.EnvUtils.getEnvironmentMode() === "production") {
|
|
135
|
-
console.warn("TypeScript is not supported in production mode.");
|
|
136
|
-
return false;
|
|
137
|
-
}
|
|
138
|
-
if (!tsconfig_utils_1.TsConfigUtils.exists()) {
|
|
139
|
-
console.warn("TypeScript configuration file not found.");
|
|
140
|
-
return false;
|
|
141
|
-
}
|
|
142
|
-
if (!tsconfig_utils_1.TsConfigUtils.hasDecoratorSupport()) {
|
|
143
|
-
console.warn("TypeScript configuration file does not support decorators.");
|
|
144
|
-
return false;
|
|
145
|
-
}
|
|
146
|
-
if (!this.isTsNodeAvailable()) {
|
|
147
|
-
console.warn("TypeScript compiler not available.");
|
|
148
|
-
return false;
|
|
149
|
-
}
|
|
150
|
-
const includePatterns = tsconfig_utils_1.TsConfigUtils.getIncludePatterns();
|
|
151
|
-
const cwd = process.cwd();
|
|
152
|
-
for (const pattern of includePatterns) {
|
|
153
|
-
try {
|
|
154
|
-
// Normalize pattern to forward slashes for Windows compatibility
|
|
155
|
-
const normalizedPattern = pattern.replace(/\\/g, "/");
|
|
156
|
-
const files = (0, glob_1.globSync)(normalizedPattern, { cwd });
|
|
157
|
-
if (files.some((file) => file.endsWith(".ts") && !file.endsWith(".d.ts"))) {
|
|
158
|
-
console.warn("TypeScript files found in project.");
|
|
159
|
-
return true;
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
catch {
|
|
163
|
-
// Ignore glob errors
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
console.warn("No TypeScript files found in project.");
|
|
167
|
-
return false;
|
|
168
|
-
}
|
|
169
|
-
static isTsNodeAvailable() {
|
|
170
|
-
if (this.tsNodeAvailable !== null) {
|
|
171
|
-
return this.tsNodeAvailable;
|
|
172
|
-
}
|
|
173
|
-
try {
|
|
174
|
-
require.resolve("ts-node");
|
|
175
|
-
this.tsNodeAvailable = true;
|
|
176
|
-
}
|
|
177
|
-
catch {
|
|
178
|
-
this.tsNodeAvailable = false;
|
|
179
|
-
}
|
|
180
|
-
return this.tsNodeAvailable;
|
|
181
|
-
}
|
|
182
|
-
static registerTsNodeIfNeeded(useTypeScript) {
|
|
183
|
-
if (useTypeScript && this.isTsNodeAvailable()) {
|
|
184
|
-
require("ts-node").register({
|
|
185
|
-
transpileOnly: true,
|
|
186
|
-
compilerOptions: {
|
|
187
|
-
module: "commonjs",
|
|
188
|
-
},
|
|
189
|
-
});
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
/**
|
|
193
|
-
* Get entity and migration paths based on current mode
|
|
194
|
-
* Uses fragment.json config when available, otherwise infers from tsconfig
|
|
195
|
-
*/
|
|
196
|
-
static getPaths(useTypeScript) {
|
|
197
|
-
const dbConfig = config_utils_1.ConfigUtils.getDatabaseConfig();
|
|
198
|
-
// Helper to resolve extension based on mode
|
|
199
|
-
const resolveExtension = (pattern) => {
|
|
200
|
-
return useTypeScript
|
|
201
|
-
? pattern.replace(/\.js$/, ".ts")
|
|
202
|
-
: pattern.replace(/\.ts$/, ".js");
|
|
203
|
-
};
|
|
204
|
-
// If fragment.json has explicit paths, use them
|
|
205
|
-
if (dbConfig.entities || dbConfig.migrations) {
|
|
206
|
-
const entities = dbConfig.entities
|
|
207
|
-
? dbConfig.entities.map(resolveExtension)
|
|
208
|
-
: [useTypeScript ? "src/**/*.entity.ts" : "dist/**/*.entity.js"];
|
|
209
|
-
const migrations = dbConfig.migrations
|
|
210
|
-
? dbConfig.migrations.map(resolveExtension)
|
|
211
|
-
: [
|
|
212
|
-
useTypeScript
|
|
213
|
-
? "src/migrations/**/*.ts"
|
|
214
|
-
: "dist/migrations/**/*.js",
|
|
215
|
-
];
|
|
216
|
-
// Extract migration directory for file creation
|
|
217
|
-
const migrationDir = this.extractMigrationDirectory(migrations[0], useTypeScript);
|
|
218
|
-
const seedsDir = path.join(migrationDir, "..", "seeds");
|
|
219
|
-
return { entities, migrations, seeds: seedsDir };
|
|
220
|
-
}
|
|
221
|
-
// No fragment.json config - infer from tsconfig
|
|
222
|
-
const rootDir = tsconfig_utils_1.TsConfigUtils.getRootDir();
|
|
223
|
-
const outDir = tsconfig_utils_1.TsConfigUtils.getOutDir();
|
|
224
|
-
const entities = [
|
|
225
|
-
useTypeScript ? `${rootDir}/**/*.entity.ts` : `${outDir}/**/*.entity.js`,
|
|
226
|
-
];
|
|
227
|
-
const migrations = [
|
|
228
|
-
useTypeScript
|
|
229
|
-
? `${rootDir}/migrations/**/*.ts`
|
|
230
|
-
: `${outDir}/migrations/**/*.js`,
|
|
231
|
-
];
|
|
232
|
-
const seeds = useTypeScript ? `${rootDir}/seeds` : `${outDir}/seeds`;
|
|
233
|
-
return { entities, migrations, seeds };
|
|
234
|
-
}
|
|
235
|
-
/**
|
|
236
|
-
* Extract the actual directory path from a glob pattern for file creation
|
|
237
|
-
*/
|
|
238
|
-
static extractMigrationDirectory(pattern, useTypeScript) {
|
|
239
|
-
// Handle common patterns
|
|
240
|
-
if (pattern.includes("/**/*")) {
|
|
241
|
-
// Pattern like "src/migrations/**/*.ts" → "src/migrations"
|
|
242
|
-
return pattern.split("/**/*")[0];
|
|
243
|
-
}
|
|
244
|
-
if (pattern.includes("/*.")) {
|
|
245
|
-
// Pattern like "src/migrations/*.ts" → "src/migrations"
|
|
246
|
-
return pattern.split("/*")[0];
|
|
247
|
-
}
|
|
248
|
-
// Fallback: assume it's a directory path
|
|
249
|
-
if (pattern.endsWith(".ts") || pattern.endsWith(".js")) {
|
|
250
|
-
return path.dirname(pattern);
|
|
251
|
-
}
|
|
252
|
-
// Default fallback
|
|
253
|
-
return useTypeScript ? "src/migrations" : "dist/migrations";
|
|
254
|
-
}
|
|
255
117
|
static async generateMigration(nameOrPath, options) {
|
|
256
|
-
const
|
|
257
|
-
|
|
118
|
+
const mode = runtime_resolver_1.RuntimeResolver.detectRuntimeMode();
|
|
119
|
+
runtime_resolver_1.RuntimeResolver.printMode();
|
|
120
|
+
const spinner = (0, ora_1.default)(`Generating ${mode.useTypeScript ? "TypeScript" : "JavaScript"} migration...`).start();
|
|
258
121
|
let dataSource = null;
|
|
259
122
|
try {
|
|
260
|
-
|
|
261
|
-
const
|
|
262
|
-
const
|
|
123
|
+
runtime_resolver_1.RuntimeResolver.registerTypeScriptRuntime();
|
|
124
|
+
const dbConfig = config_utils_1.ConfigUtils.getDatabaseConfig();
|
|
125
|
+
const paths = runtime_resolver_1.RuntimeResolver.resolvePaths(dbConfig.entities, dbConfig.migrations);
|
|
126
|
+
spinner.info(`Entity paths: ${paths.entities.join(", ")}`);
|
|
127
|
+
spinner.start();
|
|
128
|
+
const configOverride = {
|
|
129
|
+
entities: paths.entities,
|
|
130
|
+
migrations: paths.migrations,
|
|
131
|
+
};
|
|
263
132
|
dataSource = await typeorm_module_1.TypeORMModule.initialize(configOverride);
|
|
264
133
|
if (!dataSource?.isInitialized) {
|
|
265
134
|
throw new Error("Failed to initialize DataSource");
|
|
266
135
|
}
|
|
267
136
|
this.verifyEntities(dataSource);
|
|
268
|
-
// Setup migrations directory
|
|
269
|
-
const migrationsPattern = migrations[0];
|
|
270
|
-
const migrationsDir = this.extractMigrationDirectory(migrationsPattern
|
|
137
|
+
// Setup migrations directory
|
|
138
|
+
const migrationsPattern = paths.migrations[0];
|
|
139
|
+
const migrationsDir = this.extractMigrationDirectory(migrationsPattern);
|
|
271
140
|
await fs.ensureDir(migrationsDir);
|
|
272
141
|
const existingMigrations = fs.existsSync(migrationsDir)
|
|
273
142
|
? fs
|
|
274
143
|
.readdirSync(migrationsDir)
|
|
275
|
-
.filter((f) =>
|
|
276
|
-
|
|
144
|
+
.filter((f) => (mode.useTypeScript &&
|
|
145
|
+
f.endsWith(".ts") &&
|
|
146
|
+
!f.endsWith(".d.ts")) ||
|
|
147
|
+
(!mode.useTypeScript && f.endsWith(".js")))
|
|
277
148
|
: [];
|
|
278
149
|
const isFirstMigration = existingMigrations.length === 0;
|
|
279
150
|
// Generate schema changes
|
|
@@ -294,12 +165,12 @@ class MigrateCommand {
|
|
|
294
165
|
const className = `${migrationName}${timestamp}`;
|
|
295
166
|
const upStatements = upQueries.map((q) => this.buildQueryStatement(q));
|
|
296
167
|
const downStatements = downQueries.map((q) => this.buildQueryStatement(q));
|
|
297
|
-
const migrationContent = this.buildMigrationTemplate(className, upStatements, downStatements
|
|
298
|
-
const fileExt = useTypeScript ? ".ts" : ".js";
|
|
168
|
+
const migrationContent = this.buildMigrationTemplate(className, upStatements, downStatements);
|
|
169
|
+
const fileExt = mode.useTypeScript ? ".ts" : ".js";
|
|
299
170
|
const fileName = `${timestamp}-${migrationName}${fileExt}`;
|
|
300
171
|
const filePath = path.join(migrationsDir, fileName);
|
|
301
172
|
await fs.writeFile(filePath, migrationContent, "utf-8");
|
|
302
|
-
spinner.succeed(`${useTypeScript ? "TypeScript" : "JavaScript"} migration generated: ${chalk_1.default.cyan(fileName)}`);
|
|
173
|
+
spinner.succeed(`${mode.useTypeScript ? "TypeScript" : "JavaScript"} migration generated: ${chalk_1.default.cyan(fileName)}`);
|
|
303
174
|
}
|
|
304
175
|
catch (error) {
|
|
305
176
|
spinner.fail("Migration generation failed");
|
|
@@ -316,16 +187,21 @@ class MigrateCommand {
|
|
|
316
187
|
}
|
|
317
188
|
}
|
|
318
189
|
static async runMigrations() {
|
|
319
|
-
const
|
|
320
|
-
|
|
190
|
+
const mode = runtime_resolver_1.RuntimeResolver.detectRuntimeMode();
|
|
191
|
+
runtime_resolver_1.RuntimeResolver.printMode();
|
|
192
|
+
const spinner = (0, ora_1.default)(`Running ${mode.useTypeScript ? "TypeScript" : "JavaScript"} migrations...`).start();
|
|
321
193
|
let dataSource = null;
|
|
322
194
|
try {
|
|
323
|
-
|
|
324
|
-
const
|
|
325
|
-
const
|
|
195
|
+
runtime_resolver_1.RuntimeResolver.registerTypeScriptRuntime();
|
|
196
|
+
const dbConfig = config_utils_1.ConfigUtils.getDatabaseConfig();
|
|
197
|
+
const paths = runtime_resolver_1.RuntimeResolver.resolvePaths(dbConfig.entities, dbConfig.migrations);
|
|
198
|
+
const configOverride = {
|
|
199
|
+
entities: paths.entities,
|
|
200
|
+
migrations: paths.migrations,
|
|
201
|
+
};
|
|
326
202
|
dataSource = await typeorm_module_1.TypeORMModule.initialize(configOverride);
|
|
327
203
|
await typeorm_module_1.TypeORMModule.runMigrations();
|
|
328
|
-
spinner.succeed(`${useTypeScript ? "TypeScript" : "JavaScript"} migrations completed successfully`);
|
|
204
|
+
spinner.succeed(`${mode.useTypeScript ? "TypeScript" : "JavaScript"} migrations completed successfully`);
|
|
329
205
|
}
|
|
330
206
|
catch (error) {
|
|
331
207
|
spinner.fail("Migration failed");
|
|
@@ -339,26 +215,27 @@ class MigrateCommand {
|
|
|
339
215
|
}
|
|
340
216
|
}
|
|
341
217
|
static async createMigration(name) {
|
|
342
|
-
|
|
343
|
-
const useTypeScript = true; // Force TS for creation
|
|
344
|
-
const spinner = (0, ora_1.default)("Creating TypeScript migration...").start();
|
|
218
|
+
const spinner = (0, ora_1.default)("Creating migration...").start();
|
|
345
219
|
try {
|
|
346
|
-
|
|
347
|
-
const
|
|
220
|
+
// Always create TS migrations
|
|
221
|
+
const paths = runtime_resolver_1.RuntimeResolver.resolvePaths(undefined, [
|
|
222
|
+
"src/migrations/**/*.ts",
|
|
223
|
+
]);
|
|
224
|
+
const migrationsDir = paths.migrations[0].replace("/migrations/**/*.ts", "/migrations");
|
|
225
|
+
await fs.ensureDir(migrationsDir);
|
|
348
226
|
const timestamp = Date.now();
|
|
349
227
|
const fileName = `${timestamp}-${name}.ts`;
|
|
350
228
|
const filePath = path.join(migrationsDir, fileName);
|
|
351
229
|
const content = `import { MigrationInterface, QueryRunner } from 'typeorm';
|
|
352
230
|
|
|
353
|
-
export class ${name}${timestamp} implements MigrationInterface {
|
|
354
|
-
|
|
231
|
+
export class ${name}${timestamp} implements MigrationInterface {
|
|
232
|
+
async up(queryRunner: QueryRunner): Promise<void> {}
|
|
355
233
|
|
|
356
|
-
|
|
357
|
-
}
|
|
358
|
-
`;
|
|
234
|
+
async down(queryRunner: QueryRunner): Promise<void> {}
|
|
235
|
+
}`;
|
|
359
236
|
await fs.ensureDir(path.dirname(filePath));
|
|
360
237
|
await fs.writeFile(filePath, content);
|
|
361
|
-
spinner.succeed(`
|
|
238
|
+
spinner.succeed(`Migration created: ${fileName}`);
|
|
362
239
|
}
|
|
363
240
|
catch (error) {
|
|
364
241
|
spinner.fail("Failed to create migration");
|
|
@@ -367,16 +244,21 @@ export class ${name}${timestamp} implements MigrationInterface {
|
|
|
367
244
|
}
|
|
368
245
|
}
|
|
369
246
|
static async revertMigration() {
|
|
370
|
-
const
|
|
371
|
-
|
|
247
|
+
const mode = runtime_resolver_1.RuntimeResolver.detectRuntimeMode();
|
|
248
|
+
runtime_resolver_1.RuntimeResolver.printMode();
|
|
249
|
+
const spinner = (0, ora_1.default)(`Reverting ${mode.useTypeScript ? "TypeScript" : "JavaScript"} migration...`).start();
|
|
372
250
|
let dataSource = null;
|
|
373
251
|
try {
|
|
374
|
-
|
|
375
|
-
const
|
|
376
|
-
const
|
|
252
|
+
runtime_resolver_1.RuntimeResolver.registerTypeScriptRuntime();
|
|
253
|
+
const dbConfig = config_utils_1.ConfigUtils.getDatabaseConfig();
|
|
254
|
+
const paths = runtime_resolver_1.RuntimeResolver.resolvePaths(dbConfig.entities, dbConfig.migrations);
|
|
255
|
+
const configOverride = {
|
|
256
|
+
entities: paths.entities,
|
|
257
|
+
migrations: paths.migrations,
|
|
258
|
+
};
|
|
377
259
|
dataSource = await typeorm_module_1.TypeORMModule.initialize(configOverride);
|
|
378
260
|
await typeorm_module_1.TypeORMModule.revertMigration();
|
|
379
|
-
spinner.succeed(`${useTypeScript ? "TypeScript" : "JavaScript"} migration reverted successfully`);
|
|
261
|
+
spinner.succeed(`${mode.useTypeScript ? "TypeScript" : "JavaScript"} migration reverted successfully`);
|
|
380
262
|
}
|
|
381
263
|
catch (error) {
|
|
382
264
|
spinner.fail("Revert failed");
|
|
@@ -390,17 +272,22 @@ export class ${name}${timestamp} implements MigrationInterface {
|
|
|
390
272
|
}
|
|
391
273
|
}
|
|
392
274
|
static async refreshMigrations() {
|
|
393
|
-
const
|
|
394
|
-
|
|
275
|
+
const mode = runtime_resolver_1.RuntimeResolver.detectRuntimeMode();
|
|
276
|
+
runtime_resolver_1.RuntimeResolver.printMode();
|
|
277
|
+
const spinner = (0, ora_1.default)(`Refreshing ${mode.useTypeScript ? "TypeScript" : "JavaScript"} migrations...`).start();
|
|
395
278
|
let dataSource = null;
|
|
396
279
|
try {
|
|
397
|
-
|
|
398
|
-
const
|
|
399
|
-
const
|
|
280
|
+
runtime_resolver_1.RuntimeResolver.registerTypeScriptRuntime();
|
|
281
|
+
const dbConfig = config_utils_1.ConfigUtils.getDatabaseConfig();
|
|
282
|
+
const paths = runtime_resolver_1.RuntimeResolver.resolvePaths(dbConfig.entities, dbConfig.migrations);
|
|
283
|
+
const configOverride = {
|
|
284
|
+
entities: paths.entities,
|
|
285
|
+
migrations: paths.migrations,
|
|
286
|
+
};
|
|
400
287
|
dataSource = await typeorm_module_1.TypeORMModule.initialize(configOverride);
|
|
401
288
|
await typeorm_module_1.TypeORMModule.dropSchema();
|
|
402
289
|
await typeorm_module_1.TypeORMModule.runMigrations();
|
|
403
|
-
spinner.succeed(`${useTypeScript ? "TypeScript" : "JavaScript"} migrations refreshed successfully`);
|
|
290
|
+
spinner.succeed(`${mode.useTypeScript ? "TypeScript" : "JavaScript"} migrations refreshed successfully`);
|
|
404
291
|
}
|
|
405
292
|
catch (error) {
|
|
406
293
|
spinner.fail("Refresh failed");
|
|
@@ -414,16 +301,21 @@ export class ${name}${timestamp} implements MigrationInterface {
|
|
|
414
301
|
}
|
|
415
302
|
}
|
|
416
303
|
static async showStatus() {
|
|
417
|
-
const
|
|
304
|
+
const mode = runtime_resolver_1.RuntimeResolver.detectRuntimeMode();
|
|
305
|
+
runtime_resolver_1.RuntimeResolver.printMode(true);
|
|
418
306
|
let dataSource = null;
|
|
419
307
|
try {
|
|
420
|
-
|
|
421
|
-
const
|
|
422
|
-
const
|
|
308
|
+
runtime_resolver_1.RuntimeResolver.registerTypeScriptRuntime();
|
|
309
|
+
const dbConfig = config_utils_1.ConfigUtils.getDatabaseConfig();
|
|
310
|
+
const paths = runtime_resolver_1.RuntimeResolver.resolvePaths(dbConfig.entities, dbConfig.migrations);
|
|
311
|
+
const configOverride = {
|
|
312
|
+
entities: paths.entities,
|
|
313
|
+
migrations: paths.migrations,
|
|
314
|
+
};
|
|
423
315
|
dataSource = await typeorm_module_1.TypeORMModule.initialize(configOverride);
|
|
424
316
|
const executedMigrations = await dataSource.query(`SELECT * FROM migrations ORDER BY timestamp DESC`);
|
|
425
|
-
const
|
|
426
|
-
console.log(chalk_1.default.blue(`\n📊 ${
|
|
317
|
+
const modeLabel = mode.useTypeScript ? "TypeScript" : "JavaScript";
|
|
318
|
+
console.log(chalk_1.default.blue(`\n📊 ${modeLabel} Migration Status:\n`));
|
|
427
319
|
if (executedMigrations.length === 0) {
|
|
428
320
|
console.log(chalk_1.default.yellow(" No migrations have been executed yet."));
|
|
429
321
|
}
|
|
@@ -433,21 +325,16 @@ export class ${name}${timestamp} implements MigrationInterface {
|
|
|
433
325
|
console.log(` ${chalk_1.default.cyan(m.name)} - ${new Date(m.timestamp).toLocaleString()}`);
|
|
434
326
|
});
|
|
435
327
|
}
|
|
436
|
-
// Show migration
|
|
437
|
-
const migrationsPattern = migrations[0];
|
|
438
|
-
const migrationsDir = this.extractMigrationDirectory(migrationsPattern
|
|
439
|
-
const fileExt = useTypeScript ? ".ts" : ".js";
|
|
328
|
+
// Show migration files
|
|
329
|
+
const migrationsPattern = paths.migrations[0];
|
|
330
|
+
const migrationsDir = this.extractMigrationDirectory(migrationsPattern);
|
|
331
|
+
const fileExt = mode.useTypeScript ? ".ts" : ".js";
|
|
440
332
|
if (fs.existsSync(migrationsDir)) {
|
|
441
333
|
const files = fs
|
|
442
334
|
.readdirSync(migrationsDir)
|
|
443
335
|
.filter((f) => f.endsWith(fileExt));
|
|
444
|
-
console.log(chalk_1.default.blue(`\n Available ${
|
|
336
|
+
console.log(chalk_1.default.blue(`\n Available ${modeLabel.toLowerCase()} migration files: ${files.length}`));
|
|
445
337
|
}
|
|
446
|
-
// Show detection info
|
|
447
|
-
console.log(chalk_1.default.gray(`\n Configuration:`));
|
|
448
|
-
console.log(chalk_1.default.gray(` - Environment mode: ${env_utils_1.EnvUtils.getEnvironmentMode()}`));
|
|
449
|
-
console.log(chalk_1.default.gray(` - ts-node available: ${this.isTsNodeAvailable()}`));
|
|
450
|
-
console.log(chalk_1.default.gray(` - Using: ${useTypeScript ? "TypeScript" : "JavaScript"}`));
|
|
451
338
|
}
|
|
452
339
|
catch (error) {
|
|
453
340
|
console.error(chalk_1.default.red("Failed to show migration status"));
|
|
@@ -460,13 +347,15 @@ export class ${name}${timestamp} implements MigrationInterface {
|
|
|
460
347
|
}
|
|
461
348
|
}
|
|
462
349
|
static async syncSchema() {
|
|
463
|
-
const
|
|
464
|
-
|
|
350
|
+
const mode = runtime_resolver_1.RuntimeResolver.detectRuntimeMode();
|
|
351
|
+
runtime_resolver_1.RuntimeResolver.printMode();
|
|
352
|
+
const spinner = (0, ora_1.default)(`Synchronizing ${mode.useTypeScript ? "TypeScript" : "JavaScript"} schema...`).start();
|
|
465
353
|
let dataSource = null;
|
|
466
354
|
try {
|
|
467
|
-
|
|
468
|
-
const
|
|
469
|
-
const
|
|
355
|
+
runtime_resolver_1.RuntimeResolver.registerTypeScriptRuntime();
|
|
356
|
+
const dbConfig = config_utils_1.ConfigUtils.getDatabaseConfig();
|
|
357
|
+
const paths = runtime_resolver_1.RuntimeResolver.resolvePaths(dbConfig.entities);
|
|
358
|
+
const configOverride = { entities: paths.entities };
|
|
470
359
|
dataSource = await typeorm_module_1.TypeORMModule.initialize(configOverride);
|
|
471
360
|
await typeorm_module_1.TypeORMModule.syncSchema();
|
|
472
361
|
spinner.succeed("Schema synchronized");
|
|
@@ -483,13 +372,15 @@ export class ${name}${timestamp} implements MigrationInterface {
|
|
|
483
372
|
}
|
|
484
373
|
}
|
|
485
374
|
static async dropSchema() {
|
|
486
|
-
const
|
|
487
|
-
|
|
375
|
+
const mode = runtime_resolver_1.RuntimeResolver.detectRuntimeMode();
|
|
376
|
+
runtime_resolver_1.RuntimeResolver.printMode();
|
|
377
|
+
const spinner = (0, ora_1.default)(`Dropping ${mode.useTypeScript ? "TypeScript" : "JavaScript"} schema...`).start();
|
|
488
378
|
let dataSource = null;
|
|
489
379
|
try {
|
|
490
|
-
|
|
491
|
-
const
|
|
492
|
-
const
|
|
380
|
+
runtime_resolver_1.RuntimeResolver.registerTypeScriptRuntime();
|
|
381
|
+
const dbConfig = config_utils_1.ConfigUtils.getDatabaseConfig();
|
|
382
|
+
const paths = runtime_resolver_1.RuntimeResolver.resolvePaths(dbConfig.entities);
|
|
383
|
+
const configOverride = { entities: paths.entities };
|
|
493
384
|
dataSource = await typeorm_module_1.TypeORMModule.initialize(configOverride);
|
|
494
385
|
await typeorm_module_1.TypeORMModule.dropSchema();
|
|
495
386
|
spinner.succeed("Schema dropped");
|
|
@@ -506,10 +397,14 @@ export class ${name}${timestamp} implements MigrationInterface {
|
|
|
506
397
|
}
|
|
507
398
|
}
|
|
508
399
|
static async runSeeds() {
|
|
509
|
-
const
|
|
510
|
-
|
|
400
|
+
const mode = runtime_resolver_1.RuntimeResolver.detectRuntimeMode();
|
|
401
|
+
runtime_resolver_1.RuntimeResolver.printMode();
|
|
402
|
+
const spinner = (0, ora_1.default)(`Running ${mode.useTypeScript ? "TypeScript" : "JavaScript"} seeds...`).start();
|
|
511
403
|
try {
|
|
512
|
-
|
|
404
|
+
runtime_resolver_1.RuntimeResolver.registerTypeScriptRuntime();
|
|
405
|
+
const dbConfig = config_utils_1.ConfigUtils.getDatabaseConfig();
|
|
406
|
+
const paths = runtime_resolver_1.RuntimeResolver.resolvePaths(dbConfig.entities, dbConfig.migrations);
|
|
407
|
+
const seedsDir = paths.seeds;
|
|
513
408
|
if (!fs.existsSync(seedsDir)) {
|
|
514
409
|
spinner.info(`No seeds directory found at ${seedsDir}`);
|
|
515
410
|
return;
|
|
@@ -518,10 +413,8 @@ export class ${name}${timestamp} implements MigrationInterface {
|
|
|
518
413
|
for (const file of files) {
|
|
519
414
|
const isTsFile = file.endsWith(".ts") && !file.endsWith(".d.ts");
|
|
520
415
|
const isJsFile = file.endsWith(".js");
|
|
521
|
-
if ((useTypeScript && isTsFile) ||
|
|
522
|
-
|
|
523
|
-
this.registerTsNodeIfNeeded(true);
|
|
524
|
-
}
|
|
416
|
+
if ((mode.useTypeScript && isTsFile) ||
|
|
417
|
+
(!mode.useTypeScript && isJsFile)) {
|
|
525
418
|
const seedModule = require(path.join(seedsDir, file));
|
|
526
419
|
if (seedModule.default?.run) {
|
|
527
420
|
await seedModule.default.run();
|
|
@@ -537,10 +430,11 @@ export class ${name}${timestamp} implements MigrationInterface {
|
|
|
537
430
|
}
|
|
538
431
|
}
|
|
539
432
|
static async createSeed(name) {
|
|
540
|
-
const useTypeScript = true; // Always create TS seeds
|
|
541
433
|
const spinner = (0, ora_1.default)("Creating TypeScript seed...").start();
|
|
542
434
|
try {
|
|
543
|
-
const
|
|
435
|
+
const dbConfig = config_utils_1.ConfigUtils.getDatabaseConfig();
|
|
436
|
+
const paths = runtime_resolver_1.RuntimeResolver.resolvePaths(dbConfig.entities, dbConfig.migrations);
|
|
437
|
+
const seedsDir = paths.seeds;
|
|
544
438
|
const fileName = `${name}.seed.ts`;
|
|
545
439
|
const filePath = path.join(seedsDir, fileName);
|
|
546
440
|
const content = `export default class ${name}Seed {
|
|
@@ -558,7 +452,7 @@ export class ${name}${timestamp} implements MigrationInterface {
|
|
|
558
452
|
process.exit(1);
|
|
559
453
|
}
|
|
560
454
|
}
|
|
561
|
-
// Helper methods
|
|
455
|
+
// Helper methods
|
|
562
456
|
static verifyEntities(dataSource) {
|
|
563
457
|
const entities = dataSource.entityMetadatas;
|
|
564
458
|
if (entities.length === 0) {
|
|
@@ -570,6 +464,19 @@ export class ${name}${timestamp} implements MigrationInterface {
|
|
|
570
464
|
});
|
|
571
465
|
console.log();
|
|
572
466
|
}
|
|
467
|
+
static extractMigrationDirectory(pattern) {
|
|
468
|
+
// Handle common patterns
|
|
469
|
+
if (pattern.includes("/**/*")) {
|
|
470
|
+
return pattern.split("/**/*")[0];
|
|
471
|
+
}
|
|
472
|
+
if (pattern.includes("/*.")) {
|
|
473
|
+
return pattern.split("/*")[0];
|
|
474
|
+
}
|
|
475
|
+
if (pattern.endsWith(".ts") || pattern.endsWith(".js")) {
|
|
476
|
+
return path.dirname(pattern);
|
|
477
|
+
}
|
|
478
|
+
return pattern;
|
|
479
|
+
}
|
|
573
480
|
static buildQueryStatement(query) {
|
|
574
481
|
let sql = query.query
|
|
575
482
|
.replace(/\\/g, "\\\\")
|
|
@@ -585,14 +492,14 @@ export class ${name}${timestamp} implements MigrationInterface {
|
|
|
585
492
|
}
|
|
586
493
|
return `await queryRunner.query(\`${sql}\`);`;
|
|
587
494
|
}
|
|
588
|
-
static buildMigrationTemplate(className, upStatements, downStatements
|
|
495
|
+
static buildMigrationTemplate(className, upStatements, downStatements) {
|
|
589
496
|
const upBody = upStatements.length > 0
|
|
590
497
|
? upStatements.map((s) => ` ${s}`).join("\n")
|
|
591
498
|
: " // No changes";
|
|
592
499
|
const downBody = downStatements.length > 0
|
|
593
500
|
? downStatements.map((s) => ` ${s}`).join("\n")
|
|
594
501
|
: " // No changes";
|
|
595
|
-
return `import { MigrationInterface, QueryRunner } from "
|
|
502
|
+
return `import { MigrationInterface, QueryRunner } from "typeorm";
|
|
596
503
|
|
|
597
504
|
export class ${className} implements MigrationInterface {
|
|
598
505
|
public async up(queryRunner: QueryRunner): Promise<void> {
|
|
@@ -606,5 +513,4 @@ ${downBody}
|
|
|
606
513
|
}
|
|
607
514
|
}
|
|
608
515
|
exports.MigrateCommand = MigrateCommand;
|
|
609
|
-
MigrateCommand.tsNodeAvailable = null;
|
|
610
516
|
//# sourceMappingURL=migrate.command.js.map
|