1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, '__esModule', { value: true });
|
4 |
|
5 | function _interopNamespace(e) {
|
6 | if (e && e.__esModule) { return e; } else {
|
7 | var n = {};
|
8 | if (e) {
|
9 | Object.keys(e).forEach(function (k) {
|
10 | var d = Object.getOwnPropertyDescriptor(e, k);
|
11 | Object.defineProperty(n, k, d.get ? d : {
|
12 | enumerable: true,
|
13 | get: function () {
|
14 | return e[k];
|
15 | }
|
16 | });
|
17 | });
|
18 | }
|
19 | n['default'] = e;
|
20 | return n;
|
21 | }
|
22 | }
|
23 |
|
24 | const DEFAULT_IGNORED_EXTENSIONS = ['spec', 'test', 'd', 'map'];
|
25 | const DEFAULT_EXTENSIONS = ['gql', 'graphql', 'graphqls', 'ts', 'js'];
|
26 | const DEFAULT_EXPORT_NAMES = ['typeDefs', 'schema'];
|
27 | function asArray(obj) {
|
28 | if (obj instanceof Array) {
|
29 | return obj;
|
30 | }
|
31 | else {
|
32 | return [obj];
|
33 | }
|
34 | }
|
35 | function isDirectory(path) {
|
36 | const { existsSync, statSync } = require('fs');
|
37 | return existsSync(path) && statSync(path).isDirectory();
|
38 | }
|
39 | function scanForFiles(globStr, globOptions = {}) {
|
40 | const globby = require('globby');
|
41 | return globby.sync(globStr, { absolute: true, ...globOptions });
|
42 | }
|
43 | function buildGlob(basePath, extensions, ignoredExtensions = [], recursive) {
|
44 | return `${basePath}${recursive ? '/**' : ''}/${ignoredExtensions.length > 0 ? `!(${ignoredExtensions.map(e => '*.' + e).join('|')})` : '*'}+(${extensions.map(e => '*.' + e).join('|')})`;
|
45 | }
|
46 | function extractExports(fileExport, exportNames) {
|
47 | if (!fileExport) {
|
48 | return null;
|
49 | }
|
50 | if (fileExport.default) {
|
51 | for (const exportName of exportNames) {
|
52 | if (fileExport.default[exportName]) {
|
53 | return fileExport.default[exportName];
|
54 | }
|
55 | }
|
56 | return fileExport.default;
|
57 | }
|
58 | for (const exportName of exportNames) {
|
59 | if (fileExport[exportName]) {
|
60 | return fileExport[exportName];
|
61 | }
|
62 | }
|
63 | return fileExport;
|
64 | }
|
65 | const LoadFilesDefaultOptions = {
|
66 | ignoredExtensions: DEFAULT_IGNORED_EXTENSIONS,
|
67 | extensions: DEFAULT_EXTENSIONS,
|
68 | useRequire: false,
|
69 | requireMethod: null,
|
70 | globOptions: {
|
71 | absolute: true,
|
72 | },
|
73 | exportNames: DEFAULT_EXPORT_NAMES,
|
74 | recursive: true,
|
75 | ignoreIndex: false,
|
76 | };
|
77 | function loadFiles(pattern, options = LoadFilesDefaultOptions) {
|
78 | const execOptions = { ...LoadFilesDefaultOptions, ...options };
|
79 | const unixify = require('unixify');
|
80 | const relevantPaths = scanForFiles(asArray(pattern).map(path => (isDirectory(path) ? buildGlob(unixify(path), execOptions.extensions, execOptions.ignoredExtensions, execOptions.recursive) : unixify(path))), options.globOptions);
|
81 | return relevantPaths
|
82 | .map(path => {
|
83 | if (!checkExtension(path, options)) {
|
84 | return;
|
85 | }
|
86 | if (isIndex(path, execOptions.extensions) && options.ignoreIndex) {
|
87 | return false;
|
88 | }
|
89 | const { extname } = require('path');
|
90 | const extension = extname(path);
|
91 | if (extension.endsWith('.js') || extension.endsWith('.ts') || execOptions.useRequire) {
|
92 | const fileExports = (execOptions.requireMethod ? execOptions.requireMethod : require)(path);
|
93 | const extractedExport = extractExports(fileExports, execOptions.exportNames);
|
94 | if (extractedExport.typeDefs && extractedExport.resolvers) {
|
95 | return extractedExport;
|
96 | }
|
97 | if (extractedExport.schema) {
|
98 | return extractedExport.schema;
|
99 | }
|
100 | if (extractedExport.typeDef) {
|
101 | return extractedExport.typeDef;
|
102 | }
|
103 | if (extractedExport.typeDefs) {
|
104 | return extractedExport.typeDefs;
|
105 | }
|
106 | if (extractedExport.resolver) {
|
107 | return extractedExport.resolver;
|
108 | }
|
109 | if (extractedExport.resolvers) {
|
110 | return extractedExport.resolvers;
|
111 | }
|
112 | return extractedExport;
|
113 | }
|
114 | else {
|
115 | const { readFileSync } = require('fs');
|
116 | return readFileSync(path, { encoding: 'utf-8' });
|
117 | }
|
118 | })
|
119 | .filter(v => v);
|
120 | }
|
121 | async function scanForFilesAsync(globStr, globOptions = {}) {
|
122 | const { default: globby } = await new Promise(function (resolve) { resolve(_interopNamespace(require('globby'))); });
|
123 | return globby(globStr, { absolute: true, ...globOptions });
|
124 | }
|
125 | const checkExtension = (path, { extensions, ignoredExtensions }) => {
|
126 | if (ignoredExtensions) {
|
127 | for (const ignoredExtension of ignoredExtensions) {
|
128 | if (path.endsWith(ignoredExtension)) {
|
129 | return false;
|
130 | }
|
131 | }
|
132 | }
|
133 | if (extensions) {
|
134 | for (const extension of extensions) {
|
135 | if (path.endsWith(extension)) {
|
136 | return true;
|
137 | }
|
138 | }
|
139 | }
|
140 | else {
|
141 | return true;
|
142 | }
|
143 | return false;
|
144 | };
|
145 | async function loadFilesAsync(pattern, options = LoadFilesDefaultOptions) {
|
146 | const execOptions = { ...LoadFilesDefaultOptions, ...options };
|
147 | const unixify = await new Promise(function (resolve) { resolve(_interopNamespace(require('unixify'))); }).then(m => m.default || m);
|
148 | const relevantPaths = await scanForFilesAsync(asArray(pattern).map(path => (isDirectory(path) ? buildGlob(unixify(path), execOptions.extensions, execOptions.ignoredExtensions, execOptions.recursive) : unixify(path))), options.globOptions);
|
149 | const require$ = (path) => new Promise(function (resolve) { resolve(_interopNamespace(require(path))); });
|
150 | return Promise.all(relevantPaths
|
151 | .map(async (path) => {
|
152 | if (!checkExtension(path, options)) {
|
153 | return;
|
154 | }
|
155 | if (isIndex(path, execOptions.extensions) && options.ignoreIndex) {
|
156 | return false;
|
157 | }
|
158 | const { extname } = await new Promise(function (resolve) { resolve(_interopNamespace(require('path'))); });
|
159 | const extension = extname(path);
|
160 | if (extension.endsWith('.js') || extension.endsWith('.ts') || execOptions.useRequire) {
|
161 | const fileExports = await (execOptions.requireMethod ? execOptions.requireMethod : require$)(path);
|
162 | const extractedExport = extractExports(fileExports, execOptions.exportNames);
|
163 | if (extractedExport.resolver) {
|
164 | return extractedExport.resolver;
|
165 | }
|
166 | if (extractedExport.resolvers) {
|
167 | return extractedExport.resolvers;
|
168 | }
|
169 | return extractedExport;
|
170 | }
|
171 | else {
|
172 | return new Promise(async (resolve, reject) => {
|
173 | const { readFile } = await new Promise(function (resolve) { resolve(_interopNamespace(require('fs'))); });
|
174 | readFile(path, { encoding: 'utf-8' }, (err, data) => {
|
175 | if (err) {
|
176 | reject(err);
|
177 | }
|
178 | resolve(data);
|
179 | });
|
180 | });
|
181 | }
|
182 | })
|
183 | .filter(p => p));
|
184 | }
|
185 | function isIndex(path, extensions = []) {
|
186 | const IS_INDEX = /(\/|\\)index\.[^\/\\]+$/i;
|
187 | return IS_INDEX.test(path) && extensions.some(ext => path.endsWith('.' + ext));
|
188 | }
|
189 |
|
190 | exports.loadFiles = loadFiles;
|
191 | exports.loadFilesAsync = loadFilesAsync;
|
192 | exports.loadResolversFiles = loadFiles;
|
193 | exports.loadResolversFilesAsync = loadFilesAsync;
|
194 | exports.loadSchemaFiles = loadFiles;
|
195 | exports.loadSchemaFilesAsync = loadFilesAsync;
|