Repository navigation
Expand file tree
/
Copy pathcli.ts
More file actions
99 lines (86 loc) · 2.73 KB
/
Copy pathcli.ts
File metadata and controls
99 lines (86 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import {
createReadlineQuestionSession,
promptForConsent,
type QuestionSessionFactory,
type WriteLike
} from "./consent";
import { runClaudeCommand, stripWrapperFlags, type SpawnRunner } from "./claude";
import { readState, resolveStateFilePath, writeState, type WrapperState } from "./state";
export type CliDependencies = {
loadState(): WrapperState;
requestConsent(): Promise<boolean>;
runClaude(args: string[]): number;
saveState(state: WrapperState): void;
stderr: WriteLike;
stdout: WriteLike;
};
export type DefaultCliDependencyOptions = {
input?: NodeJS.ReadableStream;
platform?: NodeJS.Platform;
questionSessionFactory?: QuestionSessionFactory;
runner?: SpawnRunner;
stateFilePath?: string;
stderr?: WriteLike;
stdout?: WriteLike;
};
function writeLine(output: WriteLike, message: string): void {
output.write(`${message}\n`);
}
function printHelp(output: WriteLike): void {
writeLine(output, "Usage: claude-remote-yolo [options]");
writeLine(output, "");
writeLine(output, "Execution always wraps:");
writeLine(output, "claude remote-control --permission-mode bypassPermissions");
writeLine(output, "");
writeLine(output, "Wrapper flags:");
writeLine(output, "--yolo Accepted for compatibility and ignored");
}
export function createDefaultCliDependencies(
options: DefaultCliDependencyOptions = {}
): CliDependencies {
const stateFilePath = options.stateFilePath ?? resolveStateFilePath();
const stdout = options.stdout ?? process.stdout;
const stderr = options.stderr ?? process.stderr;
const input = options.input ?? process.stdin;
const questionSessionFactory =
options.questionSessionFactory ?? createReadlineQuestionSession;
return {
loadState() {
return readState(stateFilePath);
},
requestConsent() {
return promptForConsent(questionSessionFactory(input, stdout), stdout);
},
runClaude(args: string[]) {
return runClaudeCommand(args, options.runner, options.platform);
},
saveState(state: WrapperState) {
writeState(stateFilePath, state);
},
stderr,
stdout
};
}
export async function runCli(
args: string[],
dependencies: CliDependencies
): Promise<number> {
const state = dependencies.loadState();
if (args[0] === "help" || args.includes("--help")) {
printHelp(dependencies.stdout);
return 0;
}
const passthroughArgs = stripWrapperFlags(args);
if (!state.consentAccepted) {
const consentAccepted = await dependencies.requestConsent();
if (!consentAccepted) {
writeLine(dependencies.stderr, "Consent not granted. Aborting.");
return 1;
}
dependencies.saveState({
...state,
consentAccepted: true
});
}
return dependencies.runClaude(passthroughArgs);
}