relay-compiler 0.0.0-main-f0b65f0b → 0.0.0-main-e0026a71
Sign up to get free protection for your applications and to get access to all the features.
- package/cli.js +23 -0
- package/index.js +19 -3
- package/linux-x64/relay +0 -0
- package/macos-x64/relay +0 -0
- package/package.json +2 -24
- package/win-x64/relay.exe +0 -0
- package/bin/RelayCompilerBin.js.flow +0 -168
- package/bin/RelayCompilerMain.js.flow +0 -515
- package/bin/__fixtures__/plugin-module.js.flow +0 -17
- package/bin/relay-compiler +0 -19068
- package/codegen/CodegenDirectory.js.flow +0 -372
- package/codegen/CodegenRunner.js.flow +0 -424
- package/codegen/CodegenTypes.js.flow +0 -28
- package/codegen/CodegenWatcher.js.flow +0 -250
- package/codegen/NormalizationCodeGenerator.js.flow +0 -566
- package/codegen/ReaderCodeGenerator.js.flow +0 -510
- package/codegen/RelayCodeGenerator.js.flow +0 -85
- package/codegen/RelayFileWriter.js.flow +0 -365
- package/codegen/SourceControl.js.flow +0 -58
- package/codegen/compileRelayArtifacts.js.flow +0 -181
- package/codegen/createPrintRequireModuleDependency.js.flow +0 -19
- package/codegen/sortObjectByKey.js.flow +0 -25
- package/codegen/writeRelayGeneratedFile.js.flow +0 -235
- package/core/ASTCache.js.flow +0 -73
- package/core/ASTConvert.js.flow +0 -230
- package/core/CompilerContext.js.flow +0 -189
- package/core/CompilerError.js.flow +0 -255
- package/core/DotGraphQLParser.js.flow +0 -37
- package/core/GraphQLCompilerProfiler.js.flow +0 -341
- package/core/GraphQLDerivedFromMetadata.js.flow +0 -36
- package/core/GraphQLWatchmanClient.js.flow +0 -109
- package/core/IR.js.flow +0 -326
- package/core/IRPrinter.js.flow +0 -472
- package/core/IRTransformer.js.flow +0 -376
- package/core/IRValidator.js.flow +0 -259
- package/core/IRVisitor.js.flow +0 -150
- package/core/JSModuleParser.js.flow +0 -24
- package/core/RelayCompilerScope.js.flow +0 -199
- package/core/RelayFindGraphQLTags.js.flow +0 -118
- package/core/RelayGraphQLEnumsGenerator.js.flow +0 -55
- package/core/RelayIRTransforms.js.flow +0 -138
- package/core/RelayParser.js.flow +0 -1741
- package/core/RelaySourceModuleParser.js.flow +0 -133
- package/core/Schema.js.flow +0 -2035
- package/core/SchemaUtils.js.flow +0 -120
- package/core/filterContextForNode.js.flow +0 -49
- package/core/getFieldDefinition.js.flow +0 -156
- package/core/getIdentifierForArgumentValue.js.flow +0 -49
- package/core/getIdentifierForSelection.js.flow +0 -68
- package/core/getLiteralArgumentValues.js.flow +0 -32
- package/core/getNormalizationOperationName.js.flow +0 -19
- package/core/inferRootArgumentDefinitions.js.flow +0 -322
- package/index.js.flow +0 -198
- package/language/RelayLanguagePluginInterface.js.flow +0 -283
- package/language/javascript/FindGraphQLTags.js.flow +0 -136
- package/language/javascript/RelayFlowBabelFactories.js.flow +0 -176
- package/language/javascript/RelayFlowGenerator.js.flow +0 -1096
- package/language/javascript/RelayFlowTypeTransformers.js.flow +0 -181
- package/language/javascript/RelayLanguagePluginJavaScript.js.flow +0 -33
- package/language/javascript/formatGeneratedModule.js.flow +0 -65
- package/lib/bin/RelayCompilerBin.js +0 -143
- package/lib/bin/RelayCompilerMain.js +0 -488
- package/lib/bin/__fixtures__/plugin-module.js +0 -16
- package/lib/codegen/CodegenDirectory.js +0 -335
- package/lib/codegen/CodegenRunner.js +0 -433
- package/lib/codegen/CodegenTypes.js +0 -11
- package/lib/codegen/CodegenWatcher.js +0 -271
- package/lib/codegen/NormalizationCodeGenerator.js +0 -487
- package/lib/codegen/ReaderCodeGenerator.js +0 -473
- package/lib/codegen/RelayCodeGenerator.js +0 -75
- package/lib/codegen/RelayFileWriter.js +0 -270
- package/lib/codegen/SourceControl.js +0 -60
- package/lib/codegen/compileRelayArtifacts.js +0 -157
- package/lib/codegen/createPrintRequireModuleDependency.js +0 -19
- package/lib/codegen/sortObjectByKey.js +0 -41
- package/lib/codegen/writeRelayGeneratedFile.js +0 -206
- package/lib/core/ASTCache.js +0 -70
- package/lib/core/ASTConvert.js +0 -198
- package/lib/core/CompilerContext.js +0 -165
- package/lib/core/CompilerError.js +0 -252
- package/lib/core/DotGraphQLParser.js +0 -40
- package/lib/core/GraphQLCompilerProfiler.js +0 -299
- package/lib/core/GraphQLDerivedFromMetadata.js +0 -31
- package/lib/core/GraphQLWatchmanClient.js +0 -150
- package/lib/core/IR.js +0 -11
- package/lib/core/IRPrinter.js +0 -388
- package/lib/core/IRTransformer.js +0 -345
- package/lib/core/IRValidator.js +0 -226
- package/lib/core/IRVisitor.js +0 -45
- package/lib/core/JSModuleParser.js +0 -18
- package/lib/core/RelayCompilerScope.js +0 -183
- package/lib/core/RelayFindGraphQLTags.js +0 -79
- package/lib/core/RelayGraphQLEnumsGenerator.js +0 -50
- package/lib/core/RelayIRTransforms.js +0 -109
- package/lib/core/RelayParser.js +0 -1381
- package/lib/core/RelaySourceModuleParser.js +0 -104
- package/lib/core/Schema.js +0 -1877
- package/lib/core/SchemaUtils.js +0 -98
- package/lib/core/filterContextForNode.js +0 -50
- package/lib/core/getFieldDefinition.js +0 -145
- package/lib/core/getIdentifierForArgumentValue.js +0 -54
- package/lib/core/getIdentifierForSelection.js +0 -49
- package/lib/core/getLiteralArgumentValues.js +0 -26
- package/lib/core/getNormalizationOperationName.js +0 -17
- package/lib/core/inferRootArgumentDefinitions.js +0 -351
- package/lib/index.js +0 -178
- package/lib/language/RelayLanguagePluginInterface.js +0 -26
- package/lib/language/javascript/FindGraphQLTags.js +0 -126
- package/lib/language/javascript/RelayFlowBabelFactories.js +0 -160
- package/lib/language/javascript/RelayFlowGenerator.js +0 -856
- package/lib/language/javascript/RelayFlowTypeTransformers.js +0 -119
- package/lib/language/javascript/RelayLanguagePluginJavaScript.js +0 -30
- package/lib/language/javascript/formatGeneratedModule.js +0 -36
- package/lib/reporters/ConsoleReporter.js +0 -61
- package/lib/reporters/MultiReporter.js +0 -45
- package/lib/reporters/Reporter.js +0 -11
- package/lib/runner/Artifacts.js +0 -323
- package/lib/runner/BufferedFilesystem.js +0 -262
- package/lib/runner/GraphQLASTNodeGroup.js +0 -256
- package/lib/runner/GraphQLASTUtils.js +0 -23
- package/lib/runner/GraphQLNodeMap.js +0 -81
- package/lib/runner/Sources.js +0 -271
- package/lib/runner/StrictMap.js +0 -134
- package/lib/runner/compileArtifacts.js +0 -39
- package/lib/runner/extractAST.js +0 -77
- package/lib/runner/getChangedNodeNames.js +0 -82
- package/lib/runner/getSchemaInstance.js +0 -30
- package/lib/runner/types.js +0 -12
- package/lib/test-utils/TestSchema.js +0 -27
- package/lib/test-utils/parseGraphQLText.js +0 -30
- package/lib/transforms/ApplyFragmentArgumentTransform.js +0 -393
- package/lib/transforms/ClientExtensionsTransform.js +0 -221
- package/lib/transforms/ConnectionTransform.js +0 -639
- package/lib/transforms/DeclarativeConnectionMutationTransform.js +0 -218
- package/lib/transforms/DeferStreamTransform.js +0 -246
- package/lib/transforms/DisallowIdAsAlias.js +0 -40
- package/lib/transforms/DisallowTypenameOnRoot.js +0 -53
- package/lib/transforms/FieldHandleTransform.js +0 -79
- package/lib/transforms/FilterCompilerDirectivesTransform.js +0 -29
- package/lib/transforms/FilterDirectivesTransform.js +0 -42
- package/lib/transforms/FlattenTransform.js +0 -306
- package/lib/transforms/GenerateIDFieldTransform.js +0 -135
- package/lib/transforms/GenerateTypeNameTransform.js +0 -149
- package/lib/transforms/InlineDataFragmentTransform.js +0 -100
- package/lib/transforms/InlineFragmentsTransform.js +0 -61
- package/lib/transforms/MaskTransform.js +0 -117
- package/lib/transforms/MatchTransform.js +0 -434
- package/lib/transforms/ReactFlightComponentTransform.js +0 -158
- package/lib/transforms/RefetchableFragmentTransform.js +0 -249
- package/lib/transforms/RelayDirectiveTransform.js +0 -83
- package/lib/transforms/RequiredFieldTransform.js +0 -369
- package/lib/transforms/SkipClientExtensionsTransform.js +0 -46
- package/lib/transforms/SkipHandleFieldTransform.js +0 -45
- package/lib/transforms/SkipRedundantNodesTransform.js +0 -261
- package/lib/transforms/SkipSplitOperationTransform.js +0 -32
- package/lib/transforms/SkipUnreachableNodeTransform.js +0 -158
- package/lib/transforms/SkipUnusedVariablesTransform.js +0 -75
- package/lib/transforms/SplitModuleImportTransform.js +0 -82
- package/lib/transforms/TestOperationTransform.js +0 -144
- package/lib/transforms/TransformUtils.js +0 -21
- package/lib/transforms/ValidateGlobalVariablesTransform.js +0 -92
- package/lib/transforms/ValidateRequiredArgumentsTransform.js +0 -114
- package/lib/transforms/ValidateServerOnlyDirectivesTransform.js +0 -108
- package/lib/transforms/ValidateUnusedVariablesTransform.js +0 -96
- package/lib/transforms/query-generators/FetchableQueryGenerator.js +0 -157
- package/lib/transforms/query-generators/NodeQueryGenerator.js +0 -166
- package/lib/transforms/query-generators/QueryQueryGenerator.js +0 -48
- package/lib/transforms/query-generators/ViewerQueryGenerator.js +0 -77
- package/lib/transforms/query-generators/index.js +0 -60
- package/lib/transforms/query-generators/utils.js +0 -92
- package/lib/util/CodeMarker.js +0 -80
- package/lib/util/DefaultHandleKey.js +0 -15
- package/lib/util/RelayCompilerCache.js +0 -97
- package/lib/util/Rollout.js +0 -40
- package/lib/util/TimeReporter.js +0 -83
- package/lib/util/areEqualArgValues.js +0 -135
- package/lib/util/argumentContainsVariables.js +0 -37
- package/lib/util/dedupeJSONStringify.js +0 -160
- package/lib/util/generateAbstractTypeRefinementKey.js +0 -24
- package/lib/util/getDefinitionNodeHash.js +0 -22
- package/lib/util/getModuleName.js +0 -32
- package/lib/util/joinArgumentDefinitions.js +0 -67
- package/lib/util/md5.js +0 -17
- package/lib/util/murmurHash.js +0 -86
- package/lib/util/nullthrowsOSS.js +0 -23
- package/lib/util/orList.js +0 -36
- package/lib/util/partitionArray.js +0 -35
- package/relay-compiler.js +0 -17
- package/relay-compiler.min.js +0 -22
- package/reporters/ConsoleReporter.js.flow +0 -81
- package/reporters/MultiReporter.js.flow +0 -43
- package/reporters/Reporter.js.flow +0 -19
- package/runner/Artifacts.js.flow +0 -215
- package/runner/BufferedFilesystem.js.flow +0 -194
- package/runner/GraphQLASTNodeGroup.js.flow +0 -174
- package/runner/GraphQLASTUtils.js.flow +0 -26
- package/runner/GraphQLNodeMap.js.flow +0 -55
- package/runner/Sources.js.flow +0 -227
- package/runner/StrictMap.js.flow +0 -96
- package/runner/compileArtifacts.js.flow +0 -75
- package/runner/extractAST.js.flow +0 -98
- package/runner/getChangedNodeNames.js.flow +0 -48
- package/runner/getSchemaInstance.js.flow +0 -36
- package/runner/types.js.flow +0 -37
- package/test-utils/TestSchema.js.flow +0 -30
- package/test-utils/parseGraphQLText.js.flow +0 -41
- package/transforms/ApplyFragmentArgumentTransform.js.flow +0 -524
- package/transforms/ClientExtensionsTransform.js.flow +0 -224
- package/transforms/ConnectionTransform.js.flow +0 -850
- package/transforms/DeclarativeConnectionMutationTransform.js.flow +0 -245
- package/transforms/DeferStreamTransform.js.flow +0 -263
- package/transforms/DisallowIdAsAlias.js.flow +0 -46
- package/transforms/DisallowTypenameOnRoot.js.flow +0 -44
- package/transforms/FieldHandleTransform.js.flow +0 -77
- package/transforms/FilterCompilerDirectivesTransform.js.flow +0 -33
- package/transforms/FilterDirectivesTransform.js.flow +0 -45
- package/transforms/FlattenTransform.js.flow +0 -458
- package/transforms/GenerateIDFieldTransform.js.flow +0 -151
- package/transforms/GenerateTypeNameTransform.js.flow +0 -159
- package/transforms/InlineDataFragmentTransform.js.flow +0 -123
- package/transforms/InlineFragmentsTransform.js.flow +0 -70
- package/transforms/MaskTransform.js.flow +0 -124
- package/transforms/MatchTransform.js.flow +0 -587
- package/transforms/ReactFlightComponentTransform.js.flow +0 -207
- package/transforms/RefetchableFragmentTransform.js.flow +0 -266
- package/transforms/RelayDirectiveTransform.js.flow +0 -96
- package/transforms/RequiredFieldTransform.js.flow +0 -413
- package/transforms/SkipClientExtensionsTransform.js.flow +0 -54
- package/transforms/SkipHandleFieldTransform.js.flow +0 -44
- package/transforms/SkipRedundantNodesTransform.js.flow +0 -277
- package/transforms/SkipSplitOperationTransform.js.flow +0 -37
- package/transforms/SkipUnreachableNodeTransform.js.flow +0 -148
- package/transforms/SkipUnusedVariablesTransform.js.flow +0 -59
- package/transforms/SplitModuleImportTransform.js.flow +0 -97
- package/transforms/TestOperationTransform.js.flow +0 -142
- package/transforms/TransformUtils.js.flow +0 -26
- package/transforms/ValidateGlobalVariablesTransform.js.flow +0 -80
- package/transforms/ValidateRequiredArgumentsTransform.js.flow +0 -130
- package/transforms/ValidateServerOnlyDirectivesTransform.js.flow +0 -128
- package/transforms/ValidateUnusedVariablesTransform.js.flow +0 -88
- package/transforms/query-generators/FetchableQueryGenerator.js.flow +0 -188
- package/transforms/query-generators/NodeQueryGenerator.js.flow +0 -217
- package/transforms/query-generators/QueryQueryGenerator.js.flow +0 -57
- package/transforms/query-generators/ViewerQueryGenerator.js.flow +0 -97
- package/transforms/query-generators/index.js.flow +0 -89
- package/transforms/query-generators/utils.js.flow +0 -76
- package/util/CodeMarker.js.flow +0 -79
- package/util/DefaultHandleKey.js.flow +0 -17
- package/util/RelayCompilerCache.js.flow +0 -86
- package/util/Rollout.js.flow +0 -39
- package/util/TimeReporter.js.flow +0 -79
- package/util/areEqualArgValues.js.flow +0 -126
- package/util/argumentContainsVariables.js.flow +0 -38
- package/util/dedupeJSONStringify.js.flow +0 -156
- package/util/generateAbstractTypeRefinementKey.js.flow +0 -29
- package/util/getDefinitionNodeHash.js.flow +0 -24
- package/util/getModuleName.js.flow +0 -39
- package/util/joinArgumentDefinitions.js.flow +0 -105
- package/util/md5.js.flow +0 -19
- package/util/murmurHash.js.flow +0 -94
- package/util/nullthrowsOSS.js.flow +0 -25
- package/util/orList.js.flow +0 -37
- package/util/partitionArray.js.flow +0 -37
@@ -1,424 +0,0 @@
|
|
1
|
-
/**
|
2
|
-
* Copyright (c) Facebook, Inc. and its affiliates.
|
3
|
-
*
|
4
|
-
* This source code is licensed under the MIT license found in the
|
5
|
-
* LICENSE file in the root directory of this source tree.
|
6
|
-
*
|
7
|
-
* @flow strict-local
|
8
|
-
* @format
|
9
|
-
*/
|
10
|
-
|
11
|
-
// flowlint ambiguous-object-type:error
|
12
|
-
|
13
|
-
'use strict';
|
14
|
-
|
15
|
-
import type ASTCache from '../core/ASTCache';
|
16
|
-
import type {Schema} from '../core/Schema';
|
17
|
-
import type {Reporter} from '../reporters/Reporter';
|
18
|
-
import type {CompileResult, File} from './CodegenTypes';
|
19
|
-
import type {FileFilter, WatchmanExpression} from './CodegenWatcher';
|
20
|
-
import type {SourceControl} from './SourceControl';
|
21
|
-
import type {DocumentNode, Source} from 'graphql';
|
22
|
-
|
23
|
-
const Profiler = require('../core/GraphQLCompilerProfiler');
|
24
|
-
const GraphQLWatchmanClient = require('../core/GraphQLWatchmanClient');
|
25
|
-
const {create: createSchema} = require('../core/Schema');
|
26
|
-
const CodegenDirectory = require('./CodegenDirectory');
|
27
|
-
const CodegenWatcher = require('./CodegenWatcher');
|
28
|
-
/* $FlowFixMe[untyped-import] - importing immutable, which is untyped (and flow
|
29
|
-
* is sad about it) */
|
30
|
-
const {Map: ImmutableMap} = require('immutable');
|
31
|
-
const invariant = require('invariant');
|
32
|
-
const path = require('path');
|
33
|
-
|
34
|
-
export type ParserConfig = {|
|
35
|
-
baseDir: string,
|
36
|
-
getFileFilter?: (baseDir: string) => FileFilter,
|
37
|
-
getParser: (baseDir: string) => ASTCache,
|
38
|
-
getSchemaSource: () => Source,
|
39
|
-
schemaExtensions: $ReadOnlyArray<string>,
|
40
|
-
generatedDirectoriesWatchmanExpression?: ?WatchmanExpression,
|
41
|
-
watchmanExpression?: ?WatchmanExpression,
|
42
|
-
filepaths?: ?Array<string>,
|
43
|
-
|};
|
44
|
-
|
45
|
-
type ParserConfigs = {[parser: string]: ParserConfig, ...};
|
46
|
-
type Parsers = {[parser: string]: ASTCache, ...};
|
47
|
-
|
48
|
-
export type IsGeneratedFileFn = (filePath: string) => boolean;
|
49
|
-
export type KeepExtraFileFn = (filePath: string) => boolean;
|
50
|
-
|
51
|
-
export type WriterConfig = {|
|
52
|
-
parser: string,
|
53
|
-
baseParsers?: Array<string>,
|
54
|
-
isGeneratedFile: IsGeneratedFileFn,
|
55
|
-
writeFiles: WriteFiles,
|
56
|
-
|};
|
57
|
-
|
58
|
-
type WriterConfigs = {[writer: string]: WriterConfig, ...};
|
59
|
-
|
60
|
-
export type WriteFilesOptions = {|
|
61
|
-
onlyValidate: boolean,
|
62
|
-
schema: Schema,
|
63
|
-
documents: ImmutableMap<string, DocumentNode>,
|
64
|
-
baseDocuments: ImmutableMap<string, DocumentNode>,
|
65
|
-
sourceControl: ?SourceControl,
|
66
|
-
reporter: Reporter,
|
67
|
-
generatedDirectories?: Array<string>,
|
68
|
-
|};
|
69
|
-
|
70
|
-
export type WriteFiles = WriteFilesOptions => Promise<
|
71
|
-
Map<string, CodegenDirectory>,
|
72
|
-
>;
|
73
|
-
|
74
|
-
type OnCompleteCallback = (
|
75
|
-
codegenDirs: $ReadOnlyArray<CodegenDirectory>,
|
76
|
-
) => void;
|
77
|
-
|
78
|
-
class CodegenRunner {
|
79
|
-
parserConfigs: ParserConfigs;
|
80
|
-
writerConfigs: WriterConfigs;
|
81
|
-
onlyValidate: boolean;
|
82
|
-
parsers: Parsers;
|
83
|
-
onComplete: ?OnCompleteCallback;
|
84
|
-
|
85
|
-
// parser => writers that are affected by it
|
86
|
-
parserWriters: {[parser: string]: Set<string>, ...};
|
87
|
-
_reporter: Reporter;
|
88
|
-
_sourceControl: ?SourceControl;
|
89
|
-
|
90
|
-
constructor(options: {
|
91
|
-
parserConfigs: ParserConfigs,
|
92
|
-
writerConfigs: WriterConfigs,
|
93
|
-
onlyValidate: boolean,
|
94
|
-
reporter: Reporter,
|
95
|
-
sourceControl: ?SourceControl,
|
96
|
-
onComplete?: OnCompleteCallback,
|
97
|
-
...
|
98
|
-
}) {
|
99
|
-
this.parsers = {};
|
100
|
-
this.parserConfigs = options.parserConfigs;
|
101
|
-
this.writerConfigs = options.writerConfigs;
|
102
|
-
this.onlyValidate = options.onlyValidate;
|
103
|
-
this.onComplete = options.onComplete;
|
104
|
-
this._reporter = options.reporter;
|
105
|
-
this._sourceControl = options.sourceControl;
|
106
|
-
|
107
|
-
this.parserWriters = {};
|
108
|
-
for (const parser in options.parserConfigs) {
|
109
|
-
this.parserWriters[parser] = new Set();
|
110
|
-
}
|
111
|
-
|
112
|
-
for (const writer in options.writerConfigs) {
|
113
|
-
const config = options.writerConfigs[writer];
|
114
|
-
config.baseParsers &&
|
115
|
-
config.baseParsers.forEach(parser =>
|
116
|
-
this.parserWriters[parser].add(writer),
|
117
|
-
);
|
118
|
-
this.parserWriters[config.parser].add(writer);
|
119
|
-
}
|
120
|
-
}
|
121
|
-
|
122
|
-
async compileAll(): Promise<CompileResult> {
|
123
|
-
// reset the parsers
|
124
|
-
this.parsers = {};
|
125
|
-
for (const parserName in this.parserConfigs) {
|
126
|
-
try {
|
127
|
-
await this.parseEverything(parserName);
|
128
|
-
} catch (e) {
|
129
|
-
this._reporter.reportError('CodegenRunner.compileAll', e);
|
130
|
-
return 'ERROR';
|
131
|
-
}
|
132
|
-
}
|
133
|
-
|
134
|
-
let hasChanges = false;
|
135
|
-
for (const writerName in this.writerConfigs) {
|
136
|
-
const result = await this.write(writerName);
|
137
|
-
if (result === 'ERROR') {
|
138
|
-
return 'ERROR';
|
139
|
-
}
|
140
|
-
if (result === 'HAS_CHANGES') {
|
141
|
-
hasChanges = true;
|
142
|
-
}
|
143
|
-
}
|
144
|
-
return hasChanges ? 'HAS_CHANGES' : 'NO_CHANGES';
|
145
|
-
}
|
146
|
-
|
147
|
-
async compile(writerName: string): Promise<CompileResult> {
|
148
|
-
const writerConfig = this.writerConfigs[writerName];
|
149
|
-
|
150
|
-
const parsers = [writerConfig.parser];
|
151
|
-
if (writerConfig.baseParsers) {
|
152
|
-
writerConfig.baseParsers.forEach(parser => parsers.push(parser));
|
153
|
-
}
|
154
|
-
// Don't bother resetting the parsers
|
155
|
-
await Profiler.asyncContext('CodegenRunner:parseEverything', () =>
|
156
|
-
Promise.all(parsers.map(parser => this.parseEverything(parser))),
|
157
|
-
);
|
158
|
-
|
159
|
-
return await this.write(writerName);
|
160
|
-
}
|
161
|
-
|
162
|
-
getDirtyWriters(filePaths: Array<string>): Promise<Set<string>> {
|
163
|
-
return Profiler.asyncContext('CodegenRunner:getDirtyWriters', async () => {
|
164
|
-
const dirtyWriters = new Set();
|
165
|
-
|
166
|
-
// Check if any files are in the output
|
167
|
-
for (const configName in this.writerConfigs) {
|
168
|
-
const config = this.writerConfigs[configName];
|
169
|
-
for (const filePath of filePaths) {
|
170
|
-
if (config.isGeneratedFile(filePath)) {
|
171
|
-
dirtyWriters.add(configName);
|
172
|
-
}
|
173
|
-
}
|
174
|
-
}
|
175
|
-
|
176
|
-
// Check for files in the input
|
177
|
-
await Promise.all(
|
178
|
-
Object.keys(this.parserConfigs).map(parserConfigName =>
|
179
|
-
Profiler.waitFor('Watchman:query', async () => {
|
180
|
-
const client = new GraphQLWatchmanClient();
|
181
|
-
const config = this.parserConfigs[parserConfigName];
|
182
|
-
const dirs = await client.watchProject(config.baseDir);
|
183
|
-
|
184
|
-
const relativeFilePaths = filePaths.map(filePath =>
|
185
|
-
path.relative(config.baseDir, filePath),
|
186
|
-
);
|
187
|
-
|
188
|
-
const query = {
|
189
|
-
expression: [
|
190
|
-
'allof',
|
191
|
-
config.watchmanExpression,
|
192
|
-
['name', relativeFilePaths, 'wholename'],
|
193
|
-
],
|
194
|
-
fields: ['exists'],
|
195
|
-
relative_root: dirs.relativePath,
|
196
|
-
};
|
197
|
-
|
198
|
-
const result = await client.command('query', dirs.root, query);
|
199
|
-
client.end();
|
200
|
-
|
201
|
-
if (result.files.length > 0) {
|
202
|
-
this.parserWriters[parserConfigName].forEach(writerName =>
|
203
|
-
dirtyWriters.add(writerName),
|
204
|
-
);
|
205
|
-
}
|
206
|
-
}),
|
207
|
-
),
|
208
|
-
);
|
209
|
-
|
210
|
-
return dirtyWriters;
|
211
|
-
});
|
212
|
-
}
|
213
|
-
|
214
|
-
async parseEverything(parserName: string): Promise<void> {
|
215
|
-
if (this.parsers[parserName]) {
|
216
|
-
// no need to parse
|
217
|
-
return;
|
218
|
-
}
|
219
|
-
|
220
|
-
const parserConfig = this.parserConfigs[parserName];
|
221
|
-
this.parsers[parserName] = parserConfig.getParser(parserConfig.baseDir);
|
222
|
-
const filter = parserConfig.getFileFilter
|
223
|
-
? parserConfig.getFileFilter(parserConfig.baseDir)
|
224
|
-
: anyFileFilter;
|
225
|
-
|
226
|
-
if (parserConfig.filepaths && parserConfig.watchmanExpression) {
|
227
|
-
throw new Error(
|
228
|
-
'Provide either `watchmanExpression` or `filepaths` but not both.',
|
229
|
-
);
|
230
|
-
}
|
231
|
-
|
232
|
-
let files;
|
233
|
-
if (parserConfig.watchmanExpression) {
|
234
|
-
files = await CodegenWatcher.queryFiles(
|
235
|
-
parserConfig.baseDir,
|
236
|
-
parserConfig.watchmanExpression,
|
237
|
-
filter,
|
238
|
-
);
|
239
|
-
} else if (parserConfig.filepaths) {
|
240
|
-
files = await CodegenWatcher.queryFilepaths(
|
241
|
-
parserConfig.baseDir,
|
242
|
-
parserConfig.filepaths,
|
243
|
-
filter,
|
244
|
-
);
|
245
|
-
} else {
|
246
|
-
throw new Error(
|
247
|
-
'Either `watchmanExpression` or `filepaths` is required to query files',
|
248
|
-
);
|
249
|
-
}
|
250
|
-
this.parseFileChanges(parserName, files);
|
251
|
-
}
|
252
|
-
|
253
|
-
parseFileChanges(parserName: string, files: Set<File>): void {
|
254
|
-
return Profiler.run('CodegenRunner.parseFileChanges', () => {
|
255
|
-
const parser = this.parsers[parserName];
|
256
|
-
// this maybe should be await parser.parseFiles(files);
|
257
|
-
parser.parseFiles(files);
|
258
|
-
});
|
259
|
-
}
|
260
|
-
|
261
|
-
// We cannot do incremental writes right now.
|
262
|
-
// When we can, this could be writeChanges(writerName, parserName, parsedDefinitions)
|
263
|
-
write(writerName: string): Promise<CompileResult> {
|
264
|
-
return Profiler.asyncContext('CodegenRunner.write', async () => {
|
265
|
-
try {
|
266
|
-
this._reporter.reportMessage(`\nWriting ${writerName}`);
|
267
|
-
const {writeFiles, parser, baseParsers, isGeneratedFile} =
|
268
|
-
this.writerConfigs[writerName];
|
269
|
-
|
270
|
-
let baseDocuments = ImmutableMap();
|
271
|
-
if (baseParsers) {
|
272
|
-
baseParsers.forEach(baseParserName => {
|
273
|
-
invariant(
|
274
|
-
this.parsers[baseParserName] != null,
|
275
|
-
'Trying to access an uncompiled base parser config: %s',
|
276
|
-
baseParserName,
|
277
|
-
);
|
278
|
-
baseDocuments = baseDocuments.merge(
|
279
|
-
this.parsers[baseParserName].documents(),
|
280
|
-
);
|
281
|
-
});
|
282
|
-
}
|
283
|
-
|
284
|
-
const {baseDir, generatedDirectoriesWatchmanExpression} =
|
285
|
-
this.parserConfigs[parser];
|
286
|
-
let generatedDirectories = [];
|
287
|
-
if (generatedDirectoriesWatchmanExpression) {
|
288
|
-
const relativePaths = await CodegenWatcher.queryDirectories(
|
289
|
-
baseDir,
|
290
|
-
generatedDirectoriesWatchmanExpression,
|
291
|
-
);
|
292
|
-
generatedDirectories = relativePaths.map(x => path.join(baseDir, x));
|
293
|
-
}
|
294
|
-
|
295
|
-
// always create a new writer: we have to write everything anyways
|
296
|
-
const documents = this.parsers[parser].documents();
|
297
|
-
const schema = Profiler.run('getSchema', () =>
|
298
|
-
createSchema(
|
299
|
-
this.parserConfigs[parser].getSchemaSource(),
|
300
|
-
baseDocuments.toArray(),
|
301
|
-
this.parserConfigs[parser].schemaExtensions,
|
302
|
-
),
|
303
|
-
);
|
304
|
-
|
305
|
-
const outputDirectories = await writeFiles({
|
306
|
-
onlyValidate: this.onlyValidate,
|
307
|
-
schema,
|
308
|
-
documents,
|
309
|
-
baseDocuments,
|
310
|
-
generatedDirectories,
|
311
|
-
sourceControl: this._sourceControl,
|
312
|
-
reporter: this._reporter,
|
313
|
-
});
|
314
|
-
|
315
|
-
for (const dir of outputDirectories.values()) {
|
316
|
-
const all = [
|
317
|
-
...dir.changes.created,
|
318
|
-
...dir.changes.updated,
|
319
|
-
...dir.changes.deleted,
|
320
|
-
...dir.changes.unchanged,
|
321
|
-
];
|
322
|
-
for (const filename of all) {
|
323
|
-
const filePath = dir.getPath(filename);
|
324
|
-
invariant(
|
325
|
-
isGeneratedFile(filePath),
|
326
|
-
'CodegenRunner: %s returned false for isGeneratedFile, ' +
|
327
|
-
'but was in generated directory',
|
328
|
-
filePath,
|
329
|
-
);
|
330
|
-
}
|
331
|
-
}
|
332
|
-
|
333
|
-
const onCompleteCallback = this.onComplete;
|
334
|
-
if (onCompleteCallback != null) {
|
335
|
-
onCompleteCallback(Array.from(outputDirectories.values()));
|
336
|
-
}
|
337
|
-
|
338
|
-
const combinedChanges = CodegenDirectory.combineChanges(
|
339
|
-
Array.from(outputDirectories.values()),
|
340
|
-
);
|
341
|
-
|
342
|
-
this._reporter.reportMessage(
|
343
|
-
CodegenDirectory.formatChanges(combinedChanges, {
|
344
|
-
onlyValidate: this.onlyValidate,
|
345
|
-
}),
|
346
|
-
);
|
347
|
-
|
348
|
-
return CodegenDirectory.hasChanges(combinedChanges)
|
349
|
-
? 'HAS_CHANGES'
|
350
|
-
: 'NO_CHANGES';
|
351
|
-
} catch (e) {
|
352
|
-
this._reporter.reportError('CodegenRunner.write', e);
|
353
|
-
return 'ERROR';
|
354
|
-
}
|
355
|
-
});
|
356
|
-
}
|
357
|
-
|
358
|
-
async watchAll(): Promise<void> {
|
359
|
-
// get everything set up for watching
|
360
|
-
await this.compileAll();
|
361
|
-
|
362
|
-
for (const parserName in this.parserConfigs) {
|
363
|
-
await this.watch(parserName);
|
364
|
-
}
|
365
|
-
}
|
366
|
-
|
367
|
-
async watch(parserName: string): Promise<void> {
|
368
|
-
const parserConfig = this.parserConfigs[parserName];
|
369
|
-
|
370
|
-
if (!parserConfig.watchmanExpression) {
|
371
|
-
throw new Error('`watchmanExpression` is required to watch files');
|
372
|
-
}
|
373
|
-
|
374
|
-
// watchCompile starts with a full set of files as the changes
|
375
|
-
// But as we need to set everything up due to potential parser dependencies,
|
376
|
-
// we should prevent the first watch callback from doing anything.
|
377
|
-
let firstChange = true;
|
378
|
-
|
379
|
-
await CodegenWatcher.watchCompile(
|
380
|
-
parserConfig.baseDir,
|
381
|
-
parserConfig.watchmanExpression,
|
382
|
-
parserConfig.getFileFilter
|
383
|
-
? parserConfig.getFileFilter(parserConfig.baseDir)
|
384
|
-
: anyFileFilter,
|
385
|
-
async files => {
|
386
|
-
invariant(
|
387
|
-
this.parsers[parserName] != null,
|
388
|
-
'Trying to watch an uncompiled parser config: %s',
|
389
|
-
parserName,
|
390
|
-
);
|
391
|
-
if (firstChange) {
|
392
|
-
firstChange = false;
|
393
|
-
return;
|
394
|
-
}
|
395
|
-
const dependentWriters = [];
|
396
|
-
this.parserWriters[parserName].forEach(writer =>
|
397
|
-
dependentWriters.push(writer),
|
398
|
-
);
|
399
|
-
|
400
|
-
try {
|
401
|
-
if (!this.parsers[parserName]) {
|
402
|
-
// have to load the parser and make sure all of its dependents are set
|
403
|
-
await this.parseEverything(parserName);
|
404
|
-
} else {
|
405
|
-
this.parseFileChanges(parserName, files);
|
406
|
-
}
|
407
|
-
await Promise.all(dependentWriters.map(writer => this.write(writer)));
|
408
|
-
} catch (error) {
|
409
|
-
this._reporter.reportError('CodegenRunner.watch', error);
|
410
|
-
}
|
411
|
-
this._reporter.reportMessage(
|
412
|
-
`Watching for changes to ${parserName}...`,
|
413
|
-
);
|
414
|
-
},
|
415
|
-
);
|
416
|
-
this._reporter.reportMessage(`Watching for changes to ${parserName}...`);
|
417
|
-
}
|
418
|
-
}
|
419
|
-
|
420
|
-
function anyFileFilter(file: File): boolean {
|
421
|
-
return true;
|
422
|
-
}
|
423
|
-
|
424
|
-
module.exports = CodegenRunner;
|
@@ -1,28 +0,0 @@
|
|
1
|
-
/**
|
2
|
-
* Copyright (c) Facebook, Inc. and its affiliates.
|
3
|
-
*
|
4
|
-
* This source code is licensed under the MIT license found in the
|
5
|
-
* LICENSE file in the root directory of this source tree.
|
6
|
-
*
|
7
|
-
* @flow strict
|
8
|
-
* @format
|
9
|
-
*/
|
10
|
-
|
11
|
-
// flowlint ambiguous-object-type:error
|
12
|
-
|
13
|
-
'use strict';
|
14
|
-
|
15
|
-
export type CompileResult = 'HAS_CHANGES' | 'NO_CHANGES' | 'ERROR';
|
16
|
-
|
17
|
-
export type File =
|
18
|
-
| {
|
19
|
-
exists: false,
|
20
|
-
relPath: string,
|
21
|
-
...
|
22
|
-
}
|
23
|
-
| {
|
24
|
-
exists: true,
|
25
|
-
relPath: string,
|
26
|
-
hash: string,
|
27
|
-
...
|
28
|
-
};
|
@@ -1,250 +0,0 @@
|
|
1
|
-
/**
|
2
|
-
* Copyright (c) Facebook, Inc. and its affiliates.
|
3
|
-
*
|
4
|
-
* This source code is licensed under the MIT license found in the
|
5
|
-
* LICENSE file in the root directory of this source tree.
|
6
|
-
*
|
7
|
-
* @flow
|
8
|
-
* @format
|
9
|
-
*/
|
10
|
-
|
11
|
-
// flowlint ambiguous-object-type:error
|
12
|
-
|
13
|
-
'use strict';
|
14
|
-
|
15
|
-
import type {File} from './CodegenTypes';
|
16
|
-
|
17
|
-
const Profiler = require('../core/GraphQLCompilerProfiler');
|
18
|
-
const GraphQLWatchmanClient = require('../core/GraphQLWatchmanClient');
|
19
|
-
const crypto = require('crypto');
|
20
|
-
const fs = require('fs');
|
21
|
-
const path = require('path');
|
22
|
-
|
23
|
-
const SUBSCRIPTION_NAME = 'graphql-codegen';
|
24
|
-
const QUERY_RETRIES = 3;
|
25
|
-
|
26
|
-
export type WatchmanExpression = $ReadOnlyArray<string | WatchmanExpression>;
|
27
|
-
|
28
|
-
export type FileFilter = (file: File) => boolean;
|
29
|
-
|
30
|
-
type WatchmanChange = {|
|
31
|
-
name: string,
|
32
|
-
exists: boolean,
|
33
|
-
'content.sha1hex': ?string,
|
34
|
-
|};
|
35
|
-
type WatchmanChanges = {
|
36
|
-
files?: $ReadOnlyArray<WatchmanChange>,
|
37
|
-
...
|
38
|
-
};
|
39
|
-
|
40
|
-
async function queryFiles(
|
41
|
-
baseDir: string,
|
42
|
-
expression: WatchmanExpression,
|
43
|
-
filter: FileFilter,
|
44
|
-
): Promise<Set<File>> {
|
45
|
-
return await Profiler.waitFor('Watchman:query', async () => {
|
46
|
-
const client = new GraphQLWatchmanClient(QUERY_RETRIES);
|
47
|
-
const [watchResp, fields] = await Promise.all([
|
48
|
-
client.watchProject(baseDir),
|
49
|
-
getFields(client),
|
50
|
-
]);
|
51
|
-
const resp = await client.command('query', watchResp.root, {
|
52
|
-
expression,
|
53
|
-
fields: fields,
|
54
|
-
relative_root: watchResp.relativePath,
|
55
|
-
});
|
56
|
-
client.end();
|
57
|
-
return updateFiles(new Set(), baseDir, filter, resp.files);
|
58
|
-
});
|
59
|
-
}
|
60
|
-
|
61
|
-
async function queryDirectories(
|
62
|
-
baseDir: string,
|
63
|
-
expression: WatchmanExpression,
|
64
|
-
): Promise<$ReadOnlyArray<string>> {
|
65
|
-
return await Profiler.waitFor('Watchman:query', async () => {
|
66
|
-
const client = new GraphQLWatchmanClient();
|
67
|
-
const watchResp = await client.watchProject(baseDir);
|
68
|
-
const resp = await client.command('query', watchResp.root, {
|
69
|
-
expression,
|
70
|
-
fields: ['name'],
|
71
|
-
relative_root: watchResp.relativePath,
|
72
|
-
});
|
73
|
-
client.end();
|
74
|
-
return resp.files;
|
75
|
-
});
|
76
|
-
}
|
77
|
-
|
78
|
-
async function getFields(
|
79
|
-
client: GraphQLWatchmanClient,
|
80
|
-
): Promise<$ReadOnlyArray<string>> {
|
81
|
-
const fields = ['name', 'exists'];
|
82
|
-
if (await client.hasCapability('field-content.sha1hex')) {
|
83
|
-
fields.push('content.sha1hex');
|
84
|
-
}
|
85
|
-
return fields;
|
86
|
-
}
|
87
|
-
|
88
|
-
// For use when not using Watchman.
|
89
|
-
async function queryFilepaths(
|
90
|
-
baseDir: string,
|
91
|
-
filepaths: $ReadOnlyArray<string>,
|
92
|
-
filter: FileFilter,
|
93
|
-
): Promise<Set<File>> {
|
94
|
-
// Construct WatchmanChange objects as an intermediate step before
|
95
|
-
// calling updateFiles to produce file content.
|
96
|
-
const files = filepaths.map(filepath => ({
|
97
|
-
name: filepath,
|
98
|
-
exists: true,
|
99
|
-
'content.sha1hex': null,
|
100
|
-
}));
|
101
|
-
return updateFiles(new Set(), baseDir, filter, files);
|
102
|
-
}
|
103
|
-
|
104
|
-
/**
|
105
|
-
* Provides a simplified API to the watchman API.
|
106
|
-
* Given some base directory and a list of subdirectories it calls the callback
|
107
|
-
* with watchman change events on file changes.
|
108
|
-
*/
|
109
|
-
async function watch(
|
110
|
-
baseDir: string,
|
111
|
-
expression: WatchmanExpression,
|
112
|
-
callback: (changes: WatchmanChanges) => mixed,
|
113
|
-
): Promise<void> {
|
114
|
-
return await Profiler.waitFor('Watchman:subscribe', async () => {
|
115
|
-
const client = new GraphQLWatchmanClient();
|
116
|
-
const watchResp = await client.watchProject(baseDir);
|
117
|
-
|
118
|
-
await makeSubscription(
|
119
|
-
client,
|
120
|
-
watchResp.root,
|
121
|
-
watchResp.relativePath,
|
122
|
-
expression,
|
123
|
-
callback,
|
124
|
-
);
|
125
|
-
});
|
126
|
-
}
|
127
|
-
|
128
|
-
async function makeSubscription(
|
129
|
-
client: GraphQLWatchmanClient,
|
130
|
-
root: string,
|
131
|
-
relativePath: string,
|
132
|
-
expression: WatchmanExpression,
|
133
|
-
callback,
|
134
|
-
): Promise<void> {
|
135
|
-
client.on('subscription', resp => {
|
136
|
-
if (resp.subscription === SUBSCRIPTION_NAME) {
|
137
|
-
callback(resp);
|
138
|
-
}
|
139
|
-
});
|
140
|
-
const fields = await getFields(client);
|
141
|
-
await client.command('subscribe', root, SUBSCRIPTION_NAME, {
|
142
|
-
expression,
|
143
|
-
fields: fields,
|
144
|
-
relative_root: relativePath,
|
145
|
-
});
|
146
|
-
}
|
147
|
-
|
148
|
-
/**
|
149
|
-
* Further simplifies `watch` and calls the callback on every change with a
|
150
|
-
* full list of files that match the conditions.
|
151
|
-
*/
|
152
|
-
async function watchFiles(
|
153
|
-
baseDir: string,
|
154
|
-
expression: WatchmanExpression,
|
155
|
-
filter: FileFilter,
|
156
|
-
callback: (files: Set<File>) => any,
|
157
|
-
): Promise<void> {
|
158
|
-
let files = new Set();
|
159
|
-
await watch(baseDir, expression, changes => {
|
160
|
-
if (!changes.files) {
|
161
|
-
// Watchmen fires a change without files when a watchman state changes,
|
162
|
-
// for example during an hg update.
|
163
|
-
return;
|
164
|
-
}
|
165
|
-
files = updateFiles(files, baseDir, filter, changes.files);
|
166
|
-
callback(files);
|
167
|
-
});
|
168
|
-
}
|
169
|
-
|
170
|
-
/**
|
171
|
-
* Similar to watchFiles, but takes an async function. The `compile` function
|
172
|
-
* is awaited and not called in parallel. If multiple changes are triggered
|
173
|
-
* before a compile finishes, the latest version is called after the compile
|
174
|
-
* finished.
|
175
|
-
*
|
176
|
-
* TODO: Consider changing from a Promise to abortable, so we can abort mid
|
177
|
-
* compilation.
|
178
|
-
*/
|
179
|
-
async function watchCompile(
|
180
|
-
baseDir: string,
|
181
|
-
expression: WatchmanExpression,
|
182
|
-
filter: FileFilter,
|
183
|
-
compile: (files: Set<File>) => Promise<any>,
|
184
|
-
): Promise<void> {
|
185
|
-
let compiling = false;
|
186
|
-
let needsCompiling = false;
|
187
|
-
let latestFiles = null;
|
188
|
-
|
189
|
-
watchFiles(baseDir, expression, filter, async files => {
|
190
|
-
needsCompiling = true;
|
191
|
-
latestFiles = files;
|
192
|
-
if (compiling) {
|
193
|
-
return;
|
194
|
-
}
|
195
|
-
compiling = true;
|
196
|
-
while (needsCompiling) {
|
197
|
-
needsCompiling = false;
|
198
|
-
await compile(latestFiles);
|
199
|
-
}
|
200
|
-
compiling = false;
|
201
|
-
});
|
202
|
-
}
|
203
|
-
|
204
|
-
function updateFiles(
|
205
|
-
files: Set<File>,
|
206
|
-
baseDir: string,
|
207
|
-
filter: FileFilter,
|
208
|
-
fileChanges: $ReadOnlyArray<WatchmanChange>,
|
209
|
-
): Set<File> {
|
210
|
-
const fileMap = new Map();
|
211
|
-
files.forEach(file => {
|
212
|
-
file.exists && fileMap.set(file.relPath, file);
|
213
|
-
});
|
214
|
-
|
215
|
-
fileChanges.forEach(({name, exists, 'content.sha1hex': hash}) => {
|
216
|
-
let shouldRemove = !exists;
|
217
|
-
if (!shouldRemove) {
|
218
|
-
const file = {
|
219
|
-
exists: true,
|
220
|
-
relPath: name,
|
221
|
-
hash: hash || hashFile(path.join(baseDir, name)),
|
222
|
-
};
|
223
|
-
if (filter(file)) {
|
224
|
-
fileMap.set(name, file);
|
225
|
-
} else {
|
226
|
-
shouldRemove = true;
|
227
|
-
}
|
228
|
-
}
|
229
|
-
shouldRemove &&
|
230
|
-
fileMap.set(name, {
|
231
|
-
exists: false,
|
232
|
-
relPath: name,
|
233
|
-
});
|
234
|
-
});
|
235
|
-
return new Set(fileMap.values());
|
236
|
-
}
|
237
|
-
|
238
|
-
function hashFile(filename: string): string {
|
239
|
-
const content = fs.readFileSync(filename);
|
240
|
-
return crypto.createHash('sha1').update(content).digest('hex');
|
241
|
-
}
|
242
|
-
|
243
|
-
module.exports = {
|
244
|
-
queryDirectories,
|
245
|
-
queryFiles,
|
246
|
-
queryFilepaths,
|
247
|
-
watch,
|
248
|
-
watchFiles,
|
249
|
-
watchCompile,
|
250
|
-
};
|