Skip to content

Commit 0b8d65b

Browse files
committed
refactor(commands): inline the simple command definitions
A simple command is now one defineCommand call with its handlers written inline, where ctx is typed by inference: the exported setupX/runX/canExecuteX functions and the IXServices and XCommandContext aliases nothing else read are gone. Handlers inject what they use at their own top, before the first await. No command hands another a bag of services any more. injectPlatformCommandServices and the setupX bundles are deleted; the shared platform checks take the context and resolve through ctx.injector. A setup that survives is side-effect only - the eager initializeProjectData that has to land ahead of the arguments policy.
1 parent 955efb2 commit 0b8d65b

55 files changed

Lines changed: 1642 additions & 2273 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

lib/commands/apple-login.ts

Lines changed: 34 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,43 @@
11
import { IErrors } from "../common/declarations";
2-
import { CommandContext, defineCommand } from "../common/define-command";
2+
import { defineCommand } from "../common/define-command";
33
import { inject } from "../common/di";
44
import { IApplePortalSessionService } from "../services/apple-portal/definitions";
55

6-
export type AppleLoginCommandContext = CommandContext;
7-
8-
export function setupAppleLoginCommand() {
9-
return {
10-
$applePortalSessionService: inject<IApplePortalSessionService>(
11-
"applePortalSessionService",
12-
),
13-
$errors: inject<IErrors>("errors"),
14-
$logger: inject<ILogger>("logger"),
15-
$prompter: inject<IPrompter>("prompter"),
16-
};
17-
}
18-
19-
export type IAppleLoginCommandServices = ReturnType<
20-
typeof setupAppleLoginCommand
21-
>;
22-
23-
export async function runAppleLoginCommand(
24-
context: AppleLoginCommandContext,
25-
services: IAppleLoginCommandServices,
26-
): Promise<void> {
27-
let username = context.args[0];
28-
if (!username) {
29-
username = await services.$prompter.getString("Apple ID", {
30-
allowEmpty: false,
31-
});
32-
}
33-
34-
let password = context.args[1];
35-
if (!password) {
36-
password = await services.$prompter.getPassword("Apple ID password");
37-
}
38-
39-
const user = await services.$applePortalSessionService.createUserSession({
40-
username,
41-
password,
42-
});
43-
if (!user.areCredentialsValid) {
44-
services.$errors.fail(
45-
`Invalid username and password combination. Used '${username}' as the username.`,
46-
);
47-
}
48-
49-
const output = Buffer.from(user.userSessionCookie).toString("base64");
50-
services.$logger.info(output);
51-
}
52-
536
export const appleLoginCommandDefinition = defineCommand({
547
name: "apple-login",
558
description: "Logs in to an Apple account and prints the session cookie.",
569
arguments: [{ name: "appleId" }, { name: "password" }],
57-
setup: setupAppleLoginCommand,
58-
run: runAppleLoginCommand,
10+
async run(context) {
11+
const $applePortalSessionService = inject<IApplePortalSessionService>(
12+
"applePortalSessionService",
13+
);
14+
const $errors = inject<IErrors>("errors");
15+
const $logger = inject<ILogger>("logger");
16+
const $prompter = inject<IPrompter>("prompter");
17+
18+
let username = context.args[0];
19+
if (!username) {
20+
username = await $prompter.getString("Apple ID", {
21+
allowEmpty: false,
22+
});
23+
}
24+
25+
let password = context.args[1];
26+
if (!password) {
27+
password = await $prompter.getPassword("Apple ID password");
28+
}
29+
30+
const user = await $applePortalSessionService.createUserSession({
31+
username,
32+
password,
33+
});
34+
if (!user.areCredentialsValid) {
35+
$errors.fail(
36+
`Invalid username and password combination. Used '${username}' as the username.`,
37+
);
38+
}
39+
40+
const output = Buffer.from(user.userSessionCookie).toString("base64");
41+
$logger.info(output);
42+
},
5943
});

lib/commands/build.ts

Lines changed: 54 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@ import {
22
ANDROID_RELEASE_BUILD_ERROR_MESSAGE,
33
AndroidAppBundleMessages,
44
} from "../constants";
5-
import {
6-
canExecuteCommandBase,
7-
injectPlatformCommandServices,
8-
validatePlatformOptions,
9-
} from "./command-base";
5+
import { canExecuteCommandBase, validatePlatformOptions } from "./command-base";
106
import { hasValidAndroidSigning } from "../common/helpers";
11-
import { IAndroidBundleValidatorHelper } from "../declarations";
7+
import {
8+
IAndroidBundleValidatorHelper,
9+
IOptions,
10+
IPlatformValidationService,
11+
} from "../declarations";
1212
import { IBuildController, IBuildDataService } from "../definitions/build";
1313
import { IMigrateController } from "../definitions/migrate";
14+
import { IProjectData } from "../definitions/project";
1415
import { IErrors } from "../common/declarations";
1516
import {
1617
booleanOption,
@@ -48,87 +49,82 @@ const defineBuildCommand = <const TName extends CommandName>(
4849
description: "Builds the project for the selected target platform.",
4950
options: buildCommandOptions,
5051
arguments: "none",
51-
setup() {
52-
const devicePlatformsConstants = inject<Mobile.IDevicePlatformsConstants>(
53-
"devicePlatformsConstants",
52+
async canExecute(context): Promise<boolean> {
53+
const $devicePlatformsConstants =
54+
inject<Mobile.IDevicePlatformsConstants>("devicePlatformsConstants");
55+
const $errors = inject<IErrors>("errors");
56+
const $migrateController =
57+
inject<IMigrateController>("migrateController");
58+
const $platformValidationService = inject<IPlatformValidationService>(
59+
"platformValidationService",
5460
);
55-
const platform = devicePlatformsConstants[buildPlatform];
56-
const isAndroid = devicePlatformsConstants.isAndroid(platform);
57-
const services = {
58-
...injectPlatformCommandServices(),
59-
platform,
60-
isAndroid,
61-
$errors: inject<IErrors>("errors"),
62-
$logger: inject<ILogger>("logger"),
63-
$buildController: inject<IBuildController>("buildController"),
64-
$buildDataService: inject<IBuildDataService>("buildDataService"),
65-
$migrateController: inject<IMigrateController>("migrateController"),
66-
// Only the android build checks the runtime version.
67-
$androidBundleValidatorHelper: isAndroid
68-
? inject<IAndroidBundleValidatorHelper>(
69-
"androidBundleValidatorHelper",
70-
)
71-
: null,
72-
};
73-
services.$projectData.initializeProjectData();
74-
75-
return services;
76-
},
77-
async canExecute(context, services): Promise<boolean> {
78-
const { platform } = services;
61+
const $projectData = inject<IProjectData>("projectData");
62+
const platform = $devicePlatformsConstants[buildPlatform];
63+
const isAndroid = $devicePlatformsConstants.isAndroid(platform);
64+
// Only the android build checks the runtime version.
65+
const $androidBundleValidatorHelper = isAndroid
66+
? inject<IAndroidBundleValidatorHelper>("androidBundleValidatorHelper")
67+
: null;
68+
$projectData.initializeProjectData();
7969

8070
if (!context.options.force) {
81-
await services.$migrateController.validate({
82-
projectDir: services.$projectData.projectDir,
71+
await $migrateController.validate({
72+
projectDir: $projectData.projectDir,
8373
platforms: [platform],
8474
});
8575
}
8676

87-
if (services.isAndroid) {
88-
services.$androidBundleValidatorHelper.validateRuntimeVersion(
89-
services.$projectData,
90-
);
77+
if (isAndroid) {
78+
$androidBundleValidatorHelper.validateRuntimeVersion($projectData);
9179
} else if (
92-
!services.$platformValidationService.isPlatformSupportedForOS(
80+
!$platformValidationService.isPlatformSupportedForOS(
9381
platform,
94-
services.$projectData,
82+
$projectData,
9583
)
9684
) {
97-
services.$errors.fail(
85+
$errors.fail(
9886
`Applications for platform ${platform} can not be built on this OS`,
9987
);
10088
}
10189

102-
if (!(await canExecuteCommandBase(services, platform))) {
90+
if (!(await canExecuteCommandBase(context, platform))) {
10391
return false;
10492
}
10593

10694
if (
107-
services.isAndroid &&
95+
isAndroid &&
10896
context.options.release &&
10997
!hasValidAndroidSigning(context.options)
11098
) {
111-
services.$errors.failWithHelp(ANDROID_RELEASE_BUILD_ERROR_MESSAGE);
99+
$errors.failWithHelp(ANDROID_RELEASE_BUILD_ERROR_MESSAGE);
112100
}
113101

114-
return validatePlatformOptions(services, platform);
102+
return validatePlatformOptions(context, platform);
115103
},
116-
async run(context, services): Promise<string> {
117-
const buildData = services.$buildDataService.getBuildData(
118-
services.$projectData.projectDir,
119-
services.platform.toLowerCase(),
120-
services.$options,
104+
async run(context): Promise<string> {
105+
const $buildController = inject<IBuildController>("buildController");
106+
const $buildDataService = inject<IBuildDataService>("buildDataService");
107+
const $devicePlatformsConstants =
108+
inject<Mobile.IDevicePlatformsConstants>("devicePlatformsConstants");
109+
const $logger = inject<ILogger>("logger");
110+
const $options = inject<IOptions>("options");
111+
const $projectData = inject<IProjectData>("projectData");
112+
const platform = $devicePlatformsConstants[buildPlatform];
113+
const isAndroid = $devicePlatformsConstants.isAndroid(platform);
114+
$projectData.initializeProjectData();
115+
116+
const buildData = $buildDataService.getBuildData(
117+
$projectData.projectDir,
118+
platform.toLowerCase(),
119+
$options,
121120
);
122-
const outputPath =
123-
await services.$buildController.prepareAndBuild(buildData);
121+
const outputPath = await $buildController.prepareAndBuild(buildData);
124122

125-
if (services.isAndroid && context.options.aab) {
126-
services.$logger.info(
127-
AndroidAppBundleMessages.ANDROID_APP_BUNDLE_DOCS_MESSAGE,
128-
);
123+
if (isAndroid && context.options.aab) {
124+
$logger.info(AndroidAppBundleMessages.ANDROID_APP_BUNDLE_DOCS_MESSAGE);
129125

130126
if (context.options.release) {
131-
services.$logger.info(
127+
$logger.info(
132128
AndroidAppBundleMessages.ANDROID_APP_BUNDLE_PUBLISH_DOCS_MESSAGE,
133129
);
134130
}

0 commit comments

Comments
 (0)