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