download
Advanced tools
Comparing version 1.0.1 to 1.0.2
63
index.js
@@ -43,3 +43,7 @@ 'use strict'; | ||
dest = dest || process.cwd(); | ||
if (typeof dest === 'object') { | ||
opts = dest; | ||
dest = undefined; | ||
} | ||
opts = opts || {}; | ||
@@ -94,2 +98,3 @@ | ||
var files = []; | ||
var request = require('request'); | ||
@@ -119,2 +124,9 @@ var self = this; | ||
res.on('end', function () { | ||
files.push({ url: obj.url, contents: Buffer.concat(ret) }); | ||
if (!obj.dest) { | ||
done(); | ||
return; | ||
} | ||
if (opts.extract) { | ||
@@ -127,7 +139,7 @@ return self._extract(Buffer.concat(ret), obj.dest, opts, function (err) { | ||
done(err); | ||
done(); | ||
}); | ||
} | ||
fs.outputFile(path.join(obj.dest, name), Buffer.concat(ret), function (err) { | ||
self._write(Buffer.concat(ret), path.join(obj.dest, name), opts, function (err) { | ||
if (err) { | ||
@@ -138,13 +150,2 @@ done(err); | ||
if (opts.mode) { | ||
return fs.chmod(path.join(obj.dest, name), opts.mode, function (err) { | ||
if (err) { | ||
done(err); | ||
return; | ||
} | ||
done(); | ||
}); | ||
} | ||
done(); | ||
@@ -160,3 +161,3 @@ }); | ||
cb(); | ||
cb(null, files); | ||
}); | ||
@@ -178,2 +179,34 @@ }; | ||
/** | ||
* Write to file | ||
* | ||
* @param {Buffer} buf | ||
* @param {String} dest | ||
* @param {Object} opts | ||
* @param {Function} cb | ||
* @api private | ||
*/ | ||
Download.prototype._write = function (buf, dest, opts, cb) { | ||
fs.outputFile(dest, buf, function (err) { | ||
if (err) { | ||
cb(err); | ||
return; | ||
} | ||
if (opts.mode) { | ||
return fs.chmod(dest, opts.mode, function (err) { | ||
if (err) { | ||
cb(err); | ||
return; | ||
} | ||
cb(); | ||
}); | ||
} | ||
cb(); | ||
}); | ||
}; | ||
/** | ||
* Extract archive | ||
@@ -180,0 +213,0 @@ * |
{ | ||
"name": "download", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "Download and extract files effortlessly", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -26,3 +26,3 @@ # download [![Build Status](https://travis-ci.org/kevva/download.svg?branch=master)](https://travis-ci.org/kevva/download) | ||
download.run(function (err) { | ||
download.run(function (err, files) { | ||
if (err) { | ||
@@ -32,3 +32,4 @@ throw err; | ||
console.log('Download complete!'); | ||
console.log(files); | ||
=> [{ url: http://example.com/foo.zip, contents: <Buffer 50 4b 03 ...>, ... }] | ||
}); | ||
@@ -47,3 +48,3 @@ ``` | ||
Add a file to download. The `file` argument accepts a `String` containing a URL | ||
or an `Object` with a URL and a desired name. For example `{ url: http://example.com/file.zip, name: 'foo.zip' }`. | ||
or an `Object` with a URL and a desired name. For example `{ url: http://example.com/file.zip, name: 'foo.zip' }`. If you don't supply a `dest` no files will be written to the disk. | ||
@@ -50,0 +51,0 @@ Options defined here will only apply to the specified file. |
7906
194
91