-
Notifications
You must be signed in to change notification settings - Fork 0
/
codegen.ts
48 lines (37 loc) · 1.37 KB
/
codegen.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import { CodegenConfig } from "@graphql-codegen/cli";
import dotenv from "dotenv";
import fs from "fs";
import mri from "mri";
import path from "path";
import checkExistingCodegen from "./checkExistingCodegen";
const parsedArgv = mri(process.argv);
const mode = parsedArgv.mode ?? process.env.NODE_ENV ?? "development";
const isProduction = mode === "production";
const { generatesFile, codegenFileExists } = checkExistingCodegen(isProduction);
const isViteCommand = process.env.npm_lifecycle_script.includes("vite");
// if command source is not vite and codegen file exists, then we should skip codegen step because vite uses codegen plugin. That means process.exit(1) will terminate later vite command, but it is already handled inside vite.config.ts
if (parsedArgv.ignoreIfAlreadyExists && codegenFileExists && !isViteCommand) {
process.exit(0);
}
const envFiles = [".env", `.env.${mode}`];
for (const envFile of envFiles) {
const envPath = path.resolve(__dirname, envFile);
if (fs.existsSync(envPath)) {
dotenv.config({ path: envPath });
}
}
const config: CodegenConfig = {
schema: process.env.VITE_GRAPHQL_API_URL,
overwrite: true,
documents: ["src/api/graphql/*.graphql"],
generates: {
[generatesFile]: {
plugins: [
"typescript",
"typescript-operations",
"typescript-react-apollo",
],
},
},
};
export default config;