UNPKG

2.64 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var graphql_codegen_core_1 = require("graphql-codegen-core");
4var fs_1 = require("fs");
5var isValidPath = require("is-valid-path");
6var path_1 = require("path");
7var IntrospectionFromFileLoader = /** @class */ (function () {
8 function IntrospectionFromFileLoader() {
9 }
10 IntrospectionFromFileLoader.prototype.stripBOM = function (content) {
11 content = content.toString();
12 // Remove byte order marker. This catches EF BB BF (the UTF-8 BOM)
13 // because the buffer-to-string conversion in `fs.readFileSync()`
14 // translates it to FEFF, the UTF-16 BOM.
15 if (content.charCodeAt(0) === 0xfeff) {
16 content = content.slice(1);
17 }
18 return content;
19 };
20 IntrospectionFromFileLoader.prototype.parseBOM = function (content) {
21 return JSON.parse(this.stripBOM(content));
22 };
23 IntrospectionFromFileLoader.prototype.canHandle = function (pointerToSchema) {
24 return isValidPath(pointerToSchema) && fs_1.existsSync(pointerToSchema) && path_1.extname(pointerToSchema) === '.json';
25 };
26 IntrospectionFromFileLoader.prototype.handle = function (pointerToSchema, _cliOptions) {
27 var _this = this;
28 graphql_codegen_core_1.getLogger().info("Loading GraphQL Introspection from file: " + pointerToSchema + "...");
29 return new Promise(function (resolve, reject) {
30 var fullPath = path_1.isAbsolute(pointerToSchema) ? pointerToSchema : path_1.resolve(process.cwd(), pointerToSchema);
31 if (fs_1.existsSync(fullPath)) {
32 try {
33 var fileContent = fs_1.readFileSync(fullPath, 'utf8');
34 if (!fileContent) {
35 reject("Unable to read local introspection file: " + fullPath);
36 }
37 var introspection = _this.parseBOM(fileContent);
38 if (introspection.data) {
39 introspection = introspection.data;
40 }
41 graphql_codegen_core_1.validateIntrospection(introspection);
42 resolve(graphql_codegen_core_1.introspectionToGraphQLSchema(introspection));
43 }
44 catch (e) {
45 reject(e);
46 }
47 }
48 else {
49 reject("Unable to locate local introspection file: " + fullPath);
50 }
51 });
52 };
53 return IntrospectionFromFileLoader;
54}());
55exports.IntrospectionFromFileLoader = IntrospectionFromFileLoader;
56//# sourceMappingURL=introspection-from-file.js.map
\No newline at end of file