-
Notifications
You must be signed in to change notification settings - Fork 23
/
build_supabase.dart
89 lines (70 loc) · 2.02 KB
/
build_supabase.dart
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
import 'dart:io';
import 'package:path/path.dart' as p;
import '../../compiler.dart';
import '../../watcher.dart';
import '../base_command.dart';
class SupabaseBuildCommand extends BaseCommand {
@override
final name = "supabase_functions";
@override
final description = "Builds the project for Supabase Edge Functions.";
SupabaseBuildCommand({
required super.logger,
}) {
argParser.addFlag(
'dev',
help:
'Runs Dart Edge in a local development environment with hot reload via Vercel CLI.',
);
}
Future<void> runDev() async {
final functionDirectory = Directory(
p.join(Directory.current.path, 'supabase', 'functions', 'dart_edge'),
);
final entryFile = File(p.join(functionDirectory.path, 'index.ts'));
final watcher = Watcher(
include: '**/*.dart',
debounce: 500,
watchPath: p.join(Directory.current.path, 'lib'),
);
final compiler = Compiler(
logger: logger,
entryPoint: p.join(Directory.current.path, 'lib', 'main.dart'),
outputDirectory: functionDirectory.path,
outputFileName: 'main.dart.js',
level: CompilerLevel.O1,
);
await compiler.compile();
await entryFile.writeAsString(
edgeFunctionEntryFileDefaultValue('main.dart.js'),
);
watcher.watch().listen((event) async {
await compiler.compile();
});
}
Future<void> runBuild() async {}
@override
void run() async {
final isDev = argResults!['dev'] as bool;
if (isDev) {
await runDev();
} else {
await runBuild();
}
}
}
final edgeFunctionEntryFileDefaultValue = (String fileName) => '''
import { serve } from "https://deno.land/std@0.131.0/http/server.ts";
import "./${fileName}";
serve((request) => {
if (self.__dartSupabaseFetchHandler) {
return self.__dartSupabaseFetchHandler(request);
}
return new Response("Something went wrong", { status: 500 });
});
declare global {
interface Window {
__dartSupabaseFetchHandler?: (request: Request) => Promise<Response>;
}
}
''';