UNPKG

4.76 kBJavaScriptView Raw
1"use strict";
2var __assign = (this && this.__assign) || function () {
3 __assign = Object.assign || function(t) {
4 for (var s, i = 1, n = arguments.length; i < n; i++) {
5 s = arguments[i];
6 for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7 t[p] = s[p];
8 }
9 return t;
10 };
11 return __assign.apply(this, arguments);
12};
13Object.defineProperty(exports, "__esModule", { value: true });
14var commander = require("commander");
15var graphql_config_1 = require("graphql-config");
16var fs_1 = require("fs");
17var path_1 = require("path");
18var cli_error_1 = require("./utils/cli-error");
19function collect(val, memo) {
20 memo.push(val);
21 return memo;
22}
23exports.initCLI = function (args) {
24 commander
25 .usage('gql-gen [options]')
26 .option('-s, --schema <path>', 'Path to GraphQL schema: local JSON file, GraphQL endpoint, local file that exports GraphQLSchema/AST/JSON')
27 .option('-cs, --clientSchema <path>', 'Path to GraphQL client schema: local JSON file, local file that exports GraphQLSchema/AST/JSON')
28 .option('-h, --header [header]', 'Header to add to the introspection HTTP request when using --url/--schema with url', collect, [])
29 .option('-t, --template <template-name>', 'Language/platform name templates, or a name of NPM modules that `export default` GqlGenConfig object')
30 .option('-p, --project <project-path>', 'Project path(s) to scan for custom template files')
31 .option('--config <json-file>', 'Codegen configuration file, defaults to: ./gql-gen.json')
32 .option('-m, --skip-schema', 'Generates only client side documents, without server side schema types')
33 .option('-c, --skip-documents', 'Generates only server side schema types, without client side documents')
34 .option('-o, --out <path>', 'Output file(s) path', String, './')
35 .option('-r, --require [require]', 'module to preload (option can be repeated)', collect, [])
36 .option('-ow, --no-overwrite', 'Skip file writing if the output file(s) already exists in path')
37 .option('-w, --watch', 'Watch for changes and execute generation automatically')
38 .option('--silent', 'Does not print anything to the console')
39 .option('-ms, --merge-schema <merge-logic>', 'Merge schemas with custom logic')
40 .arguments('<options> [documents...]')
41 .parse(args);
42 return commander;
43};
44exports.validateCliOptions = function (options) {
45 var schema = options.schema;
46 var template = options.template;
47 var project = options.project;
48 if (!schema) {
49 try {
50 var graphqlProjectConfig = graphql_config_1.getGraphQLProjectConfig(project);
51 options.schema = graphqlProjectConfig.schemaPath;
52 }
53 catch (e) {
54 if (e instanceof graphql_config_1.ConfigNotFoundError) {
55 cli_error_1.cliError('Flag --schema is missing!');
56 }
57 }
58 }
59 if (!template && !project) {
60 cli_error_1.cliError('Please specify language/platform, using --template flag!');
61 }
62};
63function transformTemplatesToPlugins(options, templateSpecificConfig) {
64 if (templateSpecificConfig === void 0) { templateSpecificConfig = {}; }
65 if (options.template === 'ts' || options.template === 'typescript') {
66 return {
67 config: templateSpecificConfig,
68 plugins: [
69 templateSpecificConfig.printTime ? 'time' : null,
70 'typescript-common',
71 options.skipDocuments ? null : 'typescript-client',
72 options.skipSchema ? null : 'typescript-server'
73 ].filter(function (s) { return s; })
74 };
75 }
76 return { plugins: [] };
77}
78function createConfigFromOldCli(options) {
79 var _a;
80 exports.validateCliOptions(options);
81 var rootConfig = {};
82 var configPath = options.config ? options.config : fs_1.existsSync(path_1.join(process.cwd(), './gql-gen.json'));
83 if (configPath && typeof configPath === 'string') {
84 var rawObj = JSON.parse(fs_1.readFileSync(configPath, 'utf-8'));
85 rootConfig = (rawObj || {}).generatorConfig || {};
86 }
87 var configObject = {
88 schema: [options.schema, options.clientSchema].filter(function (s) { return s; }),
89 documents: options.args || [],
90 config: rootConfig,
91 generates: (_a = {},
92 _a[options.out] = transformTemplatesToPlugins(options, __assign({}, rootConfig, (options.templateConfig || {}))),
93 _a),
94 silent: options.silent,
95 watch: options.watch,
96 require: options.require
97 };
98 return configObject;
99}
100exports.createConfigFromOldCli = createConfigFromOldCli;
101//# sourceMappingURL=old-cli-config.js.map
\No newline at end of file