1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | Object.defineProperty(exports, "__esModule", { value: true });
|
9 | exports.ToolbeltCommand = void 0;
|
10 | const command_1 = require("@salesforce/command");
|
11 | const ts_types_1 = require("@salesforce/ts-types");
|
12 | const core_1 = require("@salesforce/core");
|
13 | const kit_1 = require("@salesforce/kit");
|
14 | const indexErrorProcessor_1 = require("./lib/indexErrorProcessor");
|
15 | const perfMetricsRequest_1 = require("./lib/perfMetricsRequest");
|
16 | const logger = require("./lib/core/logApi");
|
17 | const Org = require("./lib/core/scratchOrgApi");
|
18 | const srcDevUtil = require("./lib/core/srcDevUtil");
|
19 |
|
20 | const Messages = require("./lib/messages");
|
21 | const path = require("path");
|
22 | const messages = Messages();
|
23 | class ToolbeltCommand extends command_1.SfdxCommand {
|
24 | |
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 | static get description() {
|
33 | let description = this.theDescription;
|
34 | if (this.deprecated) {
|
35 | description = `(deprecated) ${description}\n\nWARNING: ${logger.formatDeprecationWarning(this.id, this.deprecated, 'command')}`;
|
36 | }
|
37 | if (this.extraHelp) {
|
38 | description += `\n\n${this.extraHelp}`;
|
39 | }
|
40 | if (this.help) {
|
41 | description += `\n\n${this.help}`;
|
42 | }
|
43 | return description;
|
44 | }
|
45 | static get extraHelp() {
|
46 | let extraHelp = '';
|
47 |
|
48 |
|
49 |
|
50 | if (this.requiresProject) {
|
51 | extraHelp += 'NOTE: This command must be run from within a project.';
|
52 | }
|
53 | return extraHelp;
|
54 | }
|
55 | static get flags() {
|
56 | const cmdFlags = super.flags;
|
57 | if (this.supportsPerfLogLevelFlag) {
|
58 |
|
59 | cmdFlags['perflog'] = command_1.flags.boolean({
|
60 | description: messages.getMessage('perfLogLevelOption'),
|
61 | longDescription: messages.getMessage('perfLogLevelOptionLong')
|
62 | });
|
63 | }
|
64 | if (this.schema) {
|
65 | cmdFlags['confighelp'] = command_1.flags.boolean({
|
66 | description: messages.getMessage('schemaInfoOption', this.schema.flag),
|
67 | longDescription: messages.getMessage('schemaInfoOptionLong')
|
68 | });
|
69 | }
|
70 | Object.keys(cmdFlags).forEach(flagName => {
|
71 | const flag = cmdFlags[flagName];
|
72 | if (flag.deprecated && !flag.description.startsWith('(deprecated)')) {
|
73 | flag.description = `(deprecated) ${flag.description}`;
|
74 | }
|
75 | });
|
76 | return cmdFlags;
|
77 | }
|
78 | |
79 |
|
80 |
|
81 |
|
82 | stringifyFlags() {
|
83 | Object.keys(this.flags).forEach(name => {
|
84 | const flag = this.flags[name];
|
85 | if (flag == null)
|
86 | return;
|
87 | switch (typeof this.flags[name]) {
|
88 | case 'string':
|
89 | case 'number':
|
90 | this.flags[name] = flag + '';
|
91 | break;
|
92 | case 'boolean':
|
93 | break;
|
94 | case 'object':
|
95 | if (Array.isArray(flag)) {
|
96 | this.flags[name] = flag.join(',');
|
97 | break;
|
98 | }
|
99 | else if (flag instanceof Date) {
|
100 | this.flags[name] = flag.toISOString();
|
101 | break;
|
102 |
|
103 |
|
104 |
|
105 | }
|
106 | else if (flag.constructor.name === 'Duration') {
|
107 | this.flags[name] = flag.quantity + '';
|
108 | break;
|
109 | }
|
110 | else if (ts_types_1.isFunction(flag.toString)) {
|
111 | this.flags[name] = flag.toString();
|
112 | break;
|
113 | }
|
114 |
|
115 | default:
|
116 | throw new core_1.SfdxError(`Unexpected value type for flag ${name}`, 'UnexpectedFlagValueType');
|
117 | }
|
118 | });
|
119 | }
|
120 | |
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 | async execLegacyCommand(command, context, stdin) {
|
136 |
|
137 |
|
138 | process.exitCode = undefined;
|
139 | if (this.statics.supportsPerfLogLevelFlag && context.flags.perflog === true) {
|
140 | context.org.force.setCallOptions('perfOption', 'MINIMUM');
|
141 | }
|
142 | const logger = require('./lib/core/logApi');
|
143 | logger.setHumanConsumable(!context.flags.json && !context.flags.quiet);
|
144 | context.logger = logger;
|
145 | if (ts_types_1.isFunction(command.validate)) {
|
146 | context = (await command.validate(context)) || context;
|
147 | }
|
148 | try {
|
149 | const results = await command.execute(context, stdin);
|
150 | this.legacyOutput(command, results);
|
151 | return results;
|
152 | }
|
153 | catch (error) {
|
154 | if (ts_types_1.isFunction(command.getHumanErrorMessage)) {
|
155 | const humanMessage = command.getHumanErrorMessage(error);
|
156 | if (humanMessage) {
|
157 | kit_1.set(error, 'message', humanMessage);
|
158 | }
|
159 | }
|
160 | |
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 | const errorResultForTable = error.result || error.data;
|
168 | |
169 |
|
170 |
|
171 |
|
172 |
|
173 | if (error.columns && ts_types_1.isObject(errorResultForTable)) {
|
174 | this.ux.table(errorResultForTable, { columns: error.columns });
|
175 | }
|
176 |
|
177 |
|
178 |
|
179 | throw error;
|
180 | }
|
181 | }
|
182 | |
183 |
|
184 |
|
185 |
|
186 | checkIfWildcardError(param) {
|
187 | if (param) {
|
188 | return (this.argv.length > 2 &&
|
189 | (this.id.includes('source:deploy') || this.id.includes('source:retrieve')) &&
|
190 | param.indexOf('-') != 0 &&
|
191 | param.indexOf('"') <= param.indexOf(', ') &&
|
192 | param.indexOf(path.sep) >= 0);
|
193 | }
|
194 | else {
|
195 | return false;
|
196 | }
|
197 | }
|
198 | |
199 |
|
200 |
|
201 |
|
202 | async catch(err) {
|
203 |
|
204 | if (ts_types_1.getString(err, 'code') === 'EEXIT') {
|
205 | throw err;
|
206 | }
|
207 | let project;
|
208 | let appConfig = {};
|
209 | try {
|
210 | project = await core_1.SfdxProject.resolve();
|
211 | appConfig = await project.resolveProjectConfig();
|
212 | }
|
213 | catch (noopError) { }
|
214 |
|
215 |
|
216 | if (err.name === 'NamedOrgNotFound') {
|
217 | kit_1.set(err, 'name', 'NoOrgFound');
|
218 | try {
|
219 | const username = err.message.match(/No AuthInfo found for name (.*)/)[1];
|
220 | kit_1.set(err, 'message', messages.getMessage('namedOrgNotFound', username));
|
221 | }
|
222 | catch (err) { }
|
223 |
|
224 |
|
225 | if (this.argv.includes('--json')) {
|
226 | this.ux.warn('The error message "NoOrgFound" has been deprecated and will be removed in v46 or later. It will become "NamedOrgNotFound".');
|
227 | }
|
228 | if (this.statics.requiresUsername) {
|
229 | return super.catch(err);
|
230 | }
|
231 | }
|
232 | try {
|
233 | let context = {};
|
234 | try {
|
235 | context = await this.resolveLegacyContext();
|
236 | }
|
237 | catch (e) { }
|
238 |
|
239 | const processors = indexErrorProcessor_1.getProcessors(appConfig, context, err);
|
240 | for (const processor of processors) {
|
241 | await processor;
|
242 | }
|
243 | }
|
244 | catch (newError) {
|
245 | err = newError;
|
246 | }
|
247 | if (!(err instanceof core_1.SfdxError)) {
|
248 | const sfdxErr = core_1.SfdxError.wrap(err);
|
249 | if (ts_types_1.has(err, 'action')) {
|
250 | if (!sfdxErr.actions) {
|
251 | sfdxErr.actions = [];
|
252 | }
|
253 | if (ts_types_1.isArray(err.action)) {
|
254 | for (const action of err.action) {
|
255 | if (ts_types_1.isString(action)) {
|
256 | sfdxErr.actions.push(action);
|
257 | }
|
258 | }
|
259 | }
|
260 | else if (ts_types_1.isString(err.action)) {
|
261 | sfdxErr.actions.push(err.action);
|
262 | }
|
263 | }
|
264 | const index = this.argv.indexOf('-p') > 0 ? this.argv.indexOf('-p') : 0;
|
265 | const param = this.argv[index + 2];
|
266 | if (this.checkIfWildcardError(param)) {
|
267 |
|
268 | sfdxErr.message = messages.getMessage('WildCardError');
|
269 | }
|
270 | if (ts_types_1.has(err, 'result')) {
|
271 | sfdxErr.data = err.result;
|
272 | }
|
273 | return super.catch(sfdxErr);
|
274 | }
|
275 | return super.catch(err);
|
276 | }
|
277 | getJsonResultObject(result, status) {
|
278 | return ToolbeltCommand.logPerfMetrics(super.getJsonResultObject(result, status));
|
279 | }
|
280 | async resolveLegacyContext() {
|
281 | if (this.legacyContext) {
|
282 | return this.legacyContext;
|
283 | }
|
284 | const logger = require('./lib/core/logApi');
|
285 | logger.setHumanConsumable(!this.flags.json && !this.flags.quiet);
|
286 | this.stringifyFlags();
|
287 | const legacyArgs = [];
|
288 | Object.keys(this.args || {}).forEach(argKey => {
|
289 | const val = this.args[argKey] || '';
|
290 | legacyArgs.push(`${argKey}=${val}`);
|
291 | });
|
292 |
|
293 | const strict = this.statics.strict;
|
294 | if (!strict) {
|
295 | const { args, argv } = this.parse({
|
296 | flags: this.statics.flags,
|
297 | args: this.statics.args,
|
298 | strict
|
299 | });
|
300 | const argVals = Object.values(args);
|
301 | const varargs = argv.filter(val => !argVals.includes(val));
|
302 | varargs.forEach(argKey => {
|
303 | legacyArgs.push(argKey);
|
304 | });
|
305 | }
|
306 | Object.keys(this.varargs || {}).forEach(argKey => {
|
307 | const val = this.varargs[argKey] || '';
|
308 | legacyArgs.push(`${argKey}=${val}`);
|
309 | });
|
310 | const context = {
|
311 | flags: this.flags,
|
312 | args: legacyArgs,
|
313 | varargs: this.varargs,
|
314 | ux: this.ux
|
315 | };
|
316 | if (this.org || this.hubOrg) {
|
317 | const org = this.org || this.hubOrg;
|
318 | const username = org.getUsername();
|
319 | if (this.flags.apiversion) {
|
320 |
|
321 |
|
322 |
|
323 | process.env.SFDX_API_VERSION = this.flags.apiversion;
|
324 | }
|
325 | context.org = await Org.create(username);
|
326 | }
|
327 | this.legacyContext = context;
|
328 | kit_1.set(this.legacyContext, 'command.flags', Object.values(this.ctor.flags));
|
329 | return context;
|
330 | }
|
331 | legacyOutput(command, obj) {
|
332 |
|
333 | if (Array.isArray(obj) && obj.length < 1) {
|
334 | this.ux.log(messages.getMessage('noResultsFound'));
|
335 | return;
|
336 | }
|
337 |
|
338 | if (ts_types_1.isFunction(command.getColumnData)) {
|
339 | const columnData = command.getColumnData();
|
340 |
|
341 | if (ts_types_1.isPlainObject(columnData)) {
|
342 |
|
343 | for (const key of Object.keys(columnData)) {
|
344 | const val = columnData[key];
|
345 | const rows = ts_types_1.get(obj, key);
|
346 | if (kit_1.isEmpty(rows)) {
|
347 |
|
348 | let message = messages.getMessage('noResultsFound');
|
349 | if (command.getEmptyResultMessage) {
|
350 | const _message = command.getEmptyResultMessage(key);
|
351 | if (_message != null) {
|
352 | message = _message;
|
353 | }
|
354 | }
|
355 | this.ux.log(message);
|
356 | }
|
357 | else {
|
358 |
|
359 | this.ux.table(ts_types_1.getArray(obj, key), { columns: val });
|
360 |
|
361 | this.ux.log();
|
362 | }
|
363 | }
|
364 | }
|
365 | else {
|
366 |
|
367 | this.ux.table(obj, { columns: columnData });
|
368 | }
|
369 | }
|
370 | else {
|
371 | const message = command.getHumanSuccessMessage && command.getHumanSuccessMessage(obj);
|
372 | if (message != null && message !== '') {
|
373 | this.ux.log(message);
|
374 | }
|
375 | }
|
376 | }
|
377 |
|
378 | get statics() {
|
379 | return this.constructor;
|
380 | }
|
381 | static logPerfMetrics(obj) {
|
382 | if (perfMetricsRequest_1.requestPerfMetrics.length > 0) {
|
383 | obj.perfMetrics = perfMetricsRequest_1.requestPerfMetrics;
|
384 | srcDevUtil.saveGlobalConfig('apiPerformanceLog.json', perfMetricsRequest_1.requestPerfMetrics);
|
385 | }
|
386 | return obj;
|
387 | }
|
388 | }
|
389 | exports.ToolbeltCommand = ToolbeltCommand;
|
390 | ToolbeltCommand.supportsPerfLogLevelFlag = false;
|
391 |
|
392 |
|