|
1 | 1 | import { InteractiveBaseCommand } from "./interactive-base-command.js"; |
2 | | -import { Interfaces } from "@oclif/core"; |
3 | 2 | import * as Ably from "ably"; |
4 | 3 | import chalk from "chalk"; |
5 | 4 | import colorJson from "color-json"; |
@@ -128,63 +127,31 @@ export abstract class AblyBaseCommand extends InteractiveBaseCommand { |
128 | 127 | this.isWebCliMode = isWebCliMode(); |
129 | 128 | } |
130 | 129 |
|
131 | | - /** |
132 | | - * Mapping of common arg names to their human-readable labels. |
133 | | - * The parse() override validates these are non-empty automatically. |
134 | | - */ |
135 | | - private static readonly RESOURCE_ARG_LABELS: Record<string, string> = { |
136 | | - channel: "Channel", |
137 | | - channels: "Channel", // channels:subscribe uses plural arg name (strict: false, variadic via argv) |
138 | | - room: "Room", |
139 | | - rooms: "Room", // rooms:messages:subscribe uses plural arg name (strict: false, variadic via argv) |
140 | | - space_name: "Space", |
141 | | - }; |
142 | | - |
143 | | - /** |
144 | | - * Override parse() to automatically validate that common resource name |
145 | | - * args (channel, room, space_name) are non-empty after oclif parsing. |
146 | | - */ |
147 | | - protected override async parse< |
148 | | - F extends Record<string, unknown>, |
149 | | - B extends Record<string, unknown>, |
150 | | - A extends Record<string, unknown>, |
151 | | - >( |
152 | | - options?: Interfaces.Input<F, B, A>, |
| 130 | + protected validateChannelName( |
| 131 | + args: Record<string, unknown>, |
| 132 | + flags: BaseFlags, |
153 | 133 | argv?: string[], |
154 | | - ): Promise<Interfaces.ParserOutput<F, B, A>> { |
155 | | - const result = await super.parse(options, argv); |
156 | | - |
157 | | - const parsedArgs = result.args as Record<string, unknown>; |
158 | | - const flags = result.flags as BaseFlags; |
159 | | - for (const [argName, label] of Object.entries( |
160 | | - AblyBaseCommand.RESOURCE_ARG_LABELS, |
161 | | - )) { |
162 | | - if ( |
163 | | - argName in parsedArgs && |
164 | | - typeof parsedArgs[argName] === "string" && |
165 | | - !(parsedArgs[argName] as string).trim() |
166 | | - ) { |
167 | | - this.fail(`${label} name cannot be empty`, flags, "parse"); |
168 | | - } |
| 134 | + ): void { |
| 135 | + const name = (args.channel ?? args.channels ?? "") as string; |
| 136 | + if (!name.trim()) { |
| 137 | + this.fail("Channel name cannot be empty", flags, "parse"); |
169 | 138 | } |
170 | 139 |
|
171 | | - // Also validate argv entries for variadic commands (strict: false) |
172 | | - // where extra values bypass the named arg |
173 | | - const hasResourceArg = Object.keys( |
174 | | - AblyBaseCommand.RESOURCE_ARG_LABELS, |
175 | | - ).some((name) => name in parsedArgs); |
176 | | - if (hasResourceArg && Array.isArray(result.argv)) { |
177 | | - const label = Object.entries(AblyBaseCommand.RESOURCE_ARG_LABELS).find( |
178 | | - ([name]) => name in parsedArgs, |
179 | | - )?.[1]; |
180 | | - for (const value of result.argv as string[]) { |
181 | | - if (typeof value === "string" && !value.trim()) { |
182 | | - this.fail(`${label} name cannot be empty`, flags, "parse"); |
| 140 | + if (argv) { |
| 141 | + if (argv.length === 0) { |
| 142 | + this.fail( |
| 143 | + "At least one channel name is required", |
| 144 | + flags, |
| 145 | + "channelSubscribe", |
| 146 | + ); |
| 147 | + } |
| 148 | + |
| 149 | + for (const n of argv) { |
| 150 | + if (!n.trim()) { |
| 151 | + this.fail("Channel name cannot be empty", flags, "parse"); |
183 | 152 | } |
184 | 153 | } |
185 | 154 | } |
186 | | - |
187 | | - return result; |
188 | 155 | } |
189 | 156 |
|
190 | 157 | protected isAnonymousWebMode(): boolean { |
|
0 commit comments