-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
RelayCompilerBin.js
345 lines (320 loc) · 8.58 KB
/
RelayCompilerBin.js
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/**
* Copyright (c) 2013-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
* @providesModule RelayCompilerBin
* @format
*/
'use strict';
require('babel-polyfill');
const {
CodegenRunner,
ConsoleReporter,
WatchmanClient,
DotGraphQLParser,
} = require('graphql-compiler');
const RelayJSModuleParser = require('../core/RelayJSModuleParser');
const RelayFileWriter = require('../codegen/RelayFileWriter');
const RelayIRTransforms = require('../core/RelayIRTransforms');
const formatGeneratedModule = require('../codegen/formatGeneratedModule');
const fs = require('fs');
const path = require('path');
const yargs = require('yargs');
const {
buildASTSchema,
buildClientSchema,
parse,
printSchema,
} = require('graphql');
const {
commonTransforms,
codegenTransforms,
fragmentTransforms,
printTransforms,
queryTransforms,
schemaExtensions,
} = RelayIRTransforms;
import type {GetWriterOptions} from 'graphql-compiler';
import type {GraphQLSchema} from 'graphql';
function buildWatchExpression(options: {
extensions: Array<string>,
include: Array<string>,
exclude: Array<string>,
}) {
return [
'allof',
['type', 'f'],
['anyof', ...options.extensions.map(ext => ['suffix', ext])],
[
'anyof',
...options.include.map(include => ['match', include, 'wholename']),
],
...options.exclude.map(exclude => ['not', ['match', exclude, 'wholename']]),
];
}
function getFilepathsFromGlob(
baseDir,
options: {
extensions: Array<string>,
include: Array<string>,
exclude: Array<string>,
},
): Array<string> {
const {extensions, include, exclude} = options;
const patterns = include.map(inc => `${inc}/*.+(${extensions.join('|')})`);
const glob = require('fast-glob');
return glob.sync(patterns, {
cwd: baseDir,
ignore: exclude,
});
}
async function run(options: {
schema: string,
src: string,
extensions: Array<string>,
include: Array<string>,
exclude: Array<string>,
verbose: boolean,
watchman: boolean,
watch?: ?boolean,
validate: boolean,
quiet: boolean,
}) {
const schemaPath = path.resolve(process.cwd(), options.schema);
if (!fs.existsSync(schemaPath)) {
throw new Error(`--schema path does not exist: ${schemaPath}.`);
}
const srcDir = path.resolve(process.cwd(), options.src);
if (!fs.existsSync(srcDir)) {
throw new Error(`--source path does not exist: ${srcDir}.`);
}
if (options.watch && !options.watchman) {
throw new Error('Watchman is required to watch for changes.');
}
if (options.watch && !hasWatchmanRootFile(srcDir)) {
throw new Error(
`
--watch requires that the src directory have a valid watchman "root" file.
Root files can include:
- A .git/ Git folder
- A .hg/ Mercurial folder
- A .watchmanconfig file
Ensure that one such file exists in ${srcDir} or its parents.
`.trim(),
);
}
if (options.verbose && options.quiet) {
throw new Error("I can't be quiet and verbose at the same time");
}
const reporter = new ConsoleReporter({
verbose: options.verbose,
quiet: options.quiet,
});
const useWatchman = options.watchman && (await WatchmanClient.isAvailable());
const schema = getSchema(schemaPath);
const parserConfigs = {
js: {
baseDir: srcDir,
getFileFilter: RelayJSModuleParser.getFileFilter,
getParser: RelayJSModuleParser.getParser,
getSchema: () => schema,
watchmanExpression: useWatchman ? buildWatchExpression(options) : null,
filepaths: useWatchman ? null : getFilepathsFromGlob(srcDir, options),
},
graphql: {
baseDir: srcDir,
getParser: DotGraphQLParser.getParser,
getSchema: () => schema,
watchmanExpression: useWatchman
? buildWatchExpression({
extensions: ['graphql'],
include: options.include,
exclude: options.exclude,
})
: null,
filepaths: useWatchman
? null
: getFilepathsFromGlob(srcDir, {
extensions: ['graphql'],
include: options.include,
exclude: options.exclude,
}),
},
};
const writerConfigs = {
js: {
getWriter: getRelayFileWriter(srcDir),
isGeneratedFile: (filePath: string) =>
filePath.endsWith('.js') && filePath.includes('__generated__'),
parser: 'js',
baseParsers: ['graphql'],
},
};
const codegenRunner = new CodegenRunner({
reporter,
parserConfigs,
writerConfigs,
onlyValidate: options.validate,
// TODO: allow passing in a flag or detect?
sourceControl: null,
});
if (!options.validate && !options.watch && options.watchman) {
// eslint-disable-next-line no-console
console.log('HINT: pass --watch to keep watching for changes.');
}
const result = options.watch
? await codegenRunner.watchAll()
: await codegenRunner.compileAll();
if (result === 'ERROR') {
process.exit(100);
}
if (options.validate && result !== 'NO_CHANGES') {
process.exit(101);
}
}
function getRelayFileWriter(baseDir: string) {
return ({
onlyValidate,
schema,
documents,
baseDocuments,
sourceControl,
reporter,
}: GetWriterOptions) =>
new RelayFileWriter({
config: {
baseDir,
compilerTransforms: {
commonTransforms,
codegenTransforms,
fragmentTransforms,
printTransforms,
queryTransforms,
},
customScalars: {},
formatModule: formatGeneratedModule,
inputFieldWhiteListForFlow: [],
schemaExtensions,
useHaste: false,
},
onlyValidate,
schema,
baseDocuments,
documents,
reporter,
sourceControl,
});
}
function getSchema(schemaPath: string): GraphQLSchema {
try {
let source = fs.readFileSync(schemaPath, 'utf8');
if (path.extname(schemaPath) === '.json') {
source = printSchema(buildClientSchema(JSON.parse(source).data));
}
source = `
directive @include(if: Boolean) on FRAGMENT_SPREAD | FIELD
directive @skip(if: Boolean) on FRAGMENT_SPREAD | FIELD
${source}
`;
return buildASTSchema(parse(source), {assumeValid: true});
} catch (error) {
throw new Error(
`
Error loading schema. Expected the schema to be a .graphql or a .json
file, describing your GraphQL server's API. Error detail:
${error.stack}
`.trim(),
);
}
}
// Ensure that a watchman "root" file exists in the given directory
// or a parent so that it can be watched
const WATCHMAN_ROOT_FILES = ['.git', '.hg', '.watchmanconfig'];
function hasWatchmanRootFile(testPath) {
while (path.dirname(testPath) !== testPath) {
if (
WATCHMAN_ROOT_FILES.some(file => {
return fs.existsSync(path.join(testPath, file));
})
) {
return true;
}
testPath = path.dirname(testPath);
}
return false;
}
// Collect args
const argv = yargs
.usage(
'Create Relay generated files\n\n' +
'$0 --schema <path> --src <path> [--watch]',
)
.options({
schema: {
describe: 'Path to schema.graphql or schema.json',
demandOption: true,
type: 'string',
},
src: {
describe: 'Root directory of application code',
demandOption: true,
type: 'string',
},
include: {
array: true,
default: ['**'],
describe: 'Directories to include under src',
type: 'string',
},
exclude: {
array: true,
default: [
'**/node_modules/**',
'**/__mocks__/**',
'**/__tests__/**',
'**/__generated__/**',
],
describe: 'Directories to ignore under src',
type: 'string',
},
extensions: {
array: true,
default: ['js'],
describe: 'File extensions to compile (--extensions js jsx)',
type: 'string',
},
verbose: {
describe: 'More verbose logging',
type: 'boolean',
},
quiet: {
describe: 'No output to stdout',
type: 'boolean',
},
watchman: {
describe: 'Use watchman when not in watch mode',
type: 'boolean',
default: true,
},
watch: {
describe: 'If specified, watches files and regenerates on changes',
type: 'boolean',
},
validate: {
describe:
'Looks for pending changes and exits with non-zero code instead of ' +
'writing to disk',
type: 'boolean',
default: false,
},
})
.help().argv;
// Run script with args
// $FlowFixMe: Invalid types for yargs. Please fix this when touching this code.
run(argv).catch(error => {
console.error(String(error.stack || error));
process.exit(1);
});