diff --git a/deps.ts b/deps.ts index 226778b..99dddd9 100644 --- a/deps.ts +++ b/deps.ts @@ -9,9 +9,13 @@ export { relative, } from "https://deno.land/std@0.135.0/path/mod.ts"; +export { download } from "https://deno.land/x/download@v1.0.1/mod.ts"; + + export { ensureFile, ensureFileSync, + ensureDirSync, } 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"; diff --git a/scripts.yml b/scripts.yml index ed8cd61..6462973 100644 --- a/scripts.yml +++ b/scripts.yml @@ -23,6 +23,7 @@ scripts: cmd: - vr s3 - vr routes + - vr download routes: desc: Generate the Routes used for the Project @@ -31,3 +32,6 @@ scripts: s3: desc: Generate S3 Routes cmd: deno run --unstable -A --no-check --no-prompt ./scripts/s3.ts + download: + desc: Download Files from S3 + cmd: deno run --unstable -A ./scripts/download.ts diff --git a/scripts/download.ts b/scripts/download.ts new file mode 100644 index 0000000..3454025 --- /dev/null +++ b/scripts/download.ts @@ -0,0 +1,44 @@ +import { ensureFileSync, ensureDirSync, download, wait } from "../deps.ts"; +import S3Files from "../src/generated/s3.ts"; + +if (!S3Files) { + throw new Error('This File depends on the Index generated by "vr s3", please run that and try again.') +} + +const IMAGE_FOLDER_NAME = 'dreamland-assets' + +const ImageSpinner = wait({ + text: 'Downloading Files from S3...', + spinner: 'aesthetic' +}) + +async function downloadFiles(Files: typeof S3Files) { + + + ensureDirSync(`../src/generated/${IMAGE_FOLDER_NAME}`) + try { + await Files.forEach(async (file) => { + ImageSpinner.text = `Downloading ${file.key}` + ensureFileSync(`./src/generated/${IMAGE_FOLDER_NAME}/${file.key}`) + + return await download(file.url, { + dir: `./src/generated/${IMAGE_FOLDER_NAME}`, + file: file.key, + }) + }) + } catch (error) { + return console.error(error) + } +} + +if (import.meta.main) { + try { + await downloadFiles(S3Files) + } catch (error) { + console.error(error) + ImageSpinner.fail("Something went wrong.") + Deno.exit(1) + } + ImageSpinner.succeed("Downloaded Files from S3.") + Deno.exit(0) +} \ No newline at end of file