1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | var graphql_codegen_core_1 = require("graphql-codegen-core");
|
4 | var fs_1 = require("fs");
|
5 | var isValidPath = require("is-valid-path");
|
6 | var path_1 = require("path");
|
7 | var IntrospectionFromFileLoader = (function () {
|
8 | function IntrospectionFromFileLoader() {
|
9 | }
|
10 | IntrospectionFromFileLoader.prototype.stripBOM = function (content) {
|
11 | content = content.toString();
|
12 |
|
13 |
|
14 |
|
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, config, schemaOptions) {
|
27 |
|
28 | var _this = this;
|
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 | }());
|
55 | exports.IntrospectionFromFileLoader = IntrospectionFromFileLoader;
|
56 |
|
\ | No newline at end of file |