UNPKG

infopack-cli

Version:

Command line tool for managing infopacks

28 lines (26 loc) 1.05 kB
const path = require('path') const unzipper = require('unzipper') const fetch = require('node-fetch') const fs = require('fs') const Promise = require('bluebird') module.exports = function(url, name) { let dirPath = path.resolve(name) return fetch(url) .then(res => { if(!res.ok) throw new Error('ZIP file could not be downloaded, error code: ' + res.status) return res.buffer() }) .then(buff => unzipper.Open.buffer(buff)) .then(dir => { return Promise.mapSeries(dir.files, (f) => { if(f.type == 'File') { let filePath = path.join(dirPath, f.path.replace('infopack-example-basic-master/', '')) console.log(' ' + filePath) fs.mkdirSync(path.dirname(filePath), { recursive: true }) return f.buffer() .then(b => fs.writeFileSync(filePath, b)) } }) }) // .then(dir => dir.extract({ path: dirPath, verbose: true })) }