Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

next
ts 42 lines 1.71 KB
Raw
1 import { Config, Effect, FileSystem, Option, Path } from 'effect';
2
3 /** How Windows runs a command found on `PATH`. */
4 export interface WindowsExecutable {
5 readonly path: string;
6 /** `.cmd` and `.bat` files, such as npm's command shims, only run through `cmd.exe`. */
7 readonly needsShell: boolean;
8 }
9
10 // Windows looks up the extension-less name `claude` as `claude.exe`, `claude.cmd`, and so on.
11 const EXECUTABLE_EXTENSIONS = ['.exe', '.cmd', '.bat'] as const;
12
13 // The variable is spelled `Path` on Windows, and environment snapshots keep that spelling.
14 const WINDOWS_PATH = Config.all([
15 Config.option(Config.String('Path')),
16 Config.option(Config.String('PATH')),
17 ]).pipe(Config.map(([path, upperCasePath]) => Option.orElse(path, () => upperCasePath)));
18
19 /**
20 * Finds `name` the way `cmd.exe` would: in each `PATH` directory, with each executable
21 * extension. Process spawning does not do this itself, and refuses to run a `.cmd` file directly.
22 */
23 export const resolveWindowsExecutable = (
24 name: string
25 ): Effect.Effect<Option.Option<WindowsExecutable>, never, FileSystem.FileSystem | Path.Path> =>
26 Effect.gen(function* () {
27 const fs = yield* FileSystem.FileSystem;
28 const path = yield* Path.Path;
29 const pathEnv = Option.getOrElse(yield* Effect.orDie(WINDOWS_PATH), () => '');
30 const candidates = pathEnv
31 .split(';')
32 .filter(directory => directory.length > 0)
33 .flatMap(directory =>
34 EXECUTABLE_EXTENSIONS.map(extension => ({
35 path: path.join(directory, `${name}${extension}`),
36 needsShell: extension !== '.exe',
37 }))
38 );
39 return yield* Effect.findFirst(candidates, candidate =>
40 fs.exists(candidate.path).pipe(Effect.orElseSucceed(() => false))
41 );
42 });