1+ import { createClient } from '@supabase/supabase-js'
2+ import { readFile } from 'fs/promises'
3+ import { glob } from 'glob'
4+ import path from 'path'
5+ import 'dotenv/config'
6+
7+ if ( ! process . env . SUPABASE_URL || ! process . env . SUPABASE_KEY ) {
8+ throw new Error ( 'Missing required environment variables SUPABASE_URL and/or SUPABASE_KEY' )
9+ }
10+
11+ // Configure Supabase client
12+ const supabase = createClient (
13+ process . env . SUPABASE_URL ,
14+ process . env . SUPABASE_KEY
15+ )
16+
17+ async function findAndUploadFiles ( ) {
18+ try {
19+ // Find all .app.mjs files recursively
20+ const files = await glob ( '../components/**/*.app.mjs' , {
21+ // No need to recurse into the standard subdirs, since app files are always at
22+ // the root of the components/${app} directory
23+ ignore : [ 'node_modules/**' , 'actions/**' , 'common/**' , 'sources/**' ] ,
24+ absolute : true
25+ } )
26+
27+ console . log ( `Found ${ files . length } .app.mjs files` )
28+
29+ for ( const filePath of files ) {
30+ try {
31+ const content = await readFile ( filePath , 'utf8' )
32+
33+ const filename = path . basename ( filePath )
34+ const app = filename . replace ( '.app.mjs' , '' )
35+
36+ const { data, error } = await supabase
37+ . from ( 'registry_app_files' )
38+ . insert ( {
39+ app : app ,
40+ app_file : content
41+ } )
42+
43+ if ( error ) {
44+ console . error ( `Error uploading ${ filename } :` , error )
45+ continue
46+ }
47+
48+ console . log ( `Successfully uploaded ${ filename } ` )
49+ } catch ( err ) {
50+ console . error ( `Error processing ${ filePath } :` , err )
51+ }
52+ }
53+ } catch ( err ) {
54+ console . error ( 'Error finding files:' , err )
55+ }
56+ }
57+
58+ // Run the script
59+ findAndUploadFiles ( )
60+ . then ( ( ) => console . log ( 'Upload complete' ) )
61+ . catch ( err => console . error ( 'Script failed:' , err ) )
0 commit comments