UNPKG

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