Ver código fonte

fix(cli): Fix custom config path handling for migrate & schema commands

Michael Bromley 3 meses atrás
pai
commit
3bfe632129

+ 10 - 0
packages/cli/src/commands/command-declarations.ts

@@ -186,6 +186,11 @@ export const cliCommands: CliCommandDefinition[] = [
                 description: 'Output directory for generated migrations',
                 description: 'Output directory for generated migrations',
                 required: false,
                 required: false,
             },
             },
+            {
+                long: '--config <path>',
+                description: 'Specify the path to a custom Vendure config file',
+                required: false,
+            },
         ],
         ],
         action: async options => {
         action: async options => {
             const { migrateCommand } = await import('./migrate/migrate');
             const { migrateCommand } = await import('./migrate/migrate');
@@ -221,6 +226,11 @@ export const cliCommands: CliCommandDefinition[] = [
                 description: 'Output format, either SDL or JSON',
                 description: 'Output format, either SDL or JSON',
                 required: false,
                 required: false,
             },
             },
+            {
+                long: '--config <path>',
+                description: 'Specify the path to a custom Vendure config file',
+                required: false,
+            },
         ],
         ],
         action: async options => {
         action: async options => {
             const { schemaCommand } = await import('./schema/schema');
             const { schemaCommand } = await import('./schema/schema');

+ 4 - 4
packages/cli/src/commands/migrate/generate-migration/generate-migration.ts

@@ -11,16 +11,16 @@ import { withInteractiveTimeout } from '../../../utilities/utils';
 
 
 const cancelledMessage = 'Generate migration cancelled';
 const cancelledMessage = 'Generate migration cancelled';
 
 
-export const generateMigrationCommand = new CliCommand({
+export const generateMigrationCommand = new CliCommand<{ configFile?: string }>({
     id: 'generate-migration',
     id: 'generate-migration',
     category: 'Other',
     category: 'Other',
     description: 'Generate a new database migration',
     description: 'Generate a new database migration',
-    run: () => runGenerateMigration(),
+    run: options => runGenerateMigration(options?.configFile),
 });
 });
 
 
-async function runGenerateMigration(): Promise<CliCommandReturnVal> {
+async function runGenerateMigration(configFile?: string): Promise<CliCommandReturnVal> {
     const { project, tsConfigPath } = await analyzeProject({ cancelledMessage });
     const { project, tsConfigPath } = await analyzeProject({ cancelledMessage });
-    const vendureConfig = new VendureConfigRef(project);
+    const vendureConfig = new VendureConfigRef(project, configFile);
     log.info('Using VendureConfig from ' + vendureConfig.getPathRelativeToProjectRoot());
     log.info('Using VendureConfig from ' + vendureConfig.getPathRelativeToProjectRoot());
 
 
     const name = await withInteractiveTimeout(async () => {
     const name = await withInteractiveTimeout(async () => {

+ 9 - 6
packages/cli/src/commands/migrate/migrate.ts

@@ -16,6 +16,8 @@ export interface MigrateOptions {
     run?: boolean;
     run?: boolean;
     revert?: boolean;
     revert?: boolean;
     outputDir?: string;
     outputDir?: string;
+    /** Specify the path to a custom Vendure config file */
+    config?: string;
 }
 }
 
 
 /**
 /**
@@ -31,7 +33,7 @@ export async function migrateCommand(options?: MigrateOptions) {
     }
     }
 
 
     // Interactive mode (original behavior)
     // Interactive mode (original behavior)
-    await handleInteractiveMode();
+    await handleInteractiveMode(options?.config);
 }
 }
 
 
 async function handleNonInteractiveMode(options: MigrateOptions) {
 async function handleNonInteractiveMode(options: MigrateOptions) {
@@ -42,6 +44,7 @@ async function handleNonInteractiveMode(options: MigrateOptions) {
             const result = await generateMigrationOperation({
             const result = await generateMigrationOperation({
                 name: options.generate,
                 name: options.generate,
                 outputDir: options.outputDir,
                 outputDir: options.outputDir,
+                config: options.config,
             });
             });
 
 
             if (result.success) {
             if (result.success) {
@@ -51,7 +54,7 @@ async function handleNonInteractiveMode(options: MigrateOptions) {
                 process.exit(1);
                 process.exit(1);
             }
             }
         } else if (options.run) {
         } else if (options.run) {
-            const result = await runMigrationsOperation();
+            const result = await runMigrationsOperation(options.config);
 
 
             if (result.success) {
             if (result.success) {
                 log.success(result.message);
                 log.success(result.message);
@@ -60,7 +63,7 @@ async function handleNonInteractiveMode(options: MigrateOptions) {
                 process.exit(1);
                 process.exit(1);
             }
             }
         } else if (options.revert) {
         } else if (options.revert) {
-            const result = await revertMigrationOperation();
+            const result = await revertMigrationOperation(options.config);
 
 
             if (result.success) {
             if (result.success) {
                 log.success(result.message);
                 log.success(result.message);
@@ -80,7 +83,7 @@ async function handleNonInteractiveMode(options: MigrateOptions) {
     }
     }
 }
 }
 
 
-async function handleInteractiveMode() {
+async function handleInteractiveMode(configFile?: string) {
     // eslint-disable-next-line no-console
     // eslint-disable-next-line no-console
     console.log(`\n`);
     console.log(`\n`);
     intro(pc.blue('🛠️️ Vendure migrations'));
     intro(pc.blue('🛠️️ Vendure migrations'));
@@ -104,11 +107,11 @@ async function handleInteractiveMode() {
         process.env.VENDURE_RUNNING_IN_CLI = 'true';
         process.env.VENDURE_RUNNING_IN_CLI = 'true';
         if (action === 'generate') {
         if (action === 'generate') {
             const { generateMigrationCommand } = await import('./generate-migration/generate-migration');
             const { generateMigrationCommand } = await import('./generate-migration/generate-migration');
-            await generateMigrationCommand.run();
+            await generateMigrationCommand.run({ configFile });
         }
         }
         if (action === 'run') {
         if (action === 'run') {
             const { runMigrationCommand } = await import('./run-migration/run-migration');
             const { runMigrationCommand } = await import('./run-migration/run-migration');
-            await runMigrationCommand.run();
+            await runMigrationCommand.run({ configFile });
         }
         }
         if (action === 'revert') {
         if (action === 'revert') {
             const { revertMigrationCommand } = await import('./revert-migration/revert-migration');
             const { revertMigrationCommand } = await import('./revert-migration/revert-migration');

+ 6 - 5
packages/cli/src/commands/migrate/migration-operations.ts

@@ -10,6 +10,7 @@ import { VendureConfigRef } from '../../shared/vendure-config-ref';
 export interface MigrationOptions {
 export interface MigrationOptions {
     name?: string;
     name?: string;
     outputDir?: string;
     outputDir?: string;
+    config?: string;
 }
 }
 
 
 export interface MigrationResult {
 export interface MigrationResult {
@@ -24,7 +25,7 @@ export async function generateMigrationOperation(options: MigrationOptions = {})
         validateVendureProjectDirectory();
         validateVendureProjectDirectory();
 
 
         const { project, tsConfigPath } = await analyzeProject({ cancelledMessage: '' });
         const { project, tsConfigPath } = await analyzeProject({ cancelledMessage: '' });
-        const vendureConfig = new VendureConfigRef(project);
+        const vendureConfig = new VendureConfigRef(project, options.config);
         log.info('Using VendureConfig from ' + vendureConfig.getPathRelativeToProjectRoot());
         log.info('Using VendureConfig from ' + vendureConfig.getPathRelativeToProjectRoot());
 
 
         const name = options.name;
         const name = options.name;
@@ -67,12 +68,12 @@ export async function generateMigrationOperation(options: MigrationOptions = {})
     }
     }
 }
 }
 
 
-export async function runMigrationsOperation(): Promise<MigrationResult> {
+export async function runMigrationsOperation(configFile?: string): Promise<MigrationResult> {
     try {
     try {
         validateVendureProjectDirectory();
         validateVendureProjectDirectory();
 
 
         const { project } = await analyzeProject({ cancelledMessage: '' });
         const { project } = await analyzeProject({ cancelledMessage: '' });
-        const vendureConfig = new VendureConfigRef(project);
+        const vendureConfig = new VendureConfigRef(project, configFile);
         log.info('Using VendureConfig from ' + vendureConfig.getPathRelativeToProjectRoot());
         log.info('Using VendureConfig from ' + vendureConfig.getPathRelativeToProjectRoot());
         const config = await loadVendureConfigFile(vendureConfig);
         const config = await loadVendureConfigFile(vendureConfig);
 
 
@@ -96,12 +97,12 @@ export async function runMigrationsOperation(): Promise<MigrationResult> {
     }
     }
 }
 }
 
 
-export async function revertMigrationOperation(): Promise<MigrationResult> {
+export async function revertMigrationOperation(configFile?: string): Promise<MigrationResult> {
     try {
     try {
         validateVendureProjectDirectory();
         validateVendureProjectDirectory();
 
 
         const { project } = await analyzeProject({ cancelledMessage: '' });
         const { project } = await analyzeProject({ cancelledMessage: '' });
-        const vendureConfig = new VendureConfigRef(project);
+        const vendureConfig = new VendureConfigRef(project, configFile);
         log.info('Using VendureConfig from ' + vendureConfig.getPathRelativeToProjectRoot());
         log.info('Using VendureConfig from ' + vendureConfig.getPathRelativeToProjectRoot());
         const config = await loadVendureConfigFile(vendureConfig);
         const config = await loadVendureConfigFile(vendureConfig);
 
 

+ 4 - 4
packages/cli/src/commands/migrate/revert-migration/revert-migration.ts

@@ -8,16 +8,16 @@ import { VendureConfigRef } from '../../../shared/vendure-config-ref';
 
 
 const cancelledMessage = 'Revert migrations cancelled';
 const cancelledMessage = 'Revert migrations cancelled';
 
 
-export const revertMigrationCommand = new CliCommand({
+export const revertMigrationCommand = new CliCommand<{ configFile?: string }>({
     id: 'run-migration',
     id: 'run-migration',
     category: 'Other',
     category: 'Other',
     description: 'Run any pending database migrations',
     description: 'Run any pending database migrations',
-    run: () => runRevertMigration(),
+    run: options => runRevertMigration(options?.configFile),
 });
 });
 
 
-async function runRevertMigration(): Promise<CliCommandReturnVal> {
+async function runRevertMigration(configFile?: string): Promise<CliCommandReturnVal> {
     const { project } = await analyzeProject({ cancelledMessage });
     const { project } = await analyzeProject({ cancelledMessage });
-    const vendureConfig = new VendureConfigRef(project);
+    const vendureConfig = new VendureConfigRef(project, configFile);
     log.info('Using VendureConfig from ' + vendureConfig.getPathRelativeToProjectRoot());
     log.info('Using VendureConfig from ' + vendureConfig.getPathRelativeToProjectRoot());
     const config = await loadVendureConfigFile(vendureConfig);
     const config = await loadVendureConfigFile(vendureConfig);
 
 

+ 4 - 4
packages/cli/src/commands/migrate/run-migration/run-migration.ts

@@ -8,16 +8,16 @@ import { VendureConfigRef } from '../../../shared/vendure-config-ref';
 
 
 const cancelledMessage = 'Run migrations cancelled';
 const cancelledMessage = 'Run migrations cancelled';
 
 
-export const runMigrationCommand = new CliCommand({
+export const runMigrationCommand = new CliCommand<{ configFile?: string }>({
     id: 'run-migration',
     id: 'run-migration',
     category: 'Other',
     category: 'Other',
     description: 'Run any pending database migrations',
     description: 'Run any pending database migrations',
-    run: () => runRunMigration(),
+    run: options => runRunMigration(options?.configFile),
 });
 });
 
 
-async function runRunMigration(): Promise<CliCommandReturnVal> {
+async function runRunMigration(configFile?: string): Promise<CliCommandReturnVal> {
     const { project } = await analyzeProject({ cancelledMessage });
     const { project } = await analyzeProject({ cancelledMessage });
-    const vendureConfig = new VendureConfigRef(project);
+    const vendureConfig = new VendureConfigRef(project, configFile);
     log.info('Using VendureConfig from ' + vendureConfig.getPathRelativeToProjectRoot());
     log.info('Using VendureConfig from ' + vendureConfig.getPathRelativeToProjectRoot());
     const config = await loadVendureConfigFile(vendureConfig);
     const config = await loadVendureConfigFile(vendureConfig);
 
 

+ 1 - 1
packages/cli/src/commands/schema/generate-schema/generate-schema.ts

@@ -24,7 +24,7 @@ export async function generateSchema(options: SchemaOptions) {
     resetConfig();
     resetConfig();
     try {
     try {
         const { project, vendureTsConfig } = await analyzeProject({ cancelledMessage });
         const { project, vendureTsConfig } = await analyzeProject({ cancelledMessage });
-        const vendureConfig = new VendureConfigRef(project);
+        const vendureConfig = new VendureConfigRef(project, options.config);
         log.info('Using VendureConfig from ' + vendureConfig.getPathRelativeToProjectRoot());
         log.info('Using VendureConfig from ' + vendureConfig.getPathRelativeToProjectRoot());
         const config = await loadVendureConfigFile(vendureConfig, vendureTsConfig);
         const config = await loadVendureConfigFile(vendureConfig, vendureTsConfig);
         await setConfig(config);
         await setConfig(config);

+ 5 - 2
packages/cli/src/commands/schema/schema.ts

@@ -10,6 +10,8 @@ export interface SchemaOptions {
     format?: 'sdl' | 'json';
     format?: 'sdl' | 'json';
     fileName?: string;
     fileName?: string;
     outputDir?: string;
     outputDir?: string;
+    /** Specify the path to a custom Vendure config file */
+    config?: string;
 }
 }
 
 
 /**
 /**
@@ -25,7 +27,7 @@ export async function schemaCommand(options?: SchemaOptions) {
     }
     }
 
 
     // Interactive mode (original behavior)
     // Interactive mode (original behavior)
-    await handleInteractiveMode();
+    await handleInteractiveMode(options?.config);
 }
 }
 
 
 async function handleNonInteractiveMode(options: SchemaOptions) {
 async function handleNonInteractiveMode(options: SchemaOptions) {
@@ -43,7 +45,7 @@ async function handleNonInteractiveMode(options: SchemaOptions) {
     }
     }
 }
 }
 
 
-async function handleInteractiveMode() {
+async function handleInteractiveMode(configFile?: string) {
     // eslint-disable-next-line no-console
     // eslint-disable-next-line no-console
     console.log(`\n`);
     console.log(`\n`);
     intro(pc.blue('🛠️️ Generate a schema file of your GraphQL API'));
     intro(pc.blue('🛠️️ Generate a schema file of your GraphQL API'));
@@ -108,6 +110,7 @@ async function handleInteractiveMode() {
             format,
             format,
             fileName,
             fileName,
             outputDir,
             outputDir,
+            config: configFile,
         });
         });
         outro('✅ Done!');
         outro('✅ Done!');
         process.env.VENDURE_RUNNING_IN_CLI = undefined;
         process.env.VENDURE_RUNNING_IN_CLI = undefined;