UNPKG

2.82 kBJavaScriptView Raw
1"use strict";
2var commander = require("commander");
3var introspection_from_url_1 = require("./introspection-from-url");
4var introspection_from_file_1 = require("./introspection-from-file");
5var documents_glob_1 = require("./documents-glob");
6var template_loader_1 = require("./template-loader");
7exports.initCLI = function (args) {
8 commander
9 .version(require('../package.json').version)
10 .usage('graphql-codegen [options]')
11 .option('-d, --dev', 'Turn on development mode - prints results to console')
12 .option('-f, --file <filePath>', 'Parse local GraphQL introspection JSON file')
13 .option('-u, --url <graphql-endpoint>', 'Parse remote GraphQL endpoint as introspection file')
14 .option('-t, --template <template-name>', 'Language/platform name templates')
15 .option('-o, --out <path>', 'Output file(s) path', String, './')
16 .arguments('<options> [documents...]')
17 .parse(args);
18 return commander;
19};
20exports.cliError = function (err) {
21 if (typeof err === 'object') {
22 console.log(err);
23 }
24 console.error('Error: ' + err);
25 process.exit(1);
26 return;
27};
28exports.validateCliOptions = function (options) {
29 var file = options['file'];
30 var url = options['url'];
31 var template = options['template'];
32 var out = options['out'];
33 if (!file && !url) {
34 exports.cliError('Please specify one of --file or --url flags!');
35 }
36 if (!template) {
37 exports.cliError('Please specify language/platform, using --template flag');
38 }
39};
40exports.transformOptions = function (options) {
41 var file = options['file'];
42 var url = options['url'];
43 var documents = options['args'] || [];
44 var template = options['template'];
45 var out = options['out'];
46 var isDev = options['dev'] !== undefined;
47 var result = {};
48 var introspectionPromise;
49 if (isDev) {
50 console.log('Development mode is ON - output will print to console');
51 }
52 if (file) {
53 introspectionPromise = introspection_from_file_1.introspectionFromFile(file);
54 }
55 else if (url) {
56 introspectionPromise = introspection_from_url_1.introspectionFromUrl(url);
57 }
58 var documentsPromise = documents_glob_1.documentsFromGlobs(documents);
59 var generatorTemplatePromise = template_loader_1.getTemplateGenerator(template);
60 return Promise.all([
61 introspectionPromise,
62 documentsPromise,
63 generatorTemplatePromise
64 ]).then(function (_a) {
65 var introspection = _a[0], documents = _a[1], generatorTemplate = _a[2];
66 result.introspection = introspection;
67 result.documents = documents;
68 result.template = generatorTemplate;
69 result.outPath = out;
70 result.isDev = isDev;
71 return result;
72 });
73};
74//# sourceMappingURL=cli.js.map
\No newline at end of file