@graphql-tools/load-files 6.4.0-alpha-5cac3eec.0 → 6.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,5 +1,5 @@
1
1
  Check API Reference for more information about this package;
2
- https://www.graphql-tools.com/docs/api/modules/load-files
2
+ https://www.graphql-tools.com/docs/api/modules/load_files_src
3
3
 
4
4
  You can also learn more about File Loading in this chapter;
5
5
  https://www.graphql-tools.com/docs/schema-merging#file-loading
package/index.js CHANGED
@@ -28,6 +28,7 @@ const globby__default = _interopDefault(globby);
28
28
  const unixify = _interopDefault(require('unixify'));
29
29
  const path = require('path');
30
30
  const fs = require('fs');
31
+ const graphql = require('graphql');
31
32
 
32
33
  const { readFile, stat } = fs.promises;
33
34
  const DEFAULT_IGNORED_EXTENSIONS = ['spec', 'test', 'd', 'map'];
@@ -129,7 +130,8 @@ function loadFilesSync(pattern, options = LoadFilesDefaultOptions) {
129
130
  return extractedExport;
130
131
  }
131
132
  else {
132
- return fs.readFileSync(path$1, { encoding: 'utf-8' });
133
+ const maybeSDL = fs.readFileSync(path$1, { encoding: 'utf-8' });
134
+ return tryToParse(maybeSDL);
133
135
  }
134
136
  })
135
137
  .filter(v => v);
@@ -188,7 +190,8 @@ async function loadFiles(pattern, options = LoadFilesDefaultOptions) {
188
190
  return extractedExport;
189
191
  }
190
192
  else {
191
- return readFile(path$1, { encoding: 'utf-8' });
193
+ const maybeSDL = await readFile(path$1, { encoding: 'utf-8' });
194
+ return tryToParse(maybeSDL);
192
195
  }
193
196
  }));
194
197
  }
@@ -196,6 +199,14 @@ function isIndex(path, extensions = []) {
196
199
  const IS_INDEX = /(\/|\\)index\.[^\/\\]+$/i; // (/ or \) AND `index.` AND (everything except \ and /)(end of line)
197
200
  return IS_INDEX.test(path) && extensions.some(ext => path.endsWith(formatExtension(ext)));
198
201
  }
202
+ function tryToParse(maybeSDL) {
203
+ try {
204
+ return graphql.parse(maybeSDL);
205
+ }
206
+ catch (e) {
207
+ return maybeSDL;
208
+ }
209
+ }
199
210
 
200
211
  exports.loadFiles = loadFiles;
201
212
  exports.loadFilesSync = loadFilesSync;
package/index.mjs CHANGED
@@ -2,6 +2,7 @@ import globby, { sync } from 'globby';
2
2
  import unixify from 'unixify';
3
3
  import { extname } from 'path';
4
4
  import { readFileSync, promises, statSync } from 'fs';
5
+ import { parse } from 'graphql';
5
6
 
6
7
  const { readFile, stat } = promises;
7
8
  const DEFAULT_IGNORED_EXTENSIONS = ['spec', 'test', 'd', 'map'];
@@ -103,7 +104,8 @@ function loadFilesSync(pattern, options = LoadFilesDefaultOptions) {
103
104
  return extractedExport;
104
105
  }
105
106
  else {
106
- return readFileSync(path, { encoding: 'utf-8' });
107
+ const maybeSDL = readFileSync(path, { encoding: 'utf-8' });
108
+ return tryToParse(maybeSDL);
107
109
  }
108
110
  })
109
111
  .filter(v => v);
@@ -162,7 +164,8 @@ async function loadFiles(pattern, options = LoadFilesDefaultOptions) {
162
164
  return extractedExport;
163
165
  }
164
166
  else {
165
- return readFile(path, { encoding: 'utf-8' });
167
+ const maybeSDL = await readFile(path, { encoding: 'utf-8' });
168
+ return tryToParse(maybeSDL);
166
169
  }
167
170
  }));
168
171
  }
@@ -170,5 +173,13 @@ function isIndex(path, extensions = []) {
170
173
  const IS_INDEX = /(\/|\\)index\.[^\/\\]+$/i; // (/ or \) AND `index.` AND (everything except \ and /)(end of line)
171
174
  return IS_INDEX.test(path) && extensions.some(ext => path.endsWith(formatExtension(ext)));
172
175
  }
