file_walker.js detect articles in data file tree

This commit is contained in:
Clément GOUIN
2019-06-19 16:27:49 +02:00
parent 15761551e7
commit 20b4d0353f
8 changed files with 227 additions and 8 deletions
+1 -1
View File
@@ -1,4 +1,4 @@
const refConfig = require('./default_config.json');
const refConfig = require('./config.default.json');
const fs = require('fs');
const merge = function (ref, src) {
+55
View File
@@ -0,0 +1,55 @@
const fs = require('fs');
const fileTree = (path, cb) => {
let list = [];
let remaining = 0;
fs.readdir(path, {withFileTypes: true}, (err, items) => {
if (err)
return cb(err);
items.forEach((item) => {
if (item.isDirectory()) {
remaining++;
fileTree(`${path}/${item.name}`, (err, out) => {
if (err)
return cb(err);
list.push(...out);
remaining--;
if (remaining === 0)
cb(null, list);
});
} else {
list.push(`${path}/${item.name}`);
}
});
if (remaining === 0)
cb(null, list);
});
};
module.exports = (config) => {
return {
fileTree: config['test'] ? fileTree : undefined,
fetchArticles: (cb) => {
fileTree(config['data_dir'], (err, fileList) => {
if (err)
return cb(err);
const paths = fileList
.map(path => path.substr(config['data_dir'].length))
.filter(path => path.indexOf(config['article']['index']) === path.length - config['article']['index'].length)
.map(path => path.substr(0, path.length - config['article']['index'].length))
.map(path => path.match(/^\/(\d{4})\/(\d{2})\/(\d{2})\/$/))
.filter(path => path && path.length > 1);
const list = [];
paths.forEach(path => {
list.push({
path: config['data_dir'] + path[0] + config['article']['index'],
year: parseInt(path[1]),
month: parseInt(path[2]),
day: parseInt(path[3])
});
});
cb(null, list);
});
}
};
};