Skip to content

Commit

Permalink
feat(cli): log when update is avaliable (#417)
Browse files Browse the repository at this point in the history
* Refactor update

* Log status

* Update contract

* Channels are constant

* Update message

* Fix tests
  • Loading branch information
sebastijankuzner authored Feb 1, 2024
1 parent 4d8b392 commit 67dfac0
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 36 deletions.
3 changes: 2 additions & 1 deletion packages/api/source/commands/config-cli.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Commands, Identifiers, Services } from "@mainsail/cli";
import { inject, injectable } from "@mainsail/container";
import { Constants } from "@mainsail/contracts";
import { Utils } from "@mainsail/kernel";
import Joi from "joi";

Expand All @@ -20,7 +21,7 @@ export class Command extends Commands.Command {
.setFlag(
"channel",
"The NPM registry channel that should be used.",
Joi.string().valid("alpha", "next", "latest"),
Joi.string().valid(...Constants.Channels),
);
}

Expand Down
2 changes: 2 additions & 0 deletions packages/cli/source/contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ export interface Config {
}

export interface Updater {
logStatus(): Promise<void>;

check(force?: boolean): Promise<boolean>;

update(updateProcessManager?: boolean, force?: boolean): Promise<boolean>;
Expand Down
6 changes: 2 additions & 4 deletions packages/cli/source/services/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { inject, injectable, postConstruct } from "@mainsail/container";
import { Contracts } from "@mainsail/contracts";
import { Constants, Contracts } from "@mainsail/contracts";
import { ensureFileSync, readJsonSync, writeJsonSync } from "fs-extra";

import { Application } from "../contracts";
Expand Down Expand Up @@ -87,10 +87,8 @@ export class Config {
}

#getRegistryChannel(version: string): string {
const channels: string[] = ["next"];

let channel = "latest";
for (const item of channels) {
for (const item of Constants.Channels) {
if (version.includes(`-${item}`)) {
channel = item;
}
Expand Down
13 changes: 1 addition & 12 deletions packages/cli/source/services/updater.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,6 @@ describe<{

afterAll(() => nock.enableNetConnect());

it("#check - should forget the latest version if it has one from a previous check", async ({ config, updater }) => {
nock.fake(/.*/).get("/@mainsail%2Fcore").reply(200, versionNext);

config.set("latestVersion", {});

const spyForget = spy(config, "forget");

assert.false(await updater.check());
spyForget.calledWith("latestVersion");
});

it("#check - should return false if the latest version cannot be retrieved", async ({ cli, updater }) => {
nock.fake(/.*/).get("/@mainsail%2Fcore").reply(200, {});

Expand Down Expand Up @@ -71,7 +60,7 @@ describe<{

nock.fake(/.*/).get("/@mainsail%2Fcore").reply(200, response);

config.set("latestVersion", {});
config.set("latestVersion", undefined);

const spySet = spy(config, "set");

Expand Down
40 changes: 24 additions & 16 deletions packages/cli/source/services/updater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,32 +34,40 @@ export class Updater implements Contracts_Updater {

#latestVersion: string | undefined;

public async logStatus(): Promise<void> {
if (await this.check()) {
this.app
.get<Warning>(Identifiers.Warning)
.render(
`${reset(" An update is available")} ${dim(this.#packageVersion)} ${reset(" → ")} ${green(
this.#latestVersion || "",
)}. Run ${green("mainsail update")} to update to the latest version.`,
);
}
}

public async check(force?: boolean): Promise<boolean> {
this.#latestVersion = this.config.get("latestVersion");

if (
this.#latestVersion &&
!force &&
Date.now() - this.config.get<number>("lastUpdateCheck") < this.#updateCheckInterval
!this.#latestVersion ||
force ||
Date.now() - this.config.get<number>("lastUpdateCheck") > this.#updateCheckInterval
) {
// Update is available if last seen is greater than latest.
return lt(this.#packageVersion, this.#latestVersion);
}
const latestVersion: string | undefined = await this.getLatestVersion();

const latestVersion: string | undefined = await this.getLatestVersion();
this.config.set("lastUpdateCheck", Date.now());

this.config.set("lastUpdateCheck", Date.now());
if (latestVersion === undefined) {
this.config.forget("latestVersion");
return false;
}

if (latestVersion === undefined) {
this.config.forget("latestVersion");
return false;
this.#latestVersion = latestVersion;
this.config.set("latestVersion", latestVersion);
}

this.config.set("latestVersion", latestVersion);

this.#latestVersion = latestVersion;

return true;
return lt(this.#packageVersion, this.#latestVersion);
}

public async update(updateProcessManager = false, force = false): Promise<boolean> {
Expand Down
1 change: 1 addition & 0 deletions packages/contracts/source/constants/channels.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const Channels = ["latest", "alpha", "next"];
1 change: 1 addition & 0 deletions packages/contracts/source/constants/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from "./channels";
export * from "./flags";
export * from "./units";
4 changes: 2 additions & 2 deletions packages/core/source/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ export class CommandLineInterface {
// Create the application we will work with
this.#app = ApplicationFactory.make(new Container(), package_);

// Check for updates
await this.#app.get<CliContracts.Updater>(Identifiers.Updater).check();
// Check for updates and log status
await this.#app.get<CliContracts.Updater>(Identifiers.Updater).logStatus();

// Parse arguments and flags
const { args, flags } = InputParser.parseArgv(this.argv);
Expand Down
3 changes: 2 additions & 1 deletion packages/core/source/commands/config-cli.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Commands, Identifiers, Services } from "@mainsail/cli";
import { inject, injectable } from "@mainsail/container";
import { Constants } from "@mainsail/contracts";
import { Utils } from "@mainsail/kernel";
import Joi from "joi";

Expand All @@ -20,7 +21,7 @@ export class Command extends Commands.Command {
.setFlag(
"channel",
"The NPM registry channel that should be used.",
Joi.string().valid("alpha", "next", "latest"),
Joi.string().valid(...Constants.Channels),
);
}

Expand Down

0 comments on commit 67dfac0

Please sign in to comment.