1 | "use strict";
|
2 | var __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 | };
|
13 | Object.defineProperty(exports, "__esModule", { value: true });
|
14 | var commander = require("commander");
|
15 | var graphql_config_1 = require("graphql-config");
|
16 | var fs_1 = require("fs");
|
17 | var path_1 = require("path");
|
18 | var cli_error_1 = require("./utils/cli-error");
|
19 | var YAML = require("json-to-pretty-yaml");
|
20 | function collect(val, memo) {
|
21 | memo.push(val);
|
22 | return memo;
|
23 | }
|
24 | exports.initCLI = function (args) {
|
25 | commander
|
26 | .usage('gql-gen [options]')
|
27 | .option('-s, --schema <path>', 'Path to GraphQL schema: local JSON file, GraphQL endpoint, local file that exports GraphQLSchema/AST/JSON')
|
28 | .option('-cs, --clientSchema <path>', 'Path to GraphQL client schema: local JSON file, local file that exports GraphQLSchema/AST/JSON')
|
29 | .option('-h, --header [header]', 'Header to add to the introspection HTTP request when using --url/--schema with url', collect, [])
|
30 | .option('-t, --template <template-name>', 'Language/platform name templates, or a name of NPM modules that `export default` GqlGenConfig object')
|
31 | .option('-p, --project <project-path>', 'Project path(s) to scan for custom template files')
|
32 | .option('--config <json-file>', 'Codegen configuration file, defaults to: ./gql-gen.json')
|
33 | .option('-m, --skip-schema', 'Generates only client side documents, without server side schema types')
|
34 | .option('-c, --skip-documents', 'Generates only server side schema types, without client side documents')
|
35 | .option('-o, --out <path>', 'Output file(s) path', String, './')
|
36 | .option('-r, --require [require]', 'module to preload (option can be repeated)', collect, [])
|
37 | .option('-ow, --no-overwrite', 'Skip file writing if the output file(s) already exists in path')
|
38 | .option('-w, --watch', 'Watch for changes and execute generation automatically')
|
39 | .option('--silent', 'Does not print anything to the console')
|
40 | .option('-ms, --merge-schema <merge-logic>', 'Merge schemas with custom logic')
|
41 | .arguments('<options> [documents...]')
|
42 | .parse(args);
|
43 | return commander;
|
44 | };
|
45 | exports.validateCliOptions = function (options) {
|
46 | var schema = options.schema;
|
47 | var template = options.template;
|
48 | var project = options.project;
|
49 | if (!schema) {
|
50 | try {
|
51 | var graphqlProjectConfig = graphql_config_1.getGraphQLProjectConfig(project);
|
52 | options.schema = graphqlProjectConfig.schemaPath;
|
53 | }
|
54 | catch (e) {
|
55 | if (e instanceof graphql_config_1.ConfigNotFoundError) {
|
56 | cli_error_1.cliError('Flag --schema is missing!');
|
57 | }
|
58 | }
|
59 | }
|
60 | if (!template && !project) {
|
61 | cli_error_1.cliError('Please specify language/platform, using --template flag!');
|
62 | }
|
63 | };
|
64 | function transformTemplatesToPlugins(options, templateSpecificConfig) {
|
65 | if (templateSpecificConfig === void 0) { templateSpecificConfig = {}; }
|
66 | if (options.template === 'ts' ||
|
67 | options.template === 'typescript' ||
|
68 | options.template === 'graphql-codegen-typescript-template') {
|
69 | return {
|
70 | config: templateSpecificConfig,
|
71 | plugins: [
|
72 | templateSpecificConfig.printTime ? 'time' : null,
|
73 | 'typescript-common',
|
74 | options.skipDocuments ? null : 'typescript-client',
|
75 | options.skipSchema ? null : 'typescript-server'
|
76 | ].filter(function (s) { return s; })
|
77 | };
|
78 | }
|
79 | else if (options.template === 'typescript-resolvers' ||
|
80 | options.template === 'graphql-codegen-typescript-resolvers-template') {
|
81 | return {
|
82 | config: templateSpecificConfig,
|
83 | plugins: [templateSpecificConfig.printTime ? 'time' : null, 'typescript-common', 'typescript-resolvers'].filter(function (s) { return s; })
|
84 | };
|
85 | }
|
86 | else if (options.template === 'typescript-mongodb' ||
|
87 | options.template === 'graphql-codegen-typescript-mongodb-template') {
|
88 | return {
|
89 | config: templateSpecificConfig,
|
90 | plugins: [
|
91 | templateSpecificConfig.printTime ? 'time' : null,
|
92 | 'typescript-common',
|
93 | 'typescript-server',
|
94 | 'typescript-mongodb'
|
95 | ].filter(function (s) { return s; })
|
96 | };
|
97 | }
|
98 | else if (options.template === 'apollo-angular' || options.template === 'graphql-codegen-apollo-angular-template') {
|
99 | return {
|
100 | config: templateSpecificConfig,
|
101 | plugins: [
|
102 | templateSpecificConfig.printTime ? 'time' : null,
|
103 | 'typescript-common',
|
104 | 'typescript-client',
|
105 | 'typescript-apollo-angular'
|
106 | ].filter(function (s) { return s; })
|
107 | };
|
108 | }
|
109 | else if (options.template === 'react-apollo' ||
|
110 | options.template === 'graphql-codegen-typescript-react-apollo-template') {
|
111 | return {
|
112 | config: templateSpecificConfig,
|
113 | plugins: [
|
114 | templateSpecificConfig.printTime ? 'time' : null,
|
115 | 'typescript-common',
|
116 | 'typescript-client',
|
117 | 'typescript-react-apollo'
|
118 | ].filter(function (s) { return s; })
|
119 | };
|
120 | }
|
121 | else if (options.template === 'introspection' || options.template === 'graphql-codegen-introspection-template') {
|
122 | return {
|
123 | config: templateSpecificConfig,
|
124 | plugins: ['introspection'].filter(function (s) { return s; })
|
125 | };
|
126 | }
|
127 | else if (options.template === 'graphql-files-typescript' ||
|
128 | options.template === 'graphql-codegen-graphql-files-typescript-modules') {
|
129 | return {
|
130 | config: templateSpecificConfig,
|
131 | plugins: ['typescript-graphql-files-modules'].filter(function (s) { return s; })
|
132 | };
|
133 | }
|
134 | return { plugins: [] };
|
135 | }
|
136 | function createConfigFromOldCli(options) {
|
137 | var _a;
|
138 | exports.validateCliOptions(options);
|
139 | var rootConfig = {};
|
140 | var configPath = options.config ? options.config : fs_1.existsSync(path_1.join(process.cwd(), './gql-gen.json'));
|
141 | if (configPath && typeof configPath === 'string') {
|
142 | var rawObj = JSON.parse(fs_1.readFileSync(configPath, 'utf-8'));
|
143 | rootConfig = (rawObj || {}).generatorConfig || {};
|
144 | }
|
145 | var configObject = {
|
146 | schema: [options.schema, options.clientSchema].filter(function (s) { return s; }),
|
147 | documents: options.args || [],
|
148 | config: rootConfig,
|
149 | generates: (_a = {},
|
150 | _a[options.out] = transformTemplatesToPlugins(options, __assign({}, rootConfig, (options.templateConfig || {}))),
|
151 | _a),
|
152 | silent: options.silent,
|
153 | watch: options.watch,
|
154 | require: options.require
|
155 | };
|
156 | console['warn']("\n Note: You are using the old API of graphql-code-generator. You can easily migrate by creating \"codegen.yml\" file in your project with the following content:\n \n" + YAML.stringify(configObject) + "\n\n Then, make sure that your script is executing just \"gql-gen\" (without any cli flags).\n ");
|
157 | return configObject;
|
158 | }
|
159 | exports.createConfigFromOldCli = createConfigFromOldCli;
|
160 |
|
\ | No newline at end of file |