Skip to content

Commit

Permalink
Merge 8177908 into 7efeb76
Browse files Browse the repository at this point in the history
  • Loading branch information
blond committed Dec 3, 2016
2 parents 7efeb76 + 8177908 commit 7b2e9ce
Showing 1 changed file with 20 additions and 18 deletions.
38 changes: 20 additions & 18 deletions techs/levels.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const path = require('path');
const fs = require('fs');
const stream = require('stream');

const vow = require('vow');
const enb = require('enb');
Expand Down Expand Up @@ -174,34 +175,35 @@ module.exports = buildFlow.create()
_forceScanLevel: function (levelpath) {
return new vow.Promise((resolve, reject) => {
const data = {};
const promises = [];

walk([levelpath])
.on('data', function (file) {
const id = stringifyEntity(file.entity);

promises.push(new Promise((resolve, reject) => {
fs.stat(file.path, (err, stats) => {
if (err) {
return reject(err);
}
.on('error', reject)
.pipe(new stream.Writable({
objectMode: true,
write: function (file, encoding, callback) {
tryCatch(() => {
const id = stringifyEntity(file.entity);
const stats = fs.statSync(file.path);

file.isDirectory = stats.isDirectory();
file.mtime = stats.mtime.getTime();

(data[id] || (data[id] = [])).push(file);

resolve();
});
}));
})
callback();
}, callback);
}
}))
.on('error', reject)
.on('end', () => {
Promise.all(promises)
.catch(reject)
.then(() => resolve(data));
});
.on('finish', () => resolve(data));
});
}
})
.createTech();

// try-catch optimization
function tryCatch(tryFn, catchFn) {
try {
tryFn();
} catch (e) { catchFn(e); }
}

0 comments on commit 7b2e9ce

Please sign in to comment.