use of path.join everywhere

This commit is contained in:
Clément GOUIN
2019-06-20 10:40:48 +02:00
parent fe6cca6453
commit 07dd06a338
8 changed files with 77 additions and 76 deletions
+7 -12
View File
@@ -17,11 +17,6 @@ module.exports = (config) => {
const articles = [];
const log = (status, msg) => {
if (config['test'])
return;
console.log(status, msg);
};
const reload = (callback) => {
fw.fetchArticles((err, list) => {
@@ -31,9 +26,9 @@ module.exports = (config) => {
}
articles.splice(0, articles.length, ...list);
if (articles.length > 0)
log(cons.ok, `loaded ${articles.length} article${articles.length > 1 ? 's' : ''}`);
console.log(cons.ok, `loaded ${articles.length} article${articles.length > 1 ? 's' : ''}`);
else
log(cons.warn, `no articles loaded, check your configuration`);
console.log(cons.warn, `no articles loaded, check your configuration`);
callback(true);
});
};
@@ -42,16 +37,16 @@ module.exports = (config) => {
res.render(path, data, (err, html) => {
if (err) {
res.sendStatus(500);
log(cons.error, `failed to render ${path} : ${err}`);
console.log(cons.error, `failed to render ${path} : ${err}`);
} else
res.status(code).send(html);
});
};
const showError = (path, code, res) => {
const errorPath = `${config['data_dir']}/${config['home']['error']}`;
const showError = (resPath, code, res) => {
const errorPath = path.join(config['data_dir'],config['home']['error']);
if (fs.existsSync(errorPath))
render(res, errorPath, {error: code, path: path}, code);
render(res, errorPath, {error: code, path: resPath}, code);
else
res.sendStatus(code);
};
@@ -79,7 +74,7 @@ module.exports = (config) => {
reload((res) => {
if (res)
app.listen(config['node_port'], () => {
log(cons.ok, `gitblog.md server listening on port ${config['node_port']}`);
console.log(cons.ok, `gitblog.md server listening on port ${config['node_port']}`);
});
});
};
+1 -1
View File
@@ -15,7 +15,7 @@ const merge = (ref, src) => {
module.exports = () => {
try {
let configData = fs.readFileSync('./config.json', {encoding:'UTF-8'});
let configData = fs.readFileSync('config.json', {encoding:'UTF-8'});
let config = JSON.parse(configData);
return merge(refConfig, config);
} catch (error) {
+13 -11
View File
@@ -1,15 +1,16 @@
const fs = require('fs');
const path = require('path');
const getFileTree = (path, cb) => {
const getFileTree = (dir, cb) => {
let list = [];
let remaining = 0;
fs.readdir(path, {withFileTypes: true}, (err, items) => {
fs.readdir(dir, {withFileTypes: true}, (err, items) => {
if (err)
return cb(err);
items.forEach((item) => {
if (item.isDirectory()) {
remaining++;
getFileTree(`${path}/${item.name}`, (err, out) => {
getFileTree(path.join(dir, item.name), (err, out) => {
if (err)
return cb(err);
list.push(...out);
@@ -18,7 +19,7 @@ const getFileTree = (path, cb) => {
cb(null, list);
});
} else {
list.push(`${path}/${item.name}`);
list.push(path.join(dir, item.name));
}
});
if (remaining === 0)
@@ -57,24 +58,25 @@ module.exports = (config) => {
.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);
.filter(matches => matches && matches.length > 1);
if (paths.length === 0)
cb(null, []);
const list = [];
let remaining = 0;
paths.forEach(path => {
paths.forEach(matches => {
const article = {
path: config['data_dir'] + path[0] + config['article']['index'],
year: parseInt(path[1]),
month: parseInt(path[2]),
day: parseInt(path[3])
path: path.join(config['data_dir'], matches[1], matches[2], matches[3], config['article']['index']),
parent: path.join(config['data_dir'], matches[1], matches[2], matches[3]),
year: parseInt(matches[1]),
month: parseInt(matches[2]),
day: parseInt(matches[3])
};
remaining++;
readIndexFile(article.path, config['article']['thumbnail_tag'], (err, info) => {
if (err)
return cb(err);
article.title = info.title || config['article']['default_title'];
article.thumbnail = info.thumbnail ? (config['data_dir'] + path[0] + info.thumbnail) : config['article']['default_thumbnail'];
article.thumbnail = info.thumbnail ? path.join(article.parent, info.thumbnail) : config['article']['default_thumbnail'];
list.push(article);
remaining--;
if (remaining === 0)
+11 -10
View File
@@ -1,29 +1,30 @@
const fs = require('fs');
const path = require('path');
const ncp = require('ncp').ncp;
const copy = (src,dest) => {
ncp(src,dest, function(err){
if(err)
const copy = (src, dest) => {
ncp(src, dest, function (err) {
if (err)
console.error(err);
else
console.log(`copied ${src} to ${dest}`);
});
};
copy('./src/config.default.json','./config.example.json');
copy(path.join('src', 'config.default.json'), 'config.example.json');
if (!fs.existsSync('./data')) {
fs.mkdirSync('./data');
if (!fs.existsSync('data')) {
fs.mkdirSync('data');
copy('./sample_data/home','./data');
copy(path.join('sample_data','home'), 'data');
const pad0 = n => ('0'+n).substr(-2);
const pad0 = n => ('0' + n).substr(-2);
const datetime = new Date();
const dir = `./data/${datetime.getFullYear()}/${pad0(datetime.getMonth()+1)}/${pad0(datetime.getDate())}`;
const dir = path.join('data', datetime.getFullYear().toString(), pad0(datetime.getMonth() + 1), pad0(datetime.getDate()));
if (!fs.existsSync(dir))
fs.mkdirSync(dir, {recursive: true});
copy('./sample_data/article',dir);
copy(path.join('sample_data','article'), dir);
}