forked from BeOnAuto/auto-engineer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvert-flow.ts
More file actions
109 lines (88 loc) · 3.03 KB
/
convert-flow.ts
File metadata and controls
109 lines (88 loc) · 3.03 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
#!/usr/bin/env node
import { resolve, dirname, relative, join } from 'path';
import { existsSync, writeFileSync, statSync } from 'fs';
import { spawn } from 'child_process';
import { fileURLToPath } from 'url';
const runInContext = (flowPath: string, projectRoot: string): Promise<string> => {
return new Promise((resolve, reject) => {
const __dirname = dirname(fileURLToPath(import.meta.url));
const helperScript = join(__dirname, 'convert-flow-helper.ts');
const child = spawn('npx', ['tsx', helperScript, flowPath], {
cwd: projectRoot,
stdio: ['inherit', 'pipe', 'pipe'],
});
let stdout = '';
let stderr = '';
child.stdout.on('data', (data) => {
stdout += data.toString();
});
child.stderr.on('data', (data) => {
stderr += data.toString();
});
child.on('close', (code) => {
if (code === 0) {
resolve(stdout.trim());
} else {
reject(new Error(`Process failed with code ${code}: ${stderr}`));
}
});
});
};
const main = async () => {
const args = process.argv.slice(2);
if (args.length === 0) {
console.error('Usage: pnpm convert-flow <input-file.flow.ts> [output-file-or-dir]');
process.exit(1);
}
const inputFile = args[0];
const outputFile = args[1];
if (!inputFile.endsWith('.flow.ts') && !inputFile.endsWith('.flow.js')) {
console.error('Error: Input file must be a .flow.ts or .flow.js file');
process.exit(1);
}
try {
const inputPath = resolve(inputFile);
if (!existsSync(inputPath)) {
console.error(`Error: File not found: ${inputPath}`);
process.exit(1);
}
let outputPath: string | undefined;
if (outputFile) {
const resolved = resolve(outputFile);
if (existsSync(resolved) && statSync(resolved).isDirectory()) {
outputPath = join(resolved, 'schema.json');
} else {
outputPath = resolved;
}
}
console.log(`Converting flow: ${inputPath}`);
const flowDir = dirname(inputPath);
let projectRoot = flowDir;
while (projectRoot !== '/' && !existsSync(`${projectRoot}/package.json`)) {
projectRoot = dirname(projectRoot);
}
if (!existsSync(`${projectRoot}/package.json`)) {
console.error('Error: Could not find package.json in flow directory or its parents');
process.exit(1);
}
// Get relative path from project root
const relativeFlowPath = relative(projectRoot, inputPath);
// Run the conversion in the correct context
const json = await runInContext(relativeFlowPath, projectRoot);
if (outputPath) {
writeFileSync(outputPath, json);
console.log(`✓ Flow converted successfully to: ${outputPath}`);
console.log('📋 Schema validation performed during conversion');
} else {
console.log(json);
}
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
console.error(`Error converting flow: ${message}`);
process.exit(1);
}
};
main().catch((error) => {
console.error('Fatal error:', error);
process.exit(1);
});