-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecute-space.ts
More file actions
executable file
·116 lines (98 loc) · 3.73 KB
/
execute-space.ts
File metadata and controls
executable file
·116 lines (98 loc) · 3.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/env node
import { exec } from 'child_process';
import { promises as fs } from 'fs';
import * as path from 'path';
async function executeSpace(spaceId: string) {
if (!spaceId) {
console.error('Usage: npx tsx execute-space.ts <spaceId>');
process.exit(1);
}
const spaceDir = path.join('data', 'spaces', spaceId);
// Check if space directory exists
try {
await fs.access(spaceDir);
} catch {
console.error(`Error: Space directory not found: ${spaceDir}`);
process.exit(1);
}
// Find TypeScript files (excluding ones starting with _), prioritizing space.ts
try {
const files = await fs.readdir(spaceDir);
const tsFiles = files.filter(file =>
file.endsWith('.ts') && !file.startsWith('_')
);
if (tsFiles.length === 0) {
console.error(`Error: No TypeScript files found in ${spaceDir}`);
process.exit(1);
}
// Prioritize space.ts if it exists, otherwise use first file found
let tsFile = tsFiles.find(file => file === 'space.ts') || tsFiles[0];
if (tsFiles.length > 1 && tsFile !== 'space.ts') {
console.warn(`Warning: Multiple TypeScript files found, using: ${tsFile} (consider renaming to space.ts)`);
}
const tsFilePath = path.join(spaceDir, tsFile);
const outputPath = path.join(spaceDir, 'space.json');
console.log(`Executing space script: ${tsFilePath}`);
// Execute the TypeScript file and capture output
return new Promise<void>((resolve, reject) => {
exec(`npx tsx "${tsFilePath}"`, {
maxBuffer: 1024 * 1024, // 1MB buffer for large outputs
cwd: process.cwd()
}, async (error, stdout, stderr) => {
if (error) {
console.error(`Execution error: ${error.message}`);
if (stderr) console.error(`stderr: ${stderr}`);
reject(error);
return;
}
if (stderr) {
console.warn(`stderr: ${stderr}`);
}
// Validate JSON output
try {
JSON.parse(stdout);
} catch (parseError) {
console.error('Error: Output is not valid JSON');
console.error('Output received:', stdout.substring(0, 200));
reject(parseError);
return;
}
// Write output to space.json atomically to avoid race conditions
try {
// Write to temporary file first, then rename (atomic operation)
const tempPath = outputPath + '.tmp';
await fs.writeFile(tempPath, stdout, 'utf8');
await fs.rename(tempPath, outputPath);
console.log(`✅ Space output written to: ${outputPath}`);
// Notify WebSocket server immediately after successful write via HTTP
try {
const response = await fetch(`http://localhost:3002/api/spaces/${spaceId}/broadcast`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' }
});
if (response.ok) {
console.log(`📤 WebSocket update sent for space: ${spaceId}`);
} else {
console.log(`⚠️ Failed to send WebSocket update: ${response.status}`);
}
} catch (fetchError) {
console.log(`⚠️ Could not reach WebSocket server: ${fetchError instanceof Error ? fetchError.message : String(fetchError)}`);
}
resolve();
} catch (writeError) {
console.error(`Error writing output: ${writeError}`);
reject(writeError);
}
});
});
} catch (error) {
console.error(`Error reading space directory: ${error}`);
process.exit(1);
}
}
// Get spaceId from command line arguments
const spaceId = process.argv[2];
executeSpace(spaceId).catch((error) => {
console.error('Execution failed:', error);
process.exit(1);
});