UNPKG

2.18 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.GraphQLTypesLoader = void 0;
4const tslib_1 = require("tslib");
5const merge_1 = require("@graphql-tools/merge");
6const common_1 = require("@nestjs/common");
7const glob = require("fast-glob");
8const fs = require("fs");
9const lodash_1 = require("lodash");
10const util = require("util");
11// eslint-disable-next-line @typescript-eslint/no-var-requires
12const normalize = require('normalize-path');
13const readFile = util.promisify(fs.readFile);
14let GraphQLTypesLoader = class GraphQLTypesLoader {
15 async mergeTypesByPaths(paths) {
16 if (!paths || paths.length === 0) {
17 return null;
18 }
19 const types = await this.getTypesFromPaths(paths);
20 const flatTypes = (0, lodash_1.flatten)(types);
21 return (0, merge_1.mergeTypeDefs)(flatTypes, {
22 throwOnConflict: true,
23 commentDescriptions: true,
24 reverseDirectives: true,
25 });
26 }
27 async getTypesFromPaths(paths) {
28 const includeNodeModules = this.includeNodeModules(paths);
29 paths = Array.isArray(paths)
30 ? paths.map((path) => normalize(path))
31 : normalize(paths);
32 const filePaths = await glob(paths, {
33 ignore: includeNodeModules ? [] : ['node_modules'],
34 });
35 if (filePaths.length === 0) {
36 throw new Error(`No type definitions were found with the specified file name patterns: "${paths}". Please make sure there is at least one file that matches the given patterns.`);
37 }
38 const fileContentsPromises = filePaths.sort().map((filePath) => {
39 return readFile(filePath.toString(), 'utf8');
40 });
41 return Promise.all(fileContentsPromises);
42 }
43 includeNodeModules(pathOrPaths) {
44 if (Array.isArray(pathOrPaths)) {
45 return pathOrPaths.some((path) => path.includes('node_modules'));
46 }
47 return pathOrPaths.includes('node_modules');
48 }
49};
50exports.GraphQLTypesLoader = GraphQLTypesLoader;
51exports.GraphQLTypesLoader = GraphQLTypesLoader = tslib_1.__decorate([
52 (0, common_1.Injectable)()
53], GraphQLTypesLoader);