Created
March 12, 2021 15:16
-
-
Save HR/9c03ccc4128882c0abdef5354570df72 to your computer and use it in GitHub Desktop.
Deno CLI to download JSON links to directory
This file contains hidden or 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
import { download, Destination } from 'https://deno.land/x/download/mod.ts' | |
function error(msg) { | |
console.error(msg) | |
Deno.exit(1) | |
} | |
/** | |
* @param file The JSON file path | |
* @param path The JSON object path | |
* @param dest The download destination dir (optional, defaults to current) | |
*/ | |
const { | |
args: [file, path, dest = './'], | |
} = Deno | |
if (!file) error('No file specified') | |
if (!path) error('No JSON path specified') | |
const json = Deno.readTextFile(file) | |
json.then(async (res) => { | |
try { | |
let data = JSON.parse(res) | |
let links = data[path] | |
if (!links) error('JSON path does not exist') | |
// NOTE : You need to ensure that the directory you pass exists. | |
const destination: Destination = { | |
dir: dest, | |
} | |
let downloads = links.map((url: string) => download(url, destination)) | |
await Promise.all(downloads) | |
console.log('Done.') | |
} catch (err) { | |
error(err) | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment