relay-compiler 9.0.0 → 10.1.0
Sign up to get free protection for your applications and to get access to all the features.
- package/bin/RelayCompilerBin.js.flow +169 -0
- package/bin/RelayCompilerMain.js.flow +515 -0
- package/bin/__fixtures__/plugin-module.js.flow +17 -0
- package/bin/relay-compiler +3862 -2505
- package/codegen/CodegenDirectory.js.flow +375 -0
- package/codegen/CodegenRunner.js.flow +432 -0
- package/codegen/CodegenTypes.js.flow +28 -0
- package/codegen/CodegenWatcher.js.flow +254 -0
- package/codegen/NormalizationCodeGenerator.js.flow +571 -0
- package/codegen/ReaderCodeGenerator.js.flow +512 -0
- package/codegen/RelayCodeGenerator.js.flow +85 -0
- package/codegen/RelayFileWriter.js.flow +367 -0
- package/codegen/SourceControl.js.flow +58 -0
- package/codegen/compileRelayArtifacts.js.flow +182 -0
- package/codegen/createPrintRequireModuleDependency.js.flow +21 -0
- package/codegen/sortObjectByKey.js.flow +25 -0
- package/codegen/writeRelayGeneratedFile.js.flow +223 -0
- package/core/ASTCache.js.flow +74 -0
- package/core/ASTConvert.js.flow +233 -0
- package/core/CompilerContext.js.flow +191 -0
- package/core/CompilerError.js.flow +250 -0
- package/core/DotGraphQLParser.js.flow +39 -0
- package/core/GraphQLCompilerProfiler.js.flow +341 -0
- package/core/GraphQLDerivedFromMetadata.js.flow +36 -0
- package/core/GraphQLWatchmanClient.js.flow +111 -0
- package/core/IR.js.flow +326 -0
- package/core/IRPrinter.js.flow +477 -0
- package/core/IRTransformer.js.flow +377 -0
- package/core/IRValidator.js.flow +260 -0
- package/core/IRVisitor.js.flow +150 -0
- package/core/JSModuleParser.js.flow +24 -0
- package/core/RelayCompilerScope.js.flow +199 -0
- package/core/RelayFindGraphQLTags.js.flow +119 -0
- package/core/RelayGraphQLEnumsGenerator.js.flow +55 -0
- package/core/RelayIRTransforms.js.flow +138 -0
- package/core/RelayParser.js.flow +1731 -0
- package/core/RelaySourceModuleParser.js.flow +135 -0
- package/core/Schema.js.flow +2037 -0
- package/core/SchemaUtils.js.flow +120 -0
- package/core/filterContextForNode.js.flow +50 -0
- package/core/getFieldDefinition.js.flow +156 -0
- package/core/getIdentifierForArgumentValue.js.flow +49 -0
- package/core/getIdentifierForSelection.js.flow +69 -0
- package/core/getLiteralArgumentValues.js.flow +32 -0
- package/core/getNormalizationOperationName.js.flow +19 -0
- package/core/inferRootArgumentDefinitions.js.flow +323 -0
- package/index.js +1 -1
- package/index.js.flow +200 -0
- package/language/RelayLanguagePluginInterface.js.flow +283 -0
- package/language/javascript/FindGraphQLTags.js.flow +137 -0
- package/language/javascript/RelayFlowBabelFactories.js.flow +176 -0
- package/language/javascript/RelayFlowGenerator.js.flow +1099 -0
- package/language/javascript/RelayFlowTypeTransformers.js.flow +184 -0
- package/language/javascript/RelayLanguagePluginJavaScript.js.flow +34 -0
- package/language/javascript/formatGeneratedModule.js.flow +65 -0
- package/lib/bin/RelayCompilerBin.js +10 -0
- package/lib/bin/RelayCompilerMain.js +127 -130
- package/lib/codegen/CodegenDirectory.js +2 -6
- package/lib/codegen/CodegenRunner.js +37 -76
- package/lib/codegen/CodegenWatcher.js +13 -21
- package/lib/codegen/NormalizationCodeGenerator.js +131 -50
- package/lib/codegen/ReaderCodeGenerator.js +116 -49
- package/lib/codegen/RelayCodeGenerator.js +17 -6
- package/lib/codegen/RelayFileWriter.js +15 -37
- package/lib/codegen/compileRelayArtifacts.js +16 -30
- package/lib/codegen/sortObjectByKey.js +43 -0
- package/lib/codegen/writeRelayGeneratedFile.js +86 -96
- package/lib/core/ASTCache.js +3 -4
- package/lib/core/CompilerContext.js +3 -4
- package/lib/core/CompilerError.js +27 -54
- package/lib/core/GraphQLCompilerProfiler.js +8 -12
- package/lib/core/GraphQLDerivedFromMetadata.js +1 -10
- package/lib/core/GraphQLWatchmanClient.js +4 -12
- package/lib/core/IRPrinter.js +16 -21
- package/lib/core/IRTransformer.js +8 -6
- package/lib/core/IRValidator.js +1 -3
- package/lib/core/RelayCompilerScope.js +4 -4
- package/lib/core/RelayGraphQLEnumsGenerator.js +12 -15
- package/lib/core/RelayIRTransforms.js +23 -13
- package/lib/core/RelayParser.js +53 -89
- package/lib/core/RelaySourceModuleParser.js +1 -3
- package/lib/core/Schema.js +106 -77
- package/lib/core/SchemaUtils.js +15 -1
- package/lib/core/getFieldDefinition.js +12 -15
- package/lib/core/getIdentifierForSelection.js +1 -1
- package/lib/core/inferRootArgumentDefinitions.js +27 -36
- package/lib/index.js +1 -3
- package/lib/language/javascript/FindGraphQLTags.js +7 -72
- package/lib/language/javascript/RelayFlowBabelFactories.js +5 -5
- package/lib/language/javascript/RelayFlowGenerator.js +131 -108
- package/lib/language/javascript/RelayFlowTypeTransformers.js +1 -3
- package/lib/reporters/ConsoleReporter.js +1 -3
- package/lib/reporters/MultiReporter.js +1 -3
- package/lib/runner/Artifacts.js +69 -170
- package/lib/runner/BufferedFilesystem.js +32 -66
- package/lib/runner/GraphQLASTNodeGroup.js +54 -120
- package/lib/runner/GraphQLNodeMap.js +14 -19
- package/lib/runner/Sources.js +70 -85
- package/lib/runner/StrictMap.js +21 -37
- package/lib/runner/getChangedNodeNames.js +30 -62
- package/lib/transforms/ApplyFragmentArgumentTransform.js +71 -31
- package/lib/transforms/ClientExtensionsTransform.js +15 -15
- package/lib/transforms/ConnectionTransform.js +26 -38
- package/lib/transforms/DeclarativeConnectionMutationTransform.js +225 -0
- package/lib/transforms/DeferStreamTransform.js +27 -17
- package/lib/transforms/DisallowTypenameOnRoot.js +55 -0
- package/lib/transforms/FieldHandleTransform.js +7 -3
- package/lib/transforms/FilterCompilerDirectivesTransform.js +29 -0
- package/lib/transforms/FlattenTransform.js +23 -19
- package/lib/transforms/GenerateIDFieldTransform.js +56 -35
- package/lib/transforms/GenerateTypeNameTransform.js +84 -10
- package/lib/transforms/InlineDataFragmentTransform.js +9 -4
- package/lib/transforms/MaskTransform.js +17 -17
- package/lib/transforms/MatchTransform.js +114 -32
- package/lib/transforms/ReactFlightComponentTransform.js +162 -0
- package/lib/transforms/RefetchableFragmentTransform.js +21 -17
- package/lib/transforms/RelayDirectiveTransform.js +8 -3
- package/lib/transforms/RequiredFieldTransform.js +380 -0
- package/lib/transforms/SkipClientExtensionsTransform.js +8 -0
- package/lib/transforms/SkipHandleFieldTransform.js +6 -2
- package/lib/transforms/SkipRedundantNodesTransform.js +9 -2
- package/lib/transforms/SkipSplitOperationTransform.js +32 -0
- package/lib/transforms/SkipUnreachableNodeTransform.js +9 -2
- package/lib/transforms/SkipUnusedVariablesTransform.js +18 -17
- package/lib/transforms/SplitModuleImportTransform.js +2 -2
- package/lib/transforms/TestOperationTransform.js +26 -20
- package/lib/transforms/ValidateGlobalVariablesTransform.js +18 -30
- package/lib/transforms/ValidateRequiredArgumentsTransform.js +12 -15
- package/lib/transforms/ValidateServerOnlyDirectivesTransform.js +16 -30
- package/lib/transforms/ValidateUnusedVariablesTransform.js +18 -30
- package/lib/transforms/query-generators/FetchableQueryGenerator.js +161 -0
- package/lib/transforms/query-generators/NodeQueryGenerator.js +22 -3
- package/lib/transforms/query-generators/QueryQueryGenerator.js +2 -1
- package/lib/transforms/query-generators/ViewerQueryGenerator.js +1 -0
- package/lib/transforms/query-generators/index.js +23 -6
- package/lib/transforms/query-generators/utils.js +17 -16
- package/lib/util/RelayCompilerCache.js +2 -4
- package/lib/util/argumentContainsVariables.js +37 -0
- package/lib/util/dedupeJSONStringify.js +15 -12
- package/lib/util/generateAbstractTypeRefinementKey.js +24 -0
- package/lib/util/getModuleName.js +1 -1
- package/lib/util/joinArgumentDefinitions.js +3 -1
- package/package.json +7 -7
- package/relay-compiler.js +4 -4
- package/relay-compiler.min.js +4 -4
- package/reporters/ConsoleReporter.js.flow +81 -0
- package/reporters/MultiReporter.js.flow +43 -0
- package/reporters/Reporter.js.flow +19 -0
- package/runner/Artifacts.js.flow +219 -0
- package/runner/BufferedFilesystem.js.flow +194 -0
- package/runner/GraphQLASTNodeGroup.js.flow +176 -0
- package/runner/GraphQLASTUtils.js.flow +26 -0
- package/runner/GraphQLNodeMap.js.flow +55 -0
- package/runner/Sources.js.flow +228 -0
- package/runner/StrictMap.js.flow +96 -0
- package/runner/compileArtifacts.js.flow +76 -0
- package/runner/extractAST.js.flow +100 -0
- package/runner/getChangedNodeNames.js.flow +48 -0
- package/runner/getSchemaInstance.js.flow +36 -0
- package/runner/types.js.flow +37 -0
- package/transforms/ApplyFragmentArgumentTransform.js.flow +526 -0
- package/transforms/ClientExtensionsTransform.js.flow +224 -0
- package/transforms/ConnectionTransform.js.flow +855 -0
- package/transforms/DeclarativeConnectionMutationTransform.js.flow +246 -0
- package/transforms/DeferStreamTransform.js.flow +265 -0
- package/transforms/DisallowIdAsAlias.js.flow +47 -0
- package/transforms/DisallowTypenameOnRoot.js.flow +45 -0
- package/transforms/FieldHandleTransform.js.flow +79 -0
- package/transforms/FilterCompilerDirectivesTransform.js.flow +33 -0
- package/transforms/FilterDirectivesTransform.js.flow +45 -0
- package/transforms/FlattenTransform.js.flow +454 -0
- package/transforms/GenerateIDFieldTransform.js.flow +152 -0
- package/transforms/GenerateTypeNameTransform.js.flow +161 -0
- package/transforms/InlineDataFragmentTransform.js.flow +125 -0
- package/transforms/InlineFragmentsTransform.js.flow +71 -0
- package/transforms/MaskTransform.js.flow +126 -0
- package/transforms/MatchTransform.js.flow +589 -0
- package/transforms/ReactFlightComponentTransform.js.flow +195 -0
- package/transforms/RefetchableFragmentTransform.js.flow +272 -0
- package/transforms/RelayDirectiveTransform.js.flow +97 -0
- package/transforms/RequiredFieldTransform.js.flow +415 -0
- package/transforms/SkipClientExtensionsTransform.js.flow +54 -0
- package/transforms/SkipHandleFieldTransform.js.flow +44 -0
- package/transforms/SkipRedundantNodesTransform.js.flow +257 -0
- package/transforms/SkipSplitOperationTransform.js.flow +37 -0
- package/transforms/SkipUnreachableNodeTransform.js.flow +149 -0
- package/transforms/SkipUnusedVariablesTransform.js.flow +59 -0
- package/transforms/SplitModuleImportTransform.js.flow +98 -0
- package/transforms/TestOperationTransform.js.flow +142 -0
- package/transforms/TransformUtils.js.flow +26 -0
- package/transforms/ValidateGlobalVariablesTransform.js.flow +81 -0
- package/transforms/ValidateRequiredArgumentsTransform.js.flow +127 -0
- package/transforms/ValidateServerOnlyDirectivesTransform.js.flow +112 -0
- package/transforms/ValidateUnusedVariablesTransform.js.flow +89 -0
- package/transforms/query-generators/FetchableQueryGenerator.js.flow +189 -0
- package/transforms/query-generators/NodeQueryGenerator.js.flow +219 -0
- package/transforms/query-generators/QueryQueryGenerator.js.flow +57 -0
- package/transforms/query-generators/ViewerQueryGenerator.js.flow +97 -0
- package/transforms/query-generators/index.js.flow +90 -0
- package/transforms/query-generators/utils.js.flow +76 -0
- package/util/CodeMarker.js.flow +79 -0
- package/util/DefaultHandleKey.js.flow +17 -0
- package/util/RelayCompilerCache.js.flow +88 -0
- package/util/Rollout.js.flow +39 -0
- package/util/TimeReporter.js.flow +79 -0
- package/util/areEqualOSS.js.flow +123 -0
- package/util/argumentContainsVariables.js.flow +38 -0
- package/util/dedupeJSONStringify.js.flow +152 -0
- package/util/generateAbstractTypeRefinementKey.js.flow +29 -0
- package/util/getDefinitionNodeHash.js.flow +25 -0
- package/util/getModuleName.js.flow +39 -0
- package/util/joinArgumentDefinitions.js.flow +105 -0
- package/util/md5.js.flow +22 -0
- package/util/murmurHash.js.flow +94 -0
- package/util/nullthrowsOSS.js.flow +25 -0
- package/util/orList.js.flow +37 -0
- package/util/partitionArray.js.flow +37 -0
@@ -0,0 +1,375 @@
|
|
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
|
+
const Profiler = require('../core/GraphQLCompilerProfiler');
|
16
|
+
|
17
|
+
const crypto = require('crypto');
|
18
|
+
const invariant = require('invariant');
|
19
|
+
const path = require('path');
|
20
|
+
|
21
|
+
// flowlint nonstrict-import:warn
|
22
|
+
import type {KeepExtraFileFn} from './CodegenRunner';
|
23
|
+
import type {SourceControl} from './SourceControl';
|
24
|
+
|
25
|
+
type Changes = {|
|
26
|
+
+deleted: Array<string>,
|
27
|
+
+updated: Array<string>,
|
28
|
+
+created: Array<string>,
|
29
|
+
+unchanged: Array<string>,
|
30
|
+
|};
|
31
|
+
|
32
|
+
export interface Filesystem {
|
33
|
+
existsSync(path: string): boolean;
|
34
|
+
mkdirSync(path: string): void;
|
35
|
+
readdirSync(path: string): Array<string>;
|
36
|
+
readFileSync(path: string, encoding: string): string;
|
37
|
+
statSync(path: string): {isDirectory(): boolean, ...};
|
38
|
+
unlinkSync(path: string): void;
|
39
|
+
writeFileSync(filename: string, data: string, options: string): void;
|
40
|
+
}
|
41
|
+
|
42
|
+
/**
|
43
|
+
* CodegenDirectory is a helper class for scripts that generate code into one
|
44
|
+
* output directory. The purpose is to make it easy to only write files that
|
45
|
+
* have changed and delete files that are no longer generated.
|
46
|
+
* It gives statistics about added/removed/updated/unchanged in the end.
|
47
|
+
* The class also has an option to "validate" which means that no file
|
48
|
+
* operations are performed and only the statistics are created for what would
|
49
|
+
* have happened. If there's anything but "unchanged", someone probably forgot
|
50
|
+
* to run the codegen script.
|
51
|
+
*
|
52
|
+
* Example:
|
53
|
+
*
|
54
|
+
* const dir = new CodegenDirectory('/some/path/generated', {filesystem: require('fs')});
|
55
|
+
* // write files in case content changed (less watchman/mtime changes)
|
56
|
+
* dir.writeFile('OneFile.js', contents);
|
57
|
+
* dir.writeFile('OtherFile.js', contents);
|
58
|
+
*
|
59
|
+
* // delete files that are not generated
|
60
|
+
* dir.deleteExtraFiles();
|
61
|
+
*
|
62
|
+
* // arrays of file names to print or whatever
|
63
|
+
* dir.changes.created
|
64
|
+
* dir.changes.updated
|
65
|
+
* dir.changes.deleted
|
66
|
+
* dir.changes.unchanged
|
67
|
+
*/
|
68
|
+
|
69
|
+
type Options = {|
|
70
|
+
filesystem?: Filesystem,
|
71
|
+
onlyValidate: boolean,
|
72
|
+
shards?: number,
|
73
|
+
|};
|
74
|
+
|
75
|
+
class CodegenDirectory {
|
76
|
+
changes: Changes;
|
77
|
+
_filesystem: Filesystem;
|
78
|
+
_files: Set<string>;
|
79
|
+
_dir: string;
|
80
|
+
onlyValidate: boolean;
|
81
|
+
_shards: number;
|
82
|
+
|
83
|
+
constructor(dir: string, options: Options) {
|
84
|
+
this._filesystem = options.filesystem ?? require('fs');
|
85
|
+
this.onlyValidate = options.onlyValidate;
|
86
|
+
this._shards = options.shards ?? 1;
|
87
|
+
if (this._filesystem.existsSync(dir)) {
|
88
|
+
invariant(
|
89
|
+
this._filesystem.statSync(dir).isDirectory(),
|
90
|
+
'Expected `%s` to be a directory.',
|
91
|
+
dir,
|
92
|
+
);
|
93
|
+
}
|
94
|
+
if (!this.onlyValidate) {
|
95
|
+
const dirs = [];
|
96
|
+
let parent = dir;
|
97
|
+
while (!this._filesystem.existsSync(parent)) {
|
98
|
+
dirs.unshift(parent);
|
99
|
+
parent = path.dirname(parent);
|
100
|
+
}
|
101
|
+
dirs.forEach(d => this._filesystem.mkdirSync(d));
|
102
|
+
if (this._shards > 1) {
|
103
|
+
for (let shard = 0; shard < this._shards; shard++) {
|
104
|
+
const shardDir = path.join(dir, this._getShardName(shard));
|
105
|
+
if (this._filesystem.existsSync(shardDir)) {
|
106
|
+
invariant(
|
107
|
+
this._filesystem.statSync(dir).isDirectory(),
|
108
|
+
'Expected `%s` to be a directory.',
|
109
|
+
dir,
|
110
|
+
);
|
111
|
+
} else {
|
112
|
+
this._filesystem.mkdirSync(shardDir);
|
113
|
+
}
|
114
|
+
}
|
115
|
+
}
|
116
|
+
}
|
117
|
+
this._files = new Set();
|
118
|
+
this.changes = {
|
119
|
+
deleted: [],
|
120
|
+
updated: [],
|
121
|
+
created: [],
|
122
|
+
unchanged: [],
|
123
|
+
};
|
124
|
+
this._dir = dir;
|
125
|
+
}
|
126
|
+
|
127
|
+
static combineChanges(dirs: Array<CodegenDirectory>): Changes {
|
128
|
+
const changes = {
|
129
|
+
deleted: [],
|
130
|
+
updated: [],
|
131
|
+
created: [],
|
132
|
+
unchanged: [],
|
133
|
+
};
|
134
|
+
dirs.forEach(dir => {
|
135
|
+
changes.deleted.push(...dir.changes.deleted);
|
136
|
+
changes.updated.push(...dir.changes.updated);
|
137
|
+
changes.created.push(...dir.changes.created);
|
138
|
+
changes.unchanged.push(...dir.changes.unchanged);
|
139
|
+
});
|
140
|
+
return changes;
|
141
|
+
}
|
142
|
+
|
143
|
+
static hasChanges(changes: Changes): boolean {
|
144
|
+
return (
|
145
|
+
changes.created.length > 0 ||
|
146
|
+
changes.updated.length > 0 ||
|
147
|
+
changes.deleted.length > 0
|
148
|
+
);
|
149
|
+
}
|
150
|
+
|
151
|
+
static formatChanges(
|
152
|
+
changes: Changes,
|
153
|
+
options: {onlyValidate: boolean, ...},
|
154
|
+
): string {
|
155
|
+
const output = [];
|
156
|
+
function formatFiles(label, files) {
|
157
|
+
if (files.length > 0) {
|
158
|
+
output.push(label + ':');
|
159
|
+
files.forEach(file => {
|
160
|
+
output.push(' - ' + file);
|
161
|
+
});
|
162
|
+
}
|
163
|
+
}
|
164
|
+
if (options.onlyValidate) {
|
165
|
+
formatFiles('Missing', changes.created);
|
166
|
+
formatFiles('Out of date', changes.updated);
|
167
|
+
formatFiles('Extra', changes.deleted);
|
168
|
+
} else {
|
169
|
+
formatFiles('Created', changes.created);
|
170
|
+
formatFiles('Updated', changes.updated);
|
171
|
+
formatFiles('Deleted', changes.deleted);
|
172
|
+
output.push(`Unchanged: ${changes.unchanged.length} files`);
|
173
|
+
}
|
174
|
+
|
175
|
+
return output.join('\n');
|
176
|
+
}
|
177
|
+
|
178
|
+
static printChanges(
|
179
|
+
changes: Changes,
|
180
|
+
options: {onlyValidate: boolean, ...},
|
181
|
+
): void {
|
182
|
+
Profiler.run('CodegenDirectory.printChanges', () => {
|
183
|
+
const output = CodegenDirectory.formatChanges(changes, options);
|
184
|
+
|
185
|
+
// eslint-disable-next-line no-console
|
186
|
+
console.log(output);
|
187
|
+
});
|
188
|
+
}
|
189
|
+
|
190
|
+
static getAddedRemovedFiles(
|
191
|
+
dirs: $ReadOnlyArray<CodegenDirectory>,
|
192
|
+
): {|
|
193
|
+
+added: $ReadOnlyArray<string>,
|
194
|
+
+removed: $ReadOnlyArray<string>,
|
195
|
+
|} {
|
196
|
+
const added = [];
|
197
|
+
const removed = [];
|
198
|
+
dirs.forEach(dir => {
|
199
|
+
dir.changes.created.forEach(name => {
|
200
|
+
added.push(dir.getPath(name));
|
201
|
+
});
|
202
|
+
dir.changes.deleted.forEach(name => {
|
203
|
+
removed.push(dir.getPath(name));
|
204
|
+
});
|
205
|
+
});
|
206
|
+
return {
|
207
|
+
added,
|
208
|
+
removed,
|
209
|
+
};
|
210
|
+
}
|
211
|
+
|
212
|
+
static async sourceControlAddRemove(
|
213
|
+
sourceControl: SourceControl,
|
214
|
+
dirs: $ReadOnlyArray<CodegenDirectory>,
|
215
|
+
): Promise<void> {
|
216
|
+
const {added, removed} = CodegenDirectory.getAddedRemovedFiles(dirs);
|
217
|
+
sourceControl.addRemove(added, removed);
|
218
|
+
}
|
219
|
+
|
220
|
+
printChanges(): void {
|
221
|
+
CodegenDirectory.printChanges(this.changes, {
|
222
|
+
onlyValidate: this.onlyValidate,
|
223
|
+
});
|
224
|
+
}
|
225
|
+
|
226
|
+
read(filename: string): ?string {
|
227
|
+
const filePath = path.join(this._dir, filename);
|
228
|
+
if (this._filesystem.existsSync(filePath)) {
|
229
|
+
return this._filesystem.readFileSync(filePath, 'utf8');
|
230
|
+
}
|
231
|
+
return null;
|
232
|
+
}
|
233
|
+
|
234
|
+
markUnchanged(filename: string) {
|
235
|
+
this._addGenerated(filename);
|
236
|
+
this.changes.unchanged.push(filename);
|
237
|
+
}
|
238
|
+
|
239
|
+
/**
|
240
|
+
* Marks a files as updated or out of date without actually writing the file.
|
241
|
+
* This is probably only be useful when doing validation without intention to
|
242
|
+
* actually write to disk.
|
243
|
+
*/
|
244
|
+
markUpdated(filename: string) {
|
245
|
+
this._addGenerated(filename);
|
246
|
+
this.changes.updated.push(filename);
|
247
|
+
}
|
248
|
+
|
249
|
+
writeFile(
|
250
|
+
filename: string,
|
251
|
+
content: string,
|
252
|
+
shouldRepersist: boolean = false,
|
253
|
+
): void {
|
254
|
+
Profiler.run('CodegenDirectory.writeFile', () => {
|
255
|
+
this._addGenerated(filename);
|
256
|
+
const filePath = this.getPath(filename);
|
257
|
+
if (this._filesystem.existsSync(filePath)) {
|
258
|
+
const existingContent = this._filesystem.readFileSync(filePath, 'utf8');
|
259
|
+
if (existingContent === content && !shouldRepersist) {
|
260
|
+
this.changes.unchanged.push(filename);
|
261
|
+
} else {
|
262
|
+
this._writeFile(filePath, content);
|
263
|
+
this.changes.updated.push(filename);
|
264
|
+
}
|
265
|
+
} else {
|
266
|
+
this._writeFile(filePath, content);
|
267
|
+
this.changes.created.push(filename);
|
268
|
+
}
|
269
|
+
});
|
270
|
+
}
|
271
|
+
|
272
|
+
_writeFile(filePath: string, content: string): void {
|
273
|
+
if (!this.onlyValidate) {
|
274
|
+
this._filesystem.writeFileSync(filePath, content, 'utf8');
|
275
|
+
}
|
276
|
+
}
|
277
|
+
|
278
|
+
/**
|
279
|
+
* Deletes all non-generated files, except for invisible "dot" files (ie.
|
280
|
+
* files with names starting with ".").
|
281
|
+
*/
|
282
|
+
deleteExtraFiles(keepExtraFile?: KeepExtraFileFn): void {
|
283
|
+
Profiler.run('CodegenDirectory.deleteExtraFiles', () => {
|
284
|
+
if (this._shards > 1) {
|
285
|
+
this._filesystem.readdirSync(this._dir).forEach(firstLevel => {
|
286
|
+
if (firstLevel.startsWith('.')) {
|
287
|
+
// allow hidden files on the first level of the codegen directory
|
288
|
+
return;
|
289
|
+
}
|
290
|
+
const firstLevelPath = path.join(this._dir, firstLevel);
|
291
|
+
if (!this._filesystem.statSync(firstLevelPath).isDirectory()) {
|
292
|
+
// Delete all files on the top level, all files need to be in a
|
293
|
+
// shard directory.
|
294
|
+
this._filesystem.unlinkSync(firstLevelPath);
|
295
|
+
return;
|
296
|
+
}
|
297
|
+
this._filesystem.readdirSync(firstLevelPath).forEach(actualFile => {
|
298
|
+
if (keepExtraFile && keepExtraFile(actualFile)) {
|
299
|
+
return;
|
300
|
+
}
|
301
|
+
if (this._files.has(actualFile)) {
|
302
|
+
return;
|
303
|
+
}
|
304
|
+
if (!this.onlyValidate) {
|
305
|
+
try {
|
306
|
+
this._filesystem.unlinkSync(
|
307
|
+
path.join(firstLevelPath, actualFile),
|
308
|
+
);
|
309
|
+
} catch {
|
310
|
+
throw new Error(
|
311
|
+
'CodegenDirectory: Failed to delete `' +
|
312
|
+
actualFile +
|
313
|
+
'` in `' +
|
314
|
+
this._dir +
|
315
|
+
'`.',
|
316
|
+
);
|
317
|
+
}
|
318
|
+
}
|
319
|
+
this.changes.deleted.push(actualFile);
|
320
|
+
});
|
321
|
+
});
|
322
|
+
} else {
|
323
|
+
this._filesystem.readdirSync(this._dir).forEach(actualFile => {
|
324
|
+
if (keepExtraFile && keepExtraFile(actualFile)) {
|
325
|
+
return;
|
326
|
+
}
|
327
|
+
if (actualFile.startsWith('.') || this._files.has(actualFile)) {
|
328
|
+
return;
|
329
|
+
}
|
330
|
+
if (!this.onlyValidate) {
|
331
|
+
try {
|
332
|
+
this._filesystem.unlinkSync(path.join(this._dir, actualFile));
|
333
|
+
} catch {
|
334
|
+
throw new Error(
|
335
|
+
'CodegenDirectory: Failed to delete `' +
|
336
|
+
actualFile +
|
337
|
+
'` in `' +
|
338
|
+
this._dir +
|
339
|
+
'`.',
|
340
|
+
);
|
341
|
+
}
|
342
|
+
}
|
343
|
+
this.changes.deleted.push(actualFile);
|
344
|
+
});
|
345
|
+
}
|
346
|
+
});
|
347
|
+
}
|
348
|
+
|
349
|
+
getPath(filename: string): string {
|
350
|
+
if (this._shards > 1) {
|
351
|
+
const hasher = crypto.createHash('md5');
|
352
|
+
hasher.update(filename, 'utf8');
|
353
|
+
const shard = hasher.digest().readUInt32BE(0) % this._shards;
|
354
|
+
return path.join(this._dir, this._getShardName(shard), filename);
|
355
|
+
}
|
356
|
+
return path.join(this._dir, filename);
|
357
|
+
}
|
358
|
+
|
359
|
+
_getShardName(shardNumber: number): string {
|
360
|
+
const base16length = Math.ceil(Math.log2(256) / 4);
|
361
|
+
return shardNumber.toString(16).padStart(base16length, '0');
|
362
|
+
}
|
363
|
+
|
364
|
+
_addGenerated(filename: string): void {
|
365
|
+
invariant(
|
366
|
+
!this._files.has(filename),
|
367
|
+
'CodegenDirectory: Tried to generate `%s` twice in `%s`.',
|
368
|
+
filename,
|
369
|
+
this._dir,
|
370
|
+
);
|
371
|
+
this._files.add(filename);
|
372
|
+
}
|
373
|
+
}
|
374
|
+
|
375
|
+
module.exports = CodegenDirectory;
|