|
6 | 6 | */
|
7 | 7 | import { inspect } from 'node:util';
|
8 | 8 | import chalk from 'chalk';
|
| 9 | +import { AnyJson } from '@salesforce/ts-types'; |
9 | 10 |
|
10 |
| -/* eslint-disable @typescript-eslint/no-explicit-any */ |
11 |
| - |
12 |
| -function pp(obj: any): any { |
13 |
| - if (typeof obj === 'string' || typeof obj === 'number') return obj; |
| 11 | +function prettyPrint(obj: AnyJson): string { |
| 12 | + if (!obj) return inspect(obj); |
| 13 | + if (typeof obj === 'string') return obj; |
| 14 | + if (typeof obj === 'number') return obj.toString(); |
| 15 | + if (typeof obj === 'boolean') return obj.toString(); |
14 | 16 | if (typeof obj === 'object') {
|
15 |
| - return ( |
16 |
| - // eslint-disable-next-line @typescript-eslint/no-unsafe-argument |
17 |
| - Object.keys(obj) |
18 |
| - // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access |
19 |
| - .map((k) => k + ': ' + inspect(obj[k])) |
20 |
| - .join(', ') |
21 |
| - ); |
| 17 | + return Object.entries(obj) |
| 18 | + .map(([key, value]) => `${key}: ${inspect(value)}`) |
| 19 | + .join(', '); |
22 | 20 | }
|
23 | 21 |
|
24 | 22 | return inspect(obj);
|
25 | 23 | }
|
26 | 24 |
|
27 |
| -// @oclif/core v4 will have native support for coloring JSON so we won't need this then. |
28 |
| -export default function styledObject(obj: any, keys?: string[]): string { |
| 25 | +export default function styledObject(obj: AnyJson, keys?: string[]): string { |
| 26 | + if (!obj) return inspect(obj); |
| 27 | + if (typeof obj === 'string') return obj; |
| 28 | + if (typeof obj === 'number') return obj.toString(); |
| 29 | + if (typeof obj === 'boolean') return obj.toString(); |
| 30 | + |
29 | 31 | const output: string[] = [];
|
30 |
| - // eslint-disable-next-line @typescript-eslint/no-unsafe-argument |
31 | 32 | const keyLengths = Object.keys(obj).map((key) => key.toString().length);
|
32 | 33 | const maxKeyLength = Math.max(...keyLengths) + 2;
|
33 | 34 |
|
34 |
| - const logKeyValue = (key: string, value: any): string => |
35 |
| - `${chalk.blue(key)}:` + ' '.repeat(maxKeyLength - key.length - 1) + pp(value); |
| 35 | + const logKeyValue = (key: string, value: AnyJson): string => |
| 36 | + `${chalk.blue(key)}:` + ' '.repeat(maxKeyLength - key.length - 1) + prettyPrint(value); |
36 | 37 |
|
37 |
| - // eslint-disable-next-line @typescript-eslint/no-unsafe-argument |
38 |
| - for (const key of keys ?? Object.keys(obj).sort()) { |
39 |
| - // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access |
40 |
| - const value = obj[key]; |
| 38 | + for (const [key, value] of Object.entries(obj)) { |
| 39 | + if (keys && !keys.includes(key)) continue; |
41 | 40 | if (Array.isArray(value)) {
|
42 | 41 | if (value.length > 0) {
|
43 | 42 | output.push(logKeyValue(key, value[0]));
|
44 | 43 | for (const e of value.slice(1)) {
|
45 |
| - output.push(' '.repeat(maxKeyLength) + pp(e)); |
| 44 | + output.push(' '.repeat(maxKeyLength) + prettyPrint(e)); |
46 | 45 | }
|
47 | 46 | }
|
48 | 47 | } else if (value !== null && value !== undefined) {
|
|
0 commit comments