UNPKG

9.44 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(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 apollo_language_server_1.Debug.SetLoggers({
37 info: this.log,
38 warning: this.warn,
39 error: console.error
40 });
41 const config = await this.createConfig(flags);
42 if (!config)
43 return;
44 this.createService(config, flags);
45 this.ctx.config = config;
46 this.tasks.push({
47 title: "Loading Apollo Project",
48 task: async (ctx) => {
49 await this.project.whenReady;
50 ctx = Object.assign(Object.assign({}, ctx), this.ctx);
51 }
52 });
53 }
54 async createConfig(flags) {
55 const service = flags.key ? apollo_language_server_1.getServiceFromKey(flags.key) : undefined;
56 const config = await apollo_language_server_1.loadConfig({
57 configPath: flags.config && path_1.parse(path_1.resolve(flags.config)).dir,
58 configFileName: flags.config,
59 name: service,
60 type: this.type
61 });
62 if (!config) {
63 this.error("A config failed to load, so the command couldn't be run");
64 this.exit(1);
65 return;
66 }
67 config.tag = flags.tag || config.tag || "current";
68 config.setDefaults({
69 engine: {
70 apiKey: flags.key,
71 endpoint: flags.engine,
72 frontend: flags.frontend
73 }
74 });
75 if (flags.endpoint) {
76 config.setDefaults({
77 service: {
78 endpoint: Object.assign({ url: flags.endpoint, headers: headersArrayToObject(flags.header) }, (flags.skipSSLValidation && { skipSSLValidation: true }))
79 }
80 });
81 }
82 if (flags.localSchemaFile) {
83 const files = flags.localSchemaFile.split(",");
84 if (apollo_language_server_1.isClientConfig(config)) {
85 config.setDefaults({
86 client: {
87 service: {
88 localSchemaFile: files
89 }
90 }
91 });
92 }
93 else if (apollo_language_server_1.isServiceConfig(config)) {
94 config.setDefaults({
95 service: {
96 localSchemaFile: files
97 }
98 });
99 }
100 }
101 if (this.configMap) {
102 const defaults = this.configMap(flags);
103 config.setDefaults(defaults);
104 }
105 return config;
106 }
107 createService(config, flags) {
108 const loadingHandler = new OclifLoadingHandler_1.OclifLoadingHandler(this);
109 const configPath = config.configURI.fsPath;
110 const rootURI = configPath === process.cwd()
111 ? vscode_uri_1.default.file(configPath)
112 : vscode_uri_1.default.file(path_1.parse(configPath).dir);
113 const clientIdentity = {
114 name: "Apollo CLI",
115 version,
116 referenceID
117 };
118 if (apollo_language_server_1.isServiceConfig(config)) {
119 this.project = new apollo_language_server_1.GraphQLServiceProject({
120 config,
121 loadingHandler,
122 rootURI,
123 clientIdentity
124 });
125 }
126 else if (apollo_language_server_1.isClientConfig(config)) {
127 this.project = new apollo_language_server_1.GraphQLClientProject({
128 config,
129 loadingHandler,
130 rootURI,
131 clientIdentity
132 });
133 }
134 else {
135 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");
136 }
137 this.ctx.project = this.project;
138 }
139 async runTasks(generateTasks, options) {
140 const { ctx } = this;
141 if (!ctx) {
142 throw new Error("init must be called before trying to access this.ctx");
143 }
144 const tasks = await generateTasks(ctx);
145 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();
146 }
147 async catch(err) {
148 this.error(err);
149 }
150 async finally(err) {
151 }
152}
153exports.ProjectCommand = ProjectCommand;
154ProjectCommand.flags = {
155 config: command_1.flags.string({
156 char: "c",
157 description: "Path to your Apollo config file"
158 }),
159 header: command_1.flags.string({
160 multiple: true,
161 parse: header => {
162 const separatorIndex = header.indexOf(":");
163 const key = header.substring(0, separatorIndex).trim();
164 const value = header.substring(separatorIndex + 1).trim();
165 return JSON.stringify({ [key]: value });
166 },
167 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."
168 }),
169 endpoint: command_1.flags.string({
170 description: "The url of your service"
171 }),
172 key: command_1.flags.string({
173 description: "The API key for the Apollo Engine service",
174 default: () => process.env.ENGINE_API_KEY
175 }),
176 engine: command_1.flags.string({
177 description: "Reporting URL for a custom Apollo Engine deployment",
178 hidden: true
179 }),
180 frontend: command_1.flags.string({
181 description: "URL for a custom Apollo Engine frontend",
182 hidden: true
183 })
184};
185class ClientCommand extends ProjectCommand {
186 constructor(argv, config) {
187 super(argv, config);
188 this.type = "client";
189 this.configMap = (flags) => {
190 const config = {
191 client: {
192 name: flags.clientName,
193 referenceID: flags.clientReferenceId,
194 version: flags.clientVersion
195 }
196 };
197 if (flags.endpoint) {
198 config.client.service = {
199 url: flags.endpoint,
200 headers: headersArrayToObject(flags.header)
201 };
202 }
203 if (flags.includes || flags.queries) {
204 config.client.includes = [flags.includes || flags.queries];
205 }
206 if (flags.excludes) {
207 config.client.excludes = [flags.excludes];
208 }
209 if (flags.tagName) {
210 config.client.tagName = flags.tagName;
211 }
212 return config;
213 };
214 }
215}
216exports.ClientCommand = ClientCommand;
217ClientCommand.flags = Object.assign(Object.assign({}, ProjectCommand.flags), { clientReferenceId: command_1.flags.string({
218 description: "Reference id for the client which will match ids from client traces, will use clientName if not provided"
219 }), clientName: command_1.flags.string({
220 description: "Name of the client that the queries will be attached to"
221 }), clientVersion: command_1.flags.string({
222 description: "The version of the client that the queries will be attached to"
223 }), tag: command_1.flags.string({
224 char: "t",
225 description: "The published service tag for this client"
226 }), queries: command_1.flags.string({
227 description: "Deprecated in favor of the includes flag"
228 }), includes: command_1.flags.string({
229 description: "Glob of files to search for GraphQL operations. This should be used to find queries *and* any client schema extensions"
230 }), excludes: command_1.flags.string({
231 description: "Glob of files to exclude for GraphQL operations. Caveat: this doesn't currently work in watch mode"
232 }), tagName: command_1.flags.string({
233 description: "Name of the template literal tag used to identify template literals containing GraphQL queries in Javascript/Typescript code"
234 }) });
235//# sourceMappingURL=Command.js.map
\No newline at end of file