mirror of
https://github.com/ksyasuda/SubMiner.git
synced 2026-05-15 08:12:53 -07:00
feat: add auto update support
This commit is contained in:
@@ -8,6 +8,7 @@ import { runDictionaryCommand } from './dictionary-command.js';
|
||||
import { runDoctorCommand } from './doctor-command.js';
|
||||
import { runMpvPreAppCommand } from './mpv-command.js';
|
||||
import { runStatsCommand } from './stats-command.js';
|
||||
import { runUpdateCommand } from './update-command.js';
|
||||
|
||||
class ExitSignal extends Error {
|
||||
code: number;
|
||||
@@ -240,6 +241,38 @@ test('dictionary command returns after app handoff starts', () => {
|
||||
assert.equal(handled, true);
|
||||
});
|
||||
|
||||
test('update command forwards launcher path and waits for response', async () => {
|
||||
const context = createContext();
|
||||
context.args.update = true;
|
||||
const forwarded: string[][] = [];
|
||||
const responses: string[] = [];
|
||||
|
||||
const handled = await runUpdateCommand(context, {
|
||||
createTempDir: () => '/tmp/subminer-update-test',
|
||||
joinPath: (...parts) => parts.join('/'),
|
||||
runAppCommandCaptureOutput: (_appPath, appArgs) => {
|
||||
forwarded.push(appArgs);
|
||||
return { status: 0, stdout: '', stderr: '' };
|
||||
},
|
||||
waitForUpdateResponse: async (responsePath) => {
|
||||
responses.push(responsePath);
|
||||
return { ok: true, status: 'up-to-date', version: '0.15.0' };
|
||||
},
|
||||
});
|
||||
|
||||
assert.equal(handled, true);
|
||||
assert.deepEqual(forwarded, [
|
||||
[
|
||||
'--update',
|
||||
'--update-launcher-path',
|
||||
'/tmp/subminer',
|
||||
'--update-response-path',
|
||||
'/tmp/subminer-update-test/response.json',
|
||||
],
|
||||
]);
|
||||
assert.deepEqual(responses, ['/tmp/subminer-update-test/response.json']);
|
||||
});
|
||||
|
||||
test('stats command launches attached app command with response path', async () => {
|
||||
const harness = createStatsTestHarness({ stats: true, logLevel: 'debug' });
|
||||
const handled = await runStatsCommand(harness.context, harness.commandDeps);
|
||||
|
||||
@@ -0,0 +1,87 @@
|
||||
import fs from 'node:fs';
|
||||
import os from 'node:os';
|
||||
import path from 'node:path';
|
||||
import { runAppCommandCaptureOutput } from '../mpv.js';
|
||||
import { nowMs } from '../time.js';
|
||||
import { sleep } from '../util.js';
|
||||
import type { LauncherCommandContext } from './context.js';
|
||||
|
||||
type UpdateCommandResponse = {
|
||||
ok: boolean;
|
||||
status?: string;
|
||||
version?: string;
|
||||
error?: string;
|
||||
};
|
||||
|
||||
type UpdateCommandDeps = {
|
||||
createTempDir: (prefix: string) => string;
|
||||
joinPath: (...parts: string[]) => string;
|
||||
runAppCommandCaptureOutput: (
|
||||
appPath: string,
|
||||
appArgs: string[],
|
||||
) => { status: number; stdout: string; stderr: string; error?: Error };
|
||||
waitForUpdateResponse: (responsePath: string) => Promise<UpdateCommandResponse>;
|
||||
removeDir: (targetPath: string) => void;
|
||||
};
|
||||
|
||||
const UPDATE_RESPONSE_TIMEOUT_MS = 10 * 60 * 1000;
|
||||
|
||||
const defaultDeps: UpdateCommandDeps = {
|
||||
createTempDir: (prefix) => fs.mkdtempSync(path.join(os.tmpdir(), prefix)),
|
||||
joinPath: (...parts) => path.join(...parts),
|
||||
runAppCommandCaptureOutput: (appPath, appArgs) => runAppCommandCaptureOutput(appPath, appArgs),
|
||||
waitForUpdateResponse: async (responsePath) => {
|
||||
const deadline = nowMs() + UPDATE_RESPONSE_TIMEOUT_MS;
|
||||
while (nowMs() < deadline) {
|
||||
try {
|
||||
if (fs.existsSync(responsePath)) {
|
||||
return JSON.parse(fs.readFileSync(responsePath, 'utf8')) as UpdateCommandResponse;
|
||||
}
|
||||
} catch {
|
||||
// retry until timeout
|
||||
}
|
||||
await sleep(100);
|
||||
}
|
||||
return { ok: false, error: 'Timed out waiting for SubMiner update response.' };
|
||||
},
|
||||
removeDir: (targetPath) => {
|
||||
fs.rmSync(targetPath, { recursive: true, force: true });
|
||||
},
|
||||
};
|
||||
|
||||
export async function runUpdateCommand(
|
||||
context: LauncherCommandContext,
|
||||
deps: Partial<UpdateCommandDeps> = {},
|
||||
): Promise<boolean> {
|
||||
const resolvedDeps: UpdateCommandDeps = { ...defaultDeps, ...deps };
|
||||
const { args, appPath, scriptPath } = context;
|
||||
if (!args.update || !appPath) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const tempDir = resolvedDeps.createTempDir('subminer-update-');
|
||||
const responsePath = resolvedDeps.joinPath(tempDir, 'response.json');
|
||||
|
||||
try {
|
||||
const result = resolvedDeps.runAppCommandCaptureOutput(appPath, [
|
||||
'--update',
|
||||
'--update-launcher-path',
|
||||
scriptPath,
|
||||
'--update-response-path',
|
||||
responsePath,
|
||||
]);
|
||||
if (result.error) {
|
||||
throw result.error;
|
||||
}
|
||||
if (result.status !== 0) {
|
||||
throw new Error(`SubMiner update command exited with status ${result.status}.`);
|
||||
}
|
||||
const response = await resolvedDeps.waitForUpdateResponse(responsePath);
|
||||
if (!response.ok) {
|
||||
throw new Error(response.error || 'SubMiner update check failed.');
|
||||
}
|
||||
return true;
|
||||
} finally {
|
||||
resolvedDeps.removeDir(tempDir);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user