UNPKG

6.87 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.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;
4const tslib_1 = require("tslib");
5const graphql_1 = require("graphql");
6const parse_filepath_1 = tslib_1.__importDefault(require("parse-filepath"));
7const sep = '/';
8/**
9 * Searches every node to collect used types
10 */
11function 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 // Object
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 // Input
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 // Interface
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 // Union
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 // Enum
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 // Scalar
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 // Argument
64 findRelated(resolveTypeNode(node.type));
65 }
66 else if (node.kind === graphql_1.Kind.FIELD_DEFINITION) {
67 // Field
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 // Named type
75 !isGraphQLPrimitive(node.name.value)) {
76 markAsUsed(node.name.value);
77 }
78 }
79 return used;
80}
81exports.collectUsedTypes = collectUsedTypes;
82function 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}
91exports.resolveTypeNode = resolveTypeNode;
92function isGraphQLPrimitive(name) {
93 return ['String', 'Boolean', 'ID', 'Float', 'Int'].includes(name);
94}
95exports.isGraphQLPrimitive = isGraphQLPrimitive;
96function unique(val, i, all) {
97 return i === all.indexOf(val);
98}
99exports.unique = unique;
100function withQuotes(val) {
101 return `'${val}'`;
102}
103exports.withQuotes = withQuotes;
104function 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}
114exports.indent = indent;
115function buildBlock({ name, lines }) {
116 if (!lines.length) {
117 return '';
118 }
119 return [`${name} {`, ...lines.map(indent(2)), '};'].join('\n');
120}
121exports.buildBlock = buildBlock;
122const 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};
128function groupSourcesByModule(sources, basePath) {
129 const grouped = {};
130 sources.forEach(source => {
131 const relativePath = getRelativePath(source.location, basePath);
132 if (relativePath) {
133 // PERF: we could guess the module by matching source.location with a list of already resolved paths
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}
143exports.groupSourcesByModule = groupSourcesByModule;
144function extractModuleDirectory(filepath, basePath) {
145 const relativePath = getRelativePath(filepath, basePath);
146 const [moduleDirectory] = relativePath.split(sep);
147 return moduleDirectory;
148}
149function stripFilename(path) {
150 const parsedPath = (0, parse_filepath_1.default)(path);
151 return normalize(parsedPath.dir);
152}
153exports.stripFilename = stripFilename;
154function normalize(path) {
155 return path.replace(/\\/g, '/');
156}
157exports.normalize = normalize;
158function ensureEndsWithSeparator(path) {
159 return path.endsWith(sep) ? path : path + sep;
160}
161function ensureStartsWithSeparator(path) {
162 return path.startsWith('.') ? path.replace(/^(..\/)|(.\/)/, '/') : path.startsWith('/') ? path : '/' + path;
163}
164/**
165 * Pushes an item to a list only if the list doesn't include the item
166 */
167function pushUnique(list, item) {
168 if (!list.includes(item)) {
169 list.push(item);
170 }
171}
172exports.pushUnique = pushUnique;
173function concatByKey(left, right, key) {
174 // Remove duplicate, if an element is in right & left, it will be only once in the returned array.
175 return [...new Set([...left[key], ...right[key]])];
176}
177exports.concatByKey = concatByKey;
178function uniqueByKey(left, right, key) {
179 return left[key].filter(item => !right[key].includes(item));
180}
181exports.uniqueByKey = uniqueByKey;
182function createObject(keys, valueFn) {
183 const obj = {};
184 keys.forEach(key => {
185 obj[key] = valueFn(key);
186 });
187 return obj;
188}
189exports.createObject = createObject;