UNPKG

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