Skip to content

Commit f96f30d

Browse files
committed
Fix JSON mode bugs: guard interactive prompts, route warnings to stderr, enforce --force
- Respect --json with --token-only in auth token commands - Auto-remove invalid/revoked keys in JSON mode instead of hanging on prompts - Enforce --force flag for push publish commands in JSON mode - Route formatLimitWarning output to stderr (19 commands) - Migrate this.warn() to this.logWarning() for structured JSON warnings - Fix premature logListening emission before subscription is ready - Fix shouldOutputJson({}) dead code in bench commands
1 parent 1ed697d commit f96f30d

37 files changed

Lines changed: 133 additions & 80 deletions

File tree

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2970,7 +2970,7 @@ ARGUMENTS
29702970
key. Items with "channels" are routed via channel batch publish with the payload wrapped in extras.push
29712971
29722972
FLAGS
2973-
-f, --force Skip confirmation prompt when publishing to channels (confirmation is also skipped in --json mode)
2973+
-f, --force Skip confirmation prompt (required with --json)
29742974
-v, --verbose Output verbose logs
29752975
--json Output in JSON format
29762976
--pretty-json Output in colorized JSON format
@@ -3585,8 +3585,7 @@ USAGE
35853585
[--web <value>] [-f]
35863586
35873587
FLAGS
3588-
-f, --force Skip confirmation prompt when publishing to a channel (confirmation is also skipped in
3589-
--json mode)
3588+
-f, --force Skip confirmation prompt (required with --json)
35903589
-v, --verbose Output verbose logs
35913590
--apns=<value> APNs-specific override as JSON
35923591
--badge=<value> Notification badge count

src/base-command.ts

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,10 +1008,9 @@ export abstract class AblyBaseCommand extends InteractiveBaseCommand {
10081008
// When using token auth, we don't set the clientId as it may conflict
10091009
// with any clientId embedded in the token
10101010
if (flags["client-id"] && !this.shouldSuppressOutput(flags)) {
1011-
this.logToStderr(
1012-
chalk.yellow(
1013-
"Warning: clientId is ignored when using token authentication as the clientId is embedded in the token",
1014-
),
1011+
this.logWarning(
1012+
"clientId is ignored when using token authentication as the clientId is embedded in the token.",
1013+
flags,
10151014
);
10161015
}
10171016
} else if (flags["api-key"]) {
@@ -1337,15 +1336,22 @@ export abstract class AblyBaseCommand extends InteractiveBaseCommand {
13371336
const appId = flags.app || this.configManager.getCurrentAppId();
13381337

13391338
if (appId) {
1340-
this.log("The configured API key appears to be invalid or revoked.");
1339+
if (this.shouldOutputJson(flags)) {
1340+
// In JSON mode, auto-remove the invalid key — it can't be used anyway
1341+
this.configManager.removeApiKey(appId);
1342+
} else {
1343+
this.logToStderr(
1344+
"The configured API key appears to be invalid or revoked.",
1345+
);
13411346

1342-
const shouldRemove = await this.interactiveHelper.confirm(
1343-
"Would you like to remove this invalid key from your configuration?",
1344-
);
1347+
const shouldRemove = await this.interactiveHelper.confirm(
1348+
"Would you like to remove this invalid key from your configuration?",
1349+
);
13451350

1346-
if (shouldRemove) {
1347-
this.configManager.removeApiKey(appId);
1348-
this.log("Invalid key removed from configuration.");
1351+
if (shouldRemove) {
1352+
this.configManager.removeApiKey(appId);
1353+
this.logToStderr("Invalid key removed from configuration.");
1354+
}
13491355
}
13501356
}
13511357
}

src/commands/accounts/current.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,9 @@ export default class AccountsCurrent extends ControlBaseCommand {
132132
flags,
133133
);
134134
} else {
135-
this.warn(
135+
this.logWarning(
136136
"Unable to verify account information. Your access token may have expired.",
137+
flags,
137138
);
138139
this.log(
139140
chalk.yellow(
@@ -232,7 +233,7 @@ export default class AccountsCurrent extends ControlBaseCommand {
232233
flags,
233234
);
234235
} else {
235-
this.warn(errorMessage(error));
236+
this.logWarning(errorMessage(error), flags);
236237
this.log(
237238
`${formatLabel("Info")} Your access token may have expired or is invalid.`,
238239
);

src/commands/accounts/switch.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ export default class AccountsSwitch extends ControlBaseCommand {
167167
flags,
168168
);
169169
} else {
170-
this.warn(warningMessage);
170+
this.logWarning(warningMessage, flags);
171171
}
172172
}
173173
}

src/commands/apps/current.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,9 @@ export default class AppsCurrent extends ControlBaseCommand {
205205
`${formatLabel("App")} ${chalk.green.bold("Unknown")} ${chalk.gray(`(${appId})`)}`,
206206
);
207207
this.log(`${formatLabel("API Key")} ${chalk.yellow.bold(keyId)}`);
208-
this.warn(
208+
this.logWarning(
209209
`Could not fetch additional app details: ${errorMessage(error)}`,
210+
flags,
210211
);
211212
this.log(
212213
`${formatLabel("Mode")} ${chalk.magenta.bold("Web CLI")} ${chalk.dim("(using environment variables)")}`,

src/commands/apps/list.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ export default class AppsList extends ControlBaseCommand {
9191

9292
if (hasMore) {
9393
const warning = formatLimitWarning(apps.length, flags.limit, "apps");
94-
if (warning) this.log(warning);
94+
if (warning) this.logToStderr(warning);
9595
}
9696
},
9797
"Error listing apps",

src/commands/apps/rules/list.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ export default class RulesListCommand extends ControlBaseCommand {
118118
flags.limit,
119119
"rules",
120120
);
121-
if (warning) this.log(warning);
121+
if (warning) this.logToStderr(warning);
122122
}
123123
}
124124
} catch (error) {

src/commands/auth/issue-ably-token.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,11 @@ export default class IssueAblyTokenCommand extends AblyBaseCommand {
112112

113113
// If token-only flag is set, output just the token string
114114
if (flags["token-only"]) {
115-
this.log(tokenDetails.token);
115+
if (this.shouldOutputJson(flags)) {
116+
this.logJsonResult({ token: { value: tokenDetails.token } }, flags);
117+
} else {
118+
this.log(tokenDetails.token);
119+
}
116120
return;
117121
}
118122

src/commands/auth/issue-jwt-token.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,11 @@ export default class IssueJwtTokenCommand extends AblyBaseCommand {
128128

129129
// If token-only flag is set, output just the token string
130130
if (flags["token-only"]) {
131-
this.log(token);
131+
if (this.shouldOutputJson(flags)) {
132+
this.logJsonResult({ token: { value: token } }, flags);
133+
} else {
134+
this.log(token);
135+
}
132136
return;
133137
}
134138

src/commands/auth/keys/list.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ export default class KeysListCommand extends ControlBaseCommand {
114114

115115
if (hasMore) {
116116
const warning = formatLimitWarning(keys.length, flags.limit, "keys");
117-
if (warning) this.log(warning);
117+
if (warning) this.logToStderr(warning);
118118
}
119119
}
120120
} catch (error) {

0 commit comments

Comments
 (0)