UNPKG

5.63 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.IDLFileHandler = undefined;
7
8var _toStringTag = require('babel-runtime/core-js/symbol/to-string-tag');
9
10var _toStringTag2 = _interopRequireDefault(_toStringTag);
11
12function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
13
14/**
15 * The handler, an instance of which is created for every instance of GQLBase.
16 * The handler manages the fetching and decoding of files bearing the IDL
17 * schema associated with the class represented by this instance of GQLBase.
18 *
19 * @class IDLFileHandler
20 */
21let IDLFileHandler = exports.IDLFileHandler = class IDLFileHandler {
22
23 /**
24 * The IDLFileHandler checks the SCHEMA value returned by the class type
25 * of the supplied instance. If the resulting value is a Symbol, then the
26 * handler's responsibility is to find the file, load it from disk and
27 * provide various means of using its contents; i.e. as a Buffer, a String
28 * or wrapped in a SyntaxTree instance.
29 *
30 * @memberof IDLFileHandler
31 * @method ⎆⠀constructor
32 * @constructor
33 *
34 * @param {Function} Class a function or class definition that presumably
35 * extends from GQLBase were it an instance.
36 */
37 constructor(Class) {
38 // $FlowFixMe
39 const symbol = typeof Class.SCHEMA === 'symbol' && Class.SCHEMA || null;
40 const pattern = /Symbol\(Path (.*?) Extension (.*?)\)/;
41
42 if (symbol) {
43 let symbolString = symbol.toString();
44
45 if (symbol === Class.ADJACENT_FILE) {
46 if (Class.module === module) {
47 throw new Error(`
48 The a static getter for 'module' on ${Class.name} must be present
49 that returns the module object where the Class is defined. Try the
50 following:
51
52 // your ${Class.name}.js file
53 import { GQLBase } from 'graphql-lattice'
54
55 const ${Class.name}Module = module;
56
57 class ${Class.name} extends GQLBase {
58 ...
59
60 static get module() {
61 return ${Class.name}Module;
62 }
63 }
64
65 `);
66 }
67
68 const filename = Class.module.filename;
69 const extension = Path.extname(filename);
70 const dir = Path.dirname(filename);
71 const filefixed = Path.basename(filename, extension);
72 const build = Path.resolve(Path.join(dir, `${filefixed}.graphql`));
73
74 this.path = build;
75 this.extension = '.graphql';
76 } else if (pattern.test(symbolString)) {
77 const parsed = pattern.exec(symbolString);
78 const extension = parsed[2] || '.graphql';
79 const dir = Path.dirname(parsed[1]);
80 const file = Path.basename(parsed[1], extension);
81 const build = Path.resolve(Path.join(dir, `${file}${extension}`));
82
83 this.path = build;
84 this.extension = extension;
85 }
86 } else {
87 this.path = this.extension = null;
88 }
89 }
90
91 /**
92 * Loads the calculated file determined by the decoding of the meaning of
93 * the Symbol returned by the SCHEMA property of the instance supplied to
94 * the IDLFileHandler upon creation.
95 *
96 * @instance
97 * @memberof IDLFileHandler
98 * @method ⌾⠀getFile
99 *
100 * @return {Buffer|null} returns the Buffer containing the file base IDL
101 * schema or null if none was found or a direct string schema is returned
102 * by the SCHEMA property
103 */
104 getFile() {
105 return fs.readFileSync(String(this.path));
106 }
107
108 /**
109 * If getFile() returns a Buffer, this is the string representation of the
110 * underlying file contents. As a means of validating the contents of the
111 * file, the string contents are parsed into an AST and back to a string.
112 *
113 * @instance
114 * @memberof IDLFileHandler
115 * @method ⌾⠀getSchema
116 *
117 * @return {string|null} the string contents of the Buffer containing the
118 * file based IDL schema.
119 */
120 getSchema() {
121 if (!this.path) {
122 return null;
123 }
124
125 const tree = this.getSyntaxTree();
126
127 return tree.toString();
128 }
129
130 /**
131 * If getFile() returns a Buffer, the string contents are passed to a new
132 * instance of SyntaxTree which parses this into an AST for manipulation.
133 *
134 * @instance
135 * @memberof IDLFileHandler
136 * @method ⌾⠀getSyntaxTree
137 *
138 * @return {SyntaxTree|null} a SyntaxTree instance constructed from the IDL
139 * schema contents loaded from disk. Null is returned if a calculated path
140 * cannot be found; always occurs when SCHEMA returns a string.
141 */
142 getSyntaxTree() {
143 const buffer = this.getFile();
144 const tree = new SyntaxTree(buffer.toString());
145
146 return tree;
147 }
148
149 /**
150 * Returns the `constructor` name. If invoked as the context, or `this`,
151 * object of the `toString` method of `Object`'s `prototype`, the resulting
152 * value will be `[object MyClass]`, given an instance of `MyClass`
153 *
154 * @method ⌾⠀[Symbol.toStringTag]
155 * @memberof IDLFileHandler
156 *
157 * @return {string} the name of the class this is an instance of
158 * @ComputedType
159 */
160 get [_toStringTag2.default]() {
161 return this.constructor.name;
162 }
163
164 /**
165 * Applies the same logic as {@link #[Symbol.toStringTag]} but on a static
166 * scale. So, if you perform `Object.prototype.toString.call(MyClass)`
167 * the result would be `[object MyClass]`.
168 *
169 * @method ⌾⠀[Symbol.toStringTag]
170 * @memberof IDLFileHandler
171 * @static
172 *
173 * @return {string} the name of this class
174 * @ComputedType
175 */
176 static get [_toStringTag2.default]() {
177 return this.name;
178 }
179};
180exports.default = IDLFileHandler;
181//# sourceMappingURL=IDLFileHandler.js.map
\No newline at end of file