1 | "use strict";
|
2 | var commander = require("commander");
|
3 | var introspection_from_url_1 = require("./introspection-from-url");
|
4 | var introspection_from_file_1 = require("./introspection-from-file");
|
5 | var documents_glob_1 = require("./documents-glob");
|
6 | var template_loader_1 = require("./template-loader");
|
7 | var introspection_from_export_1 = require("./introspection-from-export");
|
8 | function collect(val, memo) {
|
9 | memo.push(val);
|
10 | return memo;
|
11 | }
|
12 | exports.initCLI = function (args) {
|
13 | commander
|
14 | .version(require('../package.json').version)
|
15 | .usage('graphql-codegen [options]')
|
16 | .option('-d, --dev', 'Turn on development mode - prints results to console')
|
17 | .option('-f, --file <filePath>', 'Parse local GraphQL introspection JSON file')
|
18 | .option('-u, --url <graphql-endpoint>', 'Parse remote GraphQL endpoint as introspection file')
|
19 | .option('-u, --export <export-file>', 'Path to a JavaScript (es5/6) file that exports (as default export) your `GraphQLSchema` object')
|
20 | .option('-h, --header [header]', 'Header to add to the introspection HTTP request when using --url', collect, [])
|
21 | .option('-t, --template <template-name>', 'Language/platform name templates')
|
22 | .option('-m, --no-schema', 'Generates only client side documents, without server side schema types')
|
23 | .option('-c, --no-documents', 'Generates only server side schema types, without client side documents')
|
24 | .option('-o, --out <path>', 'Output file(s) path', String, './')
|
25 | .arguments('<options> [documents...]')
|
26 | .parse(args);
|
27 | return commander;
|
28 | };
|
29 | exports.cliError = function (err) {
|
30 | if (typeof err === 'object') {
|
31 | console.log(err);
|
32 | }
|
33 | console.error('Error: ' + err);
|
34 | process.exit(1);
|
35 | return;
|
36 | };
|
37 | exports.validateCliOptions = function (options) {
|
38 | var file = options['file'];
|
39 | var url = options['url'];
|
40 | var fsExport = options['export'];
|
41 | var template = options['template'];
|
42 | var out = options['out'];
|
43 | if (!file && !url && !fsExport) {
|
44 | exports.cliError('Please specify one of --file, --url or --export flags!');
|
45 | }
|
46 | if (!template) {
|
47 | exports.cliError('Please specify language/platform, using --template flag');
|
48 | }
|
49 | };
|
50 | exports.transformOptions = function (options) {
|
51 | var file = options['file'];
|
52 | var url = options['url'];
|
53 | var fsExport = options['export'];
|
54 | var documents = options['args'] || [];
|
55 | var template = options['template'];
|
56 | var out = options['out'];
|
57 | var headers = options['header'] || [];
|
58 | var isDev = options['dev'] !== undefined;
|
59 | var noSchema = !options['schema'];
|
60 | var noDocuments = !options['documents'];
|
61 | var result = {};
|
62 | var introspectionPromise;
|
63 | if (isDev) {
|
64 | console.log('Development mode is ON - output will print to console');
|
65 | }
|
66 | if (file) {
|
67 | introspectionPromise = introspection_from_file_1.introspectionFromFile(file);
|
68 | }
|
69 | else if (url) {
|
70 | introspectionPromise = introspection_from_url_1.introspectionFromUrl(url, headers);
|
71 | }
|
72 | else if (fsExport) {
|
73 | introspectionPromise = introspection_from_export_1.introspectionFromExport(fsExport);
|
74 | }
|
75 | var documentsPromise = documents_glob_1.documentsFromGlobs(documents);
|
76 | var generatorTemplatePromise = template_loader_1.getTemplateGenerator(template);
|
77 | return Promise.all([
|
78 | introspectionPromise,
|
79 | documentsPromise,
|
80 | generatorTemplatePromise
|
81 | ]).then(function (_a) {
|
82 | var introspection = _a[0], documents = _a[1], generatorTemplate = _a[2];
|
83 | result.introspection = introspection;
|
84 | result.documents = documents;
|
85 | result.template = generatorTemplate;
|
86 | result.outPath = out;
|
87 | result.isDev = isDev;
|
88 | result.noSchema = noSchema;
|
89 | result.noDocuments = noDocuments;
|
90 | return result;
|
91 | });
|
92 | };
|
93 |
|
\ | No newline at end of file |