Skip to content

Commit b7c4696

Browse files
committed
refactor(commands): read signing and install options from ctx.options
The command modules read provision, teamId and the test-init install options straight from the global options service, which their typed options already cover or can. The signing pair is a shared schema spread into the build, deploy, prepare, debug and run schemas, and validatePlatformOptions takes the typed context instead of resolving the service. Reads that write back, take the whole argv, or hand the service object to another service stay as they are. The redeclaration note in the docs said a redeclared CLI-wide option inherits nothing; the adapter carries the alias, default and sensitivity over when the command leaves them unset, and the docs now say so.
1 parent 7f56eac commit b7c4696

8 files changed

Lines changed: 77 additions & 29 deletions

File tree

‎defining-commands.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,9 @@ spelling _means_:
183183
stringOption({ alias: "p" })` steals `--path`'s shorthand. Restating an
184184
option's own shorthand (`path: stringOption({ alias: "p" })`) is fine.
185185

186-
The merge replaces the CLI-wide entry rather than patching it, so a
187-
redeclaration inherits nothing: restate the `alias` and `hasSensitiveValue` the
188-
global declaration carries if the command still wants them.
186+
A redeclaration that leaves `alias`, `default` or `hasSensitiveValue` unset
187+
keeps what the CLI-wide declaration carries for them, so `path: stringOption()`
188+
still answers to `-p` and stays out of the logs; set one only to change it.
189189

190190
### How validation behaves
191191

‎lib/commands/build.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ import {
22
ANDROID_RELEASE_BUILD_ERROR_MESSAGE,
33
AndroidAppBundleMessages,
44
} from "../constants";
5-
import { canExecuteCommandBase, validatePlatformOptions } from "./command-base";
5+
import {
6+
canExecuteCommandBase,
7+
platformSigningOptions,
8+
validatePlatformOptions,
9+
} from "./command-base";
610
import { hasValidAndroidSigning } from "../common/helpers";
711
import {
812
IAndroidBundleValidatorHelper,
@@ -28,6 +32,7 @@ import { inject } from "../common/di";
2832
type BuildPlatform = "iOS" | "Android" | "visionOS";
2933

3034
const buildCommandOptions = {
35+
...platformSigningOptions,
3136
watch: booleanOption({ default: false }),
3237
hmr: booleanOption({ default: false }),
3338
force: booleanOption(),

‎lib/commands/command-base.ts‎

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,31 @@ import {
55
ICanExecuteCommandOptions,
66
INotConfiguredEnvOptions,
77
} from "../common/definitions/commands";
8-
import { ArgumentSpec, CommandContext } from "../common/define-command";
8+
import {
9+
ArgumentSpec,
10+
CommandContext,
11+
CommandOptionsSchema,
12+
objectOption,
13+
} from "../common/define-command";
914
import { Injector } from "../common/di";
1015

16+
/**
17+
* The CLI-wide signing options `validatePlatformOptions` checks. A command
18+
* that validates them spreads this into its own schema.
19+
*/
20+
export const platformSigningOptions = {
21+
provision: objectOption(),
22+
teamId: objectOption(),
23+
} satisfies CommandOptionsSchema;
24+
1125
/** The part of a command context these helpers read. */
1226
type PlatformCommandContext = Pick<CommandContext<any>, "injector">;
1327

28+
type PlatformSigningContext = Pick<
29+
CommandContext<typeof platformSigningOptions>,
30+
"injector" | "options"
31+
>;
32+
1433
/**
1534
* The declarative form of `$platformCommandParameter`. Initializing the
1635
* project data is what makes the platform check possible, so it stays part of
@@ -38,17 +57,16 @@ export const platformArgument: ArgumentSpec<any> = {
3857
};
3958

4059
export function validatePlatformOptions(
41-
context: PlatformCommandContext,
60+
context: PlatformSigningContext,
4261
platform: string,
4362
): Promise<boolean> {
44-
const $options = context.injector.get<IOptions>("options");
4563
const $projectData = context.injector.get<IProjectData>("projectData");
4664

4765
return context.injector
4866
.get<IPlatformValidationService>("platformValidationService")
4967
.validateOptions(
50-
$options.provision,
51-
$options.teamId,
68+
context.options.provision,
69+
context.options.teamId,
5270
$projectData,
5371
platform,
5472
);
@@ -82,9 +100,19 @@ function hasUsableEnvironment(
82100
);
83101
}
84102

85-
export async function canExecuteCommandBase(
103+
export function canExecuteCommandBase(
104+
context: PlatformSigningContext,
105+
platform: string,
106+
options: ICanExecuteCommandOptions & { validateOptions: true },
107+
): Promise<boolean>;
108+
export function canExecuteCommandBase(
86109
context: PlatformCommandContext,
87110
platform: string,
111+
options?: ICanExecuteCommandOptions & { validateOptions?: false },
112+
): Promise<boolean>;
113+
export async function canExecuteCommandBase(
114+
context: PlatformCommandContext | PlatformSigningContext,
115+
platform: string,
88116
options: ICanExecuteCommandOptions = {},
89117
): Promise<boolean> {
90118
const validatePlatformOutput = await validatePlatformBase(
@@ -96,7 +124,10 @@ export async function canExecuteCommandBase(
96124
let result = canExecute;
97125

98126
if (canExecute && options.validateOptions) {
99-
result = await validatePlatformOptions(context, platform);
127+
result = await validatePlatformOptions(
128+
<PlatformSigningContext>context,
129+
platform,
130+
);
100131
}
101132

102133
return result;

‎lib/commands/debug.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,14 @@ import {
2727
restartShortcut,
2828
watcherShortcut,
2929
} from "../services/key-shortcuts";
30-
import { canExecuteCommandBase } from "./command-base";
30+
import { canExecuteCommandBase, platformSigningOptions } from "./command-base";
3131
import * as _ from "lodash";
3232

3333
/** Which `$devicePlatformsConstants` entry a command debugs. */
3434
type DebugPlatform = "iOS" | "Android" | "visionOS";
3535

3636
const debugCommandOptions = {
37+
...platformSigningOptions,
3738
force: booleanOption(),
3839
release: booleanOption(),
3940
aab: booleanOption(),

‎lib/commands/deploy.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ import {
22
ANDROID_RELEASE_BUILD_ERROR_MESSAGE,
33
ANDROID_APP_BUNDLE_SIGNING_ERROR_MESSAGE,
44
} from "../constants";
5-
import { canExecuteCommandBase, platformArgument } from "./command-base";
5+
import {
6+
canExecuteCommandBase,
7+
platformArgument,
8+
platformSigningOptions,
9+
} from "./command-base";
610
import { DeployCommandHelper } from "../helpers/deploy-command-helper";
711
import { hasValidAndroidSigning } from "../common/helpers";
812
import { IMigrateController } from "../definitions/migrate";
@@ -16,6 +20,7 @@ import {
1620
import { inject } from "../common/di";
1721

1822
const deployCommandOptions = {
23+
...platformSigningOptions,
1924
watch: booleanOption({ default: false }),
2025
hmr: booleanOption({ default: false }),
2126
force: booleanOption(),

‎lib/commands/prepare.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {
22
canExecuteCommandBase,
33
platformArgument,
4+
platformSigningOptions,
45
validatePlatformArgument,
56
validatePlatformOptions,
67
} from "./command-base";
@@ -18,6 +19,7 @@ import { IOptions } from "../declarations";
1819
import { IProjectData } from "../definitions/project";
1920

2021
export const prepareCommandOptions = {
22+
...platformSigningOptions,
2123
watch: booleanOption({ default: false }),
2224
hmr: booleanOption({ default: false }),
2325
skipNative: booleanOption({ default: false }),

‎lib/commands/run.ts‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
ANDROID_APP_BUNDLE_SIGNING_ERROR_MESSAGE,
1515
ANDROID_RELEASE_BUILD_ERROR_MESSAGE,
1616
} from "../constants";
17-
import { IOptions, IPlatformValidationService } from "../declarations";
17+
import { IPlatformValidationService } from "../declarations";
1818
import { IMigrateController } from "../definitions/migrate";
1919
import { IProjectData, IProjectDataService } from "../definitions/project";
2020
import {
@@ -25,8 +25,10 @@ import {
2525
restartShortcut,
2626
watcherShortcut,
2727
} from "../services/key-shortcuts";
28+
import { platformSigningOptions } from "./command-base";
2829

2930
const runCommandOptions = {
31+
...platformSigningOptions,
3032
force: booleanOption(),
3133
release: booleanOption(),
3234
aab: booleanOption(),
@@ -166,7 +168,6 @@ async function canExecuteApplePlatformRunCommand(
166168
context: RunCommandContext,
167169
platform: string,
168170
): Promise<boolean> {
169-
const $options = context.injector.get<IOptions>("options");
170171
const $platformValidationService =
171172
context.injector.get<IPlatformValidationService>(
172173
"platformValidationService",
@@ -188,8 +189,8 @@ async function canExecuteApplePlatformRunCommand(
188189
const result =
189190
(await canExecuteRunCommand(context, platform)) &&
190191
(await $platformValidationService.validateOptions(
191-
$options.provision,
192-
$options.teamId,
192+
context.options.provision,
193+
context.options.teamId,
193194
projectData,
194195
platform.toLowerCase(),
195196
));
@@ -229,7 +230,6 @@ export const androidRunCommand = defineCommand({
229230
options: runCommandOptions,
230231
arguments: "any",
231232
async canExecute(context: RunCommandContext): Promise<boolean> {
232-
const $options = inject<IOptions>("options");
233233
const $platformValidationService = inject<IPlatformValidationService>(
234234
"platformValidationService",
235235
);
@@ -265,8 +265,8 @@ export const androidRunCommand = defineCommand({
265265
}
266266

267267
return $platformValidationService.validateOptions(
268-
$options.provision,
269-
$options.teamId,
268+
context.options.provision,
269+
context.options.teamId,
270270
$projectData,
271271
platform.toLowerCase(),
272272
);

‎lib/commands/test-init.ts‎

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ import {
66
IProjectData,
77
ITestInitializationService,
88
} from "../definitions/project";
9-
import { INodePackageManager, IOptions } from "../declarations";
9+
import { INodePackageManager } from "../declarations";
1010
import { IPluginsService } from "../definitions/plugins";
1111
import {
1212
Command,
1313
CommandOptionsSchema,
14+
booleanOption,
1415
stringOption,
1516
} from "../common/define-command";
1617
import { inject } from "../common/di";
@@ -28,6 +29,10 @@ const karmaConfigAdditionalFrameworks: IDictionary<string[]> = {
2829

2930
const testInitCommandOptions = {
3031
framework: stringOption(),
32+
disableNpmInstall: booleanOption(),
33+
frameworkPath: stringOption(),
34+
ignoreScripts: booleanOption(),
35+
path: stringOption(),
3136
} satisfies CommandOptionsSchema;
3237

3338
export class TestInitCommand extends Command({
@@ -38,7 +43,6 @@ export class TestInitCommand extends Command({
3843
}) {
3944
private $fs = inject<IFileSystem>("fs");
4045
private $logger = inject<ILogger>("logger");
41-
private $options = inject<IOptions>("options");
4246
private $packageManager = inject<INodePackageManager>("packageManager");
4347
private $pluginsService = inject<IPluginsService>("pluginsService");
4448
private $projectData = inject<IProjectData>("projectData");
@@ -130,10 +134,10 @@ export class TestInitCommand extends Command({
130134
...(mod.saveInDependencies ? { save: true } : { "save-dev": true }),
131135
"save-exact": true,
132136
optional: false,
133-
disableNpmInstall: this.$options.disableNpmInstall,
134-
frameworkPath: this.$options.frameworkPath,
135-
ignoreScripts: this.$options.ignoreScripts,
136-
path: this.$options.path,
137+
disableNpmInstall: this.options.disableNpmInstall,
138+
frameworkPath: this.options.frameworkPath,
139+
ignoreScripts: this.options.ignoreScripts,
140+
path: this.options.path,
137141
});
138142

139143
const modulePath = path.join(projectDir, "node_modules", mod.name);
@@ -187,9 +191,9 @@ export class TestInitCommand extends Command({
187191
"save-dev": true,
188192
"save-exact": true,
189193
disableNpmInstall: false,
190-
frameworkPath: this.$options.frameworkPath,
191-
ignoreScripts: this.$options.ignoreScripts,
192-
path: this.$options.path,
194+
frameworkPath: this.options.frameworkPath,
195+
ignoreScripts: this.options.ignoreScripts,
196+
path: this.options.path,
193197
},
194198
);
195199
} catch (e) {

0 commit comments

Comments
 (0)