Skip to content

Commit 83fccb4

Browse files
committed
refactor: use a shared cliBinPath whenever needed
1 parent 94635f6 commit 83fccb4

File tree

5 files changed

+30
-23
lines changed

5 files changed

+30
-23
lines changed

lib/commands/clean.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
} from "../definitions/project";
1111

1212
import type { PromptObject } from "prompts";
13-
import { IOptions } from "../declarations";
13+
import { IOptions, IStaticConfig } from "../declarations";
1414
import {
1515
ITerminalSpinner,
1616
ITerminalSpinnerService,
@@ -22,8 +22,6 @@ import { resolve } from "path";
2222
import { readdir } from "fs/promises";
2323
import { isInteractive } from "../common/helpers";
2424

25-
const CLIPath = resolve(__dirname, "..", "..", "bin", "nativescript.js");
26-
2725
function bytesToHumanReadable(bytes: number): string {
2826
const units = ["B", "KB", "MB", "GB", "TB"];
2927
let unit = 0;
@@ -90,7 +88,8 @@ export class CleanCommand implements ICommand {
9088
private $prompter: IPrompter,
9189
private $logger: ILogger,
9290
private $options: IOptions,
93-
private $childProcess: IChildProcess
91+
private $childProcess: IChildProcess,
92+
private $staticConfig: IStaticConfig
9493
) {}
9594

9695
public async execute(args: string[]): Promise<void> {
@@ -198,9 +197,12 @@ export class CleanCommand implements ICommand {
198197
paths,
199198
(p) => {
200199
return this.$childProcess
201-
.exec(`node ${CLIPath} clean --dry-run --json --disable-analytics`, {
202-
cwd: p,
203-
})
200+
.exec(
201+
`node ${this.$staticConfig.cliBinPath} clean --dry-run --json --disable-analytics`,
202+
{
203+
cwd: p,
204+
}
205+
)
204206
.then((res) => {
205207
const paths: Record<string, number> = JSON.parse(res).stats;
206208
return Object.values(paths).reduce((a, b) => a + b, 0);
@@ -290,7 +292,7 @@ export class CleanCommand implements ICommand {
290292

291293
const ok = await this.$childProcess
292294
.exec(
293-
`node ${CLIPath} clean ${
295+
`node ${this.$staticConfig.cliBinPath} clean ${
294296
this.$options.dryRun ? "--dry-run" : ""
295297
} --json --disable-analytics`,
296298
{

lib/commands/typings.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { IOptions } from "../declarations";
1+
import { IOptions, IStaticConfig } from "../declarations";
22
import { IChildProcess, IFileSystem, IHostInfo } from "../common/declarations";
33
import { ICommand, ICommandParameter } from "../common/definitions/commands";
44
import { injector } from "../common/yok";
@@ -14,7 +14,8 @@ export class TypingsCommand implements ICommand {
1414
private $projectData: IProjectData,
1515
private $mobileHelper: Mobile.IMobileHelper,
1616
private $childProcess: IChildProcess,
17-
private $hostInfo: IHostInfo
17+
private $hostInfo: IHostInfo,
18+
private $staticConfig: IStaticConfig
1819
) {}
1920

2021
public async execute(args: string[]): Promise<void> {
@@ -122,11 +123,9 @@ export class TypingsCommand implements ICommand {
122123
path.resolve(this.$projectData.projectDir, "typings", "ios")
123124
);
124125

125-
const nsPath = path.resolve(__dirname, "../../bin/nativescript.js");
126-
127126
await this.$childProcess.spawnFromEvent(
128127
"node",
129-
[nsPath, "build", "ios"],
128+
[this.$staticConfig.cliBinPath, "build", "ios"],
130129
"exit",
131130
{
132131
env: {

lib/common/definitions/config.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ declare module Config {
1010
TRACK_FEATURE_USAGE_SETTING_NAME: string;
1111
ERROR_REPORT_SETTING_NAME: string;
1212
version: string;
13+
cliBinPath: string;
1314
getAdbFilePath(): Promise<string>;
1415
disableAnalytics?: boolean;
1516
disableCommandHooks?: boolean;

lib/config.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ export class StaticConfig implements IStaticConfig {
5858

5959
public version = require("../package.json").version;
6060

61+
public cliBinPath = path.resolve(__dirname, "..", "bin", "nativescript.js");
62+
6163
public get HTML_CLI_HELPERS_DIR(): string {
6264
return path.join(__dirname, "../docs/helpers");
6365
}
@@ -73,9 +75,8 @@ export class StaticConfig implements IStaticConfig {
7375
private _adbFilePath: string = null;
7476
public async getAdbFilePath(): Promise<string> {
7577
if (!this._adbFilePath) {
76-
const androidToolsInfo: IAndroidToolsInfo = this.$injector.resolve(
77-
"androidToolsInfo"
78-
);
78+
const androidToolsInfo: IAndroidToolsInfo =
79+
this.$injector.resolve("androidToolsInfo");
7980
this._adbFilePath =
8081
(await androidToolsInfo.getPathToAdbFromAndroidHome()) ||
8182
(await this.getAdbFilePathCore());
@@ -111,9 +112,8 @@ export class StaticConfig implements IStaticConfig {
111112
}
112113

113114
private async getAdbFilePathCore(): Promise<string> {
114-
const $childProcess: IChildProcess = this.$injector.resolve(
115-
"$childProcess"
116-
);
115+
const $childProcess: IChildProcess =
116+
this.$injector.resolve("$childProcess");
117117

118118
try {
119119
// Do NOT use the adb wrapper because it will end blow up with Segmentation fault because the wrapper uses this method!!!

lib/services/start-service.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
import { injector } from "../common/yok";
88
import { IProjectData } from "../definitions/project";
99
import { IStartService } from "./../definitions/start-service.d";
10+
import { IStaticConfig } from "../declarations";
1011

1112
export default class StartService implements IStartService {
1213
ios: ChildProcess;
@@ -18,7 +19,8 @@ export default class StartService implements IStartService {
1819
private $childProcess: IChildProcess,
1920
private $devicePlatformsConstants: Mobile.IDevicePlatformsConstants,
2021
private $projectData: IProjectData,
21-
private $logger: ILogger
22+
private $logger: ILogger,
23+
private $staticConfig: IStaticConfig
2224
) {}
2325

2426
toggleVerbose(): void {
@@ -35,8 +37,8 @@ export default class StartService implements IStartService {
3537
async runForPlatform(platform: string) {
3638
const platformLowerCase = platform.toLowerCase();
3739
(this as any)[platformLowerCase] = this.$childProcess.spawn(
38-
"../nativescript-cli/bin/ns",
39-
["run", platform.toLowerCase()],
40+
"node",
41+
[this.$staticConfig.cliBinPath, "run", platform.toLowerCase()],
4042
{
4143
cwd: this.$projectData.projectDir,
4244
stdio: ["ipc"],
@@ -97,7 +99,10 @@ export default class StartService implements IStartService {
9799
await this.stopIOS();
98100
await this.stopAndroid();
99101

100-
const clean = this.$childProcess.spawn("ns", ["clean"]);
102+
const clean = this.$childProcess.spawn("node", [
103+
this.$staticConfig.cliBinPath,
104+
"clean",
105+
]);
101106
clean.stdout.on("data", (data) => {
102107
process.stdout.write(data);
103108
if (

0 commit comments

Comments
 (0)