Skip to content

Commit 5d70d2c

Browse files
authored
fix: suppress [object Object] defaults (probably Org flags) (#651)
* fix: suppress [object Object] defaults (probably Org flags) * test: add plugin-user and a UT for asserting default org behavior * refactor: skip target-org regardless of return type * refactor: skip target org only for defaults * refactor: also, no defaults for devhub
1 parent abf00e3 commit 5d70d2c

File tree

7 files changed

+79
-77
lines changed

7 files changed

+79
-77
lines changed

.sfdevrc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
},
55
"wireit": {
66
"test:command-reference": {
7-
"command": "node --loader ts-node/esm --no-warnings=ExperimentalWarning \"./bin/dev.js\" commandreference generate --plugins auth --outputdir test/tmp",
7+
"command": "node --loader ts-node/esm --no-warnings=ExperimentalWarning \"./bin/dev.js\" commandreference generate --plugins auth --plugins user --outputdir test/tmp",
88
"files": ["src/**/*.ts", "messages/**", "package.json"],
99
"output": ["test/tmp"]
1010
},

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"@oclif/plugin-help": "^6.0.22",
2020
"@salesforce/dev-scripts": "^9.1.2",
2121
"@salesforce/plugin-auth": "^3.6.16",
22+
"@salesforce/plugin-user": "^3.5.11",
2223
"@types/debug": "^4.1.12",
2324
"eslint-plugin-sf-plugin": "^1.18.4",
2425
"oclif": "^4.11.2",
@@ -57,7 +58,8 @@
5758
},
5859
"devPlugins": [
5960
"@oclif/plugin-help",
60-
"@salesforce/plugin-auth"
61+
"@salesforce/plugin-auth",
62+
"@salesforce/plugin-user"
6163
]
6264
},
6365
"repository": "salesforcecli/plugin-command-reference",
@@ -157,7 +159,7 @@
157159
]
158160
},
159161
"test:command-reference": {
160-
"command": "node --loader ts-node/esm --no-warnings=ExperimentalWarning \"./bin/dev.js\" commandreference generate --plugins auth --outputdir test/tmp",
162+
"command": "node --loader ts-node/esm --no-warnings=ExperimentalWarning \"./bin/dev.js\" commandreference generate --plugins auth --plugins user --outputdir test/tmp",
161163
"files": [
162164
"src/**/*.ts",
163165
"messages/**",

src/ditamap/command.ts

Lines changed: 53 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77

88
import { join } from 'node:path';
9-
import { asString, Dictionary, ensureObject, ensureString } from '@salesforce/ts-types';
9+
import { asString, Dictionary, ensureObject, ensureString, Optional } from '@salesforce/ts-types';
1010
import { CommandClass, CommandData, CommandParameterData, punctuate, replaceConfigVariables } from '../utils.js';
1111
import { Ditamap } from './ditamap.js';
1212

@@ -21,14 +21,18 @@ type FlagInfo = {
2121
default: string | (() => Promise<string>);
2222
};
2323

24-
const getDefault = async (flag?: FlagInfo): Promise<string> => {
24+
const getDefault = async (flag: FlagInfo, flagName: string): Promise<string> => {
2525
if (!flag) {
2626
return '';
2727
}
28+
if (flagName === 'target-org' || flagName === 'target-dev-hub') {
29+
// special handling to prevent global/local default usernames from appearing in the docs, but they do appear in user's help
30+
return '';
31+
}
2832
if (typeof flag.default === 'function') {
2933
try {
3034
const help = await flag.default();
31-
return help || '';
35+
return help.includes('[object Object]') ? '' : help ?? '';
3236
} catch {
3337
return '';
3438
}
@@ -50,23 +54,23 @@ export class Command extends Ditamap {
5054
) {
5155
const commandWithUnderscores = ensureString(command.id).replace(/:/g, '_');
5256
const filename = Ditamap.file(`cli_reference_${commandWithUnderscores}`, 'xml');
53-
5457
super(filename, undefined);
5558

5659
this.flags = ensureObject(command.flags);
5760
this.commandMeta = commandMeta;
61+
const binary = readBinary(this.commandMeta);
5862

5963
const summary = punctuate(command.summary);
6064
this.commandName = command.id.replace(/:/g, asString(this.commandMeta.topicSeparator, ':'));
6165

6266
const description = command.description
63-
? replaceConfigVariables(command.description, asString(this.commandMeta.binary, 'unknown'), this.commandName)
67+
? replaceConfigVariables(command.description, binary, this.commandName)
6468
: undefined;
6569

6670
// Help are all the lines after the first line in the description. Before oclif, there was a 'help' property so continue to
6771
// support that.
6872

69-
const help = this.formatParagraphs(description);
73+
const help = formatParagraphs(description);
7074

7175
let trailblazerCommunityUrl: string | undefined;
7276
let trailblazerCommunityName: string | undefined;
@@ -90,10 +94,8 @@ export class Command extends Ditamap {
9094
}
9195

9296
return {
93-
description: replaceConfigVariables(desc ?? '', asString(this.commandMeta.binary, 'unknown'), this.commandName),
94-
commands: commands.map((cmd) =>
95-
replaceConfigVariables(cmd, asString(this.commandMeta.binary, 'unknown'), this.commandName)
96-
),
97+
description: replaceConfigVariables(desc ?? '', binary, this.commandName),
98+
commands: commands.map((cmd) => replaceConfigVariables(cmd, binary, this.commandName)),
9799
};
98100
});
99101

@@ -102,7 +104,7 @@ export class Command extends Ditamap {
102104
name: this.commandName,
103105
summary,
104106
description,
105-
binary: 'binary' in commandMeta && typeof commandMeta.binary === 'string' ? commandMeta.binary : 'unknown',
107+
binary,
106108
commandWithUnderscores,
107109
deprecated: (command.deprecated as boolean) ?? state === 'deprecated' ?? false,
108110
examples,
@@ -120,34 +122,24 @@ export class Command extends Ditamap {
120122
}
121123

122124
public async getParametersForTemplate(flags: Dictionary<FlagInfo>): Promise<CommandParameterData[]> {
123-
const final: CommandParameterData[] = [];
124-
125-
for (const [flagName, flag] of Object.entries(flags)) {
126-
if (!flag || flag.hidden) continue;
127-
const description = replaceConfigVariables(
128-
Array.isArray(flag?.description) ? flag?.description.join('\n') : flag?.description ?? '',
129-
asString(this.commandMeta.binary, 'unknown'),
130-
this.commandName
131-
);
132-
const entireDescription = flag.summary
133-
? `${replaceConfigVariables(
134-
flag.summary,
135-
asString(this.commandMeta.binary, 'unknown'),
136-
this.commandName
137-
)}\n${description}`
138-
: description;
139-
const updated = Object.assign({}, flag, {
140-
name: flagName,
141-
description: this.formatParagraphs(entireDescription),
142-
optional: !flag?.required,
143-
kind: flag?.kind ?? flag?.type,
144-
hasValue: flag?.type !== 'boolean',
145-
// eslint-disable-next-line no-await-in-loop
146-
defaultFlagValue: await getDefault(flag),
147-
});
148-
final.push(updated);
149-
}
150-
return final;
125+
const descriptionBuilder = buildDescription(this.commandName)(readBinary(this.commandMeta));
126+
return Promise.all(
127+
[...Object.entries(flags)]
128+
.filter(flagIsDefined)
129+
.filter(([, flag]) => !flag.hidden)
130+
.map(
131+
async ([flagName, flag]) =>
132+
({
133+
...flag,
134+
name: flagName,
135+
description: descriptionBuilder(flag),
136+
optional: !flag.required,
137+
kind: flag.kind ?? flag.type,
138+
hasValue: flag.type !== 'boolean',
139+
defaultFlagValue: await getDefault(flag, flagName),
140+
} satisfies CommandParameterData)
141+
)
142+
);
151143
}
152144

153145
// eslint-disable-next-line class-methods-use-this
@@ -161,3 +153,25 @@ export class Command extends Ditamap {
161153
return super.transformToDitamap();
162154
}
163155
}
156+
157+
const flagIsDefined = (input: [string, Optional<FlagInfo>]): input is [string, FlagInfo] => input[1] !== undefined;
158+
159+
const buildDescription =
160+
(commandName: string) =>
161+
(binary: string) =>
162+
(flag: FlagInfo): string[] => {
163+
const description = replaceConfigVariables(
164+
Array.isArray(flag?.description) ? flag?.description.join('\n') : flag?.description ?? '',
165+
binary,
166+
commandName
167+
);
168+
return formatParagraphs(
169+
flag.summary ? `${replaceConfigVariables(flag.summary, binary, commandName)}\n${description}` : description
170+
);
171+
};
172+
173+
const formatParagraphs = (textToFormat?: string): string[] =>
174+
textToFormat ? textToFormat.split('\n').filter((n) => n !== '') : [];
175+
176+
const readBinary = (commandMeta: Record<string, unknown>): string =>
177+
'binary' in commandMeta && typeof commandMeta.binary === 'string' ? commandMeta.binary : 'unknown';

src/ditamap/ditamap.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,6 @@ export abstract class Ditamap {
9292
await fs.writeFile(this.destination, output);
9393
}
9494

95-
// eslint-disable-next-line class-methods-use-this
96-
protected formatParagraphs(textToFormat?: string): string[] {
97-
return textToFormat ? textToFormat.split('\n').filter((n) => n !== '') : [];
98-
}
99-
10095
/**
10196
* Applies the named handlebars template to the supplied data
10297
*

src/utils.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ type ClIRefTopicData = {
6969
};
7070

7171
export type CommandParameterData = {
72+
description: string[];
7273
optional?: boolean;
7374
char?: string;
7475
name: string;

test/unit/endtoend.test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ function loadTestDitamapFile(path: string) {
2525
return readFileSync(join(testFilesPath, path), 'utf8');
2626
}
2727

28-
describe('plugin-auth', () => {
28+
describe('plugin-auth and user', () => {
2929
before(async () => {
3030
try {
3131
await access(testFilesPath);
@@ -38,6 +38,10 @@ describe('plugin-auth', () => {
3838
after(async () => {
3939
await rm(testFilesPath, { recursive: true });
4040
});
41+
it('produces no [object Object] nonsense for default flags', () => {
42+
const dita = loadTestDitamapFile(join('org', 'cli_reference_org_create_user_unified.xml'));
43+
expect(dita.includes('[object Object]')).to.be.false;
44+
});
4145
it('creates with spaced commands', async () => {
4246
const dita = loadTestDitamapFile(join('org', 'cli_reference_org_login_jwt_unified.xml'));
4347
expect(dita.includes('<title><codeph otherprops="nolang">org login jwt')).to.be.true;

yarn.lock

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1878,6 +1878,17 @@
18781878
proxy-agent "^6.4.0"
18791879
semver "^7.6.0"
18801880

1881+
"@salesforce/plugin-user@^3.5.11":
1882+
version "3.5.11"
1883+
resolved "https://registry.yarnpkg.com/@salesforce/plugin-user/-/plugin-user-3.5.11.tgz#839796a7f17562ad9ece2c69d4dfdb5bc033f4d9"
1884+
integrity sha512-GA81RgZ5KMBuK2MEsYPCgRJP6U371Zvr0qxZUmXpDiuC8rjp9EqPl/1s4BfFVzB8emQWmFXxejSqCZz5FJh1dg==
1885+
dependencies:
1886+
"@oclif/core" "^3.26.6"
1887+
"@salesforce/core" "^7.3.8"
1888+
"@salesforce/kit" "^3.1.0"
1889+
"@salesforce/sf-plugins-core" "^9.0.12"
1890+
"@salesforce/ts-types" "^2.0.9"
1891+
18811892
"@salesforce/prettier-config@^0.0.3":
18821893
version "0.0.3"
18831894
resolved "https://registry.yarnpkg.com/@salesforce/prettier-config/-/prettier-config-0.0.3.tgz#ba648d4886bb38adabe073dbea0b3a91b3753bb0"
@@ -1888,7 +1899,7 @@
18881899
resolved "https://registry.yarnpkg.com/@salesforce/schemas/-/schemas-1.9.0.tgz#ba477a112653a20b4edcf989c61c57bdff9aa3ca"
18891900
integrity sha512-LiN37zG5ODT6z70sL1fxF7BQwtCX9JOWofSU8iliSNIM+WDEeinnoFtVqPInRSNt8I0RiJxIKCrqstsmQRBNvA==
18901901

1891-
"@salesforce/sf-plugins-core@^9.0.10", "@salesforce/sf-plugins-core@^9.0.13", "@salesforce/sf-plugins-core@^9.0.7":
1902+
"@salesforce/sf-plugins-core@^9.0.10", "@salesforce/sf-plugins-core@^9.0.12", "@salesforce/sf-plugins-core@^9.0.13", "@salesforce/sf-plugins-core@^9.0.7":
18921903
version "9.0.14"
18931904
resolved "https://registry.yarnpkg.com/@salesforce/sf-plugins-core/-/sf-plugins-core-9.0.14.tgz#12a31b81692579ce2ed478bf886a53255ca62c35"
18941905
integrity sha512-sxiLFtjfViOOSh6tSZKiKGQUIYZYkrz0S33Qg/s9EtQM0hBCstDUbI+1QqmafnolHQRq2tTYGoj//lko6AzHtA==
@@ -7377,16 +7388,7 @@ srcset@^5.0.0:
73777388
resolved "https://registry.yarnpkg.com/srcset/-/srcset-5.0.0.tgz#9df6c3961b5b44a02532ce6ae4544832609e2e3f"
73787389
integrity sha512-SqEZaAEhe0A6ETEa9O1IhSPC7MdvehZtCnTR0AftXk3QhY2UNgb+NApFOUPZILXk/YTDfFxMTNJOBpzrJsEdIA==
73797390

7380-
"string-width-cjs@npm:string-width@^4.2.0":
7381-
version "4.2.3"
7382-
resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010"
7383-
integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==
7384-
dependencies:
7385-
emoji-regex "^8.0.0"
7386-
is-fullwidth-code-point "^3.0.0"
7387-
strip-ansi "^6.0.1"
7388-
7389-
string-width@^4.0.0, string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.3:
7391+
"string-width-cjs@npm:string-width@^4.2.0", string-width@^4.0.0, string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.3:
73907392
version "4.2.3"
73917393
resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010"
73927394
integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==
@@ -7445,14 +7447,7 @@ string_decoder@~1.1.1:
74457447
dependencies:
74467448
safe-buffer "~5.1.0"
74477449

7448-
"strip-ansi-cjs@npm:strip-ansi@^6.0.1":
7449-
version "6.0.1"
7450-
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9"
7451-
integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==
7452-
dependencies:
7453-
ansi-regex "^5.0.1"
7454-
7455-
strip-ansi@^6.0.0, strip-ansi@^6.0.1:
7450+
"strip-ansi-cjs@npm:strip-ansi@^6.0.1", strip-ansi@^6.0.0, strip-ansi@^6.0.1:
74567451
version "6.0.1"
74577452
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9"
74587453
integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==
@@ -7977,7 +7972,7 @@ [email protected]:
79777972
resolved "https://registry.yarnpkg.com/workerpool/-/workerpool-6.2.1.tgz#46fc150c17d826b86a008e5a4508656777e9c343"
79787973
integrity sha512-ILEIE97kDZvF9Wb9f6h5aXK4swSlKGUcOEGiIYb2OOu/IrDU9iwj0fD//SsA6E5ibwJxpEvhullJY4Sl4GcpAw==
79797974

7980-
"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0":
7975+
"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0", wrap-ansi@^7.0.0:
79817976
version "7.0.0"
79827977
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43"
79837978
integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==
@@ -7995,15 +7990,6 @@ wrap-ansi@^6.2.0:
79957990
string-width "^4.1.0"
79967991
strip-ansi "^6.0.0"
79977992

7998-
wrap-ansi@^7.0.0:
7999-
version "7.0.0"
8000-
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43"
8001-
integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==
8002-
dependencies:
8003-
ansi-styles "^4.0.0"
8004-
string-width "^4.1.0"
8005-
strip-ansi "^6.0.0"
8006-
80077993
wrap-ansi@^8.1.0:
80087994
version "8.1.0"
80097995
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-8.1.0.tgz#56dc22368ee570face1b49819975d9b9a5ead214"

0 commit comments

Comments
 (0)