1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.createObject = exports.uniqueByKey = exports.concatByKey = exports.pushUnique = exports.normalize = exports.stripFilename = exports.groupSourcesByModule = exports.buildBlock = exports.indent = exports.withQuotes = exports.unique = exports.isGraphQLPrimitive = exports.resolveTypeNode = exports.collectUsedTypes = void 0;
|
4 | const tslib_1 = require("tslib");
|
5 | const graphql_1 = require("graphql");
|
6 | const parse_filepath_1 = tslib_1.__importDefault(require("parse-filepath"));
|
7 | const sep = '/';
|
8 |
|
9 |
|
10 |
|
11 | function collectUsedTypes(doc) {
|
12 | const used = [];
|
13 | doc.definitions.forEach(findRelated);
|
14 | function markAsUsed(type) {
|
15 | pushUnique(used, type);
|
16 | }
|
17 | function findRelated(node) {
|
18 | if (node.kind === graphql_1.Kind.OBJECT_TYPE_DEFINITION || node.kind === graphql_1.Kind.OBJECT_TYPE_EXTENSION) {
|
19 |
|
20 | markAsUsed(node.name.value);
|
21 | if (node.fields) {
|
22 | node.fields.forEach(findRelated);
|
23 | }
|
24 | if (node.interfaces) {
|
25 | node.interfaces.forEach(findRelated);
|
26 | }
|
27 | }
|
28 | else if (node.kind === graphql_1.Kind.INPUT_OBJECT_TYPE_DEFINITION || node.kind === graphql_1.Kind.INPUT_OBJECT_TYPE_EXTENSION) {
|
29 |
|
30 | markAsUsed(node.name.value);
|
31 | if (node.fields) {
|
32 | node.fields.forEach(findRelated);
|
33 | }
|
34 | }
|
35 | else if (node.kind === graphql_1.Kind.INTERFACE_TYPE_DEFINITION || node.kind === graphql_1.Kind.INTERFACE_TYPE_EXTENSION) {
|
36 |
|
37 | markAsUsed(node.name.value);
|
38 | if (node.fields) {
|
39 | node.fields.forEach(findRelated);
|
40 | }
|
41 | if (node.interfaces) {
|
42 | node.interfaces.forEach(findRelated);
|
43 | }
|
44 | }
|
45 | else if (node.kind === graphql_1.Kind.UNION_TYPE_DEFINITION || node.kind === graphql_1.Kind.UNION_TYPE_EXTENSION) {
|
46 |
|
47 | markAsUsed(node.name.value);
|
48 | if (node.types) {
|
49 | node.types.forEach(findRelated);
|
50 | }
|
51 | }
|
52 | else if (node.kind === graphql_1.Kind.ENUM_TYPE_DEFINITION || node.kind === graphql_1.Kind.ENUM_TYPE_EXTENSION) {
|
53 |
|
54 | markAsUsed(node.name.value);
|
55 | }
|
56 | else if (node.kind === graphql_1.Kind.SCALAR_TYPE_DEFINITION || node.kind === graphql_1.Kind.SCALAR_TYPE_EXTENSION) {
|
57 |
|
58 | if (!isGraphQLPrimitive(node.name.value)) {
|
59 | markAsUsed(node.name.value);
|
60 | }
|
61 | }
|
62 | else if (node.kind === graphql_1.Kind.INPUT_VALUE_DEFINITION) {
|
63 |
|
64 | findRelated(resolveTypeNode(node.type));
|
65 | }
|
66 | else if (node.kind === graphql_1.Kind.FIELD_DEFINITION) {
|
67 |
|
68 | findRelated(resolveTypeNode(node.type));
|
69 | if (node.arguments) {
|
70 | node.arguments.forEach(findRelated);
|
71 | }
|
72 | }
|
73 | else if (node.kind === graphql_1.Kind.NAMED_TYPE &&
|
74 |
|
75 | !isGraphQLPrimitive(node.name.value)) {
|
76 | markAsUsed(node.name.value);
|
77 | }
|
78 | }
|
79 | return used;
|
80 | }
|
81 | exports.collectUsedTypes = collectUsedTypes;
|
82 | function resolveTypeNode(node) {
|
83 | if (node.kind === graphql_1.Kind.LIST_TYPE) {
|
84 | return resolveTypeNode(node.type);
|
85 | }
|
86 | if (node.kind === graphql_1.Kind.NON_NULL_TYPE) {
|
87 | return resolveTypeNode(node.type);
|
88 | }
|
89 | return node;
|
90 | }
|
91 | exports.resolveTypeNode = resolveTypeNode;
|
92 | function isGraphQLPrimitive(name) {
|
93 | return ['String', 'Boolean', 'ID', 'Float', 'Int'].includes(name);
|
94 | }
|
95 | exports.isGraphQLPrimitive = isGraphQLPrimitive;
|
96 | function unique(val, i, all) {
|
97 | return i === all.indexOf(val);
|
98 | }
|
99 | exports.unique = unique;
|
100 | function withQuotes(val) {
|
101 | return `'${val}'`;
|
102 | }
|
103 | exports.withQuotes = withQuotes;
|
104 | function indent(size) {
|
105 | const space = new Array(size).fill(' ').join('');
|
106 | function indentInner(val) {
|
107 | return val
|
108 | .split('\n')
|
109 | .map(line => `${space}${line}`)
|
110 | .join('\n');
|
111 | }
|
112 | return indentInner;
|
113 | }
|
114 | exports.indent = indent;
|
115 | function buildBlock({ name, lines }) {
|
116 | if (!lines.length) {
|
117 | return '';
|
118 | }
|
119 | return [`${name} {`, ...lines.map(indent(2)), '};'].join('\n');
|
120 | }
|
121 | exports.buildBlock = buildBlock;
|
122 | const getRelativePath = function (filepath, basePath) {
|
123 | const normalizedFilepath = normalize(filepath);
|
124 | const normalizedBasePath = ensureStartsWithSeparator(normalize(ensureEndsWithSeparator(basePath)));
|
125 | const [, relativePath] = normalizedFilepath.split(normalizedBasePath);
|
126 | return relativePath;
|
127 | };
|
128 | function groupSourcesByModule(sources, basePath) {
|
129 | const grouped = {};
|
130 | sources.forEach(source => {
|
131 | const relativePath = getRelativePath(source.location, basePath);
|
132 | if (relativePath) {
|
133 |
|
134 | const mod = extractModuleDirectory(source.location, basePath);
|
135 | if (!grouped[mod]) {
|
136 | grouped[mod] = [];
|
137 | }
|
138 | grouped[mod].push(source);
|
139 | }
|
140 | });
|
141 | return grouped;
|
142 | }
|
143 | exports.groupSourcesByModule = groupSourcesByModule;
|
144 | function extractModuleDirectory(filepath, basePath) {
|
145 | const relativePath = getRelativePath(filepath, basePath);
|
146 | const [moduleDirectory] = relativePath.split(sep);
|
147 | return moduleDirectory;
|
148 | }
|
149 | function stripFilename(path) {
|
150 | const parsedPath = (0, parse_filepath_1.default)(path);
|
151 | return normalize(parsedPath.dir);
|
152 | }
|
153 | exports.stripFilename = stripFilename;
|
154 | function normalize(path) {
|
155 | return path.replace(/\\/g, '/');
|
156 | }
|
157 | exports.normalize = normalize;
|
158 | function ensureEndsWithSeparator(path) {
|
159 | return path.endsWith(sep) ? path : path + sep;
|
160 | }
|
161 | function ensureStartsWithSeparator(path) {
|
162 | return path.startsWith('.') ? path.replace(/^(..\/)|(.\/)/, '/') : path.startsWith('/') ? path : '/' + path;
|
163 | }
|
164 |
|
165 |
|
166 |
|
167 | function pushUnique(list, item) {
|
168 | if (!list.includes(item)) {
|
169 | list.push(item);
|
170 | }
|
171 | }
|
172 | exports.pushUnique = pushUnique;
|
173 | function concatByKey(left, right, key) {
|
174 |
|
175 | return [...new Set([...left[key], ...right[key]])];
|
176 | }
|
177 | exports.concatByKey = concatByKey;
|
178 | function uniqueByKey(left, right, key) {
|
179 | return left[key].filter(item => !right[key].includes(item));
|
180 | }
|
181 | exports.uniqueByKey = uniqueByKey;
|
182 | function createObject(keys, valueFn) {
|
183 | const obj = {};
|
184 | keys.forEach(key => {
|
185 | obj[key] = valueFn(key);
|
186 | });
|
187 | return obj;
|
188 | }
|
189 | exports.createObject = createObject;
|