43 lines
1.3 KiB
TypeScript
43 lines
1.3 KiB
TypeScript
import fs from "node:fs";
|
|||
|
|
import { ConfigError, loadConfig } from "../../lib/config/load";
|
||
|
|
import { isPortFree } from "../lib/network";
|
||
|
|
import { resolveConfigPath } from "../lib/paths";
|
||
|
|
|
||
|
|
export interface DoctorOptions {
|
||
|
|
config?: string;
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function doctorCommand(opts: DoctorOptions): Promise<void> {
|
||
|
|
const rows: [string, string][] = [];
|
||
|
|
|
||
|
|
rows.push(["Node.js", process.version]);
|
||
|
|
|
||
|
|
const configPath = resolveConfigPath(opts.config);
|
||
|
|
const exists = fs.existsSync(configPath);
|
||
|
|
rows.push(["Config path", `${configPath} ${exists ? "(exists)" : "(not found)"}`]);
|
||
|
|
|
||
|
|
if (exists) {
|
||
|
|
try {
|
||
|
|
const { config } = loadConfig(configPath);
|
||
|
|
const portFree = await isPortFree(config.server.host, config.server.port);
|
||
|
|
rows.push([
|
||
|
|
"Port available",
|
||
|
|
portFree ? "yes" : `no (${config.server.host}:${config.server.port} in use)`,
|
||
|
|
]);
|
||
|
|
rows.push(["Scripts configured", String(config.scripts.length)]);
|
||
|
|
rows.push(["Auth enabled", String(config.auth.enabled)]);
|
||
|
|
} catch (error) {
|
||
|
|
if (error instanceof ConfigError) {
|
||
|
|
rows.push(["Config", error.message]);
|
||
|
|
} else {
|
||
|
|
throw error;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
const labelWidth = Math.max(...rows.map(([label]) => label.length));
|
||
|
|
for (const [label, value] of rows) {
|
||
|
|
console.log(`${label.padEnd(labelWidth)} ${value}`);
|
||
|
|
}
|
||
|
|
}
|