Files
reverse-engineering-anything/src/config/homeDirectory.ts
T
morluto c339f9ebe7 fix(setup): ignore empty home directory selections
Skip empty home environment values while preserving nonempty caller selections and platform precedence. Setup, doctor, and uninstall now use the same fallback home instead of writing relative paths in the current directory.

Validation: focused home tests and the public setup/doctor/uninstall lifecycle under Node 24.18. Package verification and pack dry run are included in the integrated gate.

Fixes #2325
2026-10-13 04:11:55 +08:00

30 lines
956 B
TypeScript

import { userInfo } from "node:os";
const firstNonempty = (...values: readonly (string | undefined)[]) =>
values.find(
(value): value is string => value !== undefined && value.length > 0,
);
/** Resolve the caller's selected home before consulting the host account. */
export const homeDirectoryFromEnvironment = (
environment: Readonly<NodeJS.ProcessEnv>,
platform: NodeJS.Platform,
): string => {
const windowsDriveHome =
environment.HOMEDRIVE !== undefined &&
environment.HOMEDRIVE.length > 0 &&
environment.HOMEPATH !== undefined &&
environment.HOMEPATH.length > 0
? `${environment.HOMEDRIVE}${environment.HOMEPATH}`
: undefined;
const selected =
platform === "win32"
? firstNonempty(
environment.USERPROFILE,
windowsDriveHome,
environment.HOME,
)
: firstNonempty(environment.HOME, environment.USERPROFILE);
return selected ?? userInfo().homedir;
};