-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
84 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
SUPABASE_URL= | ||
SUPABASE_KEY= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
.vscode/launch.json | ||
.vscode/settings.json | ||
.vscode/extensions.json | ||
node_modules | ||
migrate | ||
deno.lock | ||
package.json | ||
.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Supabase | ||
.branches | ||
.temp | ||
.env | ||
node_modules | ||
config.toml | ||
seed.sql |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,4 @@ | ||
import { createClient } from "https://esm.sh/@supabase/[email protected]"; | ||
|
||
// Supabase credentials | ||
const SUPABASE_URL = "YOUR_SUPABASE_URL"; | ||
const SUPABASE_KEY = "YOUR_SUPABASE_ANON_KEY"; | ||
|
||
// Create a Supabase client | ||
const supabase = createClient(SUPABASE_URL, SUPABASE_KEY); | ||
import { getSupabaseClient } from './supabase_client.ts'; | ||
|
||
async function fetchPackagistData(packageName: string) { | ||
const response = await fetch(`https://repo.packagist.org/p2/${packageName}.json`); | ||
|
@@ -16,7 +9,7 @@ async function fetchPackagistData(packageName: string) { | |
return data; | ||
} | ||
|
||
async function storeInSupabase(packageData: any) { | ||
async function storeInSupabase(supabaseClient: any, packageData: any) { | ||
if (!packageData || !packageData.packages) { | ||
console.error('Invalid package data structure:', packageData); | ||
throw new Error('Invalid package data structure.'); | ||
|
@@ -25,24 +18,47 @@ async function storeInSupabase(packageData: any) { | |
const packageName = Object.keys(packageData.packages)[0]; | ||
const versions = packageData.packages[packageName]; | ||
|
||
// Check if 'core-lib' exists in any of the versions | ||
let hasCoreLib = false; | ||
for (const version of versions) { | ||
if (version.require && version.require['mautic/core-lib']) { | ||
hasCoreLib = true; | ||
break; | ||
} | ||
} | ||
|
||
// Skip the entire package only if 'core-lib' is not found in any version | ||
if (!hasCoreLib) { | ||
console.error('Skipping package due to missing core-lib:', packageName); | ||
return; | ||
} | ||
|
||
for (const version of versions) { | ||
version.name = packageName; | ||
//add on | ||
|
||
const { name, description, homepage, version: ver, version_normalized, license, authors, source, dist, type, support, funding, time, require } = version; | ||
|
||
// if (!ver || !require) { | ||
// console.error('Skipping package due to missing version or require:', name); | ||
// continue; | ||
// } | ||
|
||
// Extract the version from 'require' | ||
let smv = ''; | ||
if (require && typeof require === 'object') { | ||
for (const key in require) { | ||
if (key === 'mautic/core-lib' && typeof require[key] === 'string') { | ||
smv = require[key]; | ||
break; // Stop after finding 'lib' | ||
} | ||
} | ||
} | ||
let storedversions: string[] = []; // Array of arrays to store versions | ||
let smv = ''; | ||
if (require && typeof require === 'object') { | ||
for (const key in require) { | ||
if (key === 'mautic/core-lib') { | ||
// Handle cases where 'core-lib' is an object or string | ||
if (typeof require[key] === 'string') { | ||
smv = require[key]; | ||
} else if (typeof require[key] === 'object' && require[key]['*']) { | ||
smv = require[key]['*']; | ||
} | ||
break; | ||
} | ||
} | ||
} | ||
|
||
let storedversions: string[] = []; | ||
const constraints = smv.split('|'); | ||
for (const constraint of constraints) { | ||
if (constraint.startsWith('^')) { | ||
|
@@ -53,11 +69,11 @@ async function storeInSupabase(packageData: any) { | |
} | ||
storedversions.push(...patchVersions); | ||
} else { | ||
storedversions.push(constraint); // Single element array for non-caret constraints | ||
storedversions.push(constraint); | ||
} | ||
} | ||
|
||
const { data, error } = await supabase | ||
const { data, error } = await supabaseClient | ||
.from('packages') | ||
.upsert([{ | ||
name, | ||
|
@@ -76,7 +92,7 @@ async function storeInSupabase(packageData: any) { | |
require, | ||
smv, | ||
storedversions | ||
}], { onConflict: 'name, version'}); | ||
}], { onConflict: 'name, version' }); | ||
|
||
if (error) { | ||
console.error('Error inserting data:', error); | ||
|
@@ -86,31 +102,35 @@ async function storeInSupabase(packageData: any) { | |
} | ||
} | ||
|
||
// Main function to fetch data and store it in database | ||
async function fetchPackages(url: string) { | ||
async function fetchPackages(url: string, supabaseClient: any) { | ||
try { | ||
const packageList = await fetch(url); | ||
if (!packageList.ok){ | ||
throw new Error(`Error fetching package list: ${packageList.statusText}`); | ||
} | ||
const packageNames = await packageList.json(); | ||
for (const packageName of packageNames.packageNames){ | ||
const packageData = await fetchPackagistData(packageName); | ||
await storeInSupabase(packageData); | ||
const packageData = await fetchPackagistData(packageName); | ||
await storeInSupabase(supabaseClient, packageData); | ||
} | ||
} catch (error) { | ||
console.error('Error:', error); | ||
} | ||
} | ||
async function main(){ | ||
|
||
async function main() { | ||
const supabaseClient = getSupabaseClient(); | ||
const urls = [ | ||
"https://packagist.org/packages/list.json?type=mautic-plugin", | ||
"https://packagist.org/packages/list.json?type=mautic-theme", | ||
"https://packagist.org/packages/list.json?type=mautic-theme", | ||
]; | ||
for (const url of urls) { | ||
await fetchPackages(url); | ||
await fetchPackages(url, supabaseClient); | ||
} | ||
} | ||
|
||
main(); | ||
export { fetchPackagistData, storeInSupabase } | ||
if (import.meta.main) { | ||
main(); | ||
} | ||
|
||
export { fetchPackagistData, storeInSupabase }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { createClient } from "https://esm.sh/@supabase/[email protected]"; | ||
import "jsr:@std/dotenv/load"; | ||
|
||
// Supabase credentials | ||
const SUPABASE_URL = Deno.env.get("SUPABASE_URL") as string; | ||
const SUPABASE_KEY = Deno.env.get("SUPABASE_KEY") as string; | ||
|
||
// Create a Supabase client | ||
const supabase = createClient(SUPABASE_URL, SUPABASE_KEY); | ||
|
||
export function getSupabaseClient() { | ||
return supabase; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters