Jelajahi Sumber

fix(cli): Improve support for migrations in monorepo setups

Michael Bromley 1 tahun lalu
induk
melakukan
3fbf4e41d3

+ 23 - 1
package-lock.json

@@ -31920,7 +31920,8 @@
         "fs-extra": "^11.2.0",
         "picocolors": "^1.0.0",
         "ts-morph": "^21.0.1",
-        "ts-node": "^10.9.2"
+        "ts-node": "^10.9.2",
+        "tsconfig-paths": "^4.2.0"
       },
       "bin": {
         "vendure": "dist/cli.js"
@@ -31940,6 +31941,27 @@
         "node": ">=16"
       }
     },
+    "packages/cli/node_modules/strip-bom": {
+      "version": "3.0.0",
+      "resolved": "https://registry.npmjs.org/strip-bom/-/strip-bom-3.0.0.tgz",
+      "integrity": "sha512-vavAMRXOgBVNF6nyEEmL3DBK19iRpDcoIwW+swQ+CbGiu7lju6t+JklA1MHweoWtadgt4ISVUsXLyDq34ddcwA==",
+      "engines": {
+        "node": ">=4"
+      }
+    },
+    "packages/cli/node_modules/tsconfig-paths": {
+      "version": "4.2.0",
+      "resolved": "https://registry.npmjs.org/tsconfig-paths/-/tsconfig-paths-4.2.0.tgz",
+      "integrity": "sha512-NoZ4roiN7LnbKn9QqE1amc9DJfzvZXxF4xDavcOWt1BPkdx+m+0gJuPM+S0vCe7zTJMYUP0R8pO2XMr+Y8oLIg==",
+      "dependencies": {
+        "json5": "^2.2.2",
+        "minimist": "^1.2.6",
+        "strip-bom": "^3.0.0"
+      },
+      "engines": {
+        "node": ">=6"
+      }
+    },
     "packages/common": {
       "name": "@vendure/common",
       "version": "2.2.0",

+ 2 - 1
packages/cli/package.json

@@ -42,7 +42,8 @@
         "fs-extra": "^11.2.0",
         "picocolors": "^1.0.0",
         "ts-morph": "^21.0.1",
-        "ts-node": "^10.9.2"
+        "ts-node": "^10.9.2",
+        "tsconfig-paths": "^4.2.0"
     },
     "devDependencies": {
         "@vendure/core": "^2.2.0",

+ 15 - 3
packages/cli/src/commands/migrate/load-vendure-config-file.ts

@@ -2,15 +2,27 @@ import path from 'node:path';
 import { register } from 'ts-node';
 
 import { VendureConfigRef } from '../../shared/vendure-config-ref';
+import { selectTsConfigFile } from '../../utilities/ast-utils';
 import { isRunningInTsNode } from '../../utilities/utils';
 
 export async function loadVendureConfigFile(vendureConfig: VendureConfigRef) {
     await import('dotenv/config');
     if (!isRunningInTsNode()) {
-        const tsConfigPath = path.join(process.cwd(), 'tsconfig.json');
+        const tsConfigPath = await selectTsConfigFile();
         // eslint-disable-next-line @typescript-eslint/no-var-requires
-        const compilerOptions = require(tsConfigPath).compilerOptions;
-        register({ compilerOptions, transpileOnly: true });
+        const compilerOptions = require(path.join(process.cwd(), tsConfigPath)).compilerOptions;
+        register({
+            compilerOptions: { ...compilerOptions, moduleResolution: 'NodeNext', module: 'NodeNext' },
+            transpileOnly: true,
+        });
+        if (compilerOptions.paths) {
+            // eslint-disable-next-line @typescript-eslint/no-var-requires
+            const tsConfigPaths = require('tsconfig-paths');
+            tsConfigPaths.register({
+                baseUrl: './',
+                paths: compilerOptions.paths,
+            });
+        }
     }
     const exportedVarName = vendureConfig.getConfigObjectVariableName();
     if (!exportedVarName) {

+ 24 - 1
packages/cli/src/utilities/ast-utils.ts

@@ -1,4 +1,4 @@
-import { log } from '@clack/prompts';
+import { cancel, isCancel, log, select } from '@clack/prompts';
 import fs from 'fs-extra';
 import path from 'node:path';
 import { Directory, Node, Project, ProjectOptions, ScriptKind, SourceFile } from 'ts-morph';
@@ -6,6 +6,29 @@ import { Directory, Node, Project, ProjectOptions, ScriptKind, SourceFile } from
 import { defaultManipulationSettings } from '../constants';
 import { EntityRef } from '../shared/entity-ref';
 
+export async function selectTsConfigFile() {
+    const tsConfigFiles = fs.readdirSync(process.cwd()).filter(f => /^tsconfig.*\.json$/.test(f));
+    if (tsConfigFiles.length === 0) {
+        throw new Error('No tsconfig files found in current directory');
+    }
+    if (tsConfigFiles.length === 1) {
+        return tsConfigFiles[0];
+    }
+    const selectedConfigFile = await select({
+        message: 'Multiple tsconfig files found. Select one:',
+        options: tsConfigFiles.map(c => ({
+            value: c,
+            label: path.basename(c),
+        })),
+        maxItems: 10,
+    });
+    if (isCancel(selectedConfigFile)) {
+        cancel();
+        process.exit(0);
+    }
+    return selectedConfigFile as string;
+}
+
 export function getTsMorphProject(options: ProjectOptions = {}) {
     const tsConfigPath = path.join(process.cwd(), 'tsconfig.json');
     if (!fs.existsSync(tsConfigPath)) {