1 | "use strict";
|
2 | var commander = require("commander");
|
3 | var fs = require("fs");
|
4 | var glob = require("glob");
|
5 | var request = require("request");
|
6 | var templates_1 = require("./templates");
|
7 | var introspectionQuery_1 = require("graphql/utilities/introspectionQuery");
|
8 | exports.initCLI = function (args) {
|
9 | commander
|
10 | .version('0.0.1')
|
11 | .usage('graphql-codegen [options]')
|
12 | .option('-d, --dev', 'Turn on development mode - prints results to console')
|
13 | .option('-f, --file <filePath>', 'Parse local GraphQL introspection JSON file')
|
14 | .option('-u, --url <graphql-endpoint>', 'Parse remote GraphQL endpoint as introspection file')
|
15 | .option('-t, --template <template-name>', 'Language/platform name templates')
|
16 | .option('-o, --out <path>', 'Output file(s) path', String, './')
|
17 | .arguments('<options> [documents...]')
|
18 | .parse(args);
|
19 | return commander;
|
20 | };
|
21 | exports.cliError = function (err) {
|
22 | if (typeof err === 'object') {
|
23 | console.log(err);
|
24 | }
|
25 | console.error('Error: ' + err);
|
26 | process.exit(1);
|
27 | return;
|
28 | };
|
29 | exports.validateCliOptions = function (options) {
|
30 | var file = options['file'];
|
31 | var url = options['url'];
|
32 | var template = options['template'];
|
33 | var out = options['out'];
|
34 | var documents = options['args'] || [];
|
35 | if (!file && !url) {
|
36 | exports.cliError('Please specify one of --file or --url flags!');
|
37 | }
|
38 | if (!template) {
|
39 | exports.cliError('Please specify language/platform, using --template flag');
|
40 | }
|
41 | };
|
42 | var getGeneratorTemplate = function (templateName) {
|
43 | return templates_1.generators.find(function (item) { return (item.aliases || []).indexOf(templateName.toLowerCase()) > -1; });
|
44 | };
|
45 | exports.transformOptions = function (options) {
|
46 | var file = options['file'];
|
47 | var url = options['url'];
|
48 | var documents = options['args'] || [];
|
49 | var template = options['template'];
|
50 | var out = options['out'];
|
51 | var isDev = options['dev'] !== undefined;
|
52 | var result = {};
|
53 | var introspectionPromise;
|
54 | if (isDev) {
|
55 | console.log('Development mode is ON - output will print to console');
|
56 | }
|
57 | if (file) {
|
58 | introspectionPromise = new Promise(function (resolve, reject) {
|
59 | if (fs.existsSync(file)) {
|
60 | try {
|
61 | var fileContent = fs.readFileSync(file, 'utf8');
|
62 | if (!fileContent) {
|
63 | reject("Unable to read local introspection file: " + file);
|
64 | }
|
65 | resolve(JSON.parse(fileContent));
|
66 | }
|
67 | catch (e) {
|
68 | reject(e);
|
69 | }
|
70 | }
|
71 | else {
|
72 | reject("Unable to locate local introspection file: " + file);
|
73 | }
|
74 | });
|
75 | }
|
76 | else if (url) {
|
77 | introspectionPromise = new Promise(function (resolve, reject) {
|
78 | request.post({
|
79 | url: url,
|
80 | json: {
|
81 | query: introspectionQuery_1.introspectionQuery
|
82 | },
|
83 | headers: {
|
84 | 'Accept': 'application/json',
|
85 | 'Content-Type': 'application/json'
|
86 | }
|
87 | }, function (err, response, body) {
|
88 | if (err) {
|
89 | reject(err);
|
90 | return;
|
91 | }
|
92 | var bodyJson = body.data;
|
93 | if (!bodyJson || (bodyJson.errors && bodyJson.errors.length > 0)) {
|
94 | reject('Unable to download schema from remote: ' + bodyJson.errors.map(function (item) { return item.message; }).join(', '));
|
95 | return;
|
96 | }
|
97 | console.log(JSON.stringify(bodyJson));
|
98 | resolve(bodyJson);
|
99 | });
|
100 | });
|
101 | }
|
102 | var documentsPromises = documents.map(function (documentGlob) {
|
103 | return new Promise(function (resolve, reject) {
|
104 | glob(documentGlob, function (err, files) {
|
105 | if (err) {
|
106 | reject(err);
|
107 | }
|
108 | if (!files || files.length === 0) {
|
109 | reject("Unable to locate files matching glob definition: " + documentGlob);
|
110 | }
|
111 | resolve(files);
|
112 | });
|
113 | });
|
114 | });
|
115 | var generatorTemplatePromise = new Promise(function (resolve, reject) {
|
116 | var generatorTemplate = getGeneratorTemplate(template);
|
117 | if (generatorTemplate) {
|
118 | resolve(generatorTemplate);
|
119 | }
|
120 | else {
|
121 | var allowedTemplates = templates_1.generators.map(function (item) { return item.aliases; }).reduce(function (a, b) { return a.concat(b); }).join(', ');
|
122 | reject("Unknown template language specified: " + template + ", available are: " + allowedTemplates);
|
123 | }
|
124 | });
|
125 | var documentsPromise = Promise.all(documentsPromises).then(function (files) {
|
126 | return files.length === 0 ? [] : files.reduce(function (a, b) {
|
127 | return a.concat(b);
|
128 | });
|
129 | });
|
130 | return Promise.all([
|
131 | introspectionPromise,
|
132 | documentsPromise,
|
133 | generatorTemplatePromise
|
134 | ]).then(function (_a) {
|
135 | var introspection = _a[0], documents = _a[1], generatorTemplate = _a[2];
|
136 | result.introspection = introspection;
|
137 | result.documents = documents;
|
138 | result.template = generatorTemplate;
|
139 | result.outPath = out;
|
140 | result.isDev = isDev;
|
141 | return result;
|
142 | });
|
143 | };
|
144 |
|
\ | No newline at end of file |