UNPKG

9.55 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;
124// Predicate for errors/data can be removed after typescript 2.7.
125// See: https://github.com/Microsoft/TypeScript/pull/19513
126function introspectionToSchema(introspection) {
127 if (introspection.errors != null) {
128 throw new Error('Introspection result contains errors');
129 }
130 return graphql_1.buildClientSchema(introspection.data ? introspection.data : introspection);
131}
132exports.introspectionToSchema = introspectionToSchema;
133function readSchema(path) {
134 // FIXME: prefix error
135 switch (path_1.extname(path)) {
136 case '.graphql':
137 return valueToSchema(graphql_import_1.importSchema(path));
138 case '.json':
139 var data = fs_1.readFileSync(path, { encoding: 'utf-8' });
140 var introspection = JSON.parse(data);
141 return valueToSchema(introspection);
142 default:
143 throw new Error('Unsupported schema file extention. Only ".graphql" and ".json" are supported');
144 }
145}
146exports.readSchema = readSchema;
147function valueToSchema(schema) {
148 if (schema instanceof graphql_1.GraphQLSchema) {
149 return schema;
150 }
151 else if (typeof schema === 'string') {
152 return graphql_1.buildSchema(schema);
153 }
154 else if (schema instanceof graphql_1.Source) {
155 return graphql_1.buildSchema(schema);
156 }
157 else if (typeof schema === 'object' && !Array.isArray(schema)) {
158 return introspectionToSchema(schema);
159 }
160 throw new Error('Can not convert data to a schema');
161}
162function writeSchema(path, schema, schemaExtensions) {
163 return __awaiter(this, void 0, void 0, function () {
164 var data, _a, name_1, introspection, _b;
165 return __generator(this, function (_c) {
166 switch (_c.label) {
167 case 0:
168 schema = valueToSchema(schema);
169 _a = path_1.extname(path);
170 switch (_a) {
171 case '.graphql': return [3 /*break*/, 1];
172 case '.json': return [3 /*break*/, 2];
173 }
174 return [3 /*break*/, 4];
175 case 1:
176 data = '';
177 if (schemaExtensions) {
178 for (name_1 in schemaExtensions) {
179 data += "# " + name_1 + ": " + schemaExtensions[name_1] + "\n";
180 }
181 data += '\n';
182 }
183 data += graphql_1.printSchema(schema);
184 return [3 /*break*/, 5];
185 case 2: return [4 /*yield*/, schemaToIntrospection(schema)];
186 case 3:
187 introspection = _c.sent();
188 introspection.extensions = (_b = {},
189 _b['graphql-config'] = schemaExtensions,
190 _b);
191 data = JSON.stringify(introspection, null, 2);
192 return [3 /*break*/, 5];
193 case 4: throw new Error('Unsupported schema file extention. Only ".graphql" and ".json" are supported');
194 case 5:
195 fs_1.writeFileSync(path, data, 'utf-8');
196 return [2 /*return*/];
197 }
198 });
199 });
200}
201exports.writeSchema = writeSchema;
202function getSchemaExtensions(path) {
203 var data = fs_1.readFileSync(path, 'utf-8');
204 switch (path_1.extname(path)) {
205 case '.graphql':
206 var extensions = {};
207 for (var _i = 0, _a = data.split('\n'); _i < _a.length; _i++) {
208 var line = _a[_i];
209 var result = /# ([^:]+): (.+)$/.exec(line);
210 if (result == null) {
211 break;
212 }
213 var _1 = result[0], key = result[1], value = result[2];
214 extensions[key] = value;
215 }
216 return extensions;
217 case '.json':
218 var introspection = JSON.parse(data);
219 if (!introspection.extentions) {
220 return {};
221 }
222 return introspection.extensions['graphql-config'] || {};
223 default:
224 throw new Error('Unsupported schema file extention. Only ".graphql" and ".json" are supported');
225 }
226}
227exports.getSchemaExtensions = getSchemaExtensions;