-
Notifications
You must be signed in to change notification settings - Fork 463
Expand file tree
/
Copy pathcodegen.ts
More file actions
234 lines (211 loc) · 8.19 KB
/
codegen.ts
File metadata and controls
234 lines (211 loc) · 8.19 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import "apollo-env";
import { flags } from "@oclif/command";
import * as path from "path";
import { Kind, DocumentNode } from "graphql";
import * as tty from "tty";
import { Gaze } from "gaze";
import URI from "vscode-uri";
import { TargetType, default as generate } from "../../generate";
import { ClientCommand } from "../../Command";
const waitForKey = async () => {
console.log("Press any key to stop.");
process.stdin.setRawMode!(true);
return new Promise(resolve =>
process.stdin.once("data", () => {
(process.stdin as any).unref();
process.stdin.setRawMode!(false);
resolve();
})
);
};
export default class Generate extends ClientCommand {
static aliases = ["codegen:generate"];
static description =
"Generate static types for GraphQL queries. Can use the published schema in Apollo Engine or a downloaded schema.";
static flags = {
...ClientCommand.flags,
watch: flags.boolean({
description: "Watch for file changes and reload codegen"
}),
// general
target: flags.string({
description:
"Type of code generator to use (swift | typescript | flow | scala)",
required: true
}),
localSchemaFile: flags.string({
description:
"Path to your local GraphQL schema file (introspection result or SDL)"
}),
addTypename: flags.boolean({
description:
"[default: true] Automatically add __typename to your queries, can be unset with --no-addTypename",
default: true,
allowNo: true
}),
passthroughCustomScalars: flags.boolean({
description: "Use your own types for custom scalars"
}),
customScalarsPrefix: flags.string({
description:
"Include a prefix when using provided types for custom scalars"
}),
mergeInFieldsFromFragmentSpreads: flags.boolean({
description: "Merge fragment fields onto its enclosing type"
}),
// swift
namespace: flags.string({
description: "The namespace to emit generated code into."
}),
operationIdsPath: flags.string({
description:
"Path to an operation id JSON map file. If specified, also stores the operation ids (hashes) as properties on operation types [currently Swift-only]"
}),
only: flags.string({
description:
"Parse all input files, but only output generated code for the specified file [Swift only]"
}),
// flow
useFlowExactObjects: flags.boolean({
description: "Use Flow exact objects for generated types [flow only]"
}),
useFlowReadOnlyTypes: flags.boolean({
description: "Use Flow read only types for generated types [flow only]"
}),
// flow / TS
outputFlat: flags.boolean({
description:
'By default, TypeScript/Flow will put each generated file in a directory next to its source file using the value of the "output" as the directory name. Set "outputFlat" to put all generated files in the directory relative to the current working directory defined by "output".'
}),
// typescript
globalTypesFile: flags.string({
description:
'By default, TypeScript will put a file named "globalTypes.ts" inside the "output" directory. Set "globalTypesFile" to specify a different path. Alternatively, set "fileExtension" to modify the extension of the file'
}),
fileExtension: flags.string({
description:
'By default, TypeScript will output ".ts" files. Set "fileExtension" to specify a different file extension'
})
};
static args = [
{
name: "output",
description: `Directory to which generated files will be written.
- For TypeScript/Flow generators, this specifies a directory relative to each source file by default.
- For TypeScript/Flow generators with the "outputFlat" flag is set, and for the Swift generator, this specifies a file or directory (absolute or relative to the current working directory) to which:
- a file will be written for each query (if "output" is a directory)
- all generated types will be written
- For all other types, this defines a file (absolute or relative to the current working directory) to which all generated types are written.`
}
];
async run() {
const {
flags: { watch }
} = this.parse(Generate);
const run = () =>
this.runTasks(({ flags, args, project }) => {
let inferredTarget: TargetType = "" as TargetType;
if (
["json", "swift", "typescript", "flow", "scala"].includes(
flags.target
)
) {
inferredTarget = flags.target as TargetType;
} else {
throw new Error(`Unsupported target: ${flags.target}`);
}
if (
!args.output &&
inferredTarget != "typescript" &&
inferredTarget != "flow"
) {
throw new Error(
"The output path must be specified in the arguments for Swift and Scala"
);
}
if (
!flags.outputFlat &&
(inferredTarget === "typescript" || inferredTarget === "flow") &&
(args.output &&
(path.isAbsolute(args.output) ||
args.output.split(path.sep).length > 1))
) {
throw new Error(
'For TypeScript and Flow generators, "output" must be empty or a single directory name, unless the "outputFlat" flag is set.'
);
}
return [
{
title: "Generating query files",
task: async (ctx, task) => {
task.title = `Generating query files with '${inferredTarget}' target`;
const schema = await project.resolveSchema({
tag: flags.tag
});
if (!schema) throw new Error("Error loading schema");
const write = () => {
const operations = Object.values(this.project.operations);
const fragments = Object.values(this.project.fragments);
if (!operations.length && !fragments.length) {
throw new Error(
"No operations or fragments found to generate code for."
);
}
const document: DocumentNode = {
kind: Kind.DOCUMENT,
definitions: [...operations, ...fragments]
};
return generate(
document,
schema,
typeof args.output === "string"
? args.output
: "__generated__",
flags.only,
inferredTarget,
flags.tagName as string,
!flags.outputFlat,
{
passthroughCustomScalars:
flags.passthroughCustomScalars ||
!!flags.customScalarsPrefix,
customScalarsPrefix: flags.customScalarsPrefix || "",
addTypename: flags.addTypename,
namespace: flags.namespace,
operationIdsPath: flags.operationIdsPath,
generateOperationIds: !!flags.operationIdsPath,
mergeInFieldsFromFragmentSpreads:
flags.mergeInFieldsFromFragmentSpreads,
useFlowExactObjects: flags.useFlowExactObjects,
useFlowReadOnlyTypes: flags.useFlowReadOnlyTypes,
globalTypesFile: flags.globalTypesFile
}
);
};
// project.validationDidFinish(write);
project.onDiagnostics(({ uri }) => {
write();
});
const writtenFiles = write();
task.title = `Generating query files with '${inferredTarget}' target - wrote ${writtenFiles} files`;
}
}
];
});
if (watch) {
await run().catch(() => {});
const watcher = new Gaze(this.project.config.client.includes);
watcher.on("all", (event, file) => {
console.log("\nChange detected, generating types...");
this.project.fileDidChange(URI.file(file).toString());
});
if (tty.isatty((process.stdin as any).fd)) {
await waitForKey();
watcher.close();
}
return;
} else {
return run();
}
}
}