mirror of
https://github.com/ksyasuda/SubMiner.git
synced 2026-04-10 16:19:24 -07:00
Fix Windows background app reuse across queued media
This commit is contained in:
@@ -51,6 +51,7 @@ function runLauncher(argv: string[], env: NodeJS.ProcessEnv): RunResult {
|
||||
}
|
||||
|
||||
function makeTestEnv(homeDir: string, xdgConfigHome: string): NodeJS.ProcessEnv {
|
||||
const pathValue = process.env.Path || process.env.PATH || '';
|
||||
return {
|
||||
...process.env,
|
||||
HOME: homeDir,
|
||||
@@ -58,6 +59,8 @@ function makeTestEnv(homeDir: string, xdgConfigHome: string): NodeJS.ProcessEnv
|
||||
APPDATA: xdgConfigHome,
|
||||
LOCALAPPDATA: path.join(homeDir, 'AppData', 'Local'),
|
||||
XDG_CONFIG_HOME: xdgConfigHome,
|
||||
PATH: pathValue,
|
||||
Path: pathValue,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -142,6 +145,12 @@ test('mpv status exits non-zero when socket is not ready', () => {
|
||||
withTempDir((root) => {
|
||||
const homeDir = path.join(root, 'home');
|
||||
const xdgConfigHome = path.join(root, 'xdg');
|
||||
const socketPath = path.join(root, 'missing.sock');
|
||||
fs.mkdirSync(path.join(xdgConfigHome, 'mpv', 'script-opts'), { recursive: true });
|
||||
fs.writeFileSync(
|
||||
path.join(xdgConfigHome, 'mpv', 'script-opts', 'subminer.conf'),
|
||||
`socket_path=${socketPath}\n`,
|
||||
);
|
||||
const result = runLauncher(['mpv', 'status'], makeTestEnv(homeDir, xdgConfigHome));
|
||||
|
||||
assert.equal(result.status, 1);
|
||||
@@ -156,6 +165,7 @@ test('doctor reports checks and exits non-zero without hard dependencies', () =>
|
||||
const env = {
|
||||
...makeTestEnv(homeDir, xdgConfigHome),
|
||||
PATH: '',
|
||||
Path: '',
|
||||
};
|
||||
const result = runLauncher(['doctor'], env);
|
||||
|
||||
@@ -188,7 +198,7 @@ test('youtube command rejects removed --mode option', () => {
|
||||
});
|
||||
});
|
||||
|
||||
test('youtube playback generates subtitles before mpv launch', () => {
|
||||
test('youtube playback generates subtitles before mpv launch', { timeout: 15000 }, () => {
|
||||
withTempDir((root) => {
|
||||
const homeDir = path.join(root, 'home');
|
||||
const xdgConfigHome = path.join(root, 'xdg');
|
||||
@@ -198,6 +208,7 @@ test('youtube playback generates subtitles before mpv launch', () => {
|
||||
const mpvCapturePath = path.join(root, 'mpv-order.txt');
|
||||
const mpvArgsPath = path.join(root, 'mpv-args.txt');
|
||||
const socketPath = path.join(root, 'mpv.sock');
|
||||
const bunBinary = JSON.stringify(process.execPath.replace(/\\/g, '/'));
|
||||
|
||||
fs.mkdirSync(binDir, { recursive: true });
|
||||
fs.mkdirSync(path.join(xdgConfigHome, 'SubMiner'), { recursive: true });
|
||||
@@ -268,7 +279,7 @@ for arg in "$@"; do
|
||||
;;
|
||||
esac
|
||||
done
|
||||
bun -e "const net=require('node:net'); const fs=require('node:fs'); const socket=process.argv[1]; try { fs.rmSync(socket,{force:true}); } catch {} const server=net.createServer((conn)=>conn.end()); server.listen(socket,()=>setTimeout(()=>server.close(()=>process.exit(0)),250));" "$socket_path"
|
||||
${bunBinary} -e "const net=require('node:net'); const fs=require('node:fs'); const socket=process.argv[1]; try { fs.rmSync(socket,{force:true}); } catch {} const server=net.createServer((conn)=>conn.end()); server.listen(socket,()=>setTimeout(()=>server.close(()=>process.exit(0)),250));" "$socket_path"
|
||||
`,
|
||||
'utf8',
|
||||
);
|
||||
@@ -276,7 +287,8 @@ bun -e "const net=require('node:net'); const fs=require('node:fs'); const socket
|
||||
|
||||
const env = {
|
||||
...makeTestEnv(homeDir, xdgConfigHome),
|
||||
PATH: `${binDir}${path.delimiter}${process.env.PATH || ''}`,
|
||||
PATH: `${binDir}${path.delimiter}${process.env.Path || process.env.PATH || ''}`,
|
||||
Path: `${binDir}${path.delimiter}${process.env.Path || process.env.PATH || ''}`,
|
||||
SUBMINER_APPIMAGE_PATH: appPath,
|
||||
SUBMINER_TEST_YTDLP_LOG: ytdlpLogPath,
|
||||
SUBMINER_TEST_MPV_ORDER: mpvCapturePath,
|
||||
@@ -284,7 +296,7 @@ bun -e "const net=require('node:net'); const fs=require('node:fs'); const socket
|
||||
};
|
||||
const result = runLauncher(['youtube', 'https://www.youtube.com/watch?v=test123'], env);
|
||||
|
||||
assert.equal(result.status, 0);
|
||||
assert.equal(result.status, 0, `stdout:\n${result.stdout}\nstderr:\n${result.stderr}`);
|
||||
assert.equal(fs.readFileSync(mpvCapturePath, 'utf8').trim(), 'generated-before-mpv');
|
||||
assert.match(
|
||||
fs.readFileSync(mpvArgsPath, 'utf8'),
|
||||
|
||||
@@ -9,8 +9,10 @@ import { log, fail, getMpvLogPath } from './log.js';
|
||||
import { buildSubminerScriptOpts, resolveAniSkipMetadataForFile } from './aniskip-metadata.js';
|
||||
import {
|
||||
commandExists,
|
||||
getPathEnv,
|
||||
isExecutable,
|
||||
resolveBinaryPathCandidate,
|
||||
resolveCommandInvocation,
|
||||
realpathMaybe,
|
||||
isYoutubeTarget,
|
||||
uniqueNormalizedLangCodes,
|
||||
@@ -204,7 +206,8 @@ export function findAppBinary(selfPath: string): string | null {
|
||||
if (isExecutable(candidate)) return candidate;
|
||||
}
|
||||
|
||||
const fromPath = process.env.PATH?.split(path.delimiter)
|
||||
const fromPath = getPathEnv()
|
||||
.split(path.delimiter)
|
||||
.map((dir) => path.join(dir, 'subminer'))
|
||||
.find((candidate) => isExecutable(candidate));
|
||||
|
||||
@@ -517,7 +520,8 @@ export async function startMpv(
|
||||
mpvArgs.push(`--input-ipc-server=${socketPath}`);
|
||||
mpvArgs.push(target);
|
||||
|
||||
state.mpvProc = spawn('mpv', mpvArgs, { stdio: 'inherit' });
|
||||
const mpvTarget = resolveCommandInvocation('mpv', mpvArgs);
|
||||
state.mpvProc = spawn(mpvTarget.command, mpvTarget.args, { stdio: 'inherit' });
|
||||
}
|
||||
|
||||
async function waitForOverlayStartCommandSettled(
|
||||
@@ -568,7 +572,8 @@ export async function startOverlay(appPath: string, args: Args, socketPath: stri
|
||||
if (args.logLevel !== 'info') overlayArgs.push('--log-level', args.logLevel);
|
||||
if (args.useTexthooker) overlayArgs.push('--texthooker');
|
||||
|
||||
state.overlayProc = spawn(appPath, overlayArgs, {
|
||||
const target = resolveAppSpawnTarget(appPath, overlayArgs);
|
||||
state.overlayProc = spawn(target.command, target.args, {
|
||||
stdio: 'inherit',
|
||||
env: { ...process.env, SUBMINER_MPV_LOG: getMpvLogPath() },
|
||||
});
|
||||
@@ -701,33 +706,7 @@ function resolveAppSpawnTarget(appPath: string, appArgs: string[]): SpawnTarget
|
||||
if (process.platform !== 'win32') {
|
||||
return { command: appPath, args: appArgs };
|
||||
}
|
||||
|
||||
const normalizeBashArg = (value: string): string => {
|
||||
const normalized = value.replace(/\\/g, '/');
|
||||
const driveMatch = normalized.match(/^([A-Za-z]):\/(.*)$/);
|
||||
if (!driveMatch) {
|
||||
return normalized;
|
||||
}
|
||||
|
||||
const [, driveLetter, remainder] = driveMatch;
|
||||
return `/mnt/${driveLetter!.toLowerCase()}/${remainder}`;
|
||||
};
|
||||
const extension = path.extname(appPath).toLowerCase();
|
||||
if (extension === '.ps1') {
|
||||
return {
|
||||
command: 'powershell.exe',
|
||||
args: ['-NoProfile', '-ExecutionPolicy', 'Bypass', '-File', appPath, ...appArgs],
|
||||
};
|
||||
}
|
||||
|
||||
if (extension === '.sh') {
|
||||
return {
|
||||
command: 'bash',
|
||||
args: [normalizeBashArg(appPath), ...appArgs.map(normalizeBashArg)],
|
||||
};
|
||||
}
|
||||
|
||||
return { command: appPath, args: appArgs };
|
||||
return resolveCommandInvocation(appPath, appArgs);
|
||||
}
|
||||
|
||||
export function runAppCommandWithInherit(appPath: string, appArgs: string[]): never {
|
||||
@@ -841,7 +820,8 @@ export function launchMpvIdleDetached(
|
||||
);
|
||||
mpvArgs.push(`--log-file=${getMpvLogPath()}`);
|
||||
mpvArgs.push(`--input-ipc-server=${socketPath}`);
|
||||
const proc = spawn('mpv', mpvArgs, {
|
||||
const mpvTarget = resolveCommandInvocation('mpv', mpvArgs);
|
||||
const proc = spawn(mpvTarget.command, mpvTarget.args, {
|
||||
stdio: 'ignore',
|
||||
detached: true,
|
||||
});
|
||||
|
||||
191
launcher/util.ts
191
launcher/util.ts
@@ -18,14 +18,139 @@ export function isExecutable(filePath: string): boolean {
|
||||
}
|
||||
}
|
||||
|
||||
export function commandExists(command: string): boolean {
|
||||
const pathEnv = process.env.PATH ?? '';
|
||||
function isRunnableFile(filePath: string): boolean {
|
||||
try {
|
||||
if (!fs.statSync(filePath).isFile()) return false;
|
||||
return process.platform === 'win32' ? true : isExecutable(filePath);
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
function isPathLikeCommand(command: string): boolean {
|
||||
return (
|
||||
command.includes('/') ||
|
||||
command.includes('\\') ||
|
||||
/^[A-Za-z]:[\\/]/.test(command) ||
|
||||
command.startsWith('.')
|
||||
);
|
||||
}
|
||||
|
||||
function getWindowsPathExts(): string[] {
|
||||
const raw = process.env.PATHEXT ?? '.COM;.EXE;.BAT;.CMD';
|
||||
return raw
|
||||
.split(';')
|
||||
.map((entry) => entry.trim())
|
||||
.filter((entry) => entry.length > 0);
|
||||
}
|
||||
|
||||
export function getPathEnv(): string {
|
||||
const pathKey = Object.keys(process.env).find((key) => key.toLowerCase() === 'path');
|
||||
return pathKey ? (process.env[pathKey] ?? '') : '';
|
||||
}
|
||||
|
||||
function resolveExecutablePath(command: string): string | null {
|
||||
const tryCandidate = (candidate: string): string | null =>
|
||||
isRunnableFile(candidate) ? candidate : null;
|
||||
|
||||
const resolveWindowsCandidate = (candidate: string): string | null => {
|
||||
const direct = tryCandidate(candidate);
|
||||
if (direct) return direct;
|
||||
if (path.extname(candidate)) return null;
|
||||
for (const ext of getWindowsPathExts()) {
|
||||
const withExt = tryCandidate(`${candidate}${ext}`);
|
||||
if (withExt) return withExt;
|
||||
}
|
||||
return null;
|
||||
};
|
||||
|
||||
if (isPathLikeCommand(command)) {
|
||||
const resolved = path.resolve(resolvePathMaybe(command));
|
||||
return process.platform === 'win32' ? resolveWindowsCandidate(resolved) : tryCandidate(resolved);
|
||||
}
|
||||
|
||||
const pathEnv = getPathEnv();
|
||||
for (const dir of pathEnv.split(path.delimiter)) {
|
||||
if (!dir) continue;
|
||||
const full = path.join(dir, command);
|
||||
if (isExecutable(full)) return true;
|
||||
const candidate = path.join(dir, command);
|
||||
const resolved =
|
||||
process.platform === 'win32' ? resolveWindowsCandidate(candidate) : tryCandidate(candidate);
|
||||
if (resolved) return resolved;
|
||||
}
|
||||
return false;
|
||||
return null;
|
||||
}
|
||||
|
||||
function normalizeWindowsBashArg(value: string): string {
|
||||
const normalized = value.replace(/\\/g, '/');
|
||||
const driveMatch = normalized.match(/^([A-Za-z]):\/(.*)$/);
|
||||
if (!driveMatch) {
|
||||
return normalized;
|
||||
}
|
||||
|
||||
const [, driveLetter, remainder] = driveMatch;
|
||||
return `/mnt/${driveLetter!.toLowerCase()}/${remainder}`;
|
||||
}
|
||||
|
||||
function resolveGitBashExecutable(): string | null {
|
||||
const directCandidates = [
|
||||
'C:\\Program Files\\Git\\bin\\bash.exe',
|
||||
'C:\\Program Files\\Git\\usr\\bin\\bash.exe',
|
||||
];
|
||||
for (const candidate of directCandidates) {
|
||||
if (isRunnableFile(candidate)) return candidate;
|
||||
}
|
||||
|
||||
const gitExecutable = resolveExecutablePath('git');
|
||||
if (!gitExecutable) return null;
|
||||
const gitDir = path.dirname(gitExecutable);
|
||||
const inferredCandidates = [
|
||||
path.resolve(gitDir, '..', 'bin', 'bash.exe'),
|
||||
path.resolve(gitDir, '..', 'usr', 'bin', 'bash.exe'),
|
||||
];
|
||||
for (const candidate of inferredCandidates) {
|
||||
if (isRunnableFile(candidate)) return candidate;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function resolveWindowsBashTarget(): {
|
||||
command: string;
|
||||
flavor: 'git' | 'wsl';
|
||||
} {
|
||||
const gitBash = resolveGitBashExecutable();
|
||||
if (gitBash) {
|
||||
return { command: gitBash, flavor: 'git' };
|
||||
}
|
||||
return {
|
||||
command: resolveExecutablePath('bash') ?? 'bash',
|
||||
flavor: 'wsl',
|
||||
};
|
||||
}
|
||||
|
||||
function normalizeWindowsShellArg(value: string, flavor: 'git' | 'wsl'): string {
|
||||
if (!isPathLikeCommand(value)) {
|
||||
return value;
|
||||
}
|
||||
return flavor === 'git' ? value.replace(/\\/g, '/') : normalizeWindowsBashArg(value);
|
||||
}
|
||||
|
||||
function readShebang(filePath: string): string {
|
||||
try {
|
||||
const fd = fs.openSync(filePath, 'r');
|
||||
try {
|
||||
const buffer = Buffer.alloc(160);
|
||||
const bytesRead = fs.readSync(fd, buffer, 0, buffer.length, 0);
|
||||
return buffer.toString('utf8', 0, bytesRead).split(/\r?\n/, 1)[0] ?? '';
|
||||
} finally {
|
||||
fs.closeSync(fd);
|
||||
}
|
||||
} catch {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
export function commandExists(command: string): boolean {
|
||||
return resolveExecutablePath(command) !== null;
|
||||
}
|
||||
|
||||
export function resolvePathMaybe(input: string): string {
|
||||
@@ -116,6 +241,51 @@ export function inferWhisperLanguage(langCodes: string[], fallback: string): str
|
||||
return fallback;
|
||||
}
|
||||
|
||||
export function resolveCommandInvocation(
|
||||
executable: string,
|
||||
args: string[],
|
||||
): { command: string; args: string[] } {
|
||||
if (process.platform !== 'win32') {
|
||||
return { command: executable, args };
|
||||
}
|
||||
|
||||
const resolvedExecutable = resolveExecutablePath(executable) ?? executable;
|
||||
const extension = path.extname(resolvedExecutable).toLowerCase();
|
||||
if (extension === '.ps1') {
|
||||
return {
|
||||
command: 'powershell.exe',
|
||||
args: ['-NoProfile', '-ExecutionPolicy', 'Bypass', '-File', resolvedExecutable, ...args],
|
||||
};
|
||||
}
|
||||
|
||||
if (extension === '.sh') {
|
||||
const bashTarget = resolveWindowsBashTarget();
|
||||
return {
|
||||
command: bashTarget.command,
|
||||
args: [
|
||||
normalizeWindowsShellArg(resolvedExecutable, bashTarget.flavor),
|
||||
...args.map((arg) => normalizeWindowsShellArg(arg, bashTarget.flavor)),
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
if (!extension) {
|
||||
const shebang = readShebang(resolvedExecutable);
|
||||
if (/^#!.*\b(?:sh|bash)\b/i.test(shebang)) {
|
||||
const bashTarget = resolveWindowsBashTarget();
|
||||
return {
|
||||
command: bashTarget.command,
|
||||
args: [
|
||||
normalizeWindowsShellArg(resolvedExecutable, bashTarget.flavor),
|
||||
...args.map((arg) => normalizeWindowsShellArg(arg, bashTarget.flavor)),
|
||||
],
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
return { command: resolvedExecutable, args };
|
||||
}
|
||||
|
||||
export function runExternalCommand(
|
||||
executable: string,
|
||||
args: string[],
|
||||
@@ -129,8 +299,13 @@ export function runExternalCommand(
|
||||
const streamOutput = opts.streamOutput === true;
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
log('debug', configuredLogLevel, `[${commandLabel}] spawn: ${executable} ${args.join(' ')}`);
|
||||
const child = spawn(executable, args, {
|
||||
const target = resolveCommandInvocation(executable, args);
|
||||
log(
|
||||
'debug',
|
||||
configuredLogLevel,
|
||||
`[${commandLabel}] spawn: ${target.command} ${target.args.join(' ')}`,
|
||||
);
|
||||
const child = spawn(target.command, target.args, {
|
||||
stdio: ['ignore', 'pipe', 'pipe'],
|
||||
env: { ...process.env, ...opts.env },
|
||||
});
|
||||
@@ -201,7 +376,7 @@ export function runExternalCommand(
|
||||
`[${commandLabel}] exit code ${code ?? 1}`,
|
||||
);
|
||||
if (code !== 0 && !allowFailure) {
|
||||
const commandString = `${executable} ${args.join(' ')}`;
|
||||
const commandString = `${target.command} ${target.args.join(' ')}`;
|
||||
reject(
|
||||
new Error(`Command failed (${commandString}): ${stderr.trim() || `exit code ${code}`}`),
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user