mirror of
https://github.com/ksyasuda/SubMiner.git
synced 2026-05-27 12:55:20 -07:00
feat: add auto update support (#65)
This commit is contained in:
@@ -1,10 +1,27 @@
|
||||
import fs from 'node:fs';
|
||||
import os from 'node:os';
|
||||
import path from 'node:path';
|
||||
import packageJson from '../../package.json';
|
||||
import { runAppCommandCaptureOutput } from '../mpv.js';
|
||||
import { log as launcherLog } from '../log.js';
|
||||
import { nowMs } from '../time.js';
|
||||
import { sleep } from '../util.js';
|
||||
import type { LauncherCommandContext } from './context.js';
|
||||
import { readLauncherMainConfigObject } from '../config/shared-config-reader.js';
|
||||
import type { UpdateChannel } from '../../src/types/config.js';
|
||||
import { updateAppImageFromRelease } from '../../src/main/runtime/update/appimage-updater.js';
|
||||
import { updateLauncherFromRelease } from '../../src/main/runtime/update/launcher-updater.js';
|
||||
import {
|
||||
compareSemverLike,
|
||||
fetchLatestStableRelease,
|
||||
fetchReleaseAssetBuffer,
|
||||
fetchReleaseAssetText,
|
||||
findReleaseAsset,
|
||||
parseReleaseVersion,
|
||||
parseSha256Sums,
|
||||
type FetchLike,
|
||||
} from '../../src/main/runtime/update/release-assets.js';
|
||||
import { updateSupportAssetsFromRelease } from '../../src/main/runtime/update/support-assets.js';
|
||||
|
||||
type UpdateCommandResponse = {
|
||||
ok: boolean;
|
||||
@@ -13,6 +30,18 @@ type UpdateCommandResponse = {
|
||||
error?: string;
|
||||
};
|
||||
|
||||
type DirectReleaseUpdateRequest = {
|
||||
appPath: string;
|
||||
launcherPath: string;
|
||||
channel: UpdateChannel;
|
||||
};
|
||||
|
||||
type DirectReleaseUpdateResult = {
|
||||
appImage: { status: string; command?: string; message?: string };
|
||||
launcher: { status: string; command?: string; message?: string };
|
||||
supportAssets: Array<{ status: string; command?: string; message?: string }>;
|
||||
};
|
||||
|
||||
type UpdateCommandDeps = {
|
||||
createTempDir: (prefix: string) => string;
|
||||
joinPath: (...parts: string[]) => string;
|
||||
@@ -22,9 +51,95 @@ type UpdateCommandDeps = {
|
||||
) => { status: number; stdout: string; stderr: string; error?: Error };
|
||||
waitForUpdateResponse: (responsePath: string) => Promise<UpdateCommandResponse>;
|
||||
removeDir: (targetPath: string) => void;
|
||||
runDirectReleaseUpdate: (
|
||||
request: DirectReleaseUpdateRequest,
|
||||
) => Promise<DirectReleaseUpdateResult>;
|
||||
readMainConfig: () => Record<string, unknown> | null;
|
||||
log: typeof launcherLog;
|
||||
};
|
||||
|
||||
const UPDATE_RESPONSE_TIMEOUT_MS = 10 * 60 * 1000;
|
||||
const CURRENT_VERSION = packageJson.version;
|
||||
|
||||
function getFetchForLauncherUpdater(): FetchLike {
|
||||
return globalThis.fetch.bind(globalThis) as FetchLike;
|
||||
}
|
||||
|
||||
async function runDirectReleaseUpdate(
|
||||
request: DirectReleaseUpdateRequest,
|
||||
): Promise<DirectReleaseUpdateResult> {
|
||||
const fetchForUpdater = getFetchForLauncherUpdater();
|
||||
const release = await fetchLatestStableRelease({
|
||||
fetch: fetchForUpdater,
|
||||
channel: request.channel,
|
||||
});
|
||||
const releaseVersion = parseReleaseVersion(release);
|
||||
if (releaseVersion && compareSemverLike(releaseVersion, CURRENT_VERSION) <= 0) {
|
||||
return {
|
||||
appImage: { status: 'up-to-date' },
|
||||
launcher: { status: 'up-to-date' },
|
||||
supportAssets: [{ status: 'up-to-date' }],
|
||||
};
|
||||
}
|
||||
|
||||
const sumsAsset = release ? findReleaseAsset(release, 'SHA256SUMS.txt') : null;
|
||||
const sha256Sums =
|
||||
sumsAsset && release
|
||||
? parseSha256Sums(
|
||||
await fetchReleaseAssetText(fetchForUpdater, sumsAsset.browser_download_url),
|
||||
)
|
||||
: new Map<string, string>();
|
||||
const downloadAsset = (url: string) => fetchReleaseAssetBuffer(fetchForUpdater, url);
|
||||
|
||||
const [appImage, launcher, supportAssets] = await Promise.all([
|
||||
updateAppImageFromRelease({
|
||||
release,
|
||||
sha256Sums,
|
||||
appImagePath: request.appPath,
|
||||
downloadAsset,
|
||||
}),
|
||||
updateLauncherFromRelease({
|
||||
release,
|
||||
sha256Sums,
|
||||
launcherPath: request.launcherPath,
|
||||
downloadAsset,
|
||||
}),
|
||||
updateSupportAssetsFromRelease({
|
||||
release,
|
||||
sha256Sums,
|
||||
downloadAsset,
|
||||
}),
|
||||
]);
|
||||
|
||||
return { appImage, launcher, supportAssets };
|
||||
}
|
||||
|
||||
function readUpdateChannel(root: Record<string, unknown> | null): UpdateChannel {
|
||||
const updates =
|
||||
root?.updates && typeof root.updates === 'object' && !Array.isArray(root.updates)
|
||||
? (root.updates as Record<string, unknown>)
|
||||
: null;
|
||||
return updates?.channel === 'prerelease' ? 'prerelease' : 'stable';
|
||||
}
|
||||
|
||||
function logUpdateResult(
|
||||
label: string,
|
||||
result: { status: string; command?: string; message?: string },
|
||||
configuredLogLevel: NonNullable<LauncherCommandContext['args']['logLevel']>,
|
||||
deps: Pick<UpdateCommandDeps, 'log'>,
|
||||
): void {
|
||||
const displayStatus = result.status === 'up-to-date' ? 'up to date' : result.status;
|
||||
deps.log('info', configuredLogLevel, `${label} update: ${displayStatus}`);
|
||||
if (result.command) {
|
||||
deps.log(
|
||||
'warn',
|
||||
configuredLogLevel,
|
||||
`${label} update requires manual command: ${result.command}`,
|
||||
);
|
||||
} else if (result.message) {
|
||||
deps.log('warn', configuredLogLevel, `${label} update note: ${result.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
const defaultDeps: UpdateCommandDeps = {
|
||||
createTempDir: (prefix) => fs.mkdtempSync(path.join(os.tmpdir(), prefix)),
|
||||
@@ -47,6 +162,9 @@ const defaultDeps: UpdateCommandDeps = {
|
||||
removeDir: (targetPath) => {
|
||||
fs.rmSync(targetPath, { recursive: true, force: true });
|
||||
},
|
||||
runDirectReleaseUpdate,
|
||||
readMainConfig: readLauncherMainConfigObject,
|
||||
log: launcherLog,
|
||||
};
|
||||
|
||||
export async function runUpdateCommand(
|
||||
@@ -59,6 +177,21 @@ export async function runUpdateCommand(
|
||||
return false;
|
||||
}
|
||||
|
||||
if (context.processAdapter.platform() === 'linux') {
|
||||
const result = await resolvedDeps.runDirectReleaseUpdate({
|
||||
appPath,
|
||||
launcherPath: scriptPath,
|
||||
channel: readUpdateChannel(resolvedDeps.readMainConfig()),
|
||||
});
|
||||
const logLevel = args.logLevel ?? 'warn';
|
||||
logUpdateResult('AppImage', result.appImage, logLevel, resolvedDeps);
|
||||
logUpdateResult('Launcher', result.launcher, logLevel, resolvedDeps);
|
||||
for (const supportResult of result.supportAssets) {
|
||||
logUpdateResult('Rofi theme', supportResult, logLevel, resolvedDeps);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
const tempDir = resolvedDeps.createTempDir('subminer-update-');
|
||||
const responsePath = resolvedDeps.joinPath(tempDir, 'response.json');
|
||||
|
||||
|
||||
Reference in New Issue
Block a user