Flow and modularization refactor. Added duplicates option and applying limit after fetch.

This commit is contained in:
2018-04-23 01:50:07 +02:00
parent dc3f3c8440
commit c66c011ff4
13 changed files with 141 additions and 119 deletions

38
save/profileDetails.js Normal file
View File

@@ -0,0 +1,38 @@
'use strict';
const config = require('config');
const interpolate = require('../interpolate.js');
const fetchItem = require('../fetch/item.js');
const textToStream = require('./textToStream.js');
const save = require('./save.js');
function saveProfileDetails(user) {
if(config.library.profile.image) {
const filepath = interpolate(config.library.profile.image, user, null, {
// pass profile image as item to interpolate extension variable
url: user.profile.image
});
fetchItem(user.profile.image).then(stream => save(filepath, stream)).catch(error => {
console.log('\x1b[33m%s\x1b[0m', `Could not save profile image for '${user.name}': ${error}`);
});
}
if(config.library.profile.description) {
if(user.profile.description) {
const filepath = interpolate(config.library.profile.description, user);
const stream = textToStream(user.profile.description);
save(filepath, stream).catch(error => {
console.log('\x1b[33m%s\x1b[0m', `Could not save profile description for '${user.name}': ${error}`);
});
} else {
console.log('\x1b[33m%s\x1b[0m', `No profile description for '${user.name}'`);
}
}
return user;
};
module.exports = saveProfileDetails;

24
save/save.js Normal file
View File

@@ -0,0 +1,24 @@
'use strict';
const fs = require('fs-extra');
const path = require('path');
function save(filepath, stream) {
return Promise.resolve().then(() => {
return fs.ensureDir(path.dirname(filepath));
}).then(() => {
const file = fs.createWriteStream(filepath);
return new Promise((resolve, reject) => {
stream.pipe(file).on('error', error => {
reject(error);
}).on('finish', () => {
console.log('\x1b[32m%s\x1b[0m', `Saved '${filepath}'`);
resolve(filepath);
});
});
});
};
module.exports = save;

14
save/textToStream.js Normal file
View File

@@ -0,0 +1,14 @@
'use strict';
const Readable = require('stream').Readable;
function textToStream(text) {
const stream = new Readable();
stream.push(text);
stream.push(null);
return stream;
};
module.exports = textToStream;