metalsmith-downloader
Advanced tools
Comparing version 0.1.0 to 0.1.1
67
index.js
@@ -6,2 +6,13 @@ var debug = require('debug')('metalsmith-downloader'); | ||
function checkFileExists(filename) { | ||
return new Promise(function(resolve, reject) { | ||
fs.stat(filename, function(err, stats) { | ||
if (err) | ||
resolve(false); | ||
else | ||
resolve(stats.isFile()); | ||
}); | ||
}); | ||
} | ||
function downloadFile(filename, url) { | ||
@@ -75,28 +86,46 @@ return new Promise(function(resolve, reject) { | ||
module.exports = function downloader(options) { | ||
var incremental = options && options.incremental; | ||
return function(files, metalsmith, done) { | ||
var dest = metalsmith.destination(); | ||
Promise.all( | ||
Object.keys(files).map(function(filename) { | ||
var file = files[filename]; | ||
if (!file || !file.contentsUrl) | ||
return Promise.resolve(); | ||
var downloadableFiles = {}; | ||
Object.keys(files).forEach(function(filename) { | ||
var file = files[filename]; | ||
if (!file || !file.contentsUrl) | ||
return; | ||
debug('Removing file ' + filename + ' from Metalsmith'); | ||
delete files[filename]; | ||
debug('Removing file ' + filename + ' from Metalsmith'); | ||
delete files[filename]; | ||
debug('Downloading file ' + filename + ' from ' + file.contentsUrl); | ||
downloadableFiles[filename] = file; | ||
}); | ||
Promise.all( | ||
Object.keys(downloadableFiles).map(function(filename) { | ||
var filepath = path.resolve(dest, filename); | ||
return downloadFile(filepath, file.contentsUrl) | ||
.then(function() { | ||
if (file.mode) { | ||
debug('Changing mode of file ' + filename); | ||
return chmodFile(filename, file.mode); | ||
var file = downloadableFiles[filename]; | ||
var contentsUrl = file.contentsUrl; | ||
return checkFileExists(filepath) | ||
.then(function(exists) { | ||
if (incremental && exists) { | ||
debug('File ' + filename + ' already exists, not downloading'); | ||
return Promise.resolve(); | ||
} | ||
return Promise.resolve(); | ||
}) | ||
.then(function() { | ||
debug('File ' + filename + ' downloaded successfully'); | ||
}).catch(function(err) { | ||
debug('Error downloading file ' + filename + ': ' + err); | ||
debug('Downloading file ' + filename + ' from ' + contentsUrl); | ||
return downloadFile(filepath, contentsUrl) | ||
.then(function() { | ||
if (file.mode) { | ||
debug('Changing mode of file ' + filename); | ||
return chmodFile(filename, file.mode); | ||
} | ||
return Promise.resolve(); | ||
}) | ||
.then(function() { | ||
debug('File ' + filename + ' downloaded successfully'); | ||
}).catch(function(err) { | ||
debug('Error downloading file ' + filename + ': ' + err); | ||
}); | ||
}); | ||
@@ -103,0 +132,0 @@ }) |
{ | ||
"name": "metalsmith-downloader", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "Download assets dynamically in your Metalsmith build", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
4939
125