Skip to content

Instantly share code, notes, and snippets.

@kellymears
Last active May 15, 2020 06:24
Show Gist options
  • Select an option

  • Save kellymears/1310f6784c7dfd4305802d8408f087ab to your computer and use it in GitHub Desktop.

Select an option

Save kellymears/1310f6784c7dfd4305802d8408f087ab to your computer and use it in GitHub Desktop.
const chalk = require('chalk')
const fs = require('fs-extra')
/**
* Write a remote image to the disk
*
* @param {string} url
* @param {string} path
*/
const download = async (url, path) => {
console.log(`
${chalk.blueBright(url)} => ${chalk.magentaBright(path)}...\n
`)
fs.ensureFileSync(path)
const res = await fetch(url)
const fileStream = fs.createWriteStream(path)
await new Promise((resolve, reject) => {
res.body.pipe(fileStream)
res.body.on('error', err => {
console.error(chalk.red(err))
reject(err)
})
fileStream.on('finish', function () {
resolve()
})
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment