Skip to content

Commit 86dd22a

Browse files
feat: introduce initializeService
Introduce initializeService which can be used to setup some specifics of CLI (currently the logger) and to print all required warnings. At the moment, whenever we want to introduce new warnings, we need to introduce new methods that we call from CLI and when it is used as a library. With the new solution all such warnings can be placed in the initialize method and it will automatically show those warnings.
1 parent 5169829 commit 86dd22a

File tree

5 files changed

+43
-12
lines changed

5 files changed

+43
-12
lines changed

lib/bootstrap.ts

+2
Original file line numberDiff line numberDiff line change
@@ -199,3 +199,5 @@ $injector.requirePublic("cleanupService", "./services/cleanup-service");
199199
$injector.require("applePortalSessionService", "./services/apple-portal/apple-portal-session-service");
200200
$injector.require("applePortalCookieService", "./services/apple-portal/apple-portal-cookie-service");
201201
$injector.require("applePortalApplicationService", "./services/apple-portal/apple-portal-application-service");
202+
203+
$injector.requirePublicClass("initializeService", "./services/initialize-service");
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
interface IInitializeService {
2+
initialize(initOpts?: { loggerOptions?: ILoggerOptions }): Promise<void>;
3+
}

lib/nativescript-cli.ts

+5-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
require("./bootstrap");
22

3-
import { EOL } from "os";
43
import * as shelljs from "shelljs";
54
shelljs.config.silent = true;
65
shelljs.config.fatal = true;
@@ -25,24 +24,18 @@ process.on = (event: string, listener: any): any => {
2524
const err: IErrors = $injector.resolve("$errors");
2625
err.printCallStack = config.DEBUG;
2726

27+
const $options = $injector.resolve<IOptions>("options");
28+
29+
const $initializeService = $injector.resolve<IInitializeService>("initializeService");
30+
await $initializeService.initialize({ loggerOptions: { level: <any>$options.log } });
31+
2832
const extensibilityService: IExtensibilityService = $injector.resolve("extensibilityService");
2933
try {
3034
await settlePromises<IExtensionData>(extensibilityService.loadExtensions());
3135
} catch (err) {
3236
logger.trace("Unable to load extensions. Error is: ", err);
3337
}
3438

35-
const $sysInfo = $injector.resolve<ISysInfo>("sysInfo");
36-
const macOSWarning = await $sysInfo.getMacOSWarningMessage();
37-
if (macOSWarning) {
38-
const message = `${EOL}${macOSWarning.message}${EOL}`;
39-
if (macOSWarning.severity === SystemWarningsSeverity.high) {
40-
logger.printOnStderr(message.red.bold);
41-
} else {
42-
logger.warn(message);
43-
}
44-
}
45-
4639
const commandDispatcher: ICommandDispatcher = $injector.resolve("commandDispatcher");
4740

4841
const messages: IMessagesService = $injector.resolve("$messagesService");

lib/services/initialize-service.ts

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { EOL } from "os";
2+
3+
export class InitializeService implements IInitializeService {
4+
// NOTE: Do not inject anything here, use $injector.resolve in the code
5+
// Injecting something may lead to logger initialization, but we want to initialize it from here.
6+
constructor(private $injector: IInjector) { }
7+
8+
public async initialize(initOpts?: { loggerOptions?: ILoggerOptions }): Promise<void> {
9+
initOpts = initOpts || {};
10+
const $logger = this.$injector.resolve<ILogger>("logger");
11+
$logger.initialize(initOpts.loggerOptions);
12+
13+
await this.showWarnings($logger);
14+
}
15+
16+
private async showWarnings($logger: ILogger): Promise<void> {
17+
const $sysInfo = $injector.resolve<ISysInfo>("sysInfo");
18+
const macOSWarning = await $sysInfo.getMacOSWarningMessage();
19+
if (macOSWarning) {
20+
const message = `${EOL}${macOSWarning.message}${EOL}`;
21+
if (macOSWarning.severity === SystemWarningsSeverity.high) {
22+
$logger.printOnStderr(message.red.bold);
23+
} else {
24+
$logger.warn(message);
25+
}
26+
}
27+
}
28+
}
29+
30+
$injector.register("initializeService", InitializeService);

test/nativescript-cli-lib.ts

+3
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ describe("nativescript-cli-lib", () => {
6767
"initialize",
6868
"getLevel",
6969
"info"
70+
],
71+
initializeService: [
72+
"initialize"
7073
]
7174
};
7275

0 commit comments

Comments
 (0)