UNPKG

8.2 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const command_1 = require("@oclif/command");
4const Listr = require("listr");
5const path_1 = require("path");
6const apollo_language_server_1 = require("apollo-language-server");
7const OclifLoadingHandler_1 = require("./OclifLoadingHandler");
8const vscode_uri_1 = require("vscode-uri");
9const { version, referenceID } = require("../package.json");
10const 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};
17const getServiceFromKey = (key) => {
18 const [type, service] = key.split(":");
19 if (type === "service")
20 return service;
21 return;
22};
23class 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 configFileName: flags.config,
48 name: service,
49 type: this.type
50 });
51 config.tag = flags.tag || config.tag || "current";
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 else {
118 throw new Error("Unable to resolve project type. Please add either a client or service config. For more information, please refer to https://bit.ly/2ByILPj");
119 }
120 this.ctx.project = this.project;
121 }
122 async runTasks(generateTasks) {
123 const tasks = await generateTasks(this.ctx);
124 return new Listr([...this.tasks, ...tasks]).run();
125 }
126 async catch(err) {
127 this.error(err);
128 }
129 async finally(err) {
130 }
131}
132ProjectCommand.flags = {
133 config: command_1.flags.string({
134 char: "c",
135 description: "Path to your Apollo config file"
136 }),
137 header: command_1.flags.string({
138 multiple: true,
139 parse: header => {
140 const separatorIndex = header.indexOf(":");
141 const key = header.substring(0, separatorIndex).trim();
142 const value = header.substring(separatorIndex + 1).trim();
143 return JSON.stringify({ [key]: value });
144 },
145 description: "Additional headers to send to server for introspectionQuery"
146 }),
147 endpoint: command_1.flags.string({
148 description: "The url of your service"
149 }),
150 key: command_1.flags.string({
151 description: "The API key for the Apollo Engine service",
152 default: () => process.env.ENGINE_API_KEY
153 }),
154 engine: command_1.flags.string({
155 description: "Reporting URL for a custom Apollo Engine deployment",
156 hidden: true
157 }),
158 frontend: command_1.flags.string({
159 description: "URL for a custom Apollo Engine frontend",
160 hidden: true
161 })
162};
163exports.ProjectCommand = ProjectCommand;
164class ClientCommand extends ProjectCommand {
165 constructor(argv, config) {
166 super(argv, config);
167 this.type = "client";
168 this.configMap = (flags) => {
169 const config = {
170 client: {
171 name: flags.clientName,
172 referenceID: flags.clientReferenceId,
173 version: flags.clientVersion
174 }
175 };
176 if (flags.endpoint) {
177 config.client.service = {
178 url: flags.endpoint,
179 headers: headersArrayToObject(flags.header)
180 };
181 }
182 if (flags.includes || flags.queries) {
183 config.client.includes = [flags.includes || flags.queries];
184 }
185 if (flags.excludes) {
186 config.client.excludes = [flags.excludes];
187 }
188 if (flags.tagName) {
189 config.client.tagName = flags.tagName;
190 }
191 return config;
192 };
193 }
194}
195ClientCommand.flags = Object.assign({}, ProjectCommand.flags, { clientReferenceId: command_1.flags.string({
196 description: "Reference id for the client which will match ids from client traces, will use clientName if not provided"
197 }), clientName: command_1.flags.string({
198 description: "Name of the client that the queries will be attached to"
199 }), clientVersion: command_1.flags.string({
200 description: "The version of the client that the queries will be attached to"
201 }), tag: command_1.flags.string({
202 char: "t",
203 description: "The published service tag for this client",
204 default: "current"
205 }), queries: command_1.flags.string({
206 description: "Deprecated in favor of the includes flag"
207 }), includes: command_1.flags.string({
208 description: "Glob of files to search for GraphQL operations. This should be used to find queries *and* any client schema extensions"
209 }), excludes: command_1.flags.string({
210 description: "Glob of files to exclude for GraphQL operations. Caveat: this doesn't currently work in watch mode"
211 }), tagName: command_1.flags.string({
212 description: "Name of the template literal tag used to identify template literals containing GraphQL queries in Javascript/Typescript code"
213 }) });
214exports.ClientCommand = ClientCommand;
215//# sourceMappingURL=Command.js.map
\No newline at end of file