mirror of
https://github.com/ksyasuda/SubMiner.git
synced 2026-11-15 16:46:08 -08:00
feat(launcher): bundle Bun and use it across all launchers (#243)
This commit is contained in:
1 parent
fb2d1a512b
commit
016a43b804
61 files changed
+6814
-294
No files matched your search
@@ -1,6 +1,13 @@
|
||||
import fs from 'node:fs';
|
||||
import os from 'node:os';
|
||||
import path from 'node:path';
|
||||
import {
|
||||
cleanupOldWindowsManagedRuntimes,
|
||||
isManagedLauncher,
|
||||
managedLauncherContent,
|
||||
shellQuote,
|
||||
stageManagedLauncher,
|
||||
} from './managed-launcher';
|
||||
import {
|
||||
accessSyncOf,
|
||||
envOf,
|
||||
@@ -115,8 +122,13 @@ export function resolveBunInstallCommand(
|
||||
}
|
||||
|
||||
export async function detectBun(options: CommonOptions = {}): Promise<BunSnapshot> {
|
||||
const bunPath = findCommand('bun', options);
|
||||
const installCommand = resolveBunInstallCommand(options);
|
||||
const bundled = options.bundledBunPath;
|
||||
const bunPath = bundled
|
||||
? existsSyncOf(options)(bundled)
|
||||
? bundled
|
||||
: null
|
||||
: findCommand('bun', options);
|
||||
const installCommand = bundled ? null : resolveBunInstallCommand(options);
|
||||
if (!bunPath) {
|
||||
return {
|
||||
status: 'missing',
|
||||
@@ -124,7 +136,9 @@ export async function detectBun(options: CommonOptions = {}): Promise<BunSnapsho
|
||||
version: null,
|
||||
installMethod: installMethodForCommand(installCommand),
|
||||
installCommand,
|
||||
message: null,
|
||||
message: bundled
|
||||
? 'The included launcher runtime is missing. Reinstall SubMiner to repair it.'
|
||||
: null,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -139,7 +153,7 @@ export async function detectBun(options: CommonOptions = {}): Promise<BunSnapsho
|
||||
version: result.stdout.trim() || null,
|
||||
installMethod: null,
|
||||
installCommand: null,
|
||||
message: null,
|
||||
message: bundled ? 'Included with SubMiner. No separate Bun installation is needed.' : null,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -158,9 +172,11 @@ export function resolveLauncherResourcePath(options: CommonOptions): string {
|
||||
if (options.launcherResourcePath) return options.launcherResourcePath;
|
||||
const resourcesPath =
|
||||
options.resourcesPath ?? (process as typeof process & { resourcesPath?: string }).resourcesPath;
|
||||
const packaged = resourcesPath ? platformPath.join(resourcesPath, 'launcher', 'subminer') : null;
|
||||
const packaged = resourcesPath
|
||||
? platformPath.join(resourcesPath, 'launcher', 'subminer.js')
|
||||
: null;
|
||||
if (packaged && existsSyncOf(options)(packaged)) return packaged;
|
||||
return platformPath.join(options.cwd ?? process.cwd(), 'dist', 'launcher', 'subminer');
|
||||
return platformPath.join(options.cwd ?? process.cwd(), 'dist', 'launcher', 'subminer.js');
|
||||
}
|
||||
|
||||
function isWritableDir(candidate: string, options: CommonOptions): boolean {
|
||||
@@ -183,6 +199,21 @@ function collectPathDirs(options: CommonOptions): string[] {
|
||||
return dirs;
|
||||
}
|
||||
|
||||
function preferredLauncherDirs(platform: NodeJS.Platform, homeDir: string): string[] {
|
||||
return platform === 'darwin'
|
||||
? [
|
||||
'/opt/homebrew/bin',
|
||||
'/usr/local/bin',
|
||||
path.posix.join(homeDir, '.local', 'bin'),
|
||||
path.posix.join(homeDir, 'bin'),
|
||||
]
|
||||
: [
|
||||
path.posix.join(homeDir, '.local', 'bin'),
|
||||
path.posix.join(homeDir, 'bin'),
|
||||
'/usr/local/bin',
|
||||
];
|
||||
}
|
||||
|
||||
export async function resolveLauncherInstallTarget(
|
||||
options: CommonOptions & WindowsPathOptions = {},
|
||||
): Promise<LauncherSnapshot> {
|
||||
@@ -201,19 +232,7 @@ export async function resolveLauncherInstallTarget(
|
||||
|
||||
const homeDir = options.homeDir ?? os.homedir();
|
||||
const pathDirs = collectPathDirs(options);
|
||||
const preferred =
|
||||
platform === 'darwin'
|
||||
? [
|
||||
'/opt/homebrew/bin',
|
||||
'/usr/local/bin',
|
||||
path.posix.join(homeDir, '.local', 'bin'),
|
||||
path.posix.join(homeDir, 'bin'),
|
||||
]
|
||||
: [
|
||||
path.posix.join(homeDir, '.local', 'bin'),
|
||||
path.posix.join(homeDir, 'bin'),
|
||||
'/usr/local/bin',
|
||||
];
|
||||
const preferred = preferredLauncherDirs(platform, homeDir);
|
||||
const manualPreferred =
|
||||
platform === 'darwin'
|
||||
? [
|
||||
@@ -251,13 +270,15 @@ export async function resolveLauncherInstallTarget(
|
||||
isWritableDir(dir, options),
|
||||
);
|
||||
if (!selected) {
|
||||
const pathDir = path.posix.join(homeDir, '.local', 'bin');
|
||||
const installPath = path.posix.join(pathDir, 'subminer');
|
||||
return {
|
||||
status: 'not_installable',
|
||||
commandPath: null,
|
||||
installPath: null,
|
||||
pathDir: null,
|
||||
status: existsSyncOf(options)(installPath) ? 'not_on_path' : 'not_installed',
|
||||
commandPath: existsSyncOf(options)(installPath) ? installPath : null,
|
||||
installPath,
|
||||
pathDir,
|
||||
shadowedBy: null,
|
||||
message: 'No writable directory was found on your command-line PATH.',
|
||||
message: `Add ${pathDir} to your terminal PATH: export PATH=${shellQuote(pathDir)}:"$PATH". Save this in your shell configuration for future terminals.`,
|
||||
};
|
||||
}
|
||||
const installPath = path.posix.join(selected, 'subminer');
|
||||
@@ -283,7 +304,29 @@ export async function detectLauncher(
|
||||
const launcherResourcePath = resolveLauncherResourcePath(options);
|
||||
const appExePath = options.appExePath ?? process.execPath;
|
||||
|
||||
if (platform === 'win32' && existsSyncOf(options)(expectedPath)) {
|
||||
if (options.bundledBunPath && existsSyncOf(options)(expectedPath)) {
|
||||
const content = String((options.readFileSync ?? fs.readFileSync)(expectedPath, 'utf8'));
|
||||
if (!isManagedLauncher(content)) {
|
||||
return {
|
||||
...target,
|
||||
status: 'not_installed',
|
||||
message: 'Reinstall the launcher to use the runtime included with SubMiner.',
|
||||
};
|
||||
}
|
||||
if (
|
||||
content !==
|
||||
managedLauncherContent({
|
||||
platform,
|
||||
appPath: envOf(options).APPIMAGE ?? appExePath,
|
||||
})
|
||||
) {
|
||||
return {
|
||||
...target,
|
||||
status: 'not_installed',
|
||||
message: 'Reinstall the launcher to refresh its SubMiner location.',
|
||||
};
|
||||
}
|
||||
} else if (platform === 'win32' && existsSyncOf(options)(expectedPath)) {
|
||||
const content = String((options.readFileSync ?? fs.readFileSync)(expectedPath, 'utf8'));
|
||||
if (!shimMatchesCurrentInstall(content, appExePath, launcherResourcePath)) {
|
||||
return {
|
||||
@@ -305,26 +348,19 @@ export async function detectLauncher(
|
||||
}
|
||||
if (!existsSyncOf(options)(expectedPath))
|
||||
return { ...target, status: 'not_installed', commandPath: null };
|
||||
if (!commandPath) {
|
||||
return {
|
||||
...target,
|
||||
status: 'not_on_path',
|
||||
commandPath: expectedPath,
|
||||
message: 'Launcher exists but its directory is not on PATH.',
|
||||
};
|
||||
}
|
||||
|
||||
const bunSnapshot = options.bunSnapshot ?? (await detectBun(options));
|
||||
if (bunSnapshot.status !== 'ready') {
|
||||
return {
|
||||
...target,
|
||||
status: 'installed_bun_missing',
|
||||
commandPath,
|
||||
message: 'Launcher is installed, but Bun is missing. Install Bun, then open a new terminal.',
|
||||
message: options.bundledBunPath
|
||||
? bunSnapshot.message
|
||||
: 'Launcher is installed, but Bun is missing. Install Bun, then open a new terminal.',
|
||||
};
|
||||
}
|
||||
|
||||
const result = await getRunCommand(options)(commandPath, ['--help'], {
|
||||
const result = await getRunCommand(options)(expectedPath, ['--help'], {
|
||||
timeoutMs: COMMAND_TIMEOUT_MS,
|
||||
env: envOf(options) as NodeJS.ProcessEnv,
|
||||
});
|
||||
@@ -336,6 +372,16 @@ export async function detectLauncher(
|
||||
message: failureMessage(result, 'subminer --help failed'),
|
||||
};
|
||||
}
|
||||
if (!commandPath) {
|
||||
return {
|
||||
...target,
|
||||
status: 'not_on_path',
|
||||
commandPath: expectedPath,
|
||||
message:
|
||||
target.message ??
|
||||
`Launcher installed. Add ${target.pathDir} to your terminal PATH: export PATH=${shellQuote(target.pathDir ?? '')}:"$PATH". Save this in your shell configuration for future terminals.`,
|
||||
};
|
||||
}
|
||||
return { ...target, status: 'ready', commandPath, message: null };
|
||||
}
|
||||
|
||||
@@ -354,6 +400,48 @@ export async function installLauncher(
|
||||
};
|
||||
}
|
||||
|
||||
if (options.bundledBunPath) {
|
||||
const bun = await detectBun(options);
|
||||
if (bun.status !== 'ready')
|
||||
return {
|
||||
...target,
|
||||
status: 'failed',
|
||||
message: bun.message ?? 'The included launcher runtime failed to start.',
|
||||
};
|
||||
try {
|
||||
stageManagedLauncher({
|
||||
...options,
|
||||
bundledBunPath: options.bundledBunPath,
|
||||
launcherResourcePath,
|
||||
force: true,
|
||||
});
|
||||
(options.mkdirSync ?? fs.mkdirSync)(target.pathDir, { recursive: true });
|
||||
(options.writeFileSync ?? fs.writeFileSync)(
|
||||
target.installPath,
|
||||
managedLauncherContent({
|
||||
platform,
|
||||
appPath: envOf(options).APPIMAGE ?? options.appExePath ?? process.execPath,
|
||||
}),
|
||||
);
|
||||
(options.chmodSync ?? fs.chmodSync)(target.installPath, 0o755);
|
||||
if (platform === 'win32') {
|
||||
cleanupOldWindowsManagedRuntimes(options);
|
||||
const nextPath = await appendWindowsUserPathDir(target.pathDir, options);
|
||||
if (nextPath && options.env) {
|
||||
options.env.PATH = nextPath;
|
||||
options.env.Path = nextPath;
|
||||
}
|
||||
}
|
||||
return await detectLauncher({ ...options, bunSnapshot: bun });
|
||||
} catch (error) {
|
||||
return {
|
||||
...target,
|
||||
status: 'failed',
|
||||
message: error instanceof Error ? error.message : String(error),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (platform === 'win32') {
|
||||
(options.mkdirSync ?? fs.mkdirSync)(target.pathDir, { recursive: true });
|
||||
(options.writeFileSync ?? fs.writeFileSync)(
|
||||
@@ -375,6 +463,8 @@ export async function installLauncher(
|
||||
};
|
||||
}
|
||||
} else {
|
||||
if (!existsSyncOf(options)(target.pathDir))
|
||||
(options.mkdirSync ?? fs.mkdirSync)(target.pathDir, { recursive: true });
|
||||
(options.copyFileSync ?? fs.copyFileSync)(launcherResourcePath, target.installPath);
|
||||
(options.chmodSync ?? fs.chmodSync)(target.installPath, 0o755);
|
||||
}
|
||||
@@ -384,6 +474,7 @@ export async function installLauncher(
|
||||
export async function installBun(
|
||||
options: CommonOptions & WindowsPathOptions = {},
|
||||
): Promise<BunSnapshot> {
|
||||
if (options.bundledBunPath) return detectBun(options);
|
||||
const platform = platformOf(options);
|
||||
if (platform === 'win32') {
|
||||
const bunDir = defaultBunRepairPath(options);
|
||||
@@ -455,6 +546,84 @@ export async function installBun(
|
||||
};
|
||||
}
|
||||
|
||||
// Runs at app startup. Migrates recognized launchers in the standard bin dirs,
|
||||
// the setup install target, and any paths a deferred update handed over.
|
||||
// Returns paths that were refreshed or are no longer eligible for migration.
|
||||
export async function refreshManagedCommandLineLauncher(
|
||||
options: CommonOptions & WindowsPathOptions & { additionalLauncherPaths?: string[] },
|
||||
): Promise<string[]> {
|
||||
if (!options.bundledBunPath) return [];
|
||||
const target = await resolveLauncherInstallTarget(options);
|
||||
const platform = platformOf(options);
|
||||
const platformPath = pathModuleFor(platform);
|
||||
// cmd.exe reads a batch file incrementally while it runs, so the launcher that
|
||||
// started this app is left alone until a later app start rewrites it.
|
||||
const runningLauncherPath =
|
||||
platform === 'win32' ? envOf(options).SUBMINER_LAUNCHER_PATH : undefined;
|
||||
const isRunningLauncher = (candidate: string) =>
|
||||
runningLauncherPath !== undefined &&
|
||||
platformPath.normalize(candidate).toLowerCase() ===
|
||||
platformPath.normalize(runningLauncherPath).toLowerCase();
|
||||
const candidates = new Set([
|
||||
...(target.installPath ? [target.installPath] : []),
|
||||
...(options.additionalLauncherPaths ?? []),
|
||||
...(platform === 'win32'
|
||||
? []
|
||||
: preferredLauncherDirs(platform, options.homeDir ?? os.homedir()).map((directory) =>
|
||||
path.posix.join(directory, 'subminer'),
|
||||
)),
|
||||
]);
|
||||
const readFile = options.readFileSync ?? fs.readFileSync;
|
||||
const acknowledgedPaths: string[] = [];
|
||||
let payload: ReturnType<typeof stageManagedLauncher> | undefined;
|
||||
for (const candidate of candidates) {
|
||||
if (isRunningLauncher(candidate)) continue;
|
||||
if (!existsSyncOf(options)(candidate)) {
|
||||
acknowledgedPaths.push(candidate);
|
||||
continue;
|
||||
}
|
||||
let existing: string;
|
||||
try {
|
||||
existing = String(readFile(candidate, 'utf8'));
|
||||
} catch {
|
||||
continue;
|
||||
}
|
||||
const legacy =
|
||||
(existing.startsWith('#!/usr/bin/env bun\n') &&
|
||||
(existing.includes('SubMiner launcher') ||
|
||||
existing.includes('Launch MPV with SubMiner'))) ||
|
||||
(platform === 'win32' &&
|
||||
existing ===
|
||||
windowsShimContent(
|
||||
options.appExePath ?? process.execPath,
|
||||
resolveLauncherResourcePath(options).replace(/subminer\.js$/, 'subminer'),
|
||||
));
|
||||
if (!isManagedLauncher(existing) && !legacy) {
|
||||
acknowledgedPaths.push(candidate);
|
||||
continue;
|
||||
}
|
||||
if (!isWritableDir(pathModuleFor(platform).dirname(candidate), options)) continue;
|
||||
try {
|
||||
accessSyncOf(options)(candidate, fs.constants.W_OK);
|
||||
} catch {
|
||||
continue;
|
||||
}
|
||||
payload ??= stageManagedLauncher({
|
||||
...options,
|
||||
bundledBunPath: options.bundledBunPath,
|
||||
launcherResourcePath: resolveLauncherResourcePath(options),
|
||||
});
|
||||
const content = managedLauncherContent({
|
||||
platform,
|
||||
appPath: envOf(options).APPIMAGE ?? options.appExePath ?? process.execPath,
|
||||
});
|
||||
if (existing !== content) (options.writeFileSync ?? fs.writeFileSync)(candidate, content);
|
||||
acknowledgedPaths.push(candidate);
|
||||
}
|
||||
if (platform === 'win32' && payload) cleanupOldWindowsManagedRuntimes(options);
|
||||
return acknowledgedPaths;
|
||||
}
|
||||
|
||||
export async function detectCommandLineLauncher(
|
||||
options: CommonOptions & WindowsPathOptions = {},
|
||||
): Promise<CommandLineLauncherSnapshot> {
|
||||
|
||||
Reference in new issue
Block a user