diff --git a/deps.ts b/deps.ts index ea06677..226778b 100644 --- a/deps.ts +++ b/deps.ts @@ -2,8 +2,7 @@ import "https://deno.land/std@0.155.0/dotenv/load.ts" export * as pogo from "https://deno.land/x/pogo@v0.5.2/main.ts" export * as validatte from "https://deno.land/x/validatte@v0.1.1/mod.ts" -export * as supabase from "npm:@supabase/supabase-js" -export { default as pika } from "npm:pika-id" + export { join, normalize, @@ -15,4 +14,11 @@ export { ensureFileSync, } from "https://deno.land/std@0.135.0/fs/mod.ts"; -export { default as serve } from "https://deno.land/x/static_files@1.1.6/mod.ts"; \ No newline at end of file +export { default as serve } from "https://deno.land/x/static_files@1.1.6/mod.ts"; + +export { S3, S3Bucket } from "https://deno.land/x/s3@0.5.0/mod.ts" +export { wait } from "https://deno.land/x/wait/mod.ts"; + +// NPM Modules +export * as supabase from "npm:@supabase/supabase-js" +export { default as pika } from "npm:pika-id" \ No newline at end of file diff --git a/scripts.yml b/scripts.yml index 83610cf..8b0e397 100644 --- a/scripts.yml +++ b/scripts.yml @@ -16,4 +16,9 @@ scripts: cmd: deno run --unstable --allow-net --no-check --no-prompt ./scripts/ipv4.ts s3: desc: Test S3 Module - cmd: deno run --unstable --allow-net --no-check --no-prompt ./scripts/s3.ts + cmd: deno run --unstable -A --no-check --no-prompt ./scripts/s3.ts + gen: + desc: Run all the Commands that Generate Files + cmd: + - vr s3 + - vr routes diff --git a/scripts/s3.ts b/scripts/s3.ts index 0156134..7da73a7 100644 --- a/scripts/s3.ts +++ b/scripts/s3.ts @@ -1,8 +1,80 @@ -import { S3, S3Bucket } from "https://deno.land/x/s3@0.5.0/mod.ts"; +import { string } from "https://raw.githubusercontent.com/soremwar/deno_types/b5a146610e2c97c1612371fcf610b541f950ee73/prop-types/v15.7.2/prop-types.d.ts"; +import { S3, S3Bucket } from "../deps.ts"; +import { wait } from "../deps.ts" + +const S3inner = wait({ + text: "Generating S3 Index...", + spinner: "aesthetic" +}) + +interface S3File { + url: string + key: string | undefined +} const s3 = new S3({ - accessKeyID: Deno.env.get("AWS_ACCESS_KEY_ID")!, - secretKey: Deno.env.get("AWS_SECRET_ACCESS_KEY")!, - region: "us-east-1", + accessKeyID: Deno.env.get("S3_ACCESS_KEY")!, + secretKey: Deno.env.get("S3_SECRET_KEY")!, endpointURL: Deno.env.get("S3_ENDPOINT_URL"), + region: "eu-central-1" }); + + +let DreamlandBucket = s3.getBucket(Deno.env.get("S3_BUCKET") || "dreamland-assets.lio.systems") + +const bucketDomain = Deno.env.get("S3_DOMAIN") || Deno.env.get("S3_ENDPOINT_URL") + +const list = DreamlandBucket.listAllObjects({ + batchSize: 5 +}) + +let Files: S3File[] = [] + +for await (const obj of list) { + if (obj.size === 0) { + S3inner.text = `${bucketDomain}/${obj.key} - Is a Folder, Continue` + continue + } + + Files.push({ + url: `${bucketDomain}/${obj.key}`, + key: obj.key + }) +} + +function createFile(files: S3File[]) { + let string = + `// This File is Autogenerated by scripts/routes.ts +// Do not edit this File manually +// run \`vr s3\` to regenerate this File + +const S3Files = [ + ${files.map(file => (`{ key: "${file.key}", url: "${file.url}" }`)).join(",\n ")} +] + +export default S3Files + ` + + return string +} + + +async function write(files: S3File[], file: string | URL) { + const content = createFile(files) + + await Deno.writeTextFile(`./src/${file}`, content) +} + + +if (import.meta.main) { + + try { + await write(Files, `s3.generated.ts`) + } catch (error) { + console.error(error) + S3inner.fail() + Deno.exit(1) + } + S3inner.succeed("S3 Index generated.") + Deno.exit(0) +} \ No newline at end of file