1
mirror of https://github.com/jakejarvis/careful-downloader.git synced 2025-04-26 06:35:22 -04:00
careful-downloader/lib/download.js

21 lines
564 B
JavaScript

import stream from "stream";
import { promisify } from "util";
import fs from "fs-extra";
import got from "got";
import debug from "./debug.js";
// Download any file to any destination. Returns a promise.
export default async (url, dest) => {
debug(`Downloading '${url}' to '${dest}'`);
// get remote file and write locally
const pipeline = promisify(stream.pipeline);
const download = pipeline(
got.stream(url, { followRedirect: true }), // GitHub releases redirect to unpredictable URLs
fs.createWriteStream(dest),
);
return download;
};