-
Notifications
You must be signed in to change notification settings - Fork 2
/
codegen-gql.mjs
50 lines (44 loc) · 1.64 KB
/
codegen-gql.mjs
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
49
50
#!/usr/bin/env zx
import * as fs from 'fs';
const NM_RUN_PATH = 'node_modules/.bin/';
const SDK_FILE_PATH = './src/sdk.ts';
process.env.NODE_TLS_REJECT_UNAUTHORIZED = 0;
await $`${NM_RUN_PATH}graphql-codegen --config ./codegen.ts`;
// 生成 hooks 的额外参数
const hooksExtraParam = process.env.HOOKS_EXTRA_PARAM;
const hooksExtraParamDefaultValue = process.env.HOOKS_EXTRA_PARAM_DEFAULT_VALUE;
let sdkContent = fs
.readFileSync(SDK_FILE_PATH)
.toString()
.replace(
'import useSWR,',
`import useSWR from './useSWR';
import`
)
.replace(
`import { GraphQLClientRequestHeaders } from 'graphql-request/build/cjs/types';`,
`import { RequestConfig } from 'graphql-request/src/types';`
)
.replaceAll('GraphQLClientRequestHeaders', `RequestConfig['headers']`)
.replace(/graphql\-request\/dist\//g, 'graphql-request/src/')
.replace(
/\s*id:\sstring,\s*fieldName:\s*keyof\s*Variables,\s*fieldValue:\s*Variables\[\s*typeof\sfieldName\s*\]\s*/,
`[
id,
fieldName,
fieldValue
]: [SWRKeyInterface, keyof Variables, Variables[keyof Variables]]`
);
if (hooksExtraParam) {
sdkContent = sdkContent
.replace(
'export function getSdkWithHooks(client: GraphQLClient,',
`export function getSdkWithHooks(client: GraphQLClient, ${hooksExtraParam}${hooksExtraParamDefaultValue ? ` = '${hooksExtraParamDefaultValue}'` : ''},`
)
.replace(
'(name: string, object: V = {} as V): SWRKeyInterface => [',
`(name: string, object: V = {} as V): SWRKeyInterface => [ ${hooksExtraParam},`
);
}
fs.writeFileSync(SDK_FILE_PATH, sdkContent);
await $`${NM_RUN_PATH}prettier --write \"./**/*.ts\"`;