-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathcreate-flows-directory.ts
More file actions
91 lines (70 loc) · 2.56 KB
/
Copy pathcreate-flows-directory.ts
File metadata and controls
91 lines (70 loc) · 2.56 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
import fs from 'fs';
import path from 'path';
import { log, confirm } from '@clack/prompts';
import chalk from 'chalk';
const INDEX_TS_TEMPLATE = `// Re-export all flows from this directory
// Example: export { MyFlow } from './my_flow.ts';
export { ExampleFlow } from './example_flow.ts';
`;
const EXAMPLE_FLOW_TEMPLATE = `import { Flow } from '@pgflow/dsl';
type Input = { name: string };
export const ExampleFlow = new Flow<Input>({ slug: 'example_flow' })
.step({ slug: 'greet' }, (input) => \`Hello, \${input.run.name}!\`);
`;
export async function createFlowsDirectory({
supabasePath,
autoConfirm = false,
}: {
supabasePath: string;
autoConfirm?: boolean;
}): Promise<boolean> {
const flowsDir = path.join(supabasePath, 'flows');
const indexPath = path.join(flowsDir, 'index.ts');
const exampleFlowPath = path.join(flowsDir, 'example_flow.ts');
// Relative paths for display
const relativeFlowsDir = 'supabase/flows';
const relativeIndexPath = `${relativeFlowsDir}/index.ts`;
const relativeExampleFlowPath = `${relativeFlowsDir}/example_flow.ts`;
// Check what needs to be created
const filesToCreate: Array<{ path: string; relativePath: string }> = [];
if (!fs.existsSync(indexPath)) {
filesToCreate.push({ path: indexPath, relativePath: relativeIndexPath });
}
if (!fs.existsSync(exampleFlowPath)) {
filesToCreate.push({ path: exampleFlowPath, relativePath: relativeExampleFlowPath });
}
// If all files exist, return success
if (filesToCreate.length === 0) {
log.success('Flows directory already up to date');
return false;
}
// Show preview and ask for confirmation only when not auto-confirming
if (!autoConfirm) {
const summaryMsg = [
`Create ${chalk.cyan('flows/')} ${chalk.dim('(flow definitions directory)')}:`,
'',
...filesToCreate.map((file) => ` ${chalk.bold(path.basename(file.relativePath))}`),
].join('\n');
log.info(summaryMsg);
const confirmResult = await confirm({
message: `Create flows/?`,
});
if (confirmResult !== true) {
log.warn('Flows directory installation skipped');
return false;
}
}
// Create the directory if it doesn't exist
if (!fs.existsSync(flowsDir)) {
fs.mkdirSync(flowsDir, { recursive: true });
}
// Create files
if (filesToCreate.some((f) => f.path === indexPath)) {
fs.writeFileSync(indexPath, INDEX_TS_TEMPLATE);
}
if (filesToCreate.some((f) => f.path === exampleFlowPath)) {
fs.writeFileSync(exampleFlowPath, EXAMPLE_FLOW_TEMPLATE);
}
log.success('Flows directory created');
return true;
}