UNPKG

11.7 kBJavaScriptView Raw
1"use strict";
2var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3 if (k2 === undefined) k2 = k;
4 Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5}) : (function(o, m, k, k2) {
6 if (k2 === undefined) k2 = k;
7 o[k2] = m[k];
8}));
9var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10 Object.defineProperty(o, "default", { enumerable: true, value: v });
11}) : function(o, v) {
12 o["default"] = v;
13});
14var __importStar = (this && this.__importStar) || function (mod) {
15 if (mod && mod.__esModule) return mod;
16 var result = {};
17 if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18 __setModuleDefault(result, mod);
19 return result;
20};
21var __importDefault = (this && this.__importDefault) || function (mod) {
22 return (mod && mod.__esModule) ? mod : { "default": mod };
23};
24Object.defineProperty(exports, "__esModule", { value: true });
25exports.ClientCommand = exports.ProjectCommand = void 0;
26const command_1 = __importStar(require("@oclif/command"));
27const listr_1 = __importDefault(require("listr"));
28const path_1 = require("path");
29const apollo_language_server_1 = require("apollo-language-server");
30const OclifLoadingHandler_1 = require("./OclifLoadingHandler");
31const vscode_uri_1 = __importDefault(require("vscode-uri"));
32const chalk_1 = __importDefault(require("chalk"));
33const global_agent_1 = require("global-agent");
34global_agent_1.bootstrap({ environmentVariableNamespace: "" });
35const { version, referenceID } = require("../package.json");
36const headersArrayToObject = (arr) => {
37 if (!arr)
38 return;
39 return arr
40 .map(val => JSON.parse(val))
41 .reduce((pre, next) => (Object.assign(Object.assign({}, pre), next)), {});
42};
43class ProjectCommand extends command_1.default {
44 constructor() {
45 super(...arguments);
46 this.tasks = [];
47 this.type = "service";
48 }
49 async init() {
50 const { flags, args } = this.parse(this
51 .constructor);
52 this.ctx = { flags, args };
53 apollo_language_server_1.Debug.SetLoggers({
54 info: this.log,
55 warning: this.warn,
56 error: console.error
57 });
58 const config = await this.createConfig(flags);
59 if (!config)
60 return;
61 this.createService(config, flags);
62 this.ctx.config = config;
63 this.tasks.push({
64 title: "Loading Apollo Project",
65 task: async (ctx) => {
66 await this.project.whenReady;
67 ctx = Object.assign(Object.assign({}, ctx), this.ctx);
68 }
69 });
70 }
71 async createConfig(flags) {
72 const service = flags.key ? apollo_language_server_1.getServiceFromKey(flags.key) : undefined;
73 const config = await apollo_language_server_1.loadConfig({
74 configPath: flags.config && path_1.parse(path_1.resolve(flags.config)).dir,
75 configFileName: flags.config,
76 name: service,
77 type: this.type
78 });
79 if (!config) {
80 this.error("A config failed to load, so the command couldn't be run");
81 this.exit(1);
82 return;
83 }
84 config.variant = flags.variant || flags.tag || config.variant;
85 config.graph = flags.graph || apollo_language_server_1.getGraphIdFromConfig(config.rawConfig);
86 if (flags.tag) {
87 console.warn(chalk_1.default.yellow("Using the --tag flag is deprecated. Please use --variant (or -v) instead."));
88 }
89 config.setDefaults({
90 engine: {
91 apiKey: flags.key,
92 endpoint: flags.engine
93 }
94 });
95 if (flags.endpoint) {
96 config.setDefaults({
97 service: {
98 endpoint: Object.assign({ url: flags.endpoint, headers: headersArrayToObject(flags.header) }, (flags.skipSSLValidation && { skipSSLValidation: true }))
99 }
100 });
101 }
102 if (flags.localSchemaFile) {
103 const files = flags.localSchemaFile.split(",");
104 if (apollo_language_server_1.isClientConfig(config)) {
105 config.setDefaults({
106 client: {
107 service: {
108 localSchemaFile: files
109 }
110 }
111 });
112 }
113 else if (apollo_language_server_1.isServiceConfig(config)) {
114 config.setDefaults({
115 service: {
116 localSchemaFile: files
117 }
118 });
119 }
120 }
121 if (this.configMap) {
122 const defaults = this.configMap(flags);
123 config.setDefaults(defaults);
124 }
125 const [tokenType, identifier] = (config.engine.apiKey && config.engine.apiKey.split(":")) || [];
126 if (tokenType == "service" && identifier !== config.graph) {
127 throw new Error(`Cannot specify a service token that does not match graph. Graph ${config.graph} does not match graph from token (${identifier})`);
128 }
129 return config;
130 }
131 createService(config, flags) {
132 const loadingHandler = new OclifLoadingHandler_1.OclifLoadingHandler(this);
133 const configPath = config.configURI.fsPath;
134 const rootURI = configPath === process.cwd()
135 ? vscode_uri_1.default.file(configPath)
136 : vscode_uri_1.default.file(path_1.parse(configPath).dir);
137 const clientIdentity = {
138 name: "Apollo CLI",
139 version,
140 referenceID
141 };
142 if (apollo_language_server_1.isServiceConfig(config)) {
143 this.project = new apollo_language_server_1.GraphQLServiceProject({
144 config,
145 loadingHandler,
146 rootURI,
147 clientIdentity
148 });
149 }
150 else if (apollo_language_server_1.isClientConfig(config)) {
151 this.project = new apollo_language_server_1.GraphQLClientProject({
152 config,
153 loadingHandler,
154 rootURI,
155 clientIdentity
156 });
157 }
158 else {
159 throw new Error("Unable to resolve project type. Please add either a client or service config. For more information, please refer to https://go.apollo.dev/t/config");
160 }
161 this.ctx.project = this.project;
162 }
163 async runTasks(generateTasks, options) {
164 const { ctx } = this;
165 if (!ctx) {
166 throw new Error("init must be called before trying to access this.ctx");
167 }
168 const tasks = await generateTasks(ctx);
169 return new listr_1.default([...this.tasks, ...tasks], Object.assign(Object.assign(Object.assign({}, (process.env.NODE_ENV === "test" && { renderer: "verbose" })), (options && typeof options === "function" ? options(ctx) : options)), { dateFormat: false })).run();
170 }
171 async catch(err) {
172 this.error(err);
173 }
174 async finally(err) {
175 }
176 printDeprecationWarning() {
177 console.error(ProjectCommand.DEPRECATION_MSG);
178 }
179}
180exports.ProjectCommand = ProjectCommand;
181ProjectCommand.flags = {
182 config: command_1.flags.string({
183 char: "c",
184 description: "Path to your Apollo config file"
185 }),
186 header: command_1.flags.string({
187 multiple: true,
188 parse: header => {
189 const separatorIndex = header.indexOf(":");
190 const key = header.substring(0, separatorIndex).trim();
191 const value = header.substring(separatorIndex + 1).trim();
192 return JSON.stringify({ [key]: value });
193 },
194 description: "Additional header to send during introspection. May be used multiple times to add multiple headers. NOTE: The `--endpoint` flag is REQUIRED if using the `--header` flag."
195 }),
196 endpoint: command_1.flags.string({
197 description: "The URL for the CLI use to introspect your service"
198 }),
199 key: command_1.flags.string({
200 description: "The API key to use for authentication to Apollo",
201 default: () => process.env.APOLLO_KEY || process.env.ENGINE_API_KEY
202 }),
203 engine: command_1.flags.string({
204 description: "URL for a custom Apollo deployment",
205 hidden: true
206 })
207};
208ProjectCommand.DEPRECATION_MSG = "\n-----------------------------------------------------------------\n" +
209 "DEPRECATED: This command will be removed from the `apollo` CLI in \n" +
210 "its next major version. Replacement functionality is available in \n" +
211 "the new Apollo Rover CLI: https://go.apollo.dev/t/migration\n" +
212 "-----------------------------------------------------------------\n";
213class ClientCommand extends ProjectCommand {
214 constructor(argv, config) {
215 super(argv, config);
216 this.type = "client";
217 this.configMap = (flags) => {
218 const config = {
219 client: {
220 name: flags.clientName,
221 referenceID: flags.clientReferenceId,
222 version: flags.clientVersion
223 }
224 };
225 if (flags.endpoint) {
226 config.client.service = {
227 url: flags.endpoint,
228 headers: headersArrayToObject(flags.header)
229 };
230 }
231 if (flags.includes || flags.queries) {
232 config.client.includes = [flags.includes || flags.queries];
233 }
234 if (flags.excludes) {
235 config.client.excludes = [flags.excludes];
236 }
237 if (flags.tagName) {
238 config.client.tagName = flags.tagName;
239 }
240 return config;
241 };
242 }
243}
244exports.ClientCommand = ClientCommand;
245ClientCommand.flags = Object.assign(Object.assign({}, ProjectCommand.flags), { clientReferenceId: command_1.flags.string({
246 description: "Reference id for the client which will match ids from client traces, will use clientName if not provided"
247 }), clientName: command_1.flags.string({
248 description: "Name of the client that the queries will be attached to"
249 }), clientVersion: command_1.flags.string({
250 description: "The version of the client that the queries will be attached to"
251 }), tag: command_1.flags.string({
252 char: "t",
253 description: "[Deprecated: please use --variant instead] The tag (AKA variant) of the graph in Apollo to associate this client to",
254 hidden: true,
255 exclusive: ["variant"]
256 }), variant: command_1.flags.string({
257 char: "v",
258 description: "The variant of the graph in Apollo to associate this client to",
259 exclusive: ["tag"]
260 }), graph: command_1.flags.string({
261 char: "g",
262 description: "The ID for the graph in Apollo to operate client commands with. Overrides config file if set."
263 }), queries: command_1.flags.string({
264 description: "Deprecated in favor of the includes flag"
265 }), includes: command_1.flags.string({
266 description: "Glob of files to search for GraphQL operations. This should be used to find queries *and* any client schema extensions"
267 }), excludes: command_1.flags.string({
268 description: "Glob of files to exclude for GraphQL operations. Caveat: this doesn't currently work in watch mode"
269 }), tagName: command_1.flags.string({
270 description: "Name of the template literal tag used to identify template literals containing GraphQL queries in Javascript/Typescript code"
271 }) });
272//# sourceMappingURL=Command.js.map
\No newline at end of file