feat: Update check against pi.dev (#3877)
This commit is contained in:
@@ -7,9 +7,11 @@ import {
|
||||
getSelfUpdateCommand,
|
||||
getSelfUpdateUnavailableInstruction,
|
||||
PACKAGE_NAME,
|
||||
VERSION,
|
||||
} from "./config.js";
|
||||
import { DefaultPackageManager } from "./core/package-manager.js";
|
||||
import { SettingsManager } from "./core/settings-manager.js";
|
||||
import { getLatestPiVersion, isNewerPackageVersion } from "./utils/version-check.js";
|
||||
|
||||
export type PackageCommand = "install" | "remove" | "update" | "list";
|
||||
|
||||
@@ -20,6 +22,7 @@ interface PackageCommandOptions {
|
||||
source?: string;
|
||||
updateTarget?: UpdateTarget;
|
||||
local: boolean;
|
||||
force: boolean;
|
||||
help: boolean;
|
||||
invalidOption?: string;
|
||||
invalidArgument?: string;
|
||||
@@ -44,7 +47,7 @@ function getPackageCommandUsage(command: PackageCommand): string {
|
||||
case "remove":
|
||||
return `${APP_NAME} remove <source> [-l]`;
|
||||
case "update":
|
||||
return `${APP_NAME} update [source|self|pi] [--self] [--extensions] [--extension <source>]`;
|
||||
return `${APP_NAME} update [source|self|pi] [--self] [--extensions] [--extension <source>] [--force]`;
|
||||
case "list":
|
||||
return `${APP_NAME} list`;
|
||||
}
|
||||
@@ -97,6 +100,7 @@ Options:
|
||||
--self Update pi only
|
||||
--extensions Update installed packages only
|
||||
--extension <source> Update one package only
|
||||
--force Reinstall pi even if the current version is latest
|
||||
|
||||
Short forms:
|
||||
${APP_NAME} update Update pi and all extensions
|
||||
@@ -128,6 +132,7 @@ function parsePackageCommand(args: string[]): PackageCommandOptions | undefined
|
||||
}
|
||||
|
||||
let local = false;
|
||||
let force = false;
|
||||
let help = false;
|
||||
let invalidOption: string | undefined;
|
||||
let invalidArgument: string | undefined;
|
||||
@@ -172,6 +177,15 @@ function parsePackageCommand(args: string[]): PackageCommandOptions | undefined
|
||||
continue;
|
||||
}
|
||||
|
||||
if (arg === "--force") {
|
||||
if (command === "update") {
|
||||
force = true;
|
||||
} else {
|
||||
invalidOption = invalidOption ?? arg;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (arg === "--extension") {
|
||||
if (command !== "update") {
|
||||
invalidOption = invalidOption ?? arg;
|
||||
@@ -240,6 +254,7 @@ function parsePackageCommand(args: string[]): PackageCommandOptions | undefined
|
||||
source,
|
||||
updateTarget,
|
||||
local,
|
||||
force,
|
||||
help,
|
||||
invalidOption,
|
||||
invalidArgument,
|
||||
@@ -277,6 +292,26 @@ function printSelfUpdateFallback(): void {
|
||||
console.error(chalk.dim(`If this keeps failing, run this command yourself: ${command.display}`));
|
||||
}
|
||||
|
||||
async function shouldRunSelfUpdate(force: boolean): Promise<boolean> {
|
||||
if (force) {
|
||||
return true;
|
||||
}
|
||||
|
||||
let latestVersion: string | undefined;
|
||||
try {
|
||||
latestVersion = await getLatestPiVersion(VERSION);
|
||||
} catch {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!latestVersion || isNewerPackageVersion(latestVersion, VERSION)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
console.log(chalk.green(`${APP_NAME} is already up to date (v${VERSION})`));
|
||||
return false;
|
||||
}
|
||||
|
||||
async function runSelfUpdate(): Promise<void> {
|
||||
const command = getSelfUpdateCommand(PACKAGE_NAME);
|
||||
if (!command) {
|
||||
@@ -459,6 +494,9 @@ export async function handlePackageCommand(args: string[]): Promise<boolean> {
|
||||
}
|
||||
if (updateTargetIncludesSelf(target)) {
|
||||
if (canSelfUpdate()) {
|
||||
if (!(await shouldRunSelfUpdate(options.force))) {
|
||||
return true;
|
||||
}
|
||||
try {
|
||||
await runSelfUpdate();
|
||||
} catch (error: unknown) {
|
||||
|
||||
Reference in New Issue
Block a user