1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | 'use strict';
|
11 |
|
12 | var _asyncToGenerator = require("@babel/runtime/helpers/asyncToGenerator");
|
13 |
|
14 | var SUBSCRIPTION_NAME = 'graphql-codegen';
|
15 | var QUERY_RETRIES = 3;
|
16 |
|
17 | function queryFiles(_x, _x2, _x3) {
|
18 | return _queryFiles.apply(this, arguments);
|
19 | }
|
20 |
|
21 | function _queryFiles() {
|
22 | _queryFiles = _asyncToGenerator(function* (baseDir, expression, filter) {
|
23 | return yield require("./GraphQLCompilerProfiler").waitFor('Watchman:query',
|
24 |
|
25 | _asyncToGenerator(function* () {
|
26 | var client = new (require("./GraphQLWatchmanClient"))(QUERY_RETRIES);
|
27 |
|
28 | var _ref = yield Promise.all([client.watchProject(baseDir), getFields(client)]),
|
29 | watchResp = _ref[0],
|
30 | fields = _ref[1];
|
31 |
|
32 | var resp = yield client.command('query', watchResp.root, {
|
33 | expression: expression,
|
34 | fields: fields,
|
35 | relative_root: watchResp.relativePath
|
36 | });
|
37 | client.end();
|
38 | return updateFiles(new Set(), baseDir, filter, resp.files);
|
39 | }));
|
40 | });
|
41 | return _queryFiles.apply(this, arguments);
|
42 | }
|
43 |
|
44 | function queryDirectories(_x4, _x5) {
|
45 | return _queryDirectories.apply(this, arguments);
|
46 | }
|
47 |
|
48 | function _queryDirectories() {
|
49 | _queryDirectories = _asyncToGenerator(function* (baseDir, expression) {
|
50 | return yield require("./GraphQLCompilerProfiler").waitFor('Watchman:query',
|
51 |
|
52 | _asyncToGenerator(function* () {
|
53 | var client = new (require("./GraphQLWatchmanClient"))();
|
54 | var watchResp = yield client.watchProject(baseDir);
|
55 | var resp = yield client.command('query', watchResp.root, {
|
56 | expression: expression,
|
57 | fields: ['name'],
|
58 | relative_root: watchResp.relativePath
|
59 | });
|
60 | client.end();
|
61 | return resp.files;
|
62 | }));
|
63 | });
|
64 | return _queryDirectories.apply(this, arguments);
|
65 | }
|
66 |
|
67 | function getFields(_x6) {
|
68 | return _getFields.apply(this, arguments);
|
69 | }
|
70 |
|
71 |
|
72 | function _getFields() {
|
73 | _getFields = _asyncToGenerator(function* (client) {
|
74 | var fields = ['name', 'exists'];
|
75 |
|
76 | if (yield client.hasCapability('field-content.sha1hex')) {
|
77 | fields.push('content.sha1hex');
|
78 | }
|
79 |
|
80 | return fields;
|
81 | });
|
82 | return _getFields.apply(this, arguments);
|
83 | }
|
84 |
|
85 | function queryFilepaths(_x7, _x8, _x9) {
|
86 | return _queryFilepaths.apply(this, arguments);
|
87 | }
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 | function _queryFilepaths() {
|
96 | _queryFilepaths = _asyncToGenerator(function* (baseDir, filepaths, filter) {
|
97 |
|
98 |
|
99 | var files = filepaths.map(function (filepath) {
|
100 | return {
|
101 | name: filepath,
|
102 | exists: true,
|
103 | 'content.sha1hex': null
|
104 | };
|
105 | });
|
106 | return updateFiles(new Set(), baseDir, filter, files);
|
107 | });
|
108 | return _queryFilepaths.apply(this, arguments);
|
109 | }
|
110 |
|
111 | function watch(_x10, _x11, _x12) {
|
112 | return _watch.apply(this, arguments);
|
113 | }
|
114 |
|
115 | function _watch() {
|
116 | _watch = _asyncToGenerator(function* (baseDir, expression, callback) {
|
117 | return yield require("./GraphQLCompilerProfiler").waitFor('Watchman:subscribe',
|
118 |
|
119 | _asyncToGenerator(function* () {
|
120 | var client = new (require("./GraphQLWatchmanClient"))();
|
121 | var watchResp = yield client.watchProject(baseDir);
|
122 | yield makeSubscription(client, watchResp.root, watchResp.relativePath, expression, callback);
|
123 | }));
|
124 | });
|
125 | return _watch.apply(this, arguments);
|
126 | }
|
127 |
|
128 | function makeSubscription(_x13, _x14, _x15, _x16, _x17) {
|
129 | return _makeSubscription.apply(this, arguments);
|
130 | }
|
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 | function _makeSubscription() {
|
138 | _makeSubscription = _asyncToGenerator(function* (client, root, relativePath, expression, callback) {
|
139 | client.on('subscription', function (resp) {
|
140 | if (resp.subscription === SUBSCRIPTION_NAME) {
|
141 | callback(resp);
|
142 | }
|
143 | });
|
144 | var fields = yield getFields(client);
|
145 | yield client.command('subscribe', root, SUBSCRIPTION_NAME, {
|
146 | expression: expression,
|
147 | fields: fields,
|
148 | relative_root: relativePath
|
149 | });
|
150 | });
|
151 | return _makeSubscription.apply(this, arguments);
|
152 | }
|
153 |
|
154 | function watchFiles(_x18, _x19, _x20, _x21) {
|
155 | return _watchFiles.apply(this, arguments);
|
156 | }
|
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 | function _watchFiles() {
|
169 | _watchFiles = _asyncToGenerator(function* (baseDir, expression, filter, callback) {
|
170 | var files = new Set();
|
171 | yield watch(baseDir, expression, function (changes) {
|
172 | if (!changes.files) {
|
173 |
|
174 |
|
175 | return;
|
176 | }
|
177 |
|
178 | files = updateFiles(files, baseDir, filter, changes.files);
|
179 | callback(files);
|
180 | });
|
181 | });
|
182 | return _watchFiles.apply(this, arguments);
|
183 | }
|
184 |
|
185 | function watchCompile(_x22, _x23, _x24, _x25) {
|
186 | return _watchCompile.apply(this, arguments);
|
187 | }
|
188 |
|
189 | function _watchCompile() {
|
190 | _watchCompile = _asyncToGenerator(function* (baseDir, expression, filter, compile) {
|
191 | var compiling = false;
|
192 | var needsCompiling = false;
|
193 | var latestFiles = null;
|
194 | watchFiles(baseDir, expression, filter,
|
195 |
|
196 | function () {
|
197 | var _ref5 = _asyncToGenerator(function* (files) {
|
198 | needsCompiling = true;
|
199 | latestFiles = files;
|
200 |
|
201 | if (compiling) {
|
202 | return;
|
203 | }
|
204 |
|
205 | compiling = true;
|
206 |
|
207 | while (needsCompiling) {
|
208 | needsCompiling = false;
|
209 | yield compile(latestFiles);
|
210 | }
|
211 |
|
212 | compiling = false;
|
213 | });
|
214 |
|
215 | return function (_x26) {
|
216 | return _ref5.apply(this, arguments);
|
217 | };
|
218 | }());
|
219 | });
|
220 | return _watchCompile.apply(this, arguments);
|
221 | }
|
222 |
|
223 | function updateFiles(files, baseDir, filter, fileChanges) {
|
224 | var fileMap = new Map();
|
225 | files.forEach(function (file) {
|
226 | file.exists && fileMap.set(file.relPath, file);
|
227 | });
|
228 | fileChanges.forEach(function (_ref2) {
|
229 | var name = _ref2.name,
|
230 | exists = _ref2.exists,
|
231 | hash = _ref2['content.sha1hex'];
|
232 | var shouldRemove = !exists;
|
233 |
|
234 | if (!shouldRemove) {
|
235 | var _file = {
|
236 | exists: true,
|
237 | relPath: name,
|
238 | hash: hash || hashFile(require("path").join(baseDir, name))
|
239 | };
|
240 |
|
241 | if (filter(_file)) {
|
242 | fileMap.set(name, _file);
|
243 | } else {
|
244 | shouldRemove = true;
|
245 | }
|
246 | }
|
247 |
|
248 | shouldRemove && fileMap.set(name, {
|
249 | exists: false,
|
250 | relPath: name
|
251 | });
|
252 | });
|
253 | return new Set(fileMap.values());
|
254 | }
|
255 |
|
256 | function hashFile(filename) {
|
257 | var content = require("fs").readFileSync(filename);
|
258 |
|
259 | return require("crypto").createHash('sha1').update(content).digest('hex');
|
260 | }
|
261 |
|
262 | module.exports = {
|
263 | queryDirectories: queryDirectories,
|
264 | queryFiles: queryFiles,
|
265 | queryFilepaths: queryFilepaths,
|
266 | watch: watch,
|
267 | watchFiles: watchFiles,
|
268 | watchCompile: watchCompile
|
269 | }; |
\ | No newline at end of file |