Skip to content

Commit 9f8a3d5

Browse files
fix(cli): Update codegen command to use schema file
1 parent 39fce1c commit 9f8a3d5

File tree

3 files changed

+43
-7
lines changed

3 files changed

+43
-7
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export async function addCodegen(options?: AddCodegenOptions): Promise<CliComman
3434
config: options?.config,
3535
});
3636

37-
const { plugin: resolvedPlugin, shouldPromptForSelection } = resolvePluginFromOptions(project, {
37+
const { plugin: resolvedPlugin } = resolvePluginFromOptions(project, {
3838
providedPlugin: providedVendurePlugin,
3939
pluginName: options?.pluginName,
4040
isNonInteractive: options?.isNonInteractive === true,
@@ -116,15 +116,15 @@ export async function addCodegen(options?: AddCodegenOptions): Promise<CliComman
116116
}
117117
}
118118

119-
packageJson.addScript('codegen', 'graphql-codegen --config codegen.ts');
119+
packageJson.addScriptToRootPackageJson('codegen', 'graphql-codegen --config codegen.ts');
120120

121121
configSpinner.stop('Configured codegen file');
122122

123123
await project.save();
124124

125125
const nextSteps = [
126126
`You can run codegen by doing the following:`,
127-
`1. Ensure your dev server is running`,
127+
`1. Run "npx vendure schema" to generate a schema file`,
128128
`2. Run "npm run codegen"`,
129129
];
130130
note(nextSteps.join('\n'));

packages/cli/src/commands/add/codegen/templates/codegen.template.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ import type { CodegenConfig } from '@graphql-codegen/cli';
22

33
const config: CodegenConfig = {
44
overwrite: true,
5-
// This assumes your server is running on the standard port
6-
// and with the default admin API path. Adjust accordingly.
7-
schema: 'http://localhost:3000/admin-api',
5+
// To generate this schema file, run `npx vendure schema`
6+
// whenever your schema changes, e.g. after adding custom fields
7+
// or API extensions
8+
schema: 'schema.graphql',
89
config: {
910
// This tells codegen that the `Money` scalar is a number
1011
scalars: { Money: 'number' },

packages/cli/src/shared/package-json-ref.ts

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,22 @@ export class PackageJson {
6363
return fs.readJsonSync(packageJsonPath);
6464
}
6565

66+
/**
67+
* @description
68+
* The Root package json can be different from the "vendure" package json when in a monorepo
69+
* setup.
70+
*/
71+
getRootPackageJsonContent() {
72+
const packageJsonPath = this.locateRootPackageJson();
73+
if (!packageJsonPath || !fs.existsSync(packageJsonPath)) {
74+
note(
75+
`Could not find root package.json in the current directory. Please run this command from the root of an npm project.`,
76+
);
77+
return false;
78+
}
79+
return fs.readJsonSync(packageJsonPath);
80+
}
81+
6682
determinePackageManager(): 'yarn' | 'npm' | 'pnpm' {
6783
const rootDir = this.getPackageRootDir().getPath();
6884
const yarnLockPath = path.join(rootDir, 'yarn.lock');
@@ -80,7 +96,11 @@ export class PackageJson {
8096
return 'npm';
8197
}
8298

83-
addScript(scriptName: string, script: string) {
99+
/**
100+
* Adds a script to the "vendure" package.json, which can differ from the root
101+
* package.json if in a monorepo.
102+
*/
103+
addScriptToVendurePackageJson(scriptName: string, script: string) {
84104
const packageJson = this.getPackageJsonContent();
85105
packageJson.scripts = packageJson.scripts || {};
86106
packageJson.scripts[scriptName] = script;
@@ -90,6 +110,21 @@ export class PackageJson {
90110
}
91111
}
92112

113+
/**
114+
* @description
115+
* The Root package json can be different from the "vendure" package json when in a monorepo
116+
* setup.
117+
*/
118+
addScriptToRootPackageJson(scriptName: string, script: string) {
119+
const packageJson = this.getRootPackageJsonContent();
120+
packageJson.scripts = packageJson.scripts || {};
121+
packageJson.scripts[scriptName] = script;
122+
const packageJsonPath = this.rootPackageJsonPath;
123+
if (packageJsonPath) {
124+
fs.writeJsonSync(packageJsonPath, packageJson, { spaces: 2 });
125+
}
126+
}
127+
93128
getPackageRootDir() {
94129
const rootDir = this.project.getDirectory('.');
95130
if (!rootDir) {

0 commit comments

Comments
 (0)