1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const command_1 = require("@oclif/command");
|
4 | const Listr = require("listr");
|
5 | const path_1 = require("path");
|
6 | const apollo_language_server_1 = require("apollo-language-server");
|
7 | const OclifLoadingHandler_1 = require("./OclifLoadingHandler");
|
8 | const vscode_uri_1 = require("vscode-uri");
|
9 | const { version, referenceID } = require("../package.json");
|
10 | const headersArrayToObject = (arr) => {
|
11 | if (!arr)
|
12 | return;
|
13 | return arr
|
14 | .map(val => JSON.parse(val))
|
15 | .reduce((pre, next) => (Object.assign({}, pre, next)), {});
|
16 | };
|
17 | const getServiceFromKey = (key) => {
|
18 | const [type, service] = key.split(":");
|
19 | if (type === "service")
|
20 | return service;
|
21 | return;
|
22 | };
|
23 | class ProjectCommand extends command_1.default {
|
24 | constructor() {
|
25 | super(...arguments);
|
26 | this.tasks = [];
|
27 | this.type = "service";
|
28 | }
|
29 | async init() {
|
30 | const { flags, args } = this.parse(this.constructor);
|
31 | this.ctx = { flags, args };
|
32 | const config = await this.createConfig(flags);
|
33 | this.createService(config, flags);
|
34 | (this.ctx.config = config),
|
35 | this.tasks.push({
|
36 | title: "Loading Apollo Project",
|
37 | task: async (ctx) => {
|
38 | await this.project.whenReady;
|
39 | ctx = Object.assign({}, ctx, this.ctx);
|
40 | }
|
41 | });
|
42 | }
|
43 | async createConfig(flags) {
|
44 | const service = flags.key ? getServiceFromKey(flags.key) : undefined;
|
45 | const config = await apollo_language_server_1.loadConfig({
|
46 | configPath: flags.config && path_1.parse(path_1.resolve(flags.config)).dir,
|
47 | name: service,
|
48 | type: this.type
|
49 | });
|
50 | if (flags.tag)
|
51 | config.tag = flags.tag;
|
52 | config.setDefaults({
|
53 | engine: {
|
54 | apiKey: flags.key,
|
55 | endpoint: flags.engine,
|
56 | frontend: flags.frontend
|
57 | }
|
58 | });
|
59 | if (flags.endpoint) {
|
60 | config.setDefaults({
|
61 | service: {
|
62 | endpoint: Object.assign({ url: flags.endpoint, headers: headersArrayToObject(flags.header) }, (flags.skipSSLValidation && { skipSSLValidation: true }))
|
63 | }
|
64 | });
|
65 | }
|
66 | if (flags.localSchemaFile) {
|
67 | if (apollo_language_server_1.isClientConfig(config)) {
|
68 | config.setDefaults({
|
69 | client: {
|
70 | service: {
|
71 | localSchemaFile: flags.localSchemaFile
|
72 | }
|
73 | }
|
74 | });
|
75 | }
|
76 | else if (apollo_language_server_1.isServiceConfig(config)) {
|
77 | config.setDefaults({
|
78 | service: {
|
79 | localSchemaFile: flags.localSchemaFile
|
80 | }
|
81 | });
|
82 | }
|
83 | }
|
84 | if (this.configMap) {
|
85 | const defaults = this.configMap(flags);
|
86 | config.setDefaults(defaults);
|
87 | }
|
88 | return config;
|
89 | }
|
90 | createService(config, flags) {
|
91 | const loadingHandler = new OclifLoadingHandler_1.OclifLoadingHandler(this);
|
92 | const configPath = config.configURI.fsPath;
|
93 | const rootURI = configPath === process.cwd()
|
94 | ? vscode_uri_1.default.file(configPath)
|
95 | : vscode_uri_1.default.file(path_1.parse(configPath).dir);
|
96 | const clientIdentity = {
|
97 | name: "Apollo CLI",
|
98 | version,
|
99 | referenceID
|
100 | };
|
101 | if (apollo_language_server_1.isServiceConfig(config)) {
|
102 | this.project = new apollo_language_server_1.GraphQLServiceProject({
|
103 | config,
|
104 | loadingHandler,
|
105 | rootURI,
|
106 | clientIdentity
|
107 | });
|
108 | }
|
109 | else if (apollo_language_server_1.isClientConfig(config)) {
|
110 | this.project = new apollo_language_server_1.GraphQLClientProject({
|
111 | config,
|
112 | loadingHandler,
|
113 | rootURI,
|
114 | clientIdentity
|
115 | });
|
116 | }
|
117 | this.ctx.project = this.project;
|
118 | }
|
119 | async runTasks(generateTasks) {
|
120 | const tasks = await generateTasks(this.ctx);
|
121 | return new Listr([...this.tasks, ...tasks]).run();
|
122 | }
|
123 | async catch(err) {
|
124 | this.error(err);
|
125 | }
|
126 | async finally(err) {
|
127 | }
|
128 | }
|
129 | ProjectCommand.flags = {
|
130 | config: command_1.flags.string({
|
131 | char: "c",
|
132 | description: "Path to your Apollo config file"
|
133 | }),
|
134 | header: command_1.flags.string({
|
135 | multiple: true,
|
136 | parse: header => {
|
137 | const separatorIndex = header.indexOf(":");
|
138 | const key = header.substring(0, separatorIndex).trim();
|
139 | const value = header.substring(separatorIndex + 1).trim();
|
140 | return JSON.stringify({ [key]: value });
|
141 | },
|
142 | description: "Additional headers to send to server for introspectionQuery"
|
143 | }),
|
144 | endpoint: command_1.flags.string({
|
145 | description: "The url of your service"
|
146 | }),
|
147 | key: command_1.flags.string({
|
148 | description: "The API key for the Apollo Engine service",
|
149 | default: () => process.env.ENGINE_API_KEY
|
150 | }),
|
151 | engine: command_1.flags.string({
|
152 | description: "Reporting URL for a custom Apollo Engine deployment",
|
153 | hidden: true
|
154 | }),
|
155 | frontend: command_1.flags.string({
|
156 | description: "URL for a custom Apollo Engine frontend",
|
157 | hidden: true
|
158 | })
|
159 | };
|
160 | exports.ProjectCommand = ProjectCommand;
|
161 | class ClientCommand extends ProjectCommand {
|
162 | constructor(argv, config) {
|
163 | super(argv, config);
|
164 | this.type = "client";
|
165 | this.configMap = (flags) => {
|
166 | const config = {
|
167 | client: {
|
168 | name: flags.clientName,
|
169 | referenceID: flags.clientReferenceId,
|
170 | version: flags.clientVersion
|
171 | }
|
172 | };
|
173 | if (flags.endpoint) {
|
174 | config.client.service = {
|
175 | url: flags.endpoint,
|
176 | headers: headersArrayToObject(flags.header)
|
177 | };
|
178 | }
|
179 | if (flags.includes || flags.queries) {
|
180 | config.client.includes = [flags.includes || flags.queries];
|
181 | }
|
182 | if (flags.excludes) {
|
183 | config.client.excludes = [flags.excludes];
|
184 | }
|
185 | if (flags.tagName) {
|
186 | config.client.tagName = flags.tagName;
|
187 | }
|
188 | return config;
|
189 | };
|
190 | }
|
191 | }
|
192 | ClientCommand.flags = Object.assign({}, ProjectCommand.flags, { clientReferenceId: command_1.flags.string({
|
193 | description: "Reference id for the client which will match ids from client traces, will use clientName if not provided"
|
194 | }), clientName: command_1.flags.string({
|
195 | description: "Name of the client that the queries will be attached to"
|
196 | }), clientVersion: command_1.flags.string({
|
197 | description: "The version of the client that the queries will be attached to"
|
198 | }), tag: command_1.flags.string({
|
199 | char: "t",
|
200 | description: "The published service tag for this client",
|
201 | default: "current"
|
202 | }), queries: command_1.flags.string({
|
203 | description: "Deprecated in favor of the includes flag"
|
204 | }), includes: command_1.flags.string({
|
205 | description: "Glob of files to search for GraphQL operations. This should be used to find queries *and* any client schema extensions"
|
206 | }), excludes: command_1.flags.string({
|
207 | description: "Glob of files to exclude for GraphQL operations. Caveat: this doesn't currently work in watch mode"
|
208 | }), tagName: command_1.flags.string({
|
209 | description: "Name of the template literal tag used to identify template literals containing GraphQL queries in Javascript/Typescript code"
|
210 | }) });
|
211 | exports.ClientCommand = ClientCommand;
|
212 |
|
\ | No newline at end of file |