Skip to content

Commit

Permalink
changes for better code
Browse files Browse the repository at this point in the history
  • Loading branch information
Kgitman committed Aug 6, 2024
1 parent c476893 commit e0dfd77
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 34 deletions.
2 changes: 2 additions & 0 deletions .env.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SUPABASE_URL=
SUPABASE_KEY=
8 changes: 8 additions & 0 deletions .gitignore
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
7 changes: 7 additions & 0 deletions supabase/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Supabase
.branches
.temp
.env
node_modules
config.toml
seed.sql
86 changes: 53 additions & 33 deletions supabase/functions/fetch_package/index.ts
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`);
Expand All @@ -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.');
Expand All @@ -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('^')) {
Expand All @@ -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,
Expand All @@ -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);
Expand All @@ -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 };
13 changes: 13 additions & 0 deletions supabase/functions/fetch_package/supabase_client.ts
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;
}
2 changes: 1 addition & 1 deletion supabase/functions/tests/index_test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { storeInSupabase } from '../tests/index_test.ts';
import { storeInSupabase } from '../fetch_package/index.ts';
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";


Expand Down

0 comments on commit e0dfd77

Please sign in to comment.