Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

os-jank.ts

46 lines1.3 KB

1import * as OS from "node:os";
2import { Effect, Path } from "effect";
3import { readPathFromLoginShell, resolveLoginShell } from "@t3tools/shared/shell";
4​
5export function fixPath(
6 options: {
7 env?: NodeJS.ProcessEnv;
8 platform?: NodeJS.Platform;
9 readPath?: typeof readPathFromLoginShell;
10 } = {},
11): void {
12 const platform = options.platform ?? process.platform;
13 if (platform !== "darwin" && platform !== "linux") return;
14​
15 const env = options.env ?? process.env;
16​
17 try {
18 const shell = resolveLoginShell(platform, env.SHELL);
19 if (!shell) return;
20 const result = (options.readPath ?? readPathFromLoginShell)(shell);
21 if (result) {
22 env.PATH = result;
23 }
24 } catch {
25 // Silently ignore — keep default PATH
26 }
28​
29export const expandHomePath = Effect.fn(function* (input: string) {
30 const { join } = yield* Path.Path;
31 if (input === "~") {
32 return OS.homedir();
33 }
34 if (input.startsWith("~/") || input.startsWith("~\\")) {
35 return join(OS.homedir(), input.slice(2));
36 }
37 return input;
38});
39​
40export const resolveBaseDir = Effect.fn(function* (raw: string | undefined) {
41 const { join, resolve } = yield* Path.Path;
42 if (!raw || raw.trim().length === 0) {
43 return join(OS.homedir(), ".t3");
44 }
45 return resolve(yield* expandHomePath(raw.trim()));
46});