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