Skip to content

Commit 36d7506

Browse files
committed
feat: copy oclif/core ux methods
1 parent 8876a4f commit 36d7506

File tree

10 files changed

+1038
-46
lines changed

10 files changed

+1038
-46
lines changed

package.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,19 @@
4949
"@salesforce/core": "^7.2.0",
5050
"@salesforce/kit": "^3.1.0",
5151
"@salesforce/ts-types": "^2.0.9",
52-
"chalk": "^5.3.0"
52+
"cardinal": "^2.1.1",
53+
"chalk": "^5.3.0",
54+
"js-yaml": "^4.1.0",
55+
"natural-orderby": "^3.0.2",
56+
"slice-ansi": "^7.1.0",
57+
"string-width": "^7.1.0",
58+
"terminal-link": "^3.0.0"
5359
},
5460
"devDependencies": {
5561
"@inquirer/type": "^1.2.1",
5662
"@salesforce/dev-scripts": "^8.5.0",
63+
"@types/cardinal": "^2.1.1",
64+
"@types/js-yaml": "^4.0.9",
5765
"eslint-plugin-sf-plugin": "^1.18.0",
5866
"ts-node": "^10.9.2",
5967
"typescript": "^5.4.5"

src/ux/styledJSON.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright (c) 2023, salesforce.com, inc.
3+
* All rights reserved.
4+
* Licensed under the BSD 3-Clause license.
5+
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
6+
*/
7+
import { createRequire } from 'node:module';
8+
import { highlight, CardinalTheme } from 'cardinal';
9+
import chalk from 'chalk';
10+
11+
// @oclif/core v4 will have native support for coloring JSON so we won't need this then.
12+
export default function styledJSON(obj: unknown): string {
13+
const json = JSON.stringify(obj, null, 2);
14+
if (!chalk.level) {
15+
return json;
16+
}
17+
18+
const require = createRequire(import.meta.url);
19+
// eslint-disable-next-line @typescript-eslint/no-var-requires
20+
const theme = require('cardinal/themes/jq') as CardinalTheme;
21+
return highlight(json, { theme });
22+
}

src/ux/styledObject.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright (c) 2023, salesforce.com, inc.
3+
* All rights reserved.
4+
* Licensed under the BSD 3-Clause license.
5+
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
6+
*/
7+
import { inspect } from 'node:util';
8+
import chalk from 'chalk';
9+
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;
14+
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+
);
22+
}
23+
24+
return inspect(obj);
25+
}
26+
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 {
29+
const output: string[] = [];
30+
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
31+
const keyLengths = Object.keys(obj).map((key) => key.toString().length);
32+
const maxKeyLength = Math.max(...keyLengths) + 2;
33+
34+
const logKeyValue = (key: string, value: any): string =>
35+
`${chalk.blue(key)}:` + ' '.repeat(maxKeyLength - key.length - 1) + pp(value);
36+
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];
41+
if (Array.isArray(value)) {
42+
if (value.length > 0) {
43+
output.push(logKeyValue(key, value[0]));
44+
for (const e of value.slice(1)) {
45+
output.push(' '.repeat(maxKeyLength) + pp(e));
46+
}
47+
}
48+
} else if (value !== null && value !== undefined) {
49+
output.push(logKeyValue(key, value));
50+
}
51+
}
52+
53+
return output.join('\n');
54+
}

0 commit comments

Comments
 (0)