Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

metalsmith-downloader

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

metalsmith-downloader - npm Package Compare versions

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",

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc