Skip to content

Commit 2d6683c

Browse files
feat: -v checking for updates (#5461)
* feat: version command check update * feat: show a spinner while fetching latest version Co-authored-by: Igor Randjelovic <[email protected]>
1 parent 0675896 commit 2d6683c

File tree

2 files changed

+38
-11
lines changed

2 files changed

+38
-11
lines changed

lib/common/dispatchers.ts

+37-10
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ import {
66
ICommandDispatcher,
77
ICancellationService,
88
ISysInfo,
9-
IFileSystem,
109
IFutureDispatcher,
1110
IQueue,
1211
IErrors,
1312
} from "./declarations";
14-
import { IOptions } from "../declarations";
13+
import { IOptions, IPackageManager, IVersionsService } from "../declarations";
1514
import { IInjector } from "./definitions/yok";
1615
import { injector } from "./yok";
16+
import { PackageManagers } from "../constants";
1717

1818
export class CommandDispatcher implements ICommandDispatcher {
1919
constructor(
@@ -25,7 +25,9 @@ export class CommandDispatcher implements ICommandDispatcher {
2525
private $staticConfig: Config.IStaticConfig,
2626
private $sysInfo: ISysInfo,
2727
private $options: IOptions,
28-
private $fs: IFileSystem
28+
private $versionsService: IVersionsService,
29+
private $packageManager: IPackageManager,
30+
private $terminalSpinnerService: ITerminalSpinnerService
2931
) {}
3032

3133
public async dispatchCommand(): Promise<void> {
@@ -99,14 +101,39 @@ export class CommandDispatcher implements ICommandDispatcher {
99101
return "";
100102
}
101103

102-
private printVersion(): void {
103-
let version = this.$staticConfig.version;
104-
105-
const json = this.$fs.readJson(this.$staticConfig.pathToPackageJson);
106-
if (json && json.buildVersion) {
107-
version = `${version}-${json.buildVersion}`;
104+
private async printVersion(): Promise<void> {
105+
this.$logger.info(this.$staticConfig.version);
106+
107+
const spinner = this.$terminalSpinnerService.createSpinner();
108+
spinner.start("Checking for updates...");
109+
const nativescriptCliVersion = await this.$versionsService.getNativescriptCliVersion();
110+
spinner.stop();
111+
112+
const packageManagerName = await this.$packageManager.getPackageManagerName();
113+
let updateCommand = "";
114+
115+
switch (packageManagerName) {
116+
case PackageManagers.npm:
117+
updateCommand = "npm i -g nativescript";
118+
break;
119+
case PackageManagers.yarn:
120+
updateCommand = "yarn global add nativescript";
121+
break;
122+
case PackageManagers.pnpm:
123+
updateCommand = "pnpm i -g nativescript";
124+
break;
125+
}
126+
if (
127+
nativescriptCliVersion.currentVersion ===
128+
nativescriptCliVersion.latestVersion
129+
) {
130+
// up-to-date
131+
spinner.succeed("Up to date.");
132+
} else {
133+
spinner.info(
134+
`New version of NativeScript CLI is available (${nativescriptCliVersion.latestVersion}), run '${updateCommand}' to update.`
135+
);
108136
}
109-
this.$logger.info(version);
110137
}
111138
}
112139
injector.register("commandDispatcher", CommandDispatcher);

lib/options.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ export class Options {
355355
opts[this.getDashedOptionName(key)] = value;
356356
});
357357

358-
const parsed = yargs(process.argv.slice(2)).help(false);
358+
const parsed = yargs(process.argv.slice(2)).version(false).help(false);
359359
this.initialArgv = parsed.argv;
360360
this.argv = parsed.options(<any>opts).argv;
361361

0 commit comments

Comments
 (0)