mirror of
https://github.com/ksyasuda/SubMiner.git
synced 2026-02-28 06:22:45 -08:00
refactor: add main.ts decomposition guardrails and extract core helpers
This commit is contained in:
31
src/core/utils/coerce.ts
Normal file
31
src/core/utils/coerce.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
export function asFiniteNumber(
|
||||
value: unknown,
|
||||
fallback: number,
|
||||
min?: number,
|
||||
max?: number,
|
||||
): number {
|
||||
if (typeof value !== "number" || !Number.isFinite(value)) return fallback;
|
||||
if (min !== undefined && value < min) return min;
|
||||
if (max !== undefined && value > max) return max;
|
||||
return value;
|
||||
}
|
||||
|
||||
export function asString(value: unknown, fallback: string): string {
|
||||
if (typeof value !== "string") return fallback;
|
||||
const trimmed = value.trim();
|
||||
return trimmed.length > 0 ? trimmed : fallback;
|
||||
}
|
||||
|
||||
export function asBoolean(value: unknown, fallback: boolean): boolean {
|
||||
if (typeof value === "boolean") return value;
|
||||
if (typeof value === "string") {
|
||||
const normalized = value.trim().toLowerCase();
|
||||
if (normalized === "yes" || normalized === "true" || normalized === "1") {
|
||||
return true;
|
||||
}
|
||||
if (normalized === "no" || normalized === "false" || normalized === "0") {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return fallback;
|
||||
}
|
||||
78
src/core/utils/config-gen.ts
Normal file
78
src/core/utils/config-gen.ts
Normal file
@@ -0,0 +1,78 @@
|
||||
import * as fs from "fs";
|
||||
import * as path from "path";
|
||||
import * as readline from "readline";
|
||||
import { CliArgs } from "../../cli/args";
|
||||
|
||||
function formatBackupTimestamp(date = new Date()): string {
|
||||
const pad = (v: number): string => String(v).padStart(2, "0");
|
||||
return `${date.getFullYear()}${pad(date.getMonth() + 1)}${pad(date.getDate())}-${pad(date.getHours())}${pad(date.getMinutes())}${pad(date.getSeconds())}`;
|
||||
}
|
||||
|
||||
function promptYesNo(question: string): Promise<boolean> {
|
||||
return new Promise((resolve) => {
|
||||
const rl = readline.createInterface({
|
||||
input: process.stdin,
|
||||
output: process.stdout,
|
||||
});
|
||||
rl.question(question, (answer) => {
|
||||
rl.close();
|
||||
const normalized = answer.trim().toLowerCase();
|
||||
resolve(normalized === "y" || normalized === "yes");
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
export async function generateDefaultConfigFile(
|
||||
args: CliArgs,
|
||||
options: {
|
||||
configDir: string;
|
||||
defaultConfig: unknown;
|
||||
generateTemplate: (config: unknown) => string;
|
||||
},
|
||||
): Promise<number> {
|
||||
const targetPath = args.configPath
|
||||
? path.resolve(args.configPath)
|
||||
: path.join(options.configDir, "config.jsonc");
|
||||
const template = options.generateTemplate(options.defaultConfig);
|
||||
|
||||
if (fs.existsSync(targetPath)) {
|
||||
if (args.backupOverwrite) {
|
||||
const backupPath = `${targetPath}.bak.${formatBackupTimestamp()}`;
|
||||
fs.copyFileSync(targetPath, backupPath);
|
||||
fs.writeFileSync(targetPath, template, "utf-8");
|
||||
console.log(`Backed up existing config to ${backupPath}`);
|
||||
console.log(`Generated config at ${targetPath}`);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!process.stdin.isTTY || !process.stdout.isTTY) {
|
||||
console.error(
|
||||
`Config exists at ${targetPath}. Re-run with --backup-overwrite to back up and overwrite.`,
|
||||
);
|
||||
return 1;
|
||||
}
|
||||
|
||||
const confirmed = await promptYesNo(
|
||||
`Config exists at ${targetPath}. Back up and overwrite? [y/N] `,
|
||||
);
|
||||
if (!confirmed) {
|
||||
console.log("Config generation cancelled.");
|
||||
return 0;
|
||||
}
|
||||
|
||||
const backupPath = `${targetPath}.bak.${formatBackupTimestamp()}`;
|
||||
fs.copyFileSync(targetPath, backupPath);
|
||||
fs.writeFileSync(targetPath, template, "utf-8");
|
||||
console.log(`Backed up existing config to ${backupPath}`);
|
||||
console.log(`Generated config at ${targetPath}`);
|
||||
return 0;
|
||||
}
|
||||
|
||||
const parentDir = path.dirname(targetPath);
|
||||
if (!fs.existsSync(parentDir)) {
|
||||
fs.mkdirSync(parentDir, { recursive: true });
|
||||
}
|
||||
fs.writeFileSync(targetPath, template, "utf-8");
|
||||
console.log(`Generated config at ${targetPath}`);
|
||||
return 0;
|
||||
}
|
||||
39
src/core/utils/electron-backend.ts
Normal file
39
src/core/utils/electron-backend.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import { CliArgs, shouldStartApp } from "../../cli/args";
|
||||
|
||||
function getElectronOzonePlatformHint(): string | null {
|
||||
const hint = process.env.ELECTRON_OZONE_PLATFORM_HINT?.trim().toLowerCase();
|
||||
if (hint) return hint;
|
||||
const ozone = process.env.OZONE_PLATFORM?.trim().toLowerCase();
|
||||
if (ozone) return ozone;
|
||||
return null;
|
||||
}
|
||||
|
||||
function shouldPreferWaylandBackend(): boolean {
|
||||
return Boolean(
|
||||
process.env.HYPRLAND_INSTANCE_SIGNATURE || process.env.SWAYSOCK,
|
||||
);
|
||||
}
|
||||
|
||||
export function forceX11Backend(args: CliArgs): void {
|
||||
if (process.platform !== "linux") return;
|
||||
if (!shouldStartApp(args)) return;
|
||||
if (shouldPreferWaylandBackend()) return;
|
||||
|
||||
const hint = getElectronOzonePlatformHint();
|
||||
if (hint === "x11") return;
|
||||
|
||||
process.env.ELECTRON_OZONE_PLATFORM_HINT = "x11";
|
||||
process.env.OZONE_PLATFORM = "x11";
|
||||
}
|
||||
|
||||
export function enforceUnsupportedWaylandMode(args: CliArgs): void {
|
||||
if (process.platform !== "linux") return;
|
||||
if (!shouldStartApp(args)) return;
|
||||
const hint = getElectronOzonePlatformHint();
|
||||
if (hint !== "wayland") return;
|
||||
|
||||
const message =
|
||||
"Unsupported Electron backend: Wayland. Set ELECTRON_OZONE_PLATFORM_HINT=x11 and restart SubMiner.";
|
||||
console.error(message);
|
||||
throw new Error(message);
|
||||
}
|
||||
30
src/core/utils/keybindings.ts
Normal file
30
src/core/utils/keybindings.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import { Config, Keybinding } from "../../types";
|
||||
|
||||
export function resolveKeybindings(
|
||||
config: Config,
|
||||
defaultKeybindings: Keybinding[],
|
||||
): Keybinding[] {
|
||||
const userBindings = config.keybindings || [];
|
||||
const bindingMap = new Map<string, (string | number)[] | null>();
|
||||
|
||||
for (const binding of defaultKeybindings) {
|
||||
bindingMap.set(binding.key, binding.command);
|
||||
}
|
||||
|
||||
for (const binding of userBindings) {
|
||||
if (binding.command === null) {
|
||||
bindingMap.delete(binding.key);
|
||||
} else {
|
||||
bindingMap.set(binding.key, binding.command);
|
||||
}
|
||||
}
|
||||
|
||||
const keybindings: Keybinding[] = [];
|
||||
for (const [key, command] of bindingMap) {
|
||||
if (command !== null) {
|
||||
keybindings.push({ key, command });
|
||||
}
|
||||
}
|
||||
|
||||
return keybindings;
|
||||
}
|
||||
Reference in New Issue
Block a user