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