Skip to content

Commit fd0d93d

Browse files
committed
fix(commands): give an option group values only when a command lists it
A group looked its values up by spelling, so one the typed command did not list could read another option's value, of another type. It reads as its defaults now, which lets two groups use one spelling. A process that uses the CLI as a library has no typed command, so groups read as their defaults there too.
1 parent 341e227 commit fd0d93d

15 files changed

Lines changed: 214 additions & 122 deletions

‎defining-commands.md‎

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,9 @@ export default defineCommand({
204204
```
205205

206206
`ctx.options` is typed as the merged values of every part. The class form
207-
takes the same list, and types `this.options` the same way. A command that
208-
keeps a single schema object can spread a group's: `...WidgetOptions.schema`.
207+
takes the same list, and types `this.options` the same way. List
208+
the group itself: spreading `...WidgetOptions.schema` declares the options, but the
209+
command does not list the group, so services reading it get the defaults.
209210

210211
A group has no name: the exported object is the token, so declare it once and
211212
import it wherever it is listed or injected.
@@ -235,16 +236,20 @@ class WidgetRenderer {
235236
fine; reading a property that early throws `The option 'compact' was read
236237
before the command line was parsed`. So keep the object and read it in the
237238
method that needs it. Do not destructure it in a field or a constructor.
239+
- **Only a command that lists the group gives it values.** When the typed command
240+
does not list it, every option reads as the group's `default`. Two groups may
241+
therefore use one spelling for different things: each gets a value only from the
242+
commands that list it.
238243
- **The values never change.** They are fixed when the typed command is parsed,
239-
are read-only, and are the same in every later callback. Assigning to the
240-
options service does not change them.
244+
are read-only, and are the same in every later callback. Nothing that assigns to
245+
the options service changes them, hooks included; a hook changes an operation
246+
through its payload.
241247
- **A command run in process does not change them.** Its own `ctx.options`
242248
holds its own values and defaults; a service still reads the typed command's.
243249
A command that needs a service to act on other values passes them to it.
244-
- **An option that was not given** reads as the group's `default`. When the
245-
typed command declares the option, its declaration decides.
250+
- **An option that was not given** reads as the group's `default`.
246251
- **A process that uses the CLI as a library** has no typed command, so reading
247-
never throws and returns the options service's current value.
252+
never throws and returns the defaults.
248253

249254
Prefer a parameter when the method already receives a data object that can
250255
carry the value; inject a group where passing it would mean threading it

‎lib/commands/build.ts‎

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { IMigrateController } from "../definitions/migrate";
1919
import {
2020
booleanOption,
2121
CommandName,
22+
CommandOptionsInput,
2223
CommandOptionsSchema,
2324
defineCommand,
2425
stringOption,
@@ -33,18 +34,20 @@ import { ProjectData } from "../contracts/project-data";
3334
*/
3435
type BuildPlatform = "iOS" | "Android" | "visionOS";
3536

36-
const buildCommandOptions = {
37-
...platformSigningOptions,
38-
watch: booleanOption({ default: false }),
39-
hmr: booleanOption({ default: false }),
40-
force: booleanOption(),
41-
release: booleanOption(),
42-
...AndroidBundleOptions.schema,
43-
keyStorePath: stringOption(),
44-
keyStorePassword: stringOption(),
45-
keyStoreAlias: stringOption(),
46-
keyStoreAliasPassword: stringOption(),
47-
} satisfies CommandOptionsSchema;
37+
const buildCommandOptions = [
38+
AndroidBundleOptions,
39+
{
40+
...platformSigningOptions,
41+
watch: booleanOption({ default: false }),
42+
hmr: booleanOption({ default: false }),
43+
force: booleanOption(),
44+
release: booleanOption(),
45+
keyStorePath: stringOption(),
46+
keyStorePassword: stringOption(),
47+
keyStoreAlias: stringOption(),
48+
keyStoreAliasPassword: stringOption(),
49+
} satisfies CommandOptionsSchema,
50+
] satisfies CommandOptionsInput;
4851

4952
const defineBuildCommand = <const TName extends CommandName>(
5053
name: TName,

‎lib/commands/debug.ts‎

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
booleanOption,
55
CommandContext,
66
CommandName,
7+
CommandOptionsInput,
78
CommandOptionsSchema,
89
defineCommand,
910
stringOption,
@@ -38,22 +39,24 @@ import { ProjectData } from "../contracts/project-data";
3839
/** Which `$devicePlatformsConstants` entry a command debugs. */
3940
type DebugPlatform = "iOS" | "Android" | "visionOS";
4041

41-
const debugCommandOptions = {
42-
...platformSigningOptions,
43-
force: booleanOption(),
44-
release: booleanOption(),
45-
...AndroidBundleOptions.schema,
46-
start: booleanOption(),
47-
emulator: booleanOption(),
48-
forDevice: booleanOption(),
49-
inspector: booleanOption(),
50-
device: stringOption(),
51-
timeout: stringOption(),
52-
keyStorePath: stringOption(),
53-
keyStorePassword: stringOption(),
54-
keyStoreAlias: stringOption(),
55-
keyStoreAliasPassword: stringOption(),
56-
} satisfies CommandOptionsSchema;
42+
const debugCommandOptions = [
43+
AndroidBundleOptions,
44+
{
45+
...platformSigningOptions,
46+
force: booleanOption(),
47+
release: booleanOption(),
48+
start: booleanOption(),
49+
emulator: booleanOption(),
50+
forDevice: booleanOption(),
51+
inspector: booleanOption(),
52+
device: stringOption(),
53+
timeout: stringOption(),
54+
keyStorePath: stringOption(),
55+
keyStorePassword: stringOption(),
56+
keyStoreAlias: stringOption(),
57+
keyStoreAliasPassword: stringOption(),
58+
} satisfies CommandOptionsSchema,
59+
] satisfies CommandOptionsInput;
5760

5861
type DebugCommandContext = CommandContext<typeof debugCommandOptions>;
5962

‎lib/commands/deploy.ts‎

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { hasValidAndroidSigning } from "../common/helpers";
1313
import { IMigrateController } from "../definitions/migrate";
1414
import {
1515
booleanOption,
16+
CommandOptionsInput,
1617
CommandOptionsSchema,
1718
defineCommand,
1819
stringOption,
@@ -21,18 +22,20 @@ import { AndroidBundleOptions } from "../option-groups/android-bundle-options";
2122
import { inject } from "../common/di";
2223
import { ProjectData } from "../contracts/project-data";
2324

24-
const deployCommandOptions = {
25-
...platformSigningOptions,
26-
watch: booleanOption({ default: false }),
27-
hmr: booleanOption({ default: false }),
28-
force: booleanOption(),
29-
release: booleanOption(),
30-
...AndroidBundleOptions.schema,
31-
keyStorePath: stringOption(),
32-
keyStorePassword: stringOption(),
33-
keyStoreAlias: stringOption(),
34-
keyStoreAliasPassword: stringOption(),
35-
} satisfies CommandOptionsSchema;
25+
const deployCommandOptions = [
26+
AndroidBundleOptions,
27+
{
28+
...platformSigningOptions,
29+
watch: booleanOption({ default: false }),
30+
hmr: booleanOption({ default: false }),
31+
force: booleanOption(),
32+
release: booleanOption(),
33+
keyStorePath: stringOption(),
34+
keyStorePassword: stringOption(),
35+
keyStoreAlias: stringOption(),
36+
keyStoreAliasPassword: stringOption(),
37+
} satisfies CommandOptionsSchema,
38+
] satisfies CommandOptionsInput;
3639

3740
export const deployCommandDefinition = defineCommand({
3841
name: "deploy",

‎lib/commands/run.ts‎

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
booleanOption,
55
CommandContext,
66
CommandName,
7+
CommandOptionsInput,
78
CommandOptionsSchema,
89
defineCommand,
910
stringOption,
@@ -29,16 +30,18 @@ import {
2930
import { platformSigningOptions, provideProject } from "./command-base";
3031
import { ProjectData } from "../contracts/project-data";
3132

32-
const runCommandOptions = {
33-
...platformSigningOptions,
34-
force: booleanOption(),
35-
release: booleanOption(),
36-
...AndroidBundleOptions.schema,
37-
keyStorePath: stringOption(),
38-
keyStorePassword: stringOption(),
39-
keyStoreAlias: stringOption(),
40-
keyStoreAliasPassword: stringOption(),
41-
} satisfies CommandOptionsSchema;
33+
const runCommandOptions = [
34+
AndroidBundleOptions,
35+
{
36+
...platformSigningOptions,
37+
force: booleanOption(),
38+
release: booleanOption(),
39+
keyStorePath: stringOption(),
40+
keyStorePassword: stringOption(),
41+
keyStoreAlias: stringOption(),
42+
keyStoreAliasPassword: stringOption(),
43+
} satisfies CommandOptionsSchema,
44+
] satisfies CommandOptionsInput;
4245

4346
type RunCommandContext = CommandContext<typeof runCommandOptions>;
4447

‎lib/commands/test.ts‎

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
import {
77
booleanOption,
88
CommandContext,
9+
CommandOptionsInput,
910
CommandOptionsSchema,
1011
defineCommand,
1112
stringOption,
@@ -32,32 +33,34 @@ import { provideProject } from "./command-base";
3233
/** The platform spelling the test services receive, verbatim. */
3334
type TestPlatform = "android" | "iOS" | "visionOS";
3435

35-
const testCommandOptions = {
36-
// The CLI-wide default is true; unit testing has always opted out of it.
37-
hmr: booleanOption({ default: false }),
38-
force: booleanOption(),
39-
// Watch mode keeps the run alive waiting for changes; CI runs must
40-
// execute once and exit, so --watch defaults to off there. The schema is
41-
// module-level, so the environment is read lazily at command resolution.
42-
watch: {
43-
type: "boolean",
44-
get default(): boolean {
45-
return !isCIEnvironment();
36+
const testCommandOptions = [
37+
AndroidBundleOptions,
38+
{
39+
// The CLI-wide default is true; unit testing has always opted out of it.
40+
hmr: booleanOption({ default: false }),
41+
force: booleanOption(),
42+
// Watch mode keeps the run alive waiting for changes; CI runs must
43+
// execute once and exit, so --watch defaults to off there. The schema is
44+
// module-level, so the environment is read lazily at command resolution.
45+
watch: {
46+
type: "boolean",
47+
get default(): boolean {
48+
return !isCIEnvironment();
49+
},
4650
},
47-
},
48-
justlaunch: booleanOption(),
49-
debugBrk: booleanOption(),
50-
device: stringOption(),
51-
emulator: booleanOption(),
52-
forDevice: booleanOption(),
53-
sdk: stringOption(),
54-
release: booleanOption(),
55-
...AndroidBundleOptions.schema,
56-
keyStorePath: stringOption(),
57-
keyStorePassword: stringOption(),
58-
keyStoreAlias: stringOption(),
59-
keyStoreAliasPassword: stringOption(),
60-
} satisfies CommandOptionsSchema;
51+
justlaunch: booleanOption(),
52+
debugBrk: booleanOption(),
53+
device: stringOption(),
54+
emulator: booleanOption(),
55+
forDevice: booleanOption(),
56+
sdk: stringOption(),
57+
release: booleanOption(),
58+
keyStorePath: stringOption(),
59+
keyStorePassword: stringOption(),
60+
keyStoreAlias: stringOption(),
61+
keyStoreAliasPassword: stringOption(),
62+
} satisfies CommandOptionsSchema,
63+
] satisfies CommandOptionsInput;
6164

6265
type TestCommandContext = CommandContext<typeof testCommandOptions>;
6366

‎lib/common/define-command.ts‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,12 @@ export type OptionsGroupSource = "commandLine" | "startup";
103103

104104
/** What a group reads its values from; the options service implements it. */
105105
export interface OptionValueReader {
106-
readGroupOption(optionName: string, source: OptionsGroupSource): any;
106+
/** @param schema The group's schema, which identifies the group. */
107+
readGroupOption(
108+
schema: CommandOptionsSchema,
109+
optionName: string,
110+
source: OptionsGroupSource,
111+
): any;
107112
}
108113

109114
const spellingOf = (optionName: string): string =>
@@ -114,7 +119,9 @@ const spellingOf = (optionName: string): string =>
114119
* group under `options` and the parser fills it; a service injects the group
115120
* and reads the parsed values, typed from the same declaration. The group is
116121
* its own token and provides itself at the root, so it can be injected at any
117-
* time; its values can be read once the command line is parsed.
122+
* time; its values can be read once the command line is parsed. Only a command
123+
* that lists the group gives it values: two groups may use one spelling, so a
124+
* group the typed command does not list reads as its defaults.
118125
*/
119126
export class OptionsGroup<
120127
TSchema extends CommandOptionsSchema = CommandOptionsSchema,
@@ -147,7 +154,7 @@ function createGroupValues<TSchema extends CommandOptionsSchema>(
147154
get: () => {
148155
const value = injector
149156
.get<OptionValueReader>("options")
150-
.readGroupOption(optionName, source);
157+
.readGroupOption(schema, optionName, source);
151158
return value === undefined ? schema[optionName].default : value;
152159
},
153160
});

‎lib/common/definitions/commands.d.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ interface ICommand extends ICommandOptions {
1616
/** @deprecated Declared here, referenced nowhere. */
1717
completionData?: string[];
1818
dashedOptions?: IDictionary<IDashedOption>;
19+
/** The schemas of the option groups the command lists. */
20+
optionGroupSchemas?: readonly object[];
1921
isHierarchicalCommand?: boolean;
2022

2123
/**

‎lib/common/services/command-definition-adapter.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,9 @@ export function createCommandFromDefinition<
535535
return {
536536
allowedParameters: [],
537537
dashedOptions,
538+
optionGroupSchemas: parts
539+
.filter(isOptionsGroup)
540+
.map((group) => group.schema),
538541
...(definition.disableAnalytics === undefined
539542
? {}
540543
: { disableAnalytics: definition.disableAnalytics }),

‎lib/common/services/commands-service.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,9 @@ export class CommandsService
263263
this.$options.validateOptions(
264264
dashedOptions,
265265
command && allowsUnknownOptions(command),
266-
!this.isExecutingInProcess,
266+
this.isExecutingInProcess
267+
? undefined
268+
: (command && command.optionGroupSchemas) || [],
267269
);
268270
}
269271

0 commit comments

Comments
 (0)