UNPKG

1.77 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var fs = require("fs");
4var graphql_codegen_core_1 = require("graphql-codegen-core");
5var path = require("path");
6// from https://github.com/npm/npm/blob/latest/lib/utils/parse-json.js
7// from read-package-json
8var stripBOM = function (content) {
9 content = content.toString();
10 // Remove byte order marker. This catches EF BB BF (the UTF-8 BOM)
11 // because the buffer-to-string conversion in `fs.readFileSync()`
12 // translates it to FEFF, the UTF-16 BOM.
13 if (content.charCodeAt(0) === 0xfeff) {
14 content = content.slice(1);
15 }
16 return content;
17};
18exports.parseBOM = function (content) { return JSON.parse(stripBOM(content)); };
19exports.introspectionFromFile = function (file) {
20 graphql_codegen_core_1.logger.info("Loading GraphQL Introspection from file: " + file + "...");
21 return new Promise(function (resolve, reject) {
22 var fullPath = path.isAbsolute(file) ? file : path.resolve(process.cwd(), file);
23 if (fs.existsSync(fullPath)) {
24 try {
25 var fileContent = fs.readFileSync(fullPath, 'utf8');
26 if (!fileContent) {
27 reject("Unable to read local introspection file: " + fullPath);
28 }
29 var introspection = exports.parseBOM(fileContent);
30 if (introspection.data) {
31 introspection = introspection.data;
32 }
33 resolve(introspection);
34 }
35 catch (e) {
36 reject(e);
37 }
38 }
39 else {
40 reject("Unable to locate local introspection file: " + fullPath);
41 }
42 });
43};
44//# sourceMappingURL=introspection-from-file.js.map
\No newline at end of file