Reading files in multiple directories, matching filenames to their data with Node and Promises
I have an array of directories.
var directories = ['/dir1', '/dir2'];
I want to read all the files in these directories and eventually have an object that matches the filenames with base64 / utf8 data. In my case, these files are images. The resulting object might look like this:
var result = {
'/dir1': {
'file1': 'iVBORw0KGgoAAAANSUhEUgAAAOEAAADhCAIAAACx0UUtAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWF...',
'file2': 'iVBORw0KGgoAAAANSUhEUgAAAOEAAADhCAIAAACx0UUtAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWF...'
}
}
I easily implemented this with a callback addon, but when I try to use Promises, I'm not sure how to pass the directory information and filename to the subsequent then () and map () functions.
For the example below, I am using the Bluebird library:
const Promise = require('bluebird'),
fs = Promise.promisifyAll(require('fs'));
var getFiles = function (dir) {
return fs.readdirAsync(dir);
};
Promise.map(directories, function(directory) {
return getFiles(directory)
}).map(function (files) {
// which directory files are these?
return files;
})
The next step is to iterate over the files and read their data.
I don't mind if the answer is with ES6, Bluebird or Q.
source to share
The easiest way with Bluebird is to use props
:
function getFiles(paths) { // an array of file/directory paths
return Promise.props(paths.reduce(function (obj, path) {
obj[path.split("/").pop()] = fs.statAsync(path).then(function (stat) {
if (stat.isDirectory())
return fs.readdirAsync(path).map(function (p) {
return path + "/" + p;
}).then(getFiles);
else if (stat.isFile())
return fs.readFileAsync(path);
}).error(function (e) {
console.error("unable to read " + path + ", because: ", e.message);
// return undefined; implied
});
return obj;
}, {}));
}
source to share
I have no idea about Bluebird, but this is the ES6 Promise approach .
var fs = require('fs');
var directories = ['/dir1', '/dir2'];
var result = {};
Promise.all(directories.map(function(dir) {
result[dir] = {};
return new Promise(function(resolve, reject) {
fs.readdir(dir, function(er, files) {
if (er) {
return reject(er);
}
resolve(files);
});
}).then(function(files) {
return Promise.all(files.map(function(file) {
return new Promise(function(resolve, reject) {
fs.readFile(dir + '/' + file, function(er, data) {
if (er) {
return reject(er);
}
result[dir][file] = data.toString('base64');
resolve();
});
});
}));
});
})).then(function() {
console.log(result);
}).catch(function(er) {
console.log(er);
});
source to share