Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Script to upload app files #15290

Merged
merged 5 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion components/clear_books/clear_books.app.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ export default {
console.log(Object.keys(this.$auth));
},
},
};
};
2 changes: 1 addition & 1 deletion components/dixa/dixa.app.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ export default {
console.log(Object.keys(this.$auth));
},
},
};
};
2 changes: 1 addition & 1 deletion components/linkup/linkup.app.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ export default {
console.log(Object.keys(this.$auth));
},
},
};
};
2 changes: 1 addition & 1 deletion components/mailboxlayer/mailboxlayer.app.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ export default {
console.log(Object.keys(this.$auth));
},
},
};
};
2 changes: 1 addition & 1 deletion components/nextdoor/nextdoor.app.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ export default {
console.log(Object.keys(this.$auth));
},
},
};
};
63 changes: 63 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pnpm-workspace.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ packages:
- 'platform/**'
- 'packages/**'
- 'docs-v2/**'
- 'scripts/**'
1 change: 1 addition & 0 deletions scripts/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
dist
.env*
17 changes: 17 additions & 0 deletions scripts/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "scripts",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@supabase/supabase-js": "^2.45.6",
"dotenv": "^16.4.7",
"glob": "^11.0.0"
}
}
61 changes: 61 additions & 0 deletions scripts/upload-app-files-to-supabase.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { createClient } from '@supabase/supabase-js'
import { readFile } from 'fs/promises'
import { glob } from 'glob'
import path from 'path'
import 'dotenv/config'

if (!process.env.SUPABASE_URL || !process.env.SUPABASE_KEY) {
throw new Error('Missing required environment variables SUPABASE_URL and/or SUPABASE_KEY')
}

// Configure Supabase client
const supabase = createClient(
process.env.SUPABASE_URL,
process.env.SUPABASE_KEY
)

async function findAndUploadFiles() {
try {
// Find all .app.mjs files recursively
const files = await glob('../components/**/*.app.mjs', {
// No need to recurse into the standard subdirs, since app files are always at
// the root of the components/${app} directory
ignore: ['node_modules/**', 'actions/**', 'common/**', 'sources/**'],
absolute: true
})
Comment on lines +20 to +25
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Make path resolution more robust.

The relative path '../components' could break if the script is run from a different directory. Consider using path.resolve to create an absolute path relative to the script's location.

-    const files = await glob('../components/**/*.app.mjs', {
+    const files = await glob(path.resolve(__dirname, '../components/**/*.app.mjs'), {

Committable suggestion skipped: line range outside the PR's diff.


console.log(`Found ${files.length} .app.mjs files`)

for (const filePath of files) {
try {
const content = await readFile(filePath, 'utf8')

const filename = path.basename(filePath)
const app = filename.replace('.app.mjs', '')

const { data, error } = await supabase
.from('registry_app_files')
.insert({
app: app,
app_file: content
})

if (error) {
console.error(`Error uploading ${filename}:`, error)
continue
}

console.log(`Successfully uploaded ${filename}`)
} catch (err) {
console.error(`Error processing ${filePath}:`, err)
}
}
Comment on lines +29 to +52
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add content validation and duplicate handling.

Consider these improvements:

  1. Validate file content before upload
  2. Handle potential duplicates in the database
         const content = await readFile(filePath, 'utf8')
+        // Validate content
+        if (!content.trim()) {
+          throw new Error('Empty file')
+        }
 
         const filename = path.basename(filePath)
         const app = filename.replace('.app.mjs', '')
 
         const { data, error } = await supabase
           .from('registry_app_files')
-          .insert({
+          .upsert({
             app: app,
             app_file: content
-          })
+          }, {
+            onConflict: 'app',
+            ignoreDuplicates: false
+          })
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
for (const filePath of files) {
try {
const content = await readFile(filePath, 'utf8')
const filename = path.basename(filePath)
const app = filename.replace('.app.mjs', '')
const { data, error } = await supabase
.from('registry_app_files')
.insert({
app: app,
app_file: content
})
if (error) {
console.error(`Error uploading ${filename}:`, error)
continue
}
console.log(`Successfully uploaded ${filename}`)
} catch (err) {
console.error(`Error processing ${filePath}:`, err)
}
}
for (const filePath of files) {
try {
const content = await readFile(filePath, 'utf8')
// Validate content
if (!content.trim()) {
throw new Error('Empty file')
}
const filename = path.basename(filePath)
const app = filename.replace('.app.mjs', '')
const { data, error } = await supabase
.from('registry_app_files')
.upsert({
app: app,
app_file: content
}, {
onConflict: 'app',
ignoreDuplicates: false
})
if (error) {
console.error(`Error uploading ${filename}:`, error)
continue
}
console.log(`Successfully uploaded ${filename}`)
} catch (err) {
console.error(`Error processing ${filePath}:`, err)
}
}

} catch (err) {
console.error('Error finding files:', err)
}
}

// Run the script
findAndUploadFiles()
.then(() => console.log('Upload complete'))
.catch(err => console.error('Script failed:', err))
Loading