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.
Files changed (263) hide show
  1. package/cli.js +23 -0
  2. package/index.js +19 -3
  3. package/linux-x64/relay +0 -0
  4. package/macos-x64/relay +0 -0
  5. package/package.json +2 -24
  6. package/win-x64/relay.exe +0 -0
  7. package/bin/RelayCompilerBin.js.flow +0 -168
  8. package/bin/RelayCompilerMain.js.flow +0 -515
  9. package/bin/__fixtures__/plugin-module.js.flow +0 -17
  10. package/bin/relay-compiler +0 -19068
  11. package/codegen/CodegenDirectory.js.flow +0 -372
  12. package/codegen/CodegenRunner.js.flow +0 -424
  13. package/codegen/CodegenTypes.js.flow +0 -28
  14. package/codegen/CodegenWatcher.js.flow +0 -250
  15. package/codegen/NormalizationCodeGenerator.js.flow +0 -566
  16. package/codegen/ReaderCodeGenerator.js.flow +0 -510
  17. package/codegen/RelayCodeGenerator.js.flow +0 -85
  18. package/codegen/RelayFileWriter.js.flow +0 -365
  19. package/codegen/SourceControl.js.flow +0 -58
  20. package/codegen/compileRelayArtifacts.js.flow +0 -181
  21. package/codegen/createPrintRequireModuleDependency.js.flow +0 -19
  22. package/codegen/sortObjectByKey.js.flow +0 -25
  23. package/codegen/writeRelayGeneratedFile.js.flow +0 -235
  24. package/core/ASTCache.js.flow +0 -73
  25. package/core/ASTConvert.js.flow +0 -230
  26. package/core/CompilerContext.js.flow +0 -189
  27. package/core/CompilerError.js.flow +0 -255
  28. package/core/DotGraphQLParser.js.flow +0 -37
  29. package/core/GraphQLCompilerProfiler.js.flow +0 -341
  30. package/core/GraphQLDerivedFromMetadata.js.flow +0 -36
  31. package/core/GraphQLWatchmanClient.js.flow +0 -109
  32. package/core/IR.js.flow +0 -326
  33. package/core/IRPrinter.js.flow +0 -472
  34. package/core/IRTransformer.js.flow +0 -376
  35. package/core/IRValidator.js.flow +0 -259
  36. package/core/IRVisitor.js.flow +0 -150
  37. package/core/JSModuleParser.js.flow +0 -24
  38. package/core/RelayCompilerScope.js.flow +0 -199
  39. package/core/RelayFindGraphQLTags.js.flow +0 -118
  40. package/core/RelayGraphQLEnumsGenerator.js.flow +0 -55
  41. package/core/RelayIRTransforms.js.flow +0 -138
  42. package/core/RelayParser.js.flow +0 -1741
  43. package/core/RelaySourceModuleParser.js.flow +0 -133
  44. package/core/Schema.js.flow +0 -2035
  45. package/core/SchemaUtils.js.flow +0 -120
  46. package/core/filterContextForNode.js.flow +0 -49
  47. package/core/getFieldDefinition.js.flow +0 -156
  48. package/core/getIdentifierForArgumentValue.js.flow +0 -49
  49. package/core/getIdentifierForSelection.js.flow +0 -68
  50. package/core/getLiteralArgumentValues.js.flow +0 -32
  51. package/core/getNormalizationOperationName.js.flow +0 -19
  52. package/core/inferRootArgumentDefinitions.js.flow +0 -322
  53. package/index.js.flow +0 -198
  54. package/language/RelayLanguagePluginInterface.js.flow +0 -283
  55. package/language/javascript/FindGraphQLTags.js.flow +0 -136
  56. package/language/javascript/RelayFlowBabelFactories.js.flow +0 -176
  57. package/language/javascript/RelayFlowGenerator.js.flow +0 -1096
  58. package/language/javascript/RelayFlowTypeTransformers.js.flow +0 -181
  59. package/language/javascript/RelayLanguagePluginJavaScript.js.flow +0 -33
  60. package/language/javascript/formatGeneratedModule.js.flow +0 -65
  61. package/lib/bin/RelayCompilerBin.js +0 -143
  62. package/lib/bin/RelayCompilerMain.js +0 -488
  63. package/lib/bin/__fixtures__/plugin-module.js +0 -16
  64. package/lib/codegen/CodegenDirectory.js +0 -335
  65. package/lib/codegen/CodegenRunner.js +0 -433
  66. package/lib/codegen/CodegenTypes.js +0 -11
  67. package/lib/codegen/CodegenWatcher.js +0 -271
  68. package/lib/codegen/NormalizationCodeGenerator.js +0 -487
  69. package/lib/codegen/ReaderCodeGenerator.js +0 -473
  70. package/lib/codegen/RelayCodeGenerator.js +0 -75
  71. package/lib/codegen/RelayFileWriter.js +0 -270
  72. package/lib/codegen/SourceControl.js +0 -60
  73. package/lib/codegen/compileRelayArtifacts.js +0 -157
  74. package/lib/codegen/createPrintRequireModuleDependency.js +0 -19
  75. package/lib/codegen/sortObjectByKey.js +0 -41
  76. package/lib/codegen/writeRelayGeneratedFile.js +0 -206
  77. package/lib/core/ASTCache.js +0 -70
  78. package/lib/core/ASTConvert.js +0 -198
  79. package/lib/core/CompilerContext.js +0 -165
  80. package/lib/core/CompilerError.js +0 -252
  81. package/lib/core/DotGraphQLParser.js +0 -40
  82. package/lib/core/GraphQLCompilerProfiler.js +0 -299
  83. package/lib/core/GraphQLDerivedFromMetadata.js +0 -31
  84. package/lib/core/GraphQLWatchmanClient.js +0 -150
  85. package/lib/core/IR.js +0 -11
  86. package/lib/core/IRPrinter.js +0 -388
  87. package/lib/core/IRTransformer.js +0 -345
  88. package/lib/core/IRValidator.js +0 -226
  89. package/lib/core/IRVisitor.js +0 -45
  90. package/lib/core/JSModuleParser.js +0 -18
  91. package/lib/core/RelayCompilerScope.js +0 -183
  92. package/lib/core/RelayFindGraphQLTags.js +0 -79
  93. package/lib/core/RelayGraphQLEnumsGenerator.js +0 -50
  94. package/lib/core/RelayIRTransforms.js +0 -109
  95. package/lib/core/RelayParser.js +0 -1381
  96. package/lib/core/RelaySourceModuleParser.js +0 -104
  97. package/lib/core/Schema.js +0 -1877
  98. package/lib/core/SchemaUtils.js +0 -98
  99. package/lib/core/filterContextForNode.js +0 -50
  100. package/lib/core/getFieldDefinition.js +0 -145
  101. package/lib/core/getIdentifierForArgumentValue.js +0 -54
  102. package/lib/core/getIdentifierForSelection.js +0 -49
  103. package/lib/core/getLiteralArgumentValues.js +0 -26
  104. package/lib/core/getNormalizationOperationName.js +0 -17
  105. package/lib/core/inferRootArgumentDefinitions.js +0 -351
  106. package/lib/index.js +0 -178
  107. package/lib/language/RelayLanguagePluginInterface.js +0 -26
  108. package/lib/language/javascript/FindGraphQLTags.js +0 -126
  109. package/lib/language/javascript/RelayFlowBabelFactories.js +0 -160
  110. package/lib/language/javascript/RelayFlowGenerator.js +0 -856
  111. package/lib/language/javascript/RelayFlowTypeTransformers.js +0 -119
  112. package/lib/language/javascript/RelayLanguagePluginJavaScript.js +0 -30
  113. package/lib/language/javascript/formatGeneratedModule.js +0 -36
  114. package/lib/reporters/ConsoleReporter.js +0 -61
  115. package/lib/reporters/MultiReporter.js +0 -45
  116. package/lib/reporters/Reporter.js +0 -11
  117. package/lib/runner/Artifacts.js +0 -323
  118. package/lib/runner/BufferedFilesystem.js +0 -262
  119. package/lib/runner/GraphQLASTNodeGroup.js +0 -256
  120. package/lib/runner/GraphQLASTUtils.js +0 -23
  121. package/lib/runner/GraphQLNodeMap.js +0 -81
  122. package/lib/runner/Sources.js +0 -271
  123. package/lib/runner/StrictMap.js +0 -134
  124. package/lib/runner/compileArtifacts.js +0 -39
  125. package/lib/runner/extractAST.js +0 -77
  126. package/lib/runner/getChangedNodeNames.js +0 -82
  127. package/lib/runner/getSchemaInstance.js +0 -30
  128. package/lib/runner/types.js +0 -12
  129. package/lib/test-utils/TestSchema.js +0 -27
  130. package/lib/test-utils/parseGraphQLText.js +0 -30
  131. package/lib/transforms/ApplyFragmentArgumentTransform.js +0 -393
  132. package/lib/transforms/ClientExtensionsTransform.js +0 -221
  133. package/lib/transforms/ConnectionTransform.js +0 -639
  134. package/lib/transforms/DeclarativeConnectionMutationTransform.js +0 -218
  135. package/lib/transforms/DeferStreamTransform.js +0 -246
  136. package/lib/transforms/DisallowIdAsAlias.js +0 -40
  137. package/lib/transforms/DisallowTypenameOnRoot.js +0 -53
  138. package/lib/transforms/FieldHandleTransform.js +0 -79
  139. package/lib/transforms/FilterCompilerDirectivesTransform.js +0 -29
  140. package/lib/transforms/FilterDirectivesTransform.js +0 -42
  141. package/lib/transforms/FlattenTransform.js +0 -306
  142. package/lib/transforms/GenerateIDFieldTransform.js +0 -135
  143. package/lib/transforms/GenerateTypeNameTransform.js +0 -149
  144. package/lib/transforms/InlineDataFragmentTransform.js +0 -100
  145. package/lib/transforms/InlineFragmentsTransform.js +0 -61
  146. package/lib/transforms/MaskTransform.js +0 -117
  147. package/lib/transforms/MatchTransform.js +0 -434
  148. package/lib/transforms/ReactFlightComponentTransform.js +0 -158
  149. package/lib/transforms/RefetchableFragmentTransform.js +0 -249
  150. package/lib/transforms/RelayDirectiveTransform.js +0 -83
  151. package/lib/transforms/RequiredFieldTransform.js +0 -369
  152. package/lib/transforms/SkipClientExtensionsTransform.js +0 -46
  153. package/lib/transforms/SkipHandleFieldTransform.js +0 -45
  154. package/lib/transforms/SkipRedundantNodesTransform.js +0 -261
  155. package/lib/transforms/SkipSplitOperationTransform.js +0 -32
  156. package/lib/transforms/SkipUnreachableNodeTransform.js +0 -158
  157. package/lib/transforms/SkipUnusedVariablesTransform.js +0 -75
  158. package/lib/transforms/SplitModuleImportTransform.js +0 -82
  159. package/lib/transforms/TestOperationTransform.js +0 -144
  160. package/lib/transforms/TransformUtils.js +0 -21
  161. package/lib/transforms/ValidateGlobalVariablesTransform.js +0 -92
  162. package/lib/transforms/ValidateRequiredArgumentsTransform.js +0 -114
  163. package/lib/transforms/ValidateServerOnlyDirectivesTransform.js +0 -108
  164. package/lib/transforms/ValidateUnusedVariablesTransform.js +0 -96
  165. package/lib/transforms/query-generators/FetchableQueryGenerator.js +0 -157
  166. package/lib/transforms/query-generators/NodeQueryGenerator.js +0 -166
  167. package/lib/transforms/query-generators/QueryQueryGenerator.js +0 -48
  168. package/lib/transforms/query-generators/ViewerQueryGenerator.js +0 -77
  169. package/lib/transforms/query-generators/index.js +0 -60
  170. package/lib/transforms/query-generators/utils.js +0 -92
  171. package/lib/util/CodeMarker.js +0 -80
  172. package/lib/util/DefaultHandleKey.js +0 -15
  173. package/lib/util/RelayCompilerCache.js +0 -97
  174. package/lib/util/Rollout.js +0 -40
  175. package/lib/util/TimeReporter.js +0 -83
  176. package/lib/util/areEqualArgValues.js +0 -135
  177. package/lib/util/argumentContainsVariables.js +0 -37
  178. package/lib/util/dedupeJSONStringify.js +0 -160
  179. package/lib/util/generateAbstractTypeRefinementKey.js +0 -24
  180. package/lib/util/getDefinitionNodeHash.js +0 -22
  181. package/lib/util/getModuleName.js +0 -32
  182. package/lib/util/joinArgumentDefinitions.js +0 -67
  183. package/lib/util/md5.js +0 -17
  184. package/lib/util/murmurHash.js +0 -86
  185. package/lib/util/nullthrowsOSS.js +0 -23
  186. package/lib/util/orList.js +0 -36
  187. package/lib/util/partitionArray.js +0 -35
  188. package/relay-compiler.js +0 -17
  189. package/relay-compiler.min.js +0 -22
  190. package/reporters/ConsoleReporter.js.flow +0 -81
  191. package/reporters/MultiReporter.js.flow +0 -43
  192. package/reporters/Reporter.js.flow +0 -19
  193. package/runner/Artifacts.js.flow +0 -215
  194. package/runner/BufferedFilesystem.js.flow +0 -194
  195. package/runner/GraphQLASTNodeGroup.js.flow +0 -174
  196. package/runner/GraphQLASTUtils.js.flow +0 -26
  197. package/runner/GraphQLNodeMap.js.flow +0 -55
  198. package/runner/Sources.js.flow +0 -227
  199. package/runner/StrictMap.js.flow +0 -96
  200. package/runner/compileArtifacts.js.flow +0 -75
  201. package/runner/extractAST.js.flow +0 -98
  202. package/runner/getChangedNodeNames.js.flow +0 -48
  203. package/runner/getSchemaInstance.js.flow +0 -36
  204. package/runner/types.js.flow +0 -37
  205. package/test-utils/TestSchema.js.flow +0 -30
  206. package/test-utils/parseGraphQLText.js.flow +0 -41
  207. package/transforms/ApplyFragmentArgumentTransform.js.flow +0 -524
  208. package/transforms/ClientExtensionsTransform.js.flow +0 -224
  209. package/transforms/ConnectionTransform.js.flow +0 -850
  210. package/transforms/DeclarativeConnectionMutationTransform.js.flow +0 -245
  211. package/transforms/DeferStreamTransform.js.flow +0 -263
  212. package/transforms/DisallowIdAsAlias.js.flow +0 -46
  213. package/transforms/DisallowTypenameOnRoot.js.flow +0 -44
  214. package/transforms/FieldHandleTransform.js.flow +0 -77
  215. package/transforms/FilterCompilerDirectivesTransform.js.flow +0 -33
  216. package/transforms/FilterDirectivesTransform.js.flow +0 -45
  217. package/transforms/FlattenTransform.js.flow +0 -458
  218. package/transforms/GenerateIDFieldTransform.js.flow +0 -151
  219. package/transforms/GenerateTypeNameTransform.js.flow +0 -159
  220. package/transforms/InlineDataFragmentTransform.js.flow +0 -123
  221. package/transforms/InlineFragmentsTransform.js.flow +0 -70
  222. package/transforms/MaskTransform.js.flow +0 -124
  223. package/transforms/MatchTransform.js.flow +0 -587
  224. package/transforms/ReactFlightComponentTransform.js.flow +0 -207
  225. package/transforms/RefetchableFragmentTransform.js.flow +0 -266
  226. package/transforms/RelayDirectiveTransform.js.flow +0 -96
  227. package/transforms/RequiredFieldTransform.js.flow +0 -413
  228. package/transforms/SkipClientExtensionsTransform.js.flow +0 -54
  229. package/transforms/SkipHandleFieldTransform.js.flow +0 -44
  230. package/transforms/SkipRedundantNodesTransform.js.flow +0 -277
  231. package/transforms/SkipSplitOperationTransform.js.flow +0 -37
  232. package/transforms/SkipUnreachableNodeTransform.js.flow +0 -148
  233. package/transforms/SkipUnusedVariablesTransform.js.flow +0 -59
  234. package/transforms/SplitModuleImportTransform.js.flow +0 -97
  235. package/transforms/TestOperationTransform.js.flow +0 -142
  236. package/transforms/TransformUtils.js.flow +0 -26
  237. package/transforms/ValidateGlobalVariablesTransform.js.flow +0 -80
  238. package/transforms/ValidateRequiredArgumentsTransform.js.flow +0 -130
  239. package/transforms/ValidateServerOnlyDirectivesTransform.js.flow +0 -128
  240. package/transforms/ValidateUnusedVariablesTransform.js.flow +0 -88
  241. package/transforms/query-generators/FetchableQueryGenerator.js.flow +0 -188
  242. package/transforms/query-generators/NodeQueryGenerator.js.flow +0 -217
  243. package/transforms/query-generators/QueryQueryGenerator.js.flow +0 -57
  244. package/transforms/query-generators/ViewerQueryGenerator.js.flow +0 -97
  245. package/transforms/query-generators/index.js.flow +0 -89
  246. package/transforms/query-generators/utils.js.flow +0 -76
  247. package/util/CodeMarker.js.flow +0 -79
  248. package/util/DefaultHandleKey.js.flow +0 -17
  249. package/util/RelayCompilerCache.js.flow +0 -86
  250. package/util/Rollout.js.flow +0 -39
  251. package/util/TimeReporter.js.flow +0 -79
  252. package/util/areEqualArgValues.js.flow +0 -126
  253. package/util/argumentContainsVariables.js.flow +0 -38
  254. package/util/dedupeJSONStringify.js.flow +0 -156
  255. package/util/generateAbstractTypeRefinementKey.js.flow +0 -29
  256. package/util/getDefinitionNodeHash.js.flow +0 -24
  257. package/util/getModuleName.js.flow +0 -39
  258. package/util/joinArgumentDefinitions.js.flow +0 -105
  259. package/util/md5.js.flow +0 -19
  260. package/util/murmurHash.js.flow +0 -94
  261. package/util/nullthrowsOSS.js.flow +0 -25
  262. package/util/orList.js.flow +0 -37
  263. 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
- };