1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | Object.defineProperty(exports, "__esModule", { value: true });
|
9 | const command_1 = require("@oclif/command");
|
10 | const core_1 = require("@salesforce/core");
|
11 | const kit_1 = require("@salesforce/kit");
|
12 | const ts_types_1 = require("@salesforce/ts-types");
|
13 | const ts_types_2 = require("@salesforce/ts-types");
|
14 | const chalk_1 = require("chalk");
|
15 | const docOpts_1 = require("./docOpts");
|
16 | const sfdxFlags_1 = require("./sfdxFlags");
|
17 | const ux_1 = require("./ux");
|
18 | core_1.Messages.importMessagesDirectory(__dirname);
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 | class Result {
|
26 | constructor(config = {}) {
|
27 | this.tableColumnData = config.tableColumnData;
|
28 | if (config.display) {
|
29 | this.display = config.display.bind(this);
|
30 | }
|
31 | }
|
32 | display() {
|
33 | if (this.tableColumnData) {
|
34 | if (Array.isArray(this.data) && this.data.length) {
|
35 | this.ux.table(this.data, this.tableColumnData);
|
36 | }
|
37 | else {
|
38 | this.ux.log('No results found.');
|
39 | }
|
40 | }
|
41 | }
|
42 | }
|
43 | exports.Result = Result;
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 | class SfdxCommand extends command_1.default {
|
53 | constructor() {
|
54 | super(...arguments);
|
55 | this.isJson = false;
|
56 | }
|
57 |
|
58 |
|
59 |
|
60 |
|
61 | static get flags() {
|
62 | return sfdxFlags_1.buildSfdxFlags(this.flagsConfig, {
|
63 | targetdevhubusername: !!(this.supportsDevhubUsername || this.requiresDevhubUsername),
|
64 | targetusername: !!(this.supportsUsername || this.requiresUsername)
|
65 | });
|
66 | }
|
67 | static get usage() {
|
68 | return docOpts_1.DocOpts.generate(this);
|
69 | }
|
70 | static getVarArgsConfig() {
|
71 | if (ts_types_1.isBoolean(this.varargs)) {
|
72 | return this.varargs ? {} : undefined;
|
73 | }
|
74 |
|
75 | return Object.assign({}, this.varargs);
|
76 | }
|
77 |
|
78 | get statics() {
|
79 | return this.constructor;
|
80 | }
|
81 | async _run() {
|
82 |
|
83 |
|
84 | if (!this.statics.result.tableColumnData && this.statics.tableColumnData) {
|
85 | this.statics.result.tableColumnData = this.statics.tableColumnData;
|
86 | }
|
87 | this.result = new Result(this.statics.result);
|
88 | let err;
|
89 | try {
|
90 | await this.init();
|
91 | return (this.result.data = await this.run());
|
92 | }
|
93 | catch (e) {
|
94 | err = e;
|
95 | await this.catch(e);
|
96 | }
|
97 | finally {
|
98 | await this.finally(err);
|
99 | }
|
100 | }
|
101 |
|
102 | async assignProject() {
|
103 |
|
104 |
|
105 | try {
|
106 | this.project = await core_1.SfdxProject.resolve();
|
107 | }
|
108 | catch (err) {
|
109 | if (err.name === 'InvalidProjectWorkspace') {
|
110 | throw core_1.SfdxError.create('@salesforce/command', 'command', 'RequiresProjectError');
|
111 | }
|
112 | throw err;
|
113 | }
|
114 | }
|
115 |
|
116 | async assignOrg() {
|
117 |
|
118 | try {
|
119 | this.org = await core_1.Org.create({
|
120 | aliasOrUsername: this.flags.targetusername,
|
121 | aggregator: this.configAggregator
|
122 | });
|
123 | if (this.flags.apiversion) {
|
124 | this.org.getConnection().setApiVersion(this.flags.apiversion);
|
125 | }
|
126 | }
|
127 | catch (err) {
|
128 | if (this.statics.requiresUsername) {
|
129 | if (err.name === 'NoUsername') {
|
130 | throw core_1.SfdxError.create('@salesforce/command', 'command', 'RequiresUsernameError');
|
131 | }
|
132 | throw err;
|
133 | }
|
134 | }
|
135 | }
|
136 |
|
137 | async assignHubOrg() {
|
138 |
|
139 | try {
|
140 | this.hubOrg = await core_1.Org.create({
|
141 | aliasOrUsername: this.flags.targetdevhubusername,
|
142 | aggregator: this.configAggregator,
|
143 | isDevHub: true
|
144 | });
|
145 | if (this.flags.apiversion) {
|
146 | this.hubOrg.getConnection().setApiVersion(this.flags.apiversion);
|
147 | }
|
148 | }
|
149 | catch (err) {
|
150 |
|
151 |
|
152 | if (this.statics.requiresDevhubUsername) {
|
153 | if (err.name === 'AuthInfoCreationError' || err.name === 'NoUsername') {
|
154 | throw core_1.SfdxError.create('@salesforce/command', 'command', 'RequiresDevhubUsernameError');
|
155 | }
|
156 | throw core_1.SfdxError.wrap(err);
|
157 | }
|
158 | }
|
159 | }
|
160 | shouldEmitHelp() {
|
161 |
|
162 |
|
163 | if (!this.argv.includes('-h')) {
|
164 |
|
165 | return false;
|
166 | }
|
167 |
|
168 | const flags = this.statics.flags || {};
|
169 | for (const k of Object.keys(flags)) {
|
170 | if (k !== 'help' && flags[k].char === 'h') {
|
171 |
|
172 | return false;
|
173 | }
|
174 | }
|
175 |
|
176 | return true;
|
177 | }
|
178 | async init() {
|
179 |
|
180 |
|
181 | process.exitCode = 0;
|
182 |
|
183 |
|
184 | const isContentTypeJSON = kit_1.env.getString('SFDX_CONTENT_TYPE', '').toUpperCase() === 'JSON';
|
185 | this.isJson = this.argv.includes('--json') || isContentTypeJSON;
|
186 |
|
187 |
|
188 | const loglevel = this.argv.join(' ').match(/--loglevel\s*=?\s*([a-z]+)/);
|
189 | if (loglevel) {
|
190 | (await core_1.Logger.root()).setLevel(core_1.Logger.getLevelByName(loglevel[1]));
|
191 | }
|
192 | await this.initLoggerAndUx();
|
193 |
|
194 | if (this.shouldEmitHelp()) {
|
195 | this._help();
|
196 | }
|
197 |
|
198 | await super.init();
|
199 |
|
200 | const messages = core_1.Messages.loadMessages('@salesforce/command', 'command');
|
201 |
|
202 | const strict = this.statics.varargs ? !this.statics.varargs : this.statics.strict;
|
203 |
|
204 | const { args, flags, argv } = this.parse({
|
205 | flags: this.statics.flags,
|
206 | args: this.statics.args,
|
207 | strict
|
208 | });
|
209 | this.flags = flags;
|
210 | this.args = args;
|
211 |
|
212 | if (isContentTypeJSON) {
|
213 | this.flags.json = true;
|
214 | }
|
215 | this.warnIfDeprecated();
|
216 |
|
217 | if (this.statics.varargs) {
|
218 | const argVals = Object.values(args);
|
219 | const varargs = argv.filter(val => !argVals.includes(val));
|
220 | this.varargs = this.parseVarargs(varargs);
|
221 | }
|
222 | this.logger.info(`Running command [${this.statics.name}] with flags [${JSON.stringify(flags)}] and args [${JSON.stringify(args)}]`);
|
223 |
|
224 |
|
225 |
|
226 | this.configAggregator = await core_1.ConfigAggregator.create();
|
227 |
|
228 | if (this.statics.requiresProject) {
|
229 | await this.assignProject();
|
230 | }
|
231 |
|
232 |
|
233 | const apiVersion = this.configAggregator.getInfo('apiVersion');
|
234 | if (apiVersion && apiVersion.value && !flags.apiversion) {
|
235 | this.ux.warn(messages.getMessage('apiVersionOverrideWarning', [JSON.stringify(apiVersion.value)]));
|
236 | }
|
237 |
|
238 | if (this.statics.supportsUsername || this.statics.requiresUsername) {
|
239 | await this.assignOrg();
|
240 | }
|
241 |
|
242 | if (this.statics.supportsDevhubUsername || this.statics.requiresDevhubUsername) {
|
243 | await this.assignHubOrg();
|
244 | }
|
245 | }
|
246 |
|
247 |
|
248 | async catch(err) {
|
249 |
|
250 | if (err.code === 'EEXIT') {
|
251 | throw err;
|
252 | }
|
253 | await this.initLoggerAndUx();
|
254 |
|
255 | const error = err.setCommandName ? err : core_1.SfdxError.wrap(err);
|
256 | error.setCommandName(this.statics.name);
|
257 | process.exitCode = process.exitCode || error.exitCode || 1;
|
258 | const userDisplayError = Object.assign(this.getJsonResultObject(error.data, error.exitCode), Object.assign({}, error.toObject(), { stack: error.stack, warnings: Array.from(ux_1.UX.warnings) }));
|
259 | if (this.isJson) {
|
260 |
|
261 | const sendToStdout = kit_1.env.getBoolean('SFDX_JSON_TO_STDOUT');
|
262 | if (sendToStdout) {
|
263 | this.ux.logJson(userDisplayError);
|
264 | }
|
265 | else {
|
266 | this.ux.errorJson(userDisplayError);
|
267 | }
|
268 | }
|
269 | else {
|
270 | this.ux.error(...this.formatError(error));
|
271 | if (err.data) {
|
272 | this.result.data = err.data;
|
273 | this.result.display();
|
274 | }
|
275 | }
|
276 |
|
277 |
|
278 |
|
279 | process.emit('cmdError', err, Object.assign({}, this.flags, this.varargs), this.org || this.hubOrg);
|
280 | }
|
281 | async finally(err) {
|
282 |
|
283 | if (!err) {
|
284 | if (this.isJson) {
|
285 | let output = this.getJsonResultObject();
|
286 | if (ux_1.UX.warnings.size > 0) {
|
287 | output = Object.assign(output, {
|
288 | warnings: Array.from(ux_1.UX.warnings)
|
289 | });
|
290 | }
|
291 | this.ux.logJson(output);
|
292 | }
|
293 | else {
|
294 | this.result.display();
|
295 | }
|
296 | }
|
297 | }
|
298 |
|
299 | warnIfDeprecated() {
|
300 | if (this.statics.deprecated) {
|
301 | let def;
|
302 | if (ts_types_2.has(this.statics.deprecated, 'version')) {
|
303 | def = Object.assign({ name: this.statics.name, type: 'command' }, this.statics.deprecated);
|
304 | }
|
305 | else {
|
306 | def = this.statics.deprecated;
|
307 | }
|
308 | this.ux.warn(ux_1.UX.formatDeprecationWarning(def));
|
309 | }
|
310 | if (this.statics.flagsConfig) {
|
311 |
|
312 | for (const flag of Object.keys(this.flags)) {
|
313 | const def = this.statics.flagsConfig[flag];
|
314 | if (def && def.deprecated) {
|
315 | this.ux.warn(ux_1.UX.formatDeprecationWarning(Object.assign({ name: flag, type: 'flag' }, def.deprecated)));
|
316 | }
|
317 | }
|
318 | }
|
319 | }
|
320 | getJsonResultObject(result = this.result.data, status = process.exitCode || 0) {
|
321 | return { status, result };
|
322 | }
|
323 | parseVarargs(args = []) {
|
324 | const varargs = {};
|
325 | const descriptor = this.statics.varargs;
|
326 |
|
327 | if (!args.length && !ts_types_1.isBoolean(descriptor) && descriptor.required) {
|
328 | throw core_1.SfdxError.create('@salesforce/command', 'command', 'VarargsRequired');
|
329 | }
|
330 |
|
331 | args.forEach(arg => {
|
332 | const split = arg.split('=');
|
333 | if (split.length !== 2) {
|
334 | throw core_1.SfdxError.create('@salesforce/command', 'command', 'InvalidVarargsFormat', [arg]);
|
335 | }
|
336 | const [name, value] = split;
|
337 | if (varargs[name]) {
|
338 | throw core_1.SfdxError.create('@salesforce/command', 'command', 'DuplicateVararg', [name]);
|
339 | }
|
340 | if (!ts_types_1.isBoolean(descriptor) && descriptor.validator) {
|
341 | descriptor.validator(name, value);
|
342 | }
|
343 | varargs[name] = value || undefined;
|
344 | });
|
345 | return varargs;
|
346 | }
|
347 | |
348 |
|
349 |
|
350 |
|
351 |
|
352 |
|
353 | formatError(error) {
|
354 | const colorizedArgs = [];
|
355 |
|
356 | const commandName = this.id || error.commandName;
|
357 | const runningWith = commandName ? ` running ${commandName}` : '';
|
358 | colorizedArgs.push(chalk_1.default.bold(`ERROR${runningWith}: `));
|
359 | colorizedArgs.push(chalk_1.default.red(error.message));
|
360 |
|
361 | if (ts_types_1.get(error, 'actions.length')) {
|
362 | colorizedArgs.push(`\n\n${chalk_1.default.blue(chalk_1.default.bold('Try this:'))}`);
|
363 | if (error.actions) {
|
364 | error.actions.forEach(action => {
|
365 | colorizedArgs.push(`\n${chalk_1.default.red(action)}`);
|
366 | });
|
367 | }
|
368 | }
|
369 | if (error.stack && core_1.Global.getEnvironmentMode() === core_1.Mode.DEVELOPMENT) {
|
370 | colorizedArgs.push(chalk_1.default.red(`\n*** Internal Diagnostic ***\n\n${error.stack}\n******\n`));
|
371 | }
|
372 | return colorizedArgs;
|
373 | }
|
374 | |
375 |
|
376 |
|
377 | async initLoggerAndUx() {
|
378 | if (!this.logger) {
|
379 | this.logger = await core_1.Logger.child(this.statics.name);
|
380 | }
|
381 | if (!this.ux) {
|
382 | this.ux = new ux_1.UX(this.logger, !this.isJson);
|
383 | }
|
384 | if (this.result && !this.result.ux) {
|
385 | this.result.ux = this.ux;
|
386 | }
|
387 | }
|
388 | }
|
389 |
|
390 | SfdxCommand.supportsUsername = false;
|
391 |
|
392 |
|
393 | SfdxCommand.requiresUsername = false;
|
394 |
|
395 | SfdxCommand.supportsDevhubUsername = false;
|
396 |
|
397 |
|
398 | SfdxCommand.requiresDevhubUsername = false;
|
399 |
|
400 | SfdxCommand.requiresProject = false;
|
401 |
|
402 |
|
403 | SfdxCommand.result = {};
|
404 |
|
405 | SfdxCommand.varargs = false;
|
406 | exports.SfdxCommand = SfdxCommand;
|
407 |
|
\ | No newline at end of file |