Skip to content

Commit

Permalink
feat(cli): add force flag on Updater.check (#416)
Browse files Browse the repository at this point in the history
* always check for updates when calling directly from update command

* store last seen version

* simplify

* style: resolve style guide violations

---------

Co-authored-by: oXtxNt9U <[email protected]>
  • Loading branch information
oXtxNt9U and oXtxNt9U authored Feb 1, 2024
1 parent 13d18c7 commit 4d8b392
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
2 changes: 1 addition & 1 deletion packages/cli/source/contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export interface Config {
}

export interface Updater {
check(): Promise<boolean>;
check(force?: boolean): Promise<boolean>;

update(updateProcessManager?: boolean, force?: boolean): Promise<boolean>;

Expand Down
22 changes: 12 additions & 10 deletions packages/cli/source/services/updater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { inject, injectable } from "@mainsail/container";
import { Contracts } from "@mainsail/contracts";
import { dim, green, reset } from "kleur";
import latestVersion from "latest-version";
import { lte } from "semver";
import { lt, lte } from "semver";

import { Application } from "../application";
import { Confirm, Spinner, Warning } from "../components";
Expand All @@ -11,7 +11,7 @@ import { Identifiers } from "../ioc";
import { Installer } from "./installer";
import { ProcessManager } from "./process-manager";

const ONE_DAY = 1000 * 60 * 60 * 24;
const ONE_HOUR = 1000 * 60 * 60;

@injectable()
export class Updater implements Contracts_Updater {
Expand All @@ -30,26 +30,28 @@ export class Updater implements Contracts_Updater {
@inject(Identifiers.ProcessManager)
private readonly processManager!: ProcessManager;

#updateCheckInterval: any = ONE_DAY;
#updateCheckInterval: number = ONE_HOUR;

#latestVersion: string | undefined;

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

if (this.#latestVersion) {
this.config.forget("latestVersion"); // ? shouldn't it be moved after lastUpdateCheck
}

if (Date.now() - this.config.get<number>("lastUpdateCheck") < this.#updateCheckInterval) {
return false;
if (
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();

this.config.set("lastUpdateCheck", Date.now());

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

Expand Down
2 changes: 1 addition & 1 deletion packages/core/source/commands/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class Command extends Commands.Command {
}

public async execute(): Promise<void> {
const hasNewVersion: boolean = await this.updater.check();
const hasNewVersion: boolean = await this.updater.check(true);

if (hasNewVersion) {
await this.updater.update(this.getFlag("updateProcessManager"), this.getFlag("force"));
Expand Down

0 comments on commit 4d8b392

Please sign in to comment.