-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathcreate-edge-function.ts
More file actions
103 lines (84 loc) · 3.12 KB
/
Copy pathcreate-edge-function.ts
File metadata and controls
103 lines (84 loc) · 3.12 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
import fs from 'fs';
import path from 'path';
import { log, confirm } from '@clack/prompts';
import chalk from 'chalk';
import { getVersion } from '../../utils/get-version.js';
const INDEX_TS_TEMPLATE = `import { ControlPlane } from '@pgflow/edge-worker';
// Import your flows here:
// import { MyFlow } from '../_flows/my_flow.ts';
ControlPlane.serve([
// Add your flows here:
// MyFlow,
]);
`;
const DENO_JSON_TEMPLATE = (version: string) => `{
"imports": {
"@pgflow/core": "npm:@pgflow/core@${version}",
"@pgflow/core/": "npm:@pgflow/core@${version}/",
"@pgflow/dsl": "npm:@pgflow/dsl@${version}",
"@pgflow/dsl/": "npm:@pgflow/dsl@${version}/",
"@pgflow/dsl/supabase": "npm:@pgflow/dsl@${version}/supabase",
"@pgflow/edge-worker": "jsr:@pgflow/edge-worker@${version}",
"@pgflow/edge-worker/": "jsr:@pgflow/edge-worker@${version}/",
"@pgflow/edge-worker/_internal": "jsr:@pgflow/edge-worker@${version}/_internal"
}
}
`;
export async function createEdgeFunction({
supabasePath,
autoConfirm = false,
}: {
supabasePath: string;
autoConfirm?: boolean;
}): Promise<boolean> {
const functionsDir = path.join(supabasePath, 'functions');
const pgflowFunctionDir = path.join(functionsDir, 'pgflow');
const indexPath = path.join(pgflowFunctionDir, 'index.ts');
const denoJsonPath = path.join(pgflowFunctionDir, 'deno.json');
// Relative paths for display
const relativeFunctionDir = 'supabase/functions/pgflow';
const relativeIndexPath = `${relativeFunctionDir}/index.ts`;
const relativeDenoJsonPath = `${relativeFunctionDir}/deno.json`;
// 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(denoJsonPath)) {
filesToCreate.push({ path: denoJsonPath, relativePath: relativeDenoJsonPath });
}
// If all files exist, return success
if (filesToCreate.length === 0) {
log.success('Control Plane already up to date');
return false;
}
// Show preview and ask for confirmation only when not auto-confirming
if (!autoConfirm) {
const summaryMsg = [
`Create ${chalk.cyan('functions/pgflow/')} ${chalk.dim('(Control Plane for flow registration and compilation)')}:`,
'',
...filesToCreate.map((file) => ` ${chalk.bold(path.basename(file.relativePath))}`),
].join('\n');
log.info(summaryMsg);
const confirmResult = await confirm({
message: `Create functions/pgflow/?`,
});
if (confirmResult !== true) {
log.warn('Control Plane installation skipped');
return false;
}
}
// Create the directory if it doesn't exist
if (!fs.existsSync(pgflowFunctionDir)) {
fs.mkdirSync(pgflowFunctionDir, { recursive: true });
}
// Create files
if (filesToCreate.some((f) => f.path === indexPath)) {
fs.writeFileSync(indexPath, INDEX_TS_TEMPLATE);
}
if (filesToCreate.some((f) => f.path === denoJsonPath)) {
fs.writeFileSync(denoJsonPath, DENO_JSON_TEMPLATE(getVersion()));
}
log.success('Control Plane installed');
return true;
}