Skip to content

Commit

Permalink
feat: -v checking for updates (#5461)
Browse files Browse the repository at this point in the history
* feat: version command check update

* feat: show a spinner while fetching latest version

Co-authored-by: Igor Randjelovic <rigor789@gmail.com>
  • Loading branch information
janoshrubos and rigor789 authored Jan 27, 2021
1 parent 0675896 commit 2d6683c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 11 deletions.
47 changes: 37 additions & 10 deletions lib/common/dispatchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import {
ICommandDispatcher,
ICancellationService,
ISysInfo,
IFileSystem,
IFutureDispatcher,
IQueue,
IErrors,
} from "./declarations";
import { IOptions } from "../declarations";
import { IOptions, IPackageManager, IVersionsService } from "../declarations";
import { IInjector } from "./definitions/yok";
import { injector } from "./yok";
import { PackageManagers } from "../constants";

export class CommandDispatcher implements ICommandDispatcher {
constructor(
Expand All @@ -25,7 +25,9 @@ export class CommandDispatcher implements ICommandDispatcher {
private $staticConfig: Config.IStaticConfig,
private $sysInfo: ISysInfo,
private $options: IOptions,
private $fs: IFileSystem
private $versionsService: IVersionsService,
private $packageManager: IPackageManager,
private $terminalSpinnerService: ITerminalSpinnerService
) {}

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

private printVersion(): void {
let version = this.$staticConfig.version;

const json = this.$fs.readJson(this.$staticConfig.pathToPackageJson);
if (json && json.buildVersion) {
version = `${version}-${json.buildVersion}`;
private async printVersion(): Promise<void> {
this.$logger.info(this.$staticConfig.version);

const spinner = this.$terminalSpinnerService.createSpinner();
spinner.start("Checking for updates...");
const nativescriptCliVersion = await this.$versionsService.getNativescriptCliVersion();
spinner.stop();

const packageManagerName = await this.$packageManager.getPackageManagerName();
let updateCommand = "";

switch (packageManagerName) {
case PackageManagers.npm:
updateCommand = "npm i -g nativescript";
break;
case PackageManagers.yarn:
updateCommand = "yarn global add nativescript";
break;
case PackageManagers.pnpm:
updateCommand = "pnpm i -g nativescript";
break;
}
if (
nativescriptCliVersion.currentVersion ===
nativescriptCliVersion.latestVersion
) {
// up-to-date
spinner.succeed("Up to date.");
} else {
spinner.info(
`New version of NativeScript CLI is available (${nativescriptCliVersion.latestVersion}), run '${updateCommand}' to update.`
);
}
this.$logger.info(version);
}
}
injector.register("commandDispatcher", CommandDispatcher);
Expand Down
2 changes: 1 addition & 1 deletion lib/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ export class Options {
opts[this.getDashedOptionName(key)] = value;
});

const parsed = yargs(process.argv.slice(2)).help(false);
const parsed = yargs(process.argv.slice(2)).version(false).help(false);
this.initialArgv = parsed.argv;
this.argv = parsed.options(<any>opts).argv;

Expand Down

0 comments on commit 2d6683c

Please sign in to comment.