rafaygen-cli 1.3.1 → 1.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/executor.js +87 -0
package/package.json
CHANGED
package/src/executor.js
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
import path from "path";
|
|
3
|
+
import { exec } from "child_process";
|
|
4
|
+
import { renderBox, renderDiffBox, printSuccess, printError } from "./ui.js";
|
|
5
|
+
import { getSessionState } from "./state.js";
|
|
6
|
+
|
|
7
|
+
export async function executeAction(action) {
|
|
8
|
+
const inquirer = (await import("inquirer")).default;
|
|
9
|
+
const chalk = (await import("chalk")).default;
|
|
10
|
+
const state = getSessionState();
|
|
11
|
+
|
|
12
|
+
if (action.type === "write") {
|
|
13
|
+
// Sandbox Check
|
|
14
|
+
if (state.sandboxMode === "read-only") {
|
|
15
|
+
console.log(chalk.red("\n✖ Action blocked: Sandbox mode is set to 'read-only'. (Cannot write files)"));
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const filepath = path.resolve(state.cwd, action.file);
|
|
20
|
+
let original = "";
|
|
21
|
+
if (fs.existsSync(filepath)) {
|
|
22
|
+
original = fs.readFileSync(filepath, "utf-8");
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// Show what will be written
|
|
26
|
+
renderDiffBox(action.file, original, action.content);
|
|
27
|
+
|
|
28
|
+
// Approval Check
|
|
29
|
+
let confirm = true;
|
|
30
|
+
if (state.approvalMode !== "full-auto") {
|
|
31
|
+
const answers = await inquirer.prompt([
|
|
32
|
+
{
|
|
33
|
+
type: "confirm",
|
|
34
|
+
name: "confirm",
|
|
35
|
+
message: `Allow RafayGen to write to ${action.file}?`,
|
|
36
|
+
default: true
|
|
37
|
+
}
|
|
38
|
+
]);
|
|
39
|
+
confirm = answers.confirm;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (confirm) {
|
|
43
|
+
fs.mkdirSync(path.dirname(filepath), { recursive: true });
|
|
44
|
+
fs.writeFileSync(filepath, action.content, "utf-8");
|
|
45
|
+
printSuccess(`Saved ${action.file}`);
|
|
46
|
+
} else {
|
|
47
|
+
console.log(chalk.yellow("Skipped write."));
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
} else if (action.type === "execute") {
|
|
51
|
+
// Sandbox Check
|
|
52
|
+
if (state.sandboxMode !== "danger-full-access") {
|
|
53
|
+
console.log(chalk.red("\n✖ Action blocked: Command execution requires 'danger-full-access' sandbox mode."));
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
renderBox(` Execute Command `, action.command, "magenta");
|
|
58
|
+
|
|
59
|
+
// Approval Check
|
|
60
|
+
let confirm = true;
|
|
61
|
+
if (state.approvalMode !== "full-auto") {
|
|
62
|
+
const answers = await inquirer.prompt([
|
|
63
|
+
{
|
|
64
|
+
type: "confirm",
|
|
65
|
+
name: "confirm",
|
|
66
|
+
message: `Allow RafayGen to execute this command?`,
|
|
67
|
+
default: true
|
|
68
|
+
}
|
|
69
|
+
]);
|
|
70
|
+
confirm = answers.confirm;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (confirm) {
|
|
74
|
+
await new Promise((resolve) => {
|
|
75
|
+
exec(action.command, { cwd: state.cwd }, (err, stdout, stderr) => {
|
|
76
|
+
if (stdout) console.log(chalk.gray(stdout));
|
|
77
|
+
if (stderr) console.log(chalk.red(stderr));
|
|
78
|
+
if (err) printError(`Command failed with code ${err.code}`);
|
|
79
|
+
else printSuccess("Command executed successfully.");
|
|
80
|
+
resolve();
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
} else {
|
|
84
|
+
console.log(chalk.yellow("Skipped execution."));
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|