176
+ function tryToParse(maybeSDL) {
177
+ try {
178
+ return parse(maybeSDL);
179
+ }
180
+ catch (e) {
181
+ return maybeSDL;
182
+ }
183
+ }
173
184
 
174
185
  export { loadFiles, loadFilesSync };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@graphql-tools/load-files",
3
- "version": "6.4.0-alpha-5cac3eec.0",
3
+ "version": "6.5.1",
4
4
  "description": "A set of utils for faster development of GraphQL tools",
5
5
  "sideEffects": false,
6
6
  "peerDependencies": {
package/es5/README.md DELETED
@@ -1,5 +0,0 @@
1
- Check API Reference for more information about this package;
2
- https://www.graphql-tools.com/docs/api/modules/load-files
3
-
4
- You can also learn more about File Loading in this chapter;
5
- https://www.graphql-tools.com/docs/schema-merging#file-loading
package/es5/index.d.ts DELETED
@@ -1,27 +0,0 @@
1
- import { GlobbyOptions } from 'globby';
2
- /**
3
- * Additional options for loading files
4
- */
5
- export interface LoadFilesOptions {
6
- ignoredExtensions?: string[];
7
- extensions?: string[];
8
- useRequire?: boolean;
9
- requireMethod?: any;
10
- globOptions?: GlobbyOptions;
11
- exportNames?: string[];
12
- recursive?: boolean;
13
- ignoreIndex?: boolean;
14
- extractExports?: (fileExport: any) => any;
15
- }
16
- /**
17
- * Synchronously loads files using the provided glob pattern.
18
- * @param pattern Glob pattern or patterns to use when loading files
19
- * @param options Additional options
20
- */
21
- export declare function loadFilesSync<T = any>(pattern: string | string[], options?: LoadFilesOptions): T[];
22
- /**
23
- * Asynchronously loads files using the provided glob pattern.
24
- * @param pattern Glob pattern or patterns to use when loading files
25
- * @param options Additional options
26
- */
27
- export declare function loadFiles(pattern: string | string[], options?: LoadFilesOptions): Promise<any[]>;
package/es5/index.js DELETED
@@ -1,307 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
6
-
7
- function _interopNamespace(e) {
8
- if (e && e.__esModule) { return e; } else {
9
- var n = {};
10
- if (e) {
11
- Object.keys(e).forEach(function (k) {
12
- var d = Object.getOwnPropertyDescriptor(e, k);
13
- Object.defineProperty(n, k, d.get ? d : {
14
- enumerable: true,
15
- get: function () {
16
- return e[k];
17
- }
18
- });
19
- });
20
- }
21
- n['default'] = e;
22
- return n;
23
- }
24
- }
25
-
26
- const tslib = require('tslib');
27
- const globby = require('globby');
28
- const globby__default = _interopDefault(globby);
29
- const unixify = _interopDefault(require('unixify'));
30
- const path = require('path');
31
- const fs = require('fs');
32
-
33
- var readFile = fs.promises.readFile, stat = fs.promises.stat;
34
- var DEFAULT_IGNORED_EXTENSIONS = ['spec', 'test', 'd', 'map'];
35
- var DEFAULT_EXTENSIONS = ['gql', 'graphql', 'graphqls', 'ts', 'js'];
36
- var DEFAULT_EXPORT_NAMES = ['schema', 'typeDef', 'typeDefs', 'resolver', 'resolvers'];
37
- var DEFAULT_EXTRACT_EXPORTS_FACTORY = function (exportNames) {
38
- return function (fileExport) {
39
- var e_1, _a, e_2, _b;
40
- if (!fileExport) {
41
- return null;
42
- }
43
- if (fileExport.default) {
44
- try {
45
- for (var exportNames_1 = tslib.__values(exportNames), exportNames_1_1 = exportNames_1.next(); !exportNames_1_1.done; exportNames_1_1 = exportNames_1.next()) {
46
- var exportName = exportNames_1_1.value;
47
- if (fileExport.default[exportName]) {
48
- return fileExport.default[exportName];
49
- }
50
- }
51
- }
52
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
53
- finally {
54
- try {
55
- if (exportNames_1_1 && !exportNames_1_1.done && (_a = exportNames_1.return)) _a.call(exportNames_1);
56
- }
57
- finally { if (e_1) throw e_1.error; }
58
- }
59
- return fileExport.default;
60
- }
61
- try {
62
- for (var exportNames_2 = tslib.__values(exportNames), exportNames_2_1 = exportNames_2.next(); !exportNames_2_1.done; exportNames_2_1 = exportNames_2.next()) {
63
- var exportName = exportNames_2_1.value;
64
- if (fileExport[exportName]) {
65
- return fileExport[exportName];
66
- }
67
- }
68
- }
69
- catch (e_2_1) { e_2 = { error: e_2_1 }; }
70
- finally {
71
- try {
72
- if (exportNames_2_1 && !exportNames_2_1.done && (_b = exportNames_2.return)) _b.call(exportNames_2);
73
- }
74
- finally { if (e_2) throw e_2.error; }
75
- }
76
- return fileExport;
77
- };
78
- };
79
- function asArray(obj) {
80
- if (obj instanceof Array) {
81
- return obj;
82
- }
83
- else {
84
- return [obj];
85
- }
86
- }
87
- function isDirectorySync(path) {
88
- try {
89
- var pathStat = fs.statSync(path);
90
- return pathStat.isDirectory();
91
- }
92
- catch (e) {
93
- return false;
94
- }
95
- }
96
- function isDirectory(path) {
97
- return tslib.__awaiter(this, void 0, void 0, function () {
98
- var pathStat, e_3;
99
- return tslib.__generator(this, function (_a) {
100
- switch (_a.label) {
101
- case 0:
102
- _a.trys.push([0, 2, , 3]);
103
- return [4 /*yield*/, stat(path)];
104
- case 1:
105
- pathStat = _a.sent();
106
- return [2 /*return*/, pathStat.isDirectory()];
107
- case 2:
108
- e_3 = _a.sent();
109
- return [2 /*return*/, false];
110
- case 3: return [2 /*return*/];
111
- }
112
- });
113
- });
114
- }
115
- function scanForFilesSync(globStr, globOptions) {
116
- if (globOptions === void 0) { globOptions = {}; }
117
- return globby.sync(globStr, tslib.__assign({ absolute: true }, globOptions));
118
- }
119
- function formatExtension(extension) {
120
- return extension.charAt(0) === '.' ? extension : "." + extension;
121
- }
122
- function buildGlob(basePath, extensions, ignoredExtensions, recursive) {
123
- if (extensions === void 0) { extensions = []; }
124
- if (ignoredExtensions === void 0) { ignoredExtensions = []; }
125
- var ignored = ignoredExtensions.length > 0 ? "!(" + ignoredExtensions.map(function (e) { return "*" + formatExtension(e); }).join('|') + ")" : '*';
126
- var ext = extensions.map(function (e) { return "*" + formatExtension(e); }).join('|');
127
- return "" + basePath + (recursive ? '/**' : '') + "/" + ignored + "+(" + ext + ")";
128
- }
129
- var LoadFilesDefaultOptions = {
130
- ignoredExtensions: DEFAULT_IGNORED_EXTENSIONS,
131
- extensions: DEFAULT_EXTENSIONS,
132
- useRequire: false,
133
- requireMethod: null,
134
- globOptions: {
135
- absolute: true,
136
- },
137
- exportNames: DEFAULT_EXPORT_NAMES,
138
- recursive: true,
139
- ignoreIndex: false,
140
- };
141
- /**
142
- * Synchronously loads files using the provided glob pattern.
143
- * @param pattern Glob pattern or patterns to use when loading files
144
- * @param options Additional options
145
- */
146
- function loadFilesSync(pattern, options) {
147
- var _a;
148
- if (options === void 0) { options = LoadFilesDefaultOptions; }
149
- var execOptions = tslib.__assign(tslib.__assign({}, LoadFilesDefaultOptions), options);
150
- var relevantPaths = scanForFilesSync(asArray(pattern).map(function (path) {
151
- return isDirectorySync(path)
152
- ? buildGlob(unixify(path), execOptions.extensions, execOptions.ignoredExtensions, execOptions.recursive)
153
- : unixify(path);
154
- }), options.globOptions);
155
- var extractExports = execOptions.extractExports || DEFAULT_EXTRACT_EXPORTS_FACTORY((_a = execOptions.exportNames) !== null && _a !== void 0 ? _a : []);
156
- var requireMethod = execOptions.requireMethod || require;
157
- return relevantPaths
158
- .map(function (path$1) {
159
- if (!checkExtension(path$1, options)) {
160
- return null;
161
- }
162
- if (isIndex(path$1, execOptions.extensions) && options.ignoreIndex) {
163
- return false;
164
- }
165
- var extension = path.extname(path$1);
166
- if (extension === formatExtension('js') || extension === formatExtension('ts') || execOptions.useRequire) {
167
- var fileExports = requireMethod(path$1);
168
- var extractedExport = extractExports(fileExports);
169
- return extractedExport;
170
- }
171
- else {
172
- return fs.readFileSync(path$1, { encoding: 'utf-8' });
173
- }
174
- })
175
- .filter(function (v) { return v; });
176
- }
177
- function scanForFiles(globStr, globOptions) {
178
- if (globOptions === void 0) { globOptions = {}; }
179
- return tslib.__awaiter(this, void 0, void 0, function () {
180
- return tslib.__generator(this, function (_a) {
181
- return [2 /*return*/, globby__default(globStr, tslib.__assign({ absolute: true }, globOptions))];
182
- });
183
- });
184
- }
185
- var checkExtension = function (path, _a) {
186
- var e_4, _b, e_5, _c, e_6, _d;
187
- var extensions = _a.extensions, ignoredExtensions = _a.ignoredExtensions;
188
- if (ignoredExtensions) {
189
- try {
190
- for (var ignoredExtensions_1 = tslib.__values(ignoredExtensions), ignoredExtensions_1_1 = ignoredExtensions_1.next(); !ignoredExtensions_1_1.done; ignoredExtensions_1_1 = ignoredExtensions_1.next()) {
191
- var ignoredExtension = ignoredExtensions_1_1.value;
192
- if (path.endsWith(formatExtension(ignoredExtension))) {
193
- return false;
194
- }
195
- }
196
- }
197
- catch (e_4_1) { e_4 = { error: e_4_1 }; }
198
- finally {
199
- try {
200
- if (ignoredExtensions_1_1 && !ignoredExtensions_1_1.done && (_b = ignoredExtensions_1.return)) _b.call(ignoredExtensions_1);
201
- }
202
- finally { if (e_4) throw e_4.error; }
203
- }
204
- }
205
- if (!extensions) {
206
- return true;
207
- }
208
- try {
209
- for (var extensions_1 = tslib.__values(extensions), extensions_1_1 = extensions_1.next(); !extensions_1_1.done; extensions_1_1 = extensions_1.next()) {
210
- var extension = extensions_1_1.value;
211
- var formattedExtension = formatExtension(extension);
212
- if (path.endsWith(formattedExtension)) {
213
- if (ignoredExtensions) {
214
- try {
215
- for (var ignoredExtensions_2 = (e_6 = void 0, tslib.__values(ignoredExtensions)), ignoredExtensions_2_1 = ignoredExtensions_2.next(); !ignoredExtensions_2_1.done; ignoredExtensions_2_1 = ignoredExtensions_2.next()) {
216
- var ignoredExtension = ignoredExtensions_2_1.value;
217
- var formattedIgnoredExtension = formatExtension(ignoredExtension);
218
- if (path.endsWith(formattedIgnoredExtension + formattedExtension)) {
219
- return false;
220
- }
221
- }
222
- }
223
- catch (e_6_1) { e_6 = { error: e_6_1 }; }
224
- finally {
225
- try {
226
- if (ignoredExtensions_2_1 && !ignoredExtensions_2_1.done && (_d = ignoredExtensions_2.return)) _d.call(ignoredExtensions_2);
227
- }
228
- finally { if (e_6) throw e_6.error; }
229
- }
230
- }
231
- return true;
232
- }
233
- }
234
- }
235
- catch (e_5_1) { e_5 = { error: e_5_1 }; }
236
- finally {
237
- try {
238
- if (extensions_1_1 && !extensions_1_1.done && (_c = extensions_1.return)) _c.call(extensions_1);
239
- }
240
- finally { if (e_5) throw e_5.error; }
241
- }
242
- return false;
243
- };
244
- /**
245
- * Asynchronously loads files using the provided glob pattern.
246
- * @param pattern Glob pattern or patterns to use when loading files
247
- * @param options Additional options
248
- */
249
- function loadFiles(pattern, options) {
250
- var _a;
251
- if (options === void 0) { options = LoadFilesDefaultOptions; }
252
- return tslib.__awaiter(this, void 0, void 0, function () {
253
- var execOptions, relevantPaths, _b, extractExports, defaultRequireMethod, requireMethod;
254
- var _this = this;
255
- return tslib.__generator(this, function (_c) {
256
- switch (_c.label) {
257
- case 0:
258
- execOptions = tslib.__assign(tslib.__assign({}, LoadFilesDefaultOptions), options);
259
- _b = scanForFiles;
260
- return [4 /*yield*/, Promise.all(asArray(pattern).map(function (path) { return tslib.__awaiter(_this, void 0, void 0, function () {
261
- return tslib.__generator(this, function (_a) {
262
- switch (_a.label) {
263
- case 0: return [4 /*yield*/, isDirectory(path)];
264
- case 1: return [2 /*return*/, (_a.sent())
265
- ? buildGlob(unixify(path), execOptions.extensions, execOptions.ignoredExtensions, execOptions.recursive)
266
- : unixify(path)];
267
- }
268
- });
269
- }); }))];
270
- case 1: return [4 /*yield*/, _b.apply(void 0, [_c.sent(), options.globOptions])];
271
- case 2:
272
- relevantPaths = _c.sent();
273
- extractExports = execOptions.extractExports || DEFAULT_EXTRACT_EXPORTS_FACTORY((_a = execOptions.exportNames) !== null && _a !== void 0 ? _a : []);
274
- defaultRequireMethod = function (path) { return new Promise(function (resolve) { resolve(_interopNamespace(require(path))); }).catch(function () { return tslib.__awaiter(_this, void 0, void 0, function () { return tslib.__generator(this, function (_a) {
275
- return [2 /*return*/, require(path)];
276
- }); }); }); };
277
- requireMethod = execOptions.requireMethod || defaultRequireMethod;
278
- return [2 /*return*/, Promise.all(relevantPaths
279
- .filter(function (path) { return checkExtension(path, options) && !(isIndex(path, execOptions.extensions) && options.ignoreIndex); })
280
- .map(function (path$1) { return tslib.__awaiter(_this, void 0, void 0, function () {
281
- var extension, fileExports, extractedExport;
282
- return tslib.__generator(this, function (_a) {
283
- switch (_a.label) {
284
- case 0:
285
- extension = path.extname(path$1);
286
- if (!(extension === formatExtension('js') || extension === formatExtension('ts') || execOptions.useRequire)) return [3 /*break*/, 2];
287
- return [4 /*yield*/, requireMethod(path$1)];
288
- case 1:
289
- fileExports = _a.sent();
290
- extractedExport = extractExports(fileExports);
291
- return [2 /*return*/, extractedExport];
292
- case 2: return [2 /*return*/, readFile(path$1, { encoding: 'utf-8' })];
293
- }
294
- });
295
- }); }))];
296
- }
297
- });
298
- });
299
- }
300
- function isIndex(path, extensions) {
301
- if (extensions === void 0) { extensions = []; }
302
- var IS_INDEX = /(\/|\\)index\.[^\/\\]+$/i; // (/ or \) AND `index.` AND (everything except \ and /)(end of line)
303
- return IS_INDEX.test(path) && extensions.some(function (ext) { return path.endsWith(formatExtension(ext)); });
304
- }
305
-
306
- exports.loadFiles = loadFiles;
307
- exports.loadFilesSync = loadFilesSync;
package/es5/index.mjs DELETED
@@ -1,280 +0,0 @@
1
- import { __assign, __values, __awaiter, __generator } from 'tslib';
2
- import globby, { sync } from 'globby';
3
- import unixify from 'unixify';
4
- import { extname } from 'path';
5
- import { promises, readFileSync, statSync } from 'fs';
6
-
7
- var readFile = promises.readFile, stat = promises.stat;
8
- var DEFAULT_IGNORED_EXTENSIONS = ['spec', 'test', 'd', 'map'];
9
- var DEFAULT_EXTENSIONS = ['gql', 'graphql', 'graphqls', 'ts', 'js'];
10
- var DEFAULT_EXPORT_NAMES = ['schema', 'typeDef', 'typeDefs', 'resolver', 'resolvers'];
11
- var DEFAULT_EXTRACT_EXPORTS_FACTORY = function (exportNames) {
12
- return function (fileExport) {
13
- var e_1, _a, e_2, _b;
14
- if (!fileExport) {
15
- return null;
16
- }
17
- if (fileExport.default) {
18
- try {
19
- for (var exportNames_1 = __values(exportNames), exportNames_1_1 = exportNames_1.next(); !exportNames_1_1.done; exportNames_1_1 = exportNames_1.next()) {
20
- var exportName = exportNames_1_1.value;
21
- if (fileExport.default[exportName]) {
22
- return fileExport.default[exportName];
23
- }
24
- }
25
- }
26
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
27
- finally {
28
- try {
29
- if (exportNames_1_1 && !exportNames_1_1.done && (_a = exportNames_1.return)) _a.call(exportNames_1);
30
- }
31
- finally { if (e_1) throw e_1.error; }
32
- }
33
- return fileExport.default;
34
- }
35
- try {
36
- for (var exportNames_2 = __values(exportNames), exportNames_2_1 = exportNames_2.next(); !exportNames_2_1.done; exportNames_2_1 = exportNames_2.next()) {
37
- var exportName = exportNames_2_1.value;
38
- if (fileExport[exportName]) {
39
- return fileExport[exportName];
40
- }
41
- }
42
- }
43
- catch (e_2_1) { e_2 = { error: e_2_1 }; }
44
- finally {
45
- try {
46
- if (exportNames_2_1 && !exportNames_2_1.done && (_b = exportNames_2.return)) _b.call(exportNames_2);
47
- }
48
- finally { if (e_2) throw e_2.error; }
49
- }
50
- return fileExport;
51
- };
52
- };
53
- function asArray(obj) {
54
- if (obj instanceof Array) {
55
- return obj;
56
- }
57
- else {
58
- return [obj];
59
- }
60
- }
61
- function isDirectorySync(path) {
62
- try {
63
- var pathStat = statSync(path);
64
- return pathStat.isDirectory();
65
- }
66
- catch (e) {
67
- return false;
68
- }
69
- }
70
- function isDirectory(path) {
71
- return __awaiter(this, void 0, void 0, function () {
72
- var pathStat, e_3;
73
- return __generator(this, function (_a) {
74
- switch (_a.label) {
75
- case 0:
76
- _a.trys.push([0, 2, , 3]);
77
- return [4 /*yield*/, stat(path)];
78
- case 1:
79
- pathStat = _a.sent();
80
- return [2 /*return*/, pathStat.isDirectory()];
81
- case 2:
82
- e_3 = _a.sent();
83
- return [2 /*return*/, false];
84
- case 3: return [2 /*return*/];
85
- }
86
- });
87
- });
88
- }
89
- function scanForFilesSync(globStr, globOptions) {
90
- if (globOptions === void 0) { globOptions = {}; }
91
- return sync(globStr, __assign({ absolute: true }, globOptions));
92
- }
93
- function formatExtension(extension) {
94
- return extension.charAt(0) === '.' ? extension : "." + extension;
95
- }
96
- function buildGlob(basePath, extensions, ignoredExtensions, recursive) {
97
- if (extensions === void 0) { extensions = []; }
98
- if (ignoredExtensions === void 0) { ignoredExtensions = []; }
99
- var ignored = ignoredExtensions.length > 0 ? "!(" + ignoredExtensions.map(function (e) { return "*" + formatExtension(e); }).join('|') + ")" : '*';
100
- var ext = extensions.map(function (e) { return "*" + formatExtension(e); }).join('|');
101
- return "" + basePath + (recursive ? '/**' : '') + "/" + ignored + "+(" + ext + ")";
102
- }
103
- var LoadFilesDefaultOptions = {
104
- ignoredExtensions: DEFAULT_IGNORED_EXTENSIONS,
105
- extensions: DEFAULT_EXTENSIONS,
106
- useRequire: false,
107
- requireMethod: null,
108
- globOptions: {
109
- absolute: true,
110
- },
111
- exportNames: DEFAULT_EXPORT_NAMES,
112
- recursive: true,
113
- ignoreIndex: false,
114
- };
115
- /**
116
- * Synchronously loads files using the provided glob pattern.
117
- * @param pattern Glob pattern or patterns to use when loading files
118
- * @param options Additional options
119
- */
120
- function loadFilesSync(pattern, options) {
121
- var _a;
122
- if (options === void 0) { options = LoadFilesDefaultOptions; }
123
- var execOptions = __assign(__assign({}, LoadFilesDefaultOptions), options);
124
- var relevantPaths = scanForFilesSync(asArray(pattern).map(function (path) {
125
- return isDirectorySync(path)
126
- ? buildGlob(unixify(path), execOptions.extensions, execOptions.ignoredExtensions, execOptions.recursive)
127
- : unixify(path);
128
- }), options.globOptions);
129
- var extractExports = execOptions.extractExports || DEFAULT_EXTRACT_EXPORTS_FACTORY((_a = execOptions.exportNames) !== null && _a !== void 0 ? _a : []);
130
- var requireMethod = execOptions.requireMethod || require;
131
- return relevantPaths
132
- .map(function (path) {
133
- if (!checkExtension(path, options)) {
134
- return null;
135
- }
136
- if (isIndex(path, execOptions.extensions) && options.ignoreIndex) {
137
- return false;
138
- }
139
- var extension = extname(path);
140
- if (extension === formatExtension('js') || extension === formatExtension('ts') || execOptions.useRequire) {
141
- var fileExports = requireMethod(path);
142
- var extractedExport = extractExports(fileExports);
143
- return extractedExport;
144
- }
145
- else {
146
- return readFileSync(path, { encoding: 'utf-8' });
147
- }
148
- })
149
- .filter(function (v) { return v; });
150
- }
151
- function scanForFiles(globStr, globOptions) {
152
- if (globOptions === void 0) { globOptions = {}; }
153
- return __awaiter(this, void 0, void 0, function () {
154
- return __generator(this, function (_a) {
155
- return [2 /*return*/, globby(globStr, __assign({ absolute: true }, globOptions))];
156
- });
157
- });
158
- }
159
- var checkExtension = function (path, _a) {
160
- var e_4, _b, e_5, _c, e_6, _d;
161
- var extensions = _a.extensions, ignoredExtensions = _a.ignoredExtensions;
162
- if (ignoredExtensions) {
163
- try {
164
- for (var ignoredExtensions_1 = __values(ignoredExtensions), ignoredExtensions_1_1 = ignoredExtensions_1.next(); !ignoredExtensions_1_1.done; ignoredExtensions_1_1 = ignoredExtensions_1.next()) {
165
- var ignoredExtension = ignoredExtensions_1_1.value;
166
- if (path.endsWith(formatExtension(ignoredExtension))) {
167
- return false;
168
- }
169
- }
170
- }
171
- catch (e_4_1) { e_4 = { error: e_4_1 }; }
172
- finally {
173
- try {
174
- if (ignoredExtensions_1_1 && !ignoredExtensions_1_1.done && (_b = ignoredExtensions_1.return)) _b.call(ignoredExtensions_1);
175
- }
176
- finally { if (e_4) throw e_4.error; }
177
- }
178
- }
179
- if (!extensions) {
180
- return true;
181
- }
182
- try {
183
- for (var extensions_1 = __values(extensions), extensions_1_1 = extensions_1.next(); !extensions_1_1.done; extensions_1_1 = extensions_1.next()) {
184
- var extension = extensions_1_1.value;
185
- var formattedExtension = formatExtension(extension);
186
- if (path.endsWith(formattedExtension)) {
187
- if (ignoredExtensions) {
188
- try {
189
- for (var ignoredExtensions_2 = (e_6 = void 0, __values(ignoredExtensions)), ignoredExtensions_2_1 = ignoredExtensions_2.next(); !ignoredExtensions_2_1.done; ignoredExtensions_2_1 = ignoredExtensions_2.next()) {
190
- var ignoredExtension = ignoredExtensions_2_1.value;
191
- var formattedIgnoredExtension = formatExtension(ignoredExtension);
192
- if (path.endsWith(formattedIgnoredExtension + formattedExtension)) {
193
- return false;
194
- }
195
- }
196
- }
197
- catch (e_6_1) { e_6 = { error: e_6_1 }; }
198
- finally {
199
- try {
200
- if (ignoredExtensions_2_1 && !ignoredExtensions_2_1.done && (_d = ignoredExtensions_2.return)) _d.call(ignoredExtensions_2);
201
- }
202
- finally { if (e_6) throw e_6.error; }
203
- }
204
- }
205
- return true;
206
- }
207
- }
208
- }
209
- catch (e_5_1) { e_5 = { error: e_5_1 }; }
210
- finally {
211
- try {
212
- if (extensions_1_1 && !extensions_1_1.done && (_c = extensions_1.return)) _c.call(extensions_1);
213
- }
214
- finally { if (e_5) throw e_5.error; }
215
- }
216
- return false;
217
- };
218
- /**
219
- * Asynchronously loads files using the provided glob pattern.
220
- * @param pattern Glob pattern or patterns to use when loading files
221
- * @param options Additional options
222
- */
223
- function loadFiles(pattern, options) {
224
- var _a;
225
- if (options === void 0) { options = LoadFilesDefaultOptions; }
226
- return __awaiter(this, void 0, void 0, function () {
227
- var execOptions, relevantPaths, _b, extractExports, defaultRequireMethod, requireMethod;
228
- var _this = this;
229
- return __generator(this, function (_c) {
230
- switch (_c.label) {
231
- case 0:
232
- execOptions = __assign(__assign({}, LoadFilesDefaultOptions), options);
233
- _b = scanForFiles;
234
- return [4 /*yield*/, Promise.all(asArray(pattern).map(function (path) { return __awaiter(_this, void 0, void 0, function () {
235
- return __generator(this, function (_a) {
236
- switch (_a.label) {
237
- case 0: return [4 /*yield*/, isDirectory(path)];
238
- case 1: return [2 /*return*/, (_a.sent())
239
- ? buildGlob(unixify(path), execOptions.extensions, execOptions.ignoredExtensions, execOptions.recursive)
240
- : unixify(path)];
241
- }
242
- });
243
- }); }))];
244
- case 1: return [4 /*yield*/, _b.apply(void 0, [_c.sent(), options.globOptions])];
245
- case 2:
246
- relevantPaths = _c.sent();
247
- extractExports = execOptions.extractExports || DEFAULT_EXTRACT_EXPORTS_FACTORY((_a = execOptions.exportNames) !== null && _a !== void 0 ? _a : []);
248
- defaultRequireMethod = function (path) { return import(path).catch(function () { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
249
- return [2 /*return*/, require(path)];
250
- }); }); }); };
251
- requireMethod = execOptions.requireMethod || defaultRequireMethod;
252
- return [2 /*return*/, Promise.all(relevantPaths
253
- .filter(function (path) { return checkExtension(path, options) && !(isIndex(path, execOptions.extensions) && options.ignoreIndex); })
254
- .map(function (path) { return __awaiter(_this, void 0, void 0, function () {
255
- var extension, fileExports, extractedExport;
256
- return __generator(this, function (_a) {
257
- switch (_a.label) {
258
- case 0:
259
- extension = extname(path);
260
- if (!(extension === formatExtension('js') || extension === formatExtension('ts') || execOptions.useRequire)) return [3 /*break*/, 2];
261
- return [4 /*yield*/, requireMethod(path)];
262
- case 1:
263
- fileExports = _a.sent();
264
- extractedExport = extractExports(fileExports);
265
- return [2 /*return*/, extractedExport];
266
- case 2: return [2 /*return*/, readFile(path, { encoding: 'utf-8' })];
267
- }
268
- });
269
- }); }))];
270
- }
271
- });
272
- });
273
- }
274
- function isIndex(path, extensions) {
275
- if (extensions === void 0) { extensions = []; }
276
- var IS_INDEX = /(\/|\\)index\.[^\/\\]+$/i; // (/ or \) AND `index.` AND (everything except \ and /)(end of line)
277
- return IS_INDEX.test(path) && extensions.some(function (ext) { return path.endsWith(formatExtension(ext)); });
278
- }
279
-
280
- export { loadFiles, loadFilesSync };
package/es5/package.json DELETED
@@ -1,37 +0,0 @@
1
- {
2
- "name": "@graphql-tools/load-files/es5",
3
- "version": "6.4.0-alpha-5cac3eec.0",
4
- "description": "A set of utils for faster development of GraphQL tools",
5
- "sideEffects": false,
6
- "peerDependencies": {
7
- "graphql": "^14.0.0 || ^15.0.0 || ^16.0.0"
8
- },
9
- "dependencies": {
10
- "globby": "11.0.4",
11
- "tslib": "~2.3.0",
12
- "unixify": "1.0.0"
13
- },
14
- "repository": {
15
- "type": "git",
16
- "url": "ardatan/graphql-tools",
17
- "directory": "packages/load-files"
18
- },
19
- "author": "Dotan Simha <dotansimha@gmail.com>",
20
- "license": "MIT",
21
- "main": "index.js",
22
- "module": "index.mjs",
23
- "typings": "index.d.ts",
24
- "typescript": {
25
- "definition": "index.d.ts"
26
- },
27
- "exports": {
28
- ".": {
29
- "require": "./index.js",
30
- "import": "./index.mjs"
31
- },
32
- "./*": {
33
- "require": "./*.js",
34
- "import": "./*.mjs"
35
- }
36
- }
37
- }