-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
73 lines (57 loc) · 2.09 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
require('string.prototype.startswith');
// TODO: remove this polyfill when cashay addresses
// https://github.com/mattkrick/cashay/issues/136
var Filter = require('broccoli-filter');
var fs = require('fs');
var mkdirp = require('mkdirp');
var path = require('path');
module.exports = CashaySchema;
CashaySchema.prototype = Object.create(Filter.prototype);
CashaySchema.constructor = CashaySchema;
function checkOptions(options) {
if (!options.graphql) {
throw new Error(
'You must provide the graphql package to broccoli-cashay-schema');
}
if (!options.cashay) {
throw new Error(
'You must provide the cashay package to broccoli-cashay-schema');
}
}
function CashaySchema(inputNode, _options) {
var options = _options || {};
if (!(this instanceof CashaySchema)) {
return new CashaySchema(inputNode, options);
}
checkOptions(options);
Filter.call(this, inputNode, {
annotation: options.annotation
});
// Cashay package
this.cashay = options.cashay;
// GraphQL package
this.graphql = options.graphql;
// Server schema path relative to `inputNode`
this.serverSchemaPath = options.serverSchemaPath || 'schema.js';
// Output path for the client schema (will be appended to plugin output dir)
this.clientSchemaPath = options.clientSchemaPath || 'client/schema.js';
}
CashaySchema.prototype.canProcessFile = function(relativePath) {
return relativePath === this.serverSchemaPath;
}
CashaySchema.prototype.getDestFilePath = function() {
return this.clientSchemaPath;
}
CashaySchema.prototype.processFile = function(srcDir, destDir, relativePath) {
var outputPath = path.join(destDir, this.getDestFilePath(relativePath));
// Load the server schema
var fullPath = path.join(srcDir, relativePath);
var rootSchema = require(fullPath);
// Generate a client-safe schema
return this.cashay.transformSchema(rootSchema, this.graphql.graphql).
then(function(clientSchema) {
var content = 'export default ' + JSON.stringify(clientSchema);
mkdirp.sync(path.dirname(outputPath));
fs.writeFileSync(outputPath, content, { encoding: 'utf-8' });
});
}