Browse Source

fix(cli): Update codegen command to use schema file

Michael Bromley 3 months ago
parent
commit
9f8a3d5320

+ 3 - 3
packages/cli/src/commands/add/codegen/add-codegen.ts

@@ -34,7 +34,7 @@ export async function addCodegen(options?: AddCodegenOptions): Promise<CliComman
         config: options?.config,
     });
 
-    const { plugin: resolvedPlugin, shouldPromptForSelection } = resolvePluginFromOptions(project, {
+    const { plugin: resolvedPlugin } = resolvePluginFromOptions(project, {
         providedPlugin: providedVendurePlugin,
         pluginName: options?.pluginName,
         isNonInteractive: options?.isNonInteractive === true,
@@ -116,7 +116,7 @@ export async function addCodegen(options?: AddCodegenOptions): Promise<CliComman
         }
     }
 
-    packageJson.addScript('codegen', 'graphql-codegen --config codegen.ts');
+    packageJson.addScriptToRootPackageJson('codegen', 'graphql-codegen --config codegen.ts');
 
     configSpinner.stop('Configured codegen file');
 
@@ -124,7 +124,7 @@ export async function addCodegen(options?: AddCodegenOptions): Promise<CliComman
 
     const nextSteps = [
         `You can run codegen by doing the following:`,
-        `1. Ensure your dev server is running`,
+        `1. Run "npx vendure schema" to generate a schema file`,
         `2. Run "npm run codegen"`,
     ];
     note(nextSteps.join('\n'));

+ 4 - 3
packages/cli/src/commands/add/codegen/templates/codegen.template.ts

@@ -2,9 +2,10 @@ import type { CodegenConfig } from '@graphql-codegen/cli';
 
 const config: CodegenConfig = {
     overwrite: true,
-    // This assumes your server is running on the standard port
-    // and with the default admin API path. Adjust accordingly.
-    schema: 'http://localhost:3000/admin-api',
+    // To generate this schema file, run `npx vendure schema`
+    // whenever your schema changes, e.g. after adding custom fields
+    // or API extensions
+    schema: 'schema.graphql',
     config: {
         // This tells codegen that the `Money` scalar is a number
         scalars: { Money: 'number' },

+ 36 - 1
packages/cli/src/shared/package-json-ref.ts

@@ -63,6 +63,22 @@ export class PackageJson {
         return fs.readJsonSync(packageJsonPath);
     }
 
+    /**
+     * @description
+     * The Root package json can be different from the "vendure" package json when in a monorepo
+     * setup.
+     */
+    getRootPackageJsonContent() {
+        const packageJsonPath = this.locateRootPackageJson();
+        if (!packageJsonPath || !fs.existsSync(packageJsonPath)) {
+            note(
+                `Could not find root package.json in the current directory. Please run this command from the root of an npm project.`,
+            );
+            return false;
+        }
+        return fs.readJsonSync(packageJsonPath);
+    }
+
     determinePackageManager(): 'yarn' | 'npm' | 'pnpm' {
         const rootDir = this.getPackageRootDir().getPath();
         const yarnLockPath = path.join(rootDir, 'yarn.lock');
@@ -80,7 +96,11 @@ export class PackageJson {
         return 'npm';
     }
 
-    addScript(scriptName: string, script: string) {
+    /**
+     * Adds a script to the "vendure" package.json, which can differ from the root
+     * package.json if in a monorepo.
+     */
+    addScriptToVendurePackageJson(scriptName: string, script: string) {
         const packageJson = this.getPackageJsonContent();
         packageJson.scripts = packageJson.scripts || {};
         packageJson.scripts[scriptName] = script;
@@ -90,6 +110,21 @@ export class PackageJson {
         }
     }
 
+    /**
+     * @description
+     * The Root package json can be different from the "vendure" package json when in a monorepo
+     * setup.
+     */
+    addScriptToRootPackageJson(scriptName: string, script: string) {
+        const packageJson = this.getRootPackageJsonContent();
+        packageJson.scripts = packageJson.scripts || {};
+        packageJson.scripts[scriptName] = script;
+        const packageJsonPath = this.rootPackageJsonPath;
+        if (packageJsonPath) {
+            fs.writeJsonSync(packageJsonPath, packageJson, { spaces: 2 });
+        }
+    }
+
     getPackageRootDir() {
         const rootDir = this.project.getDirectory('.');
         if (!rootDir) {