vg-coder-cli 1.0.10 → 1.0.12

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.
@@ -0,0 +1,130 @@
1
+ const { exec } = require('child_process');
2
+ const path = require('path');
3
+ const fs = require('fs-extra');
4
+ const { promisify } = require('util');
5
+
6
+ const execAsync = promisify(exec);
7
+
8
+ /**
9
+ * Bash Script Executor
10
+ * Validates and executes bash scripts safely
11
+ */
12
+ class BashExecutor {
13
+ constructor(workingDir) {
14
+ this.workingDir = workingDir || process.cwd();
15
+ this.tempDir = path.join(this.workingDir, '.vg', 'temp-execute');
16
+ }
17
+
18
+ /**
19
+ * Ensure temp directory exists
20
+ */
21
+ async ensureTempDir() {
22
+ await fs.ensureDir(this.tempDir);
23
+ }
24
+
25
+ /**
26
+ * Cleanup temp directory
27
+ */
28
+ async cleanup() {
29
+ try {
30
+ if (await fs.pathExists(this.tempDir)) {
31
+ await fs.remove(this.tempDir);
32
+ }
33
+ } catch (error) {
34
+ console.error('Cleanup error:', error.message);
35
+ }
36
+ }
37
+
38
+ /**
39
+ * Validate bash script syntax
40
+ * @param {string} bashScript - The bash script to validate
41
+ * @returns {Promise<{valid: boolean, error: string|null}>}
42
+ */
43
+ async validateSyntax(bashScript) {
44
+ await this.ensureTempDir();
45
+
46
+ const scriptPath = path.join(this.tempDir, 'validate.sh');
47
+
48
+ try {
49
+ // Write script to temp file
50
+ await fs.writeFile(scriptPath, bashScript, 'utf8');
51
+
52
+ // Validate syntax using bash -n
53
+ await execAsync(`bash -n "${scriptPath}"`);
54
+
55
+ return { valid: true, error: null };
56
+
57
+ } catch (error) {
58
+ return {
59
+ valid: false,
60
+ error: error.stderr || error.message
61
+ };
62
+ }
63
+ }
64
+
65
+ /**
66
+ * Execute bash script in working directory
67
+ * @param {string} bashScript - The bash script to execute
68
+ * @returns {Promise<{success: boolean, stdout: string, stderr: string, exitCode: number}>}
69
+ */
70
+ async execute(bashScript) {
71
+ const startTime = Date.now();
72
+
73
+ try {
74
+ // First validate syntax
75
+ const validation = await this.validateSyntax(bashScript);
76
+
77
+ if (!validation.valid) {
78
+ return {
79
+ success: false,
80
+ error: 'Syntax validation failed',
81
+ details: validation.error,
82
+ executionTime: Date.now() - startTime
83
+ };
84
+ }
85
+
86
+ // Execute script in working directory
87
+ const result = await execAsync(bashScript, {
88
+ cwd: this.workingDir,
89
+ maxBuffer: 10 * 1024 * 1024, // 10MB buffer
90
+ shell: '/bin/bash'
91
+ });
92
+
93
+ // Cleanup temp directory
94
+ await this.cleanup();
95
+
96
+ return {
97
+ success: true,
98
+ stdout: result.stdout || '',
99
+ stderr: result.stderr || '',
100
+ exitCode: 0,
101
+ executionTime: Date.now() - startTime
102
+ };
103
+
104
+ } catch (error) {
105
+ // Cleanup even on error
106
+ await this.cleanup();
107
+
108
+ // Check if it's an execution error (not syntax)
109
+ if (error.code !== undefined) {
110
+ return {
111
+ success: false,
112
+ stdout: error.stdout || '',
113
+ stderr: error.stderr || error.message,
114
+ exitCode: error.code || 1,
115
+ executionTime: Date.now() - startTime
116
+ };
117
+ }
118
+
119
+ // Unknown error
120
+ return {
121
+ success: false,
122
+ error: 'Execution failed',
123
+ details: error.message,
124
+ executionTime: Date.now() - startTime
125
+ };
126
+ }
127
+ }
128
+ }
129
+
130
+ module.exports = BashExecutor;