1 | import { __awaiter, __generator, __spreadArray, __read, __assign, __values } from 'tslib';
|
2 | import { isValidPath, asArray, parseGraphQLSDL } from '@graphql-tools/utils/es5';
|
3 | import { isAbsolute, resolve } from 'path';
|
4 | import { promises, existsSync, readFileSync } from 'fs';
|
5 | import { cwd } from 'process';
|
6 | import { processImport } from '@graphql-tools/import/es5';
|
7 | import globby from 'globby';
|
8 | import unixify from 'unixify';
|
9 |
|
10 | var readFile = promises.readFile, access = promises.access;
|
11 | var FILE_EXTENSIONS = ['.gql', '.gqls', '.graphql', '.graphqls'];
|
12 | function isGraphQLImportFile(rawSDL) {
|
13 | var trimmedRawSDL = rawSDL.trim();
|
14 | return trimmedRawSDL.startsWith('# import') || trimmedRawSDL.startsWith('#import');
|
15 | }
|
16 | function createGlobbyOptions(options) {
|
17 | return __assign(__assign({ absolute: true }, options), { ignore: [] });
|
18 | }
|
19 | var buildIgnoreGlob = function (path) { return "!" + path; };
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 | var GraphQLFileLoader = (function () {
|
44 | function GraphQLFileLoader() {
|
45 | }
|
46 | GraphQLFileLoader.prototype.canLoad = function (pointer, options) {
|
47 | return __awaiter(this, void 0, void 0, function () {
|
48 | var normalizedFilePath, _a;
|
49 | return __generator(this, function (_b) {
|
50 | switch (_b.label) {
|
51 | case 0:
|
52 | if (!isValidPath(pointer)) return [3 , 4];
|
53 | if (!FILE_EXTENSIONS.find(function (extension) { return pointer.endsWith(extension); })) return [3 , 4];
|
54 | normalizedFilePath = isAbsolute(pointer) ? pointer : resolve(options.cwd || cwd(), pointer);
|
55 | _b.label = 1;
|
56 | case 1:
|
57 | _b.trys.push([1, 3, , 4]);
|
58 | return [4 , access(normalizedFilePath)];
|
59 | case 2:
|
60 | _b.sent();
|
61 | return [2 , true];
|
62 | case 3:
|
63 | _a = _b.sent();
|
64 | return [2 , false];
|
65 | case 4: return [2 , false];
|
66 | }
|
67 | });
|
68 | });
|
69 | };
|
70 | GraphQLFileLoader.prototype.canLoadSync = function (pointer, options) {
|
71 | if (isValidPath(pointer)) {
|
72 | if (FILE_EXTENSIONS.find(function (extension) { return pointer.endsWith(extension); })) {
|
73 | var normalizedFilePath = isAbsolute(pointer) ? pointer : resolve(options.cwd || cwd(), pointer);
|
74 | return existsSync(normalizedFilePath);
|
75 | }
|
76 | }
|
77 | return false;
|
78 | };
|
79 | GraphQLFileLoader.prototype._buildGlobs = function (glob, options) {
|
80 | var ignores = asArray(options.ignore || []);
|
81 | var globs = __spreadArray([unixify(glob)], __read(ignores.map(function (v) { return buildIgnoreGlob(unixify(v)); })));
|
82 | return globs;
|
83 | };
|
84 | GraphQLFileLoader.prototype.resolveGlobs = function (glob, options) {
|
85 | return __awaiter(this, void 0, void 0, function () {
|
86 | var globs, result;
|
87 | return __generator(this, function (_a) {
|
88 | switch (_a.label) {
|
89 | case 0:
|
90 | globs = this._buildGlobs(glob, options);
|
91 | return [4 , globby(globs, createGlobbyOptions(options))];
|
92 | case 1:
|
93 | result = _a.sent();
|
94 | return [2 , result];
|
95 | }
|
96 | });
|
97 | });
|
98 | };
|
99 | GraphQLFileLoader.prototype.resolveGlobsSync = function (glob, options) {
|
100 | var globs = this._buildGlobs(glob, options);
|
101 | var result = globby.sync(globs, createGlobbyOptions(options));
|
102 | return result;
|
103 | };
|
104 | GraphQLFileLoader.prototype.load = function (pointer, options) {
|
105 | return __awaiter(this, void 0, void 0, function () {
|
106 | var resolvedPaths, finalResult;
|
107 | var _this = this;
|
108 | return __generator(this, function (_a) {
|
109 | switch (_a.label) {
|
110 | case 0: return [4 , this.resolveGlobs(pointer, options)];
|
111 | case 1:
|
112 | resolvedPaths = _a.sent();
|
113 | finalResult = [];
|
114 | return [4 , Promise.all(resolvedPaths.map(function (path) { return __awaiter(_this, void 0, void 0, function () {
|
115 | var normalizedFilePath, rawSDL;
|
116 | return __generator(this, function (_a) {
|
117 | switch (_a.label) {
|
118 | case 0: return [4 , this.canLoad(path, options)];
|
119 | case 1:
|
120 | if (!_a.sent()) return [3 , 3];
|
121 | normalizedFilePath = isAbsolute(path) ? path : resolve(options.cwd || cwd(), path);
|
122 | return [4 , readFile(normalizedFilePath, { encoding: 'utf8' })];
|
123 | case 2:
|
124 | rawSDL = _a.sent();
|
125 | finalResult.push(this.handleFileContent(rawSDL, normalizedFilePath, options));
|
126 | _a.label = 3;
|
127 | case 3: return [2 ];
|
128 | }
|
129 | });
|
130 | }); }))];
|
131 | case 2:
|
132 | _a.sent();
|
133 | return [2 , finalResult];
|
134 | }
|
135 | });
|
136 | });
|
137 | };
|
138 | GraphQLFileLoader.prototype.loadSync = function (pointer, options) {
|
139 | var e_1, _a;
|
140 | var resolvedPaths = this.resolveGlobsSync(pointer, options);
|
141 | var finalResult = [];
|
142 | try {
|
143 | for (var resolvedPaths_1 = __values(resolvedPaths), resolvedPaths_1_1 = resolvedPaths_1.next(); !resolvedPaths_1_1.done; resolvedPaths_1_1 = resolvedPaths_1.next()) {
|
144 | var path = resolvedPaths_1_1.value;
|
145 | if (this.canLoadSync(path, options)) {
|
146 | var normalizedFilePath = isAbsolute(path) ? path : resolve(options.cwd || cwd(), path);
|
147 | var rawSDL = readFileSync(normalizedFilePath, { encoding: 'utf8' });
|
148 | finalResult.push(this.handleFileContent(rawSDL, normalizedFilePath, options));
|
149 | }
|
150 | }
|
151 | }
|
152 | catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
153 | finally {
|
154 | try {
|
155 | if (resolvedPaths_1_1 && !resolvedPaths_1_1.done && (_a = resolvedPaths_1.return)) _a.call(resolvedPaths_1);
|
156 | }
|
157 | finally { if (e_1) throw e_1.error; }
|
158 | }
|
159 | return finalResult;
|
160 | };
|
161 | GraphQLFileLoader.prototype.handleFileContent = function (rawSDL, pointer, options) {
|
162 | if (!options.skipGraphQLImport && isGraphQLImportFile(rawSDL)) {
|
163 | var document_1 = processImport(pointer, options.cwd);
|
164 | return {
|
165 | location: pointer,
|
166 | document: document_1,
|
167 | };
|
168 | }
|
169 | return parseGraphQLSDL(pointer, rawSDL, options);
|
170 | };
|
171 | return GraphQLFileLoader;
|
172 | }());
|
173 |
|
174 | export { GraphQLFileLoader };
|