UNPKG

9.51 kBJavaScriptView Raw
1"use strict";
2var __assign = (this && this.__assign) || Object.assign || function(t) {
3 for (var s, i = 1, n = arguments.length; i < n; i++) {
4 s = arguments[i];
5 for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
6 t[p] = s[p];
7 }
8 return t;
9};
10var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
11 return new (P || (P = Promise))(function (resolve, reject) {
12 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
13 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
14 function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
15 step((generator = generator.apply(thisArg, _arguments || [])).next());
16 });
17};
18var __generator = (this && this.__generator) || function (thisArg, body) {
19 var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
20 return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
21 function verb(n) { return function (v) { return step([n, v]); }; }
22 function step(op) {
23 if (f) throw new TypeError("Generator is already executing.");
24 while (_) try {
25 if (f = 1, y && (t = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t;
26 if (y = 0, t) op = [0, t.value];
27 switch (op[0]) {
28 case 0: case 1: t = op; break;
29 case 4: _.label++; return { value: op[1], done: false };
30 case 5: _.label++; y = op[1]; op = [0]; continue;
31 case 7: op = _.ops.pop(); _.trys.pop(); continue;
32 default:
33 if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
34 if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
35 if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
36 if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
37 if (t[2]) _.ops.pop();
38 _.trys.pop(); continue;
39 }
40 op = body.call(thisArg, _);
41 } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
42 if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
43 }
44};
45Object.defineProperty(exports, "__esModule", { value: true });
46var fs_1 = require("fs");
47var path_1 = require("path");
48var graphql_import_1 = require("graphql-import");
49var minimatch = require("minimatch");
50var yaml = require("js-yaml");
51var graphql_1 = require("graphql");
52function readConfig(configPath) {
53 var config;
54 try {
55 var rawConfig = fs_1.readFileSync(configPath, 'utf-8');
56 if (configPath.endsWith('.yaml') || configPath.endsWith('.yml')) {
57 config = yaml.safeLoad(rawConfig);
58 }
59 else {
60 config = JSON.parse(rawConfig);
61 }
62 }
63 catch (error) {
64 error.message = "Parsing " + configPath + " file has failed.\n" + error.message;
65 throw error;
66 }
67 return config;
68}
69exports.readConfig = readConfig;
70function writeConfig(configPath, config) {
71 var configContents;
72 if (configPath.endsWith('.yaml')) {
73 configContents = yaml.safeDump(config);
74 }
75 else {
76 configContents = JSON.stringify(config);
77 }
78 fs_1.writeFileSync(configPath, configContents, 'utf-8');
79}
80exports.writeConfig = writeConfig;
81function normalizeGlob(glob) {
82 if (glob.startsWith('./')) {
83 return glob.substr(2);
84 }
85 return glob;
86}
87exports.normalizeGlob = normalizeGlob;
88function matchesGlobs(filePath, configDir, globs) {
89 return (globs || []).some(function (glob) {
90 try {
91 var globStat = fs_1.lstatSync(path_1.join(configDir, glob));
92 var newGlob = glob.length === 0 ? '.' : glob;
93 var globToMatch = globStat.isDirectory() ? glob + "/**" : glob;
94 return minimatch(filePath, globToMatch, { matchBase: true });
95 }
96 catch (error) {
97 // Out of errors that lstat provides, EACCES and ENOENT are the
98 // most likely. For both cases, run the match with the raw glob
99 // and return the result.
100 return minimatch(filePath, glob, { matchBase: true });
101 }
102 });
103}
104exports.matchesGlobs = matchesGlobs;
105function validateConfig(config) {
106 // FIXME: implement
107}
108exports.validateConfig = validateConfig;
109function mergeConfigs(dest, src) {
110 var result = __assign({}, dest, src);
111 if (dest.extensions && src.extensions) {
112 result.extensions = __assign({}, dest.extensions, src.extensions);
113 }
114 if (dest.projects && src.projects) {
115 result.projects = __assign({}, dest.projects, src.projects);
116 }
117 return result;
118}
119exports.mergeConfigs = mergeConfigs;
120function schemaToIntrospection(schema) {
121 return graphql_1.graphql(schema, graphql_1.introspectionQuery);
122}
123exports.schemaToIntrospection = schemaToIntrospection;
124function introspectionToSchema(introspection) {
125 if (introspection.errors != null) {
126 throw new Error('Introspection result containts errors');
127 }
128 if (introspection.data == null) {
129 throw new Error('Missing "data" property from introspection result');
130 }
131 return graphql_1.buildClientSchema(introspection.data);
132}
133exports.introspectionToSchema = introspectionToSchema;
134function readSchema(path) {
135 // FIXME: prefix error
136 switch (path_1.extname(path)) {
137 case '.graphql':
138 return valueToSchema(graphql_import_1.importSchema(path));
139 case '.json':
140 var data = fs_1.readFileSync(path, { encoding: 'utf-8' });
141 var introspection = JSON.parse(data);
142 return valueToSchema(introspection);
143 default:
144 throw new Error('Unsupported schema file extention. Only ".graphql" and ".json" are supported');
145 }
146}
147exports.readSchema = readSchema;
148function valueToSchema(schema) {
149 if (schema instanceof graphql_1.GraphQLSchema) {
150 return schema;
151 }
152 else if (typeof schema === 'string') {
153 return graphql_1.buildSchema(schema);
154 }
155 else if (schema instanceof graphql_1.Source) {
156 return graphql_1.buildSchema(schema);
157 }
158 else if (typeof schema === 'object' && !Array.isArray(schema)) {
159 return introspectionToSchema(schema);
160 }
161 throw new Error('Can not convert data to a schema');
162}
163function writeSchema(path, schema, schemaExtensions) {
164 return __awaiter(this, void 0, void 0, function () {
165 var data, _a, name_1, introspection, _b;
166 return __generator(this, function (_c) {
167 switch (_c.label) {
168 case 0:
169 schema = valueToSchema(schema);
170 _a = path_1.extname(path);
171 switch (_a) {
172 case '.graphql': return [3 /*break*/, 1];
173 case '.json': return [3 /*break*/, 2];
174 }
175 return [3 /*break*/, 4];
176 case 1:
177 data = '';
178 if (schemaExtensions) {
179 for (name_1 in schemaExtensions) {
180 data += "# " + name_1 + ": " + schemaExtensions[name_1] + "\n";
181 }
182 data += '\n';
183 }
184 data += graphql_1.printSchema(schema);
185 return [3 /*break*/, 5];
186 case 2: return [4 /*yield*/, schemaToIntrospection(schema)];
187 case 3:
188 introspection = _c.sent();
189 introspection.extensions = (_b = {},
190 _b['graphql-config'] = schemaExtensions,
191 _b);
192 data = JSON.stringify(introspection, null, 2);
193 return [3 /*break*/, 5];
194 case 4: throw new Error('Unsupported schema file extention. Only ".graphql" and ".json" are supported');
195 case 5:
196 fs_1.writeFileSync(path, data, 'utf-8');
197 return [2 /*return*/];
198 }
199 });
200 });
201}
202exports.writeSchema = writeSchema;
203function getSchemaExtensions(path) {
204 var data = fs_1.readFileSync(path, 'utf-8');
205 switch (path_1.extname(path)) {
206 case '.graphql':
207 var extensions = {};
208 for (var _i = 0, _a = data.split('\n'); _i < _a.length; _i++) {
209 var line = _a[_i];
210 var result = /# ([^:]+): (.+)$/.exec(line);
211 if (result == null) {
212 break;
213 }
214 var _1 = result[0], key = result[1], value = result[2];
215 extensions[key] = value;
216 }
217 return extensions;
218 case '.json':
219 var introspection = JSON.parse(data);
220 if (!introspection.extentions) {
221 return {};
222 }
223 return introspection.extensions['graphql-config'] || {};
224 default:
225 throw new Error('Unsupported schema file extention. Only ".graphql" and ".json" are supported');
226 }
227}
228exports.getSchemaExtensions = getSchemaExtensions;