Code formatting with 2 space indent

This commit is contained in:
Clément GOUIN
2019-06-20 14:24:34 +02:00
parent 3df43b4872
commit 92021164c9
11 changed files with 676 additions and 674 deletions
+132 -132
View File
@@ -8,155 +8,155 @@ const path = require('path');
* @type {{warn: string, ok: string, error: string}}
*/
const cons = {
ok: '\x1b[32m✔\x1b[0m %s',
warn: '\x1b[33m⚠\x1b[0m %s',
error: '\x1b[31m✘\x1b[0m %s',
ok: '\x1b[32m✔\x1b[0m %s',
warn: '\x1b[33m⚠\x1b[0m %s',
error: '\x1b[31m✘\x1b[0m %s',
};
module.exports = (config) => {
const fw = require('./file_walker')(config);
const renderer = require('./renderer')(config);
const fw = require('./file_walker')(config);
const renderer = require('./renderer')(config);
// set view engine from configuration
app.set('view engine', config['view_engine']);
// reroute the views folder to the root folder
app.set('views', path.join(__dirname, '..'));
// set view engine from configuration
app.set('view engine', config['view_engine']);
// reroute the views folder to the root folder
app.set('views', path.join(__dirname, '..'));
const articles = {};
const articles = {};
/**
* Fetch articles from the data folder and send success as a response
* @param callback
*/
const reload = (callback) => {
fw.fetchArticles((err, dict) => {
if (err) {
callback(false);
return console.error(cons.error, 'loading articles : ' + err);
}
Object.keys(articles).forEach((key) => delete articles[key]);
Object.keys(dict).forEach((key) => articles[key] = dict[key]);
const nb = Object.keys(articles).length;
if (nb > 0)
console.log(cons.ok, `loaded ${nb} article${nb > 1 ? 's' : ''}`);
else
console.log(cons.warn, `no articles loaded, check your configuration`);
callback(true);
});
};
if (config['test'])
app.reload = reload;
/**
* Render the page with the view engine and catch errors
* @param res
* @param vPath - path of the view
* @param data - data to pass to the view
* @param code - code to send along the page
*/
const render = (res, vPath, data, code = 200) => {
res.render(vPath, data, (err, html) => {
if (err) {
res.sendStatus(500);
console.log(cons.error, `failed to render ${vPath} : ${err}`);
} else
res.status(code).send(html);
});
};
/**
* Show an error with the correct page
* @param resPath - the page of the original error
* @param code - error code
* @param res
*/
const showError = (resPath, code, res) => {
const errorPath = path.join(config['data_dir'], config['home']['error']);
fs.access(errorPath, fs.constants.R_OK, (err) => {
if (err)
res.sendStatus(code);
else
render(res, errorPath, {error: code, path: resPath}, code);
});
};
// home endpoint : send the correct index page or error if not existing
app.get('/', (req, res) => {
const homePath = path.join(config['data_dir'], config['home']['index']);
fs.access(homePath, fs.constants.R_OK, (err) => {
if (err)
showError(req.path, 404, res);
else
render(res, homePath, {articles: Object.values(articles)});
});
/**
* Fetch articles from the data folder and send success as a response
* @param callback
*/
const reload = (callback) => {
fw.fetchArticles((err, dict) => {
if (err) {
callback(false);
return console.error(cons.error, 'loading articles : ' + err);
}
Object.keys(articles).forEach((key) => delete articles[key]);
Object.keys(dict).forEach((key) => articles[key] = dict[key]);
const nb = Object.keys(articles).length;
if (nb > 0)
console.log(cons.ok, `loaded ${nb} article${nb > 1 ? 's' : ''}`);
else
console.log(cons.warn, `no articles loaded, check your configuration`);
callback(true);
});
};
if (config['test'])
app.reload = reload;
// catch all article urls and render them
app.get('*', (req, res, next) => {
if (/^\/\d{4}\/\d{2}\/\d{2}\/(\w*\/)?$/.test(req.path)) {
const articlePath = req.path.substr(1, 10);
const article = articles[articlePath];
if (!article)
showError(req.path, 404, res);
else {
renderer.render(path.join(article.realPath, config['article']['index']), (err, html) => {
if (err) {
console.log(cons.error, `failed to render article ${req.path} : ${err}`);
return showError(req.path, 500, res);
}
article.content = html;
const templatePath = path.join(config['data_dir'], config['article']['template']);
fs.access(templatePath, fs.constants.R_OK, (err) => {
if (err) {
console.log(cons.error, `no template found at ${templatePath}`);
showError(req.path, 500, res);
} else
render(res, templatePath, {article: article});
});
});
}
} else {
next();
}
/**
* Render the page with the view engine and catch errors
* @param res
* @param vPath - path of the view
* @param data - data to pass to the view
* @param code - code to send along the page
*/
const render = (res, vPath, data, code = 200) => {
res.render(vPath, data, (err, html) => {
if (err) {
res.sendStatus(500);
console.log(cons.error, `failed to render ${vPath} : ${err}`);
} else
res.status(code).send(html);
});
};
// catch all hidden file type and return 404
app.get('*', (req, res, next) => {
if (config['home']['hidden'].includes(path.extname(req.path)))
showError(req.path, 404, res);
else
next();
/**
* Show an error with the correct page
* @param resPath - the page of the original error
* @param code - error code
* @param res
*/
const showError = (resPath, code, res) => {
const errorPath = path.join(config['data_dir'], config['home']['error']);
fs.access(errorPath, fs.constants.R_OK, (err) => {
if (err)
res.sendStatus(code);
else
render(res, errorPath, {error: code, path: resPath}, code);
});
};
// serve all static files via get
app.get('*', express.static(config['data_dir']));
// catch express.static errors (mostly not found) by displaying 404
app.get('*', (req, res) => {
// home endpoint : send the correct index page or error if not existing
app.get('/', (req, res) => {
const homePath = path.join(config['data_dir'], config['home']['index']);
fs.access(homePath, fs.constants.R_OK, (err) => {
if (err)
showError(req.path, 404, res);
else
render(res, homePath, {articles: Object.values(articles)});
});
});
// catch all other methods and return 400
app.all('*', (req, res) => {
res.status(400).send('bad request');
});
app.use((err, req, res, next) => {
console.log(cons.error, `error when handling ${req.path} request : ${err}`);
console.error(err.stack);
next(err);
});
// must be use in a server.js to start the server
app.start = () => {
reload((res) => {
if (res)
app.listen(config['node_port'], () => {
console.log(cons.ok, `gitblog.md server listening on port ${config['node_port']}`);
});
// catch all article urls and render them
app.get('*', (req, res, next) => {
if (/^\/\d{4}\/\d{2}\/\d{2}\/(\w*\/)?$/.test(req.path)) {
const articlePath = req.path.substr(1, 10);
const article = articles[articlePath];
if (!article)
showError(req.path, 404, res);
else {
renderer.render(path.join(article.realPath, config['article']['index']), (err, html) => {
if (err) {
console.log(cons.error, `failed to render article ${req.path} : ${err}`);
return showError(req.path, 500, res);
}
article.content = html;
const templatePath = path.join(config['data_dir'], config['article']['template']);
fs.access(templatePath, fs.constants.R_OK, (err) => {
if (err) {
console.log(cons.error, `no template found at ${templatePath}`);
showError(req.path, 500, res);
} else
render(res, templatePath, {article: article});
});
});
};
}
} else {
next();
}
});
return app;
// catch all hidden file type and return 404
app.get('*', (req, res, next) => {
if (config['home']['hidden'].includes(path.extname(req.path)))
showError(req.path, 404, res);
else
next();
});
// serve all static files via get
app.get('*', express.static(config['data_dir']));
// catch express.static errors (mostly not found) by displaying 404
app.get('*', (req, res) => {
showError(req.path, 404, res);
});
// catch all other methods and return 400
app.all('*', (req, res) => {
res.status(400).send('bad request');
});
app.use((err, req, res, next) => {
console.log(cons.error, `error when handling ${req.path} request : ${err}`);
console.error(err.stack);
next(err);
});
// must be use in a server.js to start the server
app.start = () => {
reload((res) => {
if (res)
app.listen(config['node_port'], () => {
console.log(cons.ok, `gitblog.md server listening on port ${config['node_port']}`);
});
});
};
return app;
};
+3 -1
View File
@@ -10,7 +10,9 @@
"home": {
"index": "index.ejs",
"error": "error.ejs",
"hidden": [".ejs"]
"hidden": [
".ejs"
]
},
"article": {
"index": "index.md",
+17 -17
View File
@@ -8,24 +8,24 @@ const fs = require('fs');
* @returns {*}
*/
const merge = (ref, src) => {
if (typeof ref !== typeof src) {
return ref;
} else if (typeof ref === 'object') {
const out = {};
Object.keys(ref).forEach((key) =>out[key] = merge(ref[key], src[key]));
return out;
} else {
return src;
}
if (typeof ref !== typeof src) {
return ref;
} else if (typeof ref === 'object') {
const out = {};
Object.keys(ref).forEach((key) => out[key] = merge(ref[key], src[key]));
return out;
} else {
return src;
}
};
module.exports = () => {
try {
let configData = fs.readFileSync('config.json', {encoding:'UTF-8'});
let config = JSON.parse(configData);
return merge(refConfig, config);
} catch (error) {
console.error('Failed to load config.json : '+error);
return refConfig;
}
try {
let configData = fs.readFileSync('config.json', {encoding: 'UTF-8'});
let config = JSON.parse(configData);
return merge(refConfig, config);
} catch (error) {
console.error('Failed to load config.json : ' + error);
return refConfig;
}
};
+78 -78
View File
@@ -7,29 +7,29 @@ const path = require('path');
* @param cb
*/
const getFileTree = (dir, cb) => {
let list = [];
let remaining = 0;
fs.readdir(dir, {withFileTypes: true}, (err, items) => {
if (err)
let list = [];
let remaining = 0;
fs.readdir(dir, {withFileTypes: true}, (err, items) => {
if (err)
return cb(err);
items.forEach((item) => {
if (item.isDirectory()) {
remaining++;
getFileTree(path.join(dir, item.name), (err, out) => {
if (err)
return cb(err);
items.forEach((item) => {
if (item.isDirectory()) {
remaining++;
getFileTree(path.join(dir, item.name), (err, out) => {
if (err)
return cb(err);
list.push(...out);
remaining--;
if (remaining === 0)
cb(null, list);
});
} else {
list.push(path.join(dir, item.name));
}
});
if (remaining === 0)
list.push(...out);
remaining--;
if (remaining === 0)
cb(null, list);
});
} else {
list.push(path.join(dir, item.name));
}
});
if (remaining === 0)
cb(null, list);
});
};
/**
@@ -39,70 +39,70 @@ const getFileTree = (dir, cb) => {
* @param cb
*/
const readIndexFile = (path, thumbnailTag, cb) => {
fs.readFile(path, {encoding: 'UTF-8'}, (err, data) => {
if (err)
return cb(err);
fs.readFile(path, {encoding: 'UTF-8'}, (err, data) => {
if (err)
return cb(err);
let info = {};
let info = {};
const regRes1 = data.match(/(^|[^#])#([^#\r\n]*)\r?\n?$/m);
info.title = regRes1 ? regRes1[2].trim() : undefined;
const regRes1 = data.match(/(^|[^#])#([^#\r\n]*)\r?\n?$/m);
info.title = regRes1 ? regRes1[2].trim() : undefined;
const thumbnailRegEx = new RegExp(`!\\[${thumbnailTag}]\\(([^)]*)\\)`, 'i');
const regRes2 = data.match(thumbnailRegEx);
info.thumbnail = regRes2 ? regRes2[1].trim() : undefined;
const thumbnailRegEx = new RegExp(`!\\[${thumbnailTag}]\\(([^)]*)\\)`, 'i');
const regRes2 = data.match(thumbnailRegEx);
info.thumbnail = regRes2 ? regRes2[1].trim() : undefined;
cb(null, info);
});
cb(null, info);
});
};
module.exports = (config) => {
return {
fileTree: config['test'] ? getFileTree : undefined,
readIndexFile: config['test'] ? readIndexFile : undefined,
/**
* find and read all articles inside the data directory
* @param cb
*/
fetchArticles: (cb) => {
getFileTree(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((matches) => matches && matches.length > 1);
if (paths.length === 0)
cb(null, {});
const articles = {};
let remaining = 0;
paths.forEach((matches) => {
const article = {
path: path.join(matches[1], matches[2], matches[3]),
realPath: path.join(config['data_dir'], matches[1], matches[2], matches[3]),
year: parseInt(matches[1]),
month: parseInt(matches[2]),
day: parseInt(matches[3])
};
article.date = new Date(article.year, article.month, article.day);
remaining++;
readIndexFile(path.join(article.realPath, config['article']['index']), config['article']['thumbnail_tag'], (err, info) => {
if (err)
return cb(err);
article.title = info.title || config['article']['default_title'];
article.thumbnail = info.thumbnail ? path.join(article.path, info.thumbnail) : config['article']['default_thumbnail'];
article.escapedTitle = article.title.toLowerCase().replace(/[^\w]/gm, ' ').trim().replace(/ /gm, '_');
article.url = '/' + path.join(article.path, article.escapedTitle) + '/';
articles[article.path] = article;
remaining--;
if (remaining === 0)
cb(null, articles);
});
});
return {
fileTree: config['test'] ? getFileTree : undefined,
readIndexFile: config['test'] ? readIndexFile : undefined,
/**
* find and read all articles inside the data directory
* @param cb
*/
fetchArticles: (cb) => {
getFileTree(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((matches) => matches && matches.length > 1);
if (paths.length === 0)
cb(null, {});
const articles = {};
let remaining = 0;
paths.forEach((matches) => {
const article = {
path: path.join(matches[1], matches[2], matches[3]),
realPath: path.join(config['data_dir'], matches[1], matches[2], matches[3]),
year: parseInt(matches[1]),
month: parseInt(matches[2]),
day: parseInt(matches[3])
};
article.date = new Date(article.year, article.month, article.day);
remaining++;
readIndexFile(path.join(article.realPath, config['article']['index']), config['article']['thumbnail_tag'], (err, info) => {
if (err)
return cb(err);
article.title = info.title || config['article']['default_title'];
article.thumbnail = info.thumbnail ? path.join(article.path, info.thumbnail) : config['article']['default_thumbnail'];
article.escapedTitle = article.title.toLowerCase().replace(/[^\w]/gm, ' ').trim().replace(/ /gm, '_');
article.url = '/' + path.join(article.path, article.escapedTitle) + '/';
articles[article.path] = article;
remaining--;
if (remaining === 0)
cb(null, articles);
});
});
});
}
};
});
}
};
};
+14 -14
View File
@@ -3,28 +3,28 @@ const path = require('path');
const ncp = require('ncp').ncp;
const copy = (src, dest) => {
ncp(src, dest, function (err) {
if (err)
console.error(err);
else
console.log(`copied ${src} to ${dest}`);
});
ncp(src, dest, function (err) {
if (err)
console.error(err);
else
console.log(`copied ${src} to ${dest}`);
});
};
copy(path.join('src', 'config.default.json'), 'config.example.json');
if (!fs.existsSync('data')) {
fs.mkdirSync('data');
fs.mkdirSync('data');
copy(path.join('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 = path.join('data', datetime.getFullYear().toString(), pad0(datetime.getMonth() + 1), pad0(datetime.getDate()));
const datetime = new Date();
const dir = path.join('data', datetime.getFullYear().toString(), pad0(datetime.getMonth() + 1), pad0(datetime.getDate()));
if (!fs.existsSync(dir))
fs.mkdirSync(dir, {recursive: true});
if (!fs.existsSync(dir))
fs.mkdirSync(dir, {recursive: true});
copy(path.join('sample_data','article'), dir);
copy(path.join('sample_data', 'article'), dir);
}
+11 -11
View File
@@ -2,15 +2,15 @@ const fs = require('fs');
const showdown = require('showdown');
module.exports = (config) => {
const converter = new showdown.Converter(config['showdown']);
return {
render : (file, cb) => {
fs.readFile(file, {encoding:'UTF-8'}, (err, data) => {
if(err)
return cb(err);
const html = converter.makeHtml(data);
cb(null,html);
});
}
};
const converter = new showdown.Converter(config['showdown']);
return {
render: (file, cb) => {
fs.readFile(file, {encoding: 'UTF-8'}, (err, data) => {
if (err)
return cb(err);
const html = converter.makeHtml(data);
cb(null, html);
});
}
};
};
+152 -152
View File
@@ -10,197 +10,197 @@ const testError = 'testerror.ejs';
const testTemplate = 'testtemplate.ejs';
const config = {
'test': true,
'data_dir': dataDir,
'view_engine': 'ejs',
'home': {
'index': testIndex,
'error': testError,
'hidden': ['.ejs','.test']
},
'article': {
'index': 'index.md',
'template' : testTemplate,
'thumbnail_tag': 'thumbnail',
'default_title': 'Untitled',
'default_thumbnail': null
},
'showdown' : {}
'test': true,
'data_dir': dataDir,
'view_engine': 'ejs',
'home': {
'index': testIndex,
'error': testError,
'hidden': ['.ejs', '.test']
},
'article': {
'index': 'index.md',
'template': testTemplate,
'thumbnail_tag': 'thumbnail',
'default_title': 'Untitled',
'default_thumbnail': null
},
'showdown': {}
};
const app = require('../src/app')(config);
beforeEach(() => {
utils.deleteFolderSync(dataDir);
fs.mkdirSync(dataDir);
utils.deleteFolderSync(dataDir);
fs.mkdirSync(dataDir);
});
afterAll(() => {
if (fs.existsSync(dataDir)) {
utils.deleteFolderSync(dataDir);
}
if (fs.existsSync(dataDir)) {
utils.deleteFolderSync(dataDir);
}
});
describe('Test root path', () => {
test('404 no index no error', (done) =>{
request(app).get('/').then((response) =>{
expect(response.statusCode).toBe(404);
done();
});
test('404 no index no error', (done) => {
request(app).get('/').then((response) => {
expect(response.statusCode).toBe(404);
done();
});
test('404 no index but error page', (done) =>{
fs.writeFileSync(path.join(dataDir,testError), 'error <%= error %> at <%= path %>');
request(app).get('/').then((response) =>{
expect(response.statusCode).toBe(404);
expect(response.text).toBe('error 404 at /');
done();
});
});
test('404 no index but error page', (done) => {
fs.writeFileSync(path.join(dataDir, testError), 'error <%= error %> at <%= path %>');
request(app).get('/').then((response) => {
expect(response.statusCode).toBe(404);
expect(response.text).toBe('error 404 at /');
done();
});
test('200 no articles', (done) =>{
fs.writeFileSync(path.join(dataDir,testIndex), 'articles <%= articles.length %>');
request(app).get('/').then((response) =>{
expect(response.statusCode).toBe(200);
expect(response.text).toBe('articles 0');
done();
});
});
test('200 no articles', (done) => {
fs.writeFileSync(path.join(dataDir, testIndex), 'articles <%= articles.length %>');
request(app).get('/').then((response) => {
expect(response.statusCode).toBe(200);
expect(response.text).toBe('articles 0');
done();
});
});
test('200 2 articles', (done) => {
utils.createEmptyDirs([
path.join(dataDir, '2019', '05', '05'),
path.join(dataDir, '2018', '05', '05')
]);
utils.createEmptyFiles([
path.join(dataDir, '2019', '05', '05', 'index.md'),
path.join(dataDir, '2018', '05', '05', 'index.md')
]);
fs.writeFileSync(path.join(dataDir, testIndex), 'articles <%= articles.length %>');
app.reload((res) => {
expect(res).toBe(true);
request(app).get('/').then((response) => {
expect(response.statusCode).toBe(200);
expect(response.text).toBe('articles 2');
done();
});
});
test('200 2 articles', (done) =>{
utils.createEmptyDirs([
path.join(dataDir, '2019', '05', '05'),
path.join(dataDir, '2018', '05', '05')
]);
utils.createEmptyFiles([
path.join(dataDir, '2019', '05', '05','index.md'),
path.join(dataDir, '2018', '05', '05','index.md')
]);
fs.writeFileSync(path.join(dataDir,testIndex), 'articles <%= articles.length %>');
app.reload((res) => {
expect(res).toBe(true);
request(app).get('/').then((response) =>{
expect(response.statusCode).toBe(200);
expect(response.text).toBe('articles 2');
done();
});
});
});
});
});
describe('Test articles rendering', () => {
test('404 article not found', (done) =>{
request(app).get('/2019/05/06/untitled/').then((response) =>{
expect(response.statusCode).toBe(404);
done();
});
test('404 article not found', (done) => {
request(app).get('/2019/05/06/untitled/').then((response) => {
expect(response.statusCode).toBe(404);
done();
});
});
test('500 no template', (done) =>{
utils.createEmptyDirs([path.join(dataDir, '2019', '05', '05'),]);
fs.writeFileSync(path.join(dataDir, '2019', '05', '05','index.md'), '# Hello');
app.reload((res) => {
expect(res).toBe(true);
request(app).get('/2019/05/05/hello/').then((response) =>{
expect(response.statusCode).toBe(500);
done();
});
});
test('500 no template', (done) => {
utils.createEmptyDirs([path.join(dataDir, '2019', '05', '05'),]);
fs.writeFileSync(path.join(dataDir, '2019', '05', '05', 'index.md'), '# Hello');
app.reload((res) => {
expect(res).toBe(true);
request(app).get('/2019/05/05/hello/').then((response) => {
expect(response.statusCode).toBe(500);
done();
});
});
});
test('200 rendered article', (done) =>{
utils.createEmptyDirs([path.join(dataDir, '2019', '05', '05'),]);
fs.writeFileSync(path.join(dataDir, '2019', '05', '05','index.md'), '# Hello');
fs.writeFileSync(path.join(dataDir, testTemplate), '<%- article.content %><%- `<a href="${article.url}">reload</a>` %>');
app.reload((res) => {
expect(res).toBe(true);
request(app).get('/2019/05/05/hello/').then((response) =>{
expect(response.statusCode).toBe(200);
expect(response.text).toBe('<h1 id="hello">Hello</h1><a href="/2019/05/05/hello/">reload</a>');
done();
});
});
test('200 rendered article', (done) => {
utils.createEmptyDirs([path.join(dataDir, '2019', '05', '05'),]);
fs.writeFileSync(path.join(dataDir, '2019', '05', '05', 'index.md'), '# Hello');
fs.writeFileSync(path.join(dataDir, testTemplate), '<%- article.content %><%- `<a href="${article.url}">reload</a>` %>');
app.reload((res) => {
expect(res).toBe(true);
request(app).get('/2019/05/05/hello/').then((response) => {
expect(response.statusCode).toBe(200);
expect(response.text).toBe('<h1 id="hello">Hello</h1><a href="/2019/05/05/hello/">reload</a>');
done();
});
});
});
test('200 other url', (done) =>{
utils.createEmptyDirs([path.join(dataDir, '2019', '05', '05'),]);
utils.createEmptyFiles([
path.join(dataDir, '2019', '05', '05','index.md'),
path.join(dataDir, testTemplate)
]);
app.reload((res) => {
expect(res).toBe(true);
request(app).get('/2019/05/05/anything/').then((response) =>{
expect(response.statusCode).toBe(200);
done();
});
});
test('200 other url', (done) => {
utils.createEmptyDirs([path.join(dataDir, '2019', '05', '05'),]);
utils.createEmptyFiles([
path.join(dataDir, '2019', '05', '05', 'index.md'),
path.join(dataDir, testTemplate)
]);
app.reload((res) => {
expect(res).toBe(true);
request(app).get('/2019/05/05/anything/').then((response) => {
expect(response.statusCode).toBe(200);
done();
});
});
});
test('200 other url 2', (done) =>{
utils.createEmptyDirs([path.join(dataDir, '2019', '05', '05'),]);
utils.createEmptyFiles([
path.join(dataDir, '2019', '05', '05','index.md'),
path.join(dataDir, testTemplate)
]);
app.reload((res) => {
expect(res).toBe(true);
request(app).get('/2019/05/05/').then((response) =>{
expect(response.statusCode).toBe(200);
done();
});
});
test('200 other url 2', (done) => {
utils.createEmptyDirs([path.join(dataDir, '2019', '05', '05'),]);
utils.createEmptyFiles([
path.join(dataDir, '2019', '05', '05', 'index.md'),
path.join(dataDir, testTemplate)
]);
app.reload((res) => {
expect(res).toBe(true);
request(app).get('/2019/05/05/').then((response) => {
expect(response.statusCode).toBe(200);
done();
});
});
});
});
describe('Test static files', () => {
test('404 invalid file no error page', (done) =>{
request(app).get('/somefile.txt').then((response) =>{
expect(response.statusCode).toBe(404);
done();
});
test('404 invalid file no error page', (done) => {
request(app).get('/somefile.txt').then((response) => {
expect(response.statusCode).toBe(404);
done();
});
test('404 invalid file but error page', (done) =>{
fs.writeFileSync(path.join(dataDir,testError), 'error <%= error %> at <%= path %>');
request(app).get('/somefile.txt').then((response) =>{
expect(response.statusCode).toBe(404);
expect(response.text).toBe('error 404 at /somefile.txt');
done();
});
});
test('404 invalid file but error page', (done) => {
fs.writeFileSync(path.join(dataDir, testError), 'error <%= error %> at <%= path %>');
request(app).get('/somefile.txt').then((response) => {
expect(response.statusCode).toBe(404);
expect(response.text).toBe('error 404 at /somefile.txt');
done();
});
test('404 hidden file', (done) =>{
fs.writeFileSync(path.join(dataDir,'somefile.test'), '');
request(app).get('/somefile.test').then((response) =>{
expect(response.statusCode).toBe(404);
done();
});
});
test('404 hidden file', (done) => {
fs.writeFileSync(path.join(dataDir, 'somefile.test'), '');
request(app).get('/somefile.test').then((response) => {
expect(response.statusCode).toBe(404);
done();
});
test('200 valid file', (done) =>{
fs.writeFileSync(`${dataDir}/somefile.txt`, 'filecontent');
request(app).get('/somefile.txt').then((response) =>{
expect(response.statusCode).toBe(200);
expect(response.text).toBe('filecontent');
done();
});
});
test('200 valid file', (done) => {
fs.writeFileSync(`${dataDir}/somefile.txt`, 'filecontent');
request(app).get('/somefile.txt').then((response) => {
expect(response.statusCode).toBe(200);
expect(response.text).toBe('filecontent');
done();
});
});
});
describe('Test other requests', () => {
test('400 POST', (done) =>{
request(app).post('/').then((response) =>{
expect(response.statusCode).toBe(400);
done();
});
test('400 POST', (done) => {
request(app).post('/').then((response) => {
expect(response.statusCode).toBe(400);
done();
});
test('400 PUT', (done) =>{
request(app).put('/').then((response) =>{
expect(response.statusCode).toBe(400);
done();
});
});
test('400 PUT', (done) => {
request(app).put('/').then((response) => {
expect(response.statusCode).toBe(400);
done();
});
test('400 DELETE', (done) =>{
request(app).delete('/').then((response) =>{
expect(response.statusCode).toBe(400);
done();
});
});
test('400 DELETE', (done) => {
request(app).delete('/').then((response) => {
expect(response.statusCode).toBe(400);
done();
});
});
});
+31 -31
View File
@@ -5,50 +5,50 @@ const configFile = 'config.json';
const tmpConfigFile = 'config.temp.json';
beforeAll(() => {
if (fs.existsSync(configFile)) {
fs.renameSync(configFile, tmpConfigFile);
}
expect(fs.existsSync(configFile)).toBeFalsy();
if (fs.existsSync(configFile)) {
fs.renameSync(configFile, tmpConfigFile);
}
expect(fs.existsSync(configFile)).toBeFalsy();
});
afterAll(() => {
if (fs.existsSync(tmpConfigFile)) {
fs.renameSync(tmpConfigFile, configFile);
} else if (fs.existsSync(configFile)) {
fs.unlinkSync(configFile); //remove config file if remaining
}
if (fs.existsSync(tmpConfigFile)) {
fs.renameSync(tmpConfigFile, configFile);
} else if (fs.existsSync(configFile)) {
fs.unlinkSync(configFile); //remove config file if remaining
}
});
test('no config', () => {
if (fs.existsSync(configFile))
fs.unlinkSync(configFile);
expect(fs.existsSync(configFile)).toBeFalsy();
const config = require('../src/config')();
expect(config).toBeDefined();
expect(config['node_port']).toBe(3000);
expect(config['data_dir']).toBe('data');
if (fs.existsSync(configFile))
fs.unlinkSync(configFile);
expect(fs.existsSync(configFile)).toBeFalsy();
const config = require('../src/config')();
expect(config).toBeDefined();
expect(config['node_port']).toBe(3000);
expect(config['data_dir']).toBe('data');
});
test('invalid config ignored', () => {
fs.writeFileSync(configFile, 'invalid JSON');
const config = require('../src/config')();
expect(config).toBeDefined();
expect(config['node_port']).toBe(3000);
expect(config['data_dir']).toBe('data');
fs.writeFileSync(configFile, 'invalid JSON');
const config = require('../src/config')();
expect(config).toBeDefined();
expect(config['node_port']).toBe(3000);
expect(config['data_dir']).toBe('data');
});
test('good config merged', () => {
fs.writeFileSync(configFile, '{"node_port":5000}');
const config = require('../src/config')();
expect(config).toBeDefined();
expect(config['node_port']).toBe(5000);
expect(config['data_dir']).toBe('data');
fs.writeFileSync(configFile, '{"node_port":5000}');
const config = require('../src/config')();
expect(config).toBeDefined();
expect(config['node_port']).toBe(5000);
expect(config['data_dir']).toBe('data');
});
test('wrong config fixed', () => {
fs.writeFileSync(configFile, '{"node_port":"hello","data_dir":"data2"}');
const config = require('../src/config')();
expect(config).toBeDefined();
expect(config['node_port']).toBe(3000);
expect(config['data_dir']).toBe('data2');
fs.writeFileSync(configFile, '{"node_port":"hello","data_dir":"data2"}');
const config = require('../src/config')();
expect(config).toBeDefined();
expect(config['node_port']).toBe(3000);
expect(config['data_dir']).toBe('data2');
});
+200 -200
View File
@@ -7,265 +7,265 @@ const dataDir = 'test_data';
const testIndex = 'testindex.md';
const config = {
'test': true,
'data_dir': dataDir,
'article': {
'index': testIndex,
'default_title': 'Untitled',
'default_thumbnail': 'default.png',
'thumbnail_tag': 'thumbnail'
}
'test': true,
'data_dir': dataDir,
'article': {
'index': testIndex,
'default_title': 'Untitled',
'default_thumbnail': 'default.png',
'thumbnail_tag': 'thumbnail'
}
};
const fw = require('../src/file_walker')(config);
beforeEach(() => {
utils.deleteFolderSync(dataDir);
fs.mkdirSync(dataDir);
utils.deleteFolderSync(dataDir);
fs.mkdirSync(dataDir);
});
afterAll(() => {
if (fs.existsSync(dataDir)) {
utils.deleteFolderSync(dataDir);
}
if (fs.existsSync(dataDir)) {
utils.deleteFolderSync(dataDir);
}
});
describe('Test function fileTree', () => {
test('empty root', (done) => {
fw.fileTree(dataDir, (err, list) => {
expect(err).toBeNull();
expect(list).toBeDefined();
expect(list.length).toBe(0);
done();
});
test('empty root', (done) => {
fw.fileTree(dataDir, (err, list) => {
expect(err).toBeNull();
expect(list).toBeDefined();
expect(list.length).toBe(0);
done();
});
test('empty folders', (done) => {
utils.createEmptyDirs([
path.join(dataDir, 'test', 'test'),
path.join(dataDir, 'test', 'test2'),
path.join(dataDir, 'test2')
]);
fw.fileTree(dataDir, (err, list) => {
expect(err).toBeNull();
expect(list).toBeDefined();
expect(list.length).toBe(0);
done();
});
});
test('empty folders', (done) => {
utils.createEmptyDirs([
path.join(dataDir, 'test', 'test'),
path.join(dataDir, 'test', 'test2'),
path.join(dataDir, 'test2')
]);
fw.fileTree(dataDir, (err, list) => {
expect(err).toBeNull();
expect(list).toBeDefined();
expect(list.length).toBe(0);
done();
});
test('simple files', (done) => {
const fileList = [
path.join(dataDir, 'f1.txt'),
path.join(dataDir, 'f2.txt')
];
utils.createEmptyFiles(fileList);
fw.fileTree(dataDir, (err, list) => {
expect(err).toBeNull();
expect(list).toBeDefined();
expect(list.length).toBe(fileList.length);
expect(list).toEqual(expect.arrayContaining(fileList));
done();
});
});
test('simple files', (done) => {
const fileList = [
path.join(dataDir, 'f1.txt'),
path.join(dataDir, 'f2.txt')
];
utils.createEmptyFiles(fileList);
fw.fileTree(dataDir, (err, list) => {
expect(err).toBeNull();
expect(list).toBeDefined();
expect(list.length).toBe(fileList.length);
expect(list).toEqual(expect.arrayContaining(fileList));
done();
});
test('nested files', (done) => {
utils.createEmptyDirs([
path.join(dataDir, 'test', 'test'),
path.join(dataDir, 'test2')
]);
const fileList = [
path.join(dataDir, 'f1.txt'),
path.join(dataDir, 'test', 'f2.txt'),
path.join(dataDir, 'test', 'test', 'f3.txt'),
path.join(dataDir, 'test2', 'f4.txt')
];
utils.createEmptyFiles(fileList);
fw.fileTree(dataDir, (err, list) => {
expect(err).toBeNull();
expect(list).toBeDefined();
expect(list.length).toBe(fileList.length);
expect(list).toEqual(expect.arrayContaining(fileList));
done();
});
});
test('nested files', (done) => {
utils.createEmptyDirs([
path.join(dataDir, 'test', 'test'),
path.join(dataDir, 'test2')
]);
const fileList = [
path.join(dataDir, 'f1.txt'),
path.join(dataDir, 'test', 'f2.txt'),
path.join(dataDir, 'test', 'test', 'f3.txt'),
path.join(dataDir, 'test2', 'f4.txt')
];
utils.createEmptyFiles(fileList);
fw.fileTree(dataDir, (err, list) => {
expect(err).toBeNull();
expect(list).toBeDefined();
expect(list.length).toBe(fileList.length);
expect(list).toEqual(expect.arrayContaining(fileList));
done();
});
test('invalid root', (done) => {
fw.fileTree('invalid root', (err, list) => {
expect(err).not.toBeNull();
expect(list).not.toBeDefined();
done();
});
});
test('invalid root', (done) => {
fw.fileTree('invalid root', (err, list) => {
expect(err).not.toBeNull();
expect(list).not.toBeDefined();
done();
});
});
});
describe('Test index article reading', () => {
const file = path.join(dataDir, testIndex);
test('invalid file', (done) => {
fw.readIndexFile('invalid file', 'thumbnail', (err, info) => {
expect(err).not.toBeNull();
expect(info).not.toBeDefined();
done();
});
const file = path.join(dataDir, testIndex);
test('invalid file', (done) => {
fw.readIndexFile('invalid file', 'thumbnail', (err, info) => {
expect(err).not.toBeNull();
expect(info).not.toBeDefined();
done();
});
test('correct file', (done) => {
fs.writeFileSync(file, `
});
test('correct file', (done) => {
fs.writeFileSync(file, `
# This is an awesome title !?¤
![custom_thumbnail](./thumbnail.jpg)
this is some text
`);
fw.readIndexFile(file, 'custom_thumbnail', (err, info) => {
expect(err).toBeNull();
expect(info).toBeDefined();
expect(info.title).toBe('This is an awesome title !?¤');
expect(info.thumbnail).toBe('./thumbnail.jpg');
done();
});
fw.readIndexFile(file, 'custom_thumbnail', (err, info) => {
expect(err).toBeNull();
expect(info).toBeDefined();
expect(info.title).toBe('This is an awesome title !?¤');
expect(info.thumbnail).toBe('./thumbnail.jpg');
done();
});
});
test('no title', (done) => {
fs.writeFileSync(file, `
test('no title', (done) => {
fs.writeFileSync(file, `
## This is an awesome title !?¤
![custom_thumbnail](./thumbnail.jpg)
### this is some text
`);
fw.readIndexFile(file, 'custom_thumbnail', (err, info) => {
expect(err).toBeNull();
expect(info).toBeDefined();
expect(info.title).not.toBeDefined();
expect(info.thumbnail).toBe('./thumbnail.jpg');
done();
});
fw.readIndexFile(file, 'custom_thumbnail', (err, info) => {
expect(err).toBeNull();
expect(info).toBeDefined();
expect(info.title).not.toBeDefined();
expect(info.thumbnail).toBe('./thumbnail.jpg');
done();
});
});
test('title at beginning', (done) => {
fs.writeFileSync(file, '#title');
fw.readIndexFile(file, 'custom_thumbnail', (err, info) => {
expect(err).toBeNull();
expect(info).toBeDefined();
expect(info.title).toBe('title');
expect(info.thumbnail).not.toBeDefined();
done();
});
test('title at beginning', (done) => {
fs.writeFileSync(file, '#title');
fw.readIndexFile(file, 'custom_thumbnail', (err, info) => {
expect(err).toBeNull();
expect(info).toBeDefined();
expect(info.title).toBe('title');
expect(info.thumbnail).not.toBeDefined();
done();
});
});
test('no thumbnail', (done) => {
fs.writeFileSync(file, `
test('no thumbnail', (done) => {
fs.writeFileSync(file, `
# This is an awesome title !?¤
![custom_thumbnail](./thumbnail.jpg)
this is some text
`);
fw.readIndexFile(file, 'thumbnail', (err, info) => {
expect(err).toBeNull();
expect(info).toBeDefined();
expect(info.title).toBe('This is an awesome title !?¤');
expect(info.thumbnail).not.toBeDefined();
done();
});
fw.readIndexFile(file, 'thumbnail', (err, info) => {
expect(err).toBeNull();
expect(info).toBeDefined();
expect(info.title).toBe('This is an awesome title !?¤');
expect(info.thumbnail).not.toBeDefined();
done();
});
});
test('multiple thumbnails', (done) => {
fs.writeFileSync(file, `
test('multiple thumbnails', (done) => {
fs.writeFileSync(file, `
# This is an awesome title !?¤
![custom_thumbnail](./thumbnail.jpg)
this is some text
![custom_thumbnail](./thumbnail2.jpg)
`);
fw.readIndexFile(file, 'custom_thumbnail', (err, info) => {
expect(err).toBeNull();
expect(info).toBeDefined();
expect(info.title).toBe('This is an awesome title !?¤');
expect(info.thumbnail).toBe('./thumbnail.jpg');
done();
});
fw.readIndexFile(file, 'custom_thumbnail', (err, info) => {
expect(err).toBeNull();
expect(info).toBeDefined();
expect(info.title).toBe('This is an awesome title !?¤');
expect(info.thumbnail).toBe('./thumbnail.jpg');
done();
});
});
});
describe('Test article fetching', () => {
test('invalid data dir', (done) => {
config['data_dir'] = 'invalid root';
fw.fetchArticles((err, list) => {
expect(err).not.toBeNull();
expect(list).not.toBeDefined();
config['data_dir'] = dataDir;
done();
});
test('invalid data dir', (done) => {
config['data_dir'] = 'invalid root';
fw.fetchArticles((err, list) => {
expect(err).not.toBeNull();
expect(list).not.toBeDefined();
config['data_dir'] = dataDir;
done();
});
test('empty data dir', (done) => {
fw.fetchArticles((err, dict) => {
expect(err).toBeNull();
expect(dict).toBeDefined();
expect(Object.keys(dict).length).toBe(0);
done();
});
});
test('empty data dir', (done) => {
fw.fetchArticles((err, dict) => {
expect(err).toBeNull();
expect(dict).toBeDefined();
expect(Object.keys(dict).length).toBe(0);
done();
});
test('misplaced index file', (done) => {
utils.createEmptyDirs([
path.join(dataDir, 'test', 'test'),
path.join(dataDir, '2019', '05', '05')
]);
utils.createEmptyFiles([
path.join(dataDir, testIndex),
path.join(dataDir, 'test', 'test', testIndex),
path.join(dataDir, '2019', '05', testIndex)
]);
fw.fetchArticles((err, dict) => {
expect(err).toBeNull();
expect(dict).toBeDefined();
expect(Object.keys(dict).length).toBe(0);
done();
});
});
test('misplaced index file', (done) => {
utils.createEmptyDirs([
path.join(dataDir, 'test', 'test'),
path.join(dataDir, '2019', '05', '05')
]);
utils.createEmptyFiles([
path.join(dataDir, testIndex),
path.join(dataDir, 'test', 'test', testIndex),
path.join(dataDir, '2019', '05', testIndex)
]);
fw.fetchArticles((err, dict) => {
expect(err).toBeNull();
expect(dict).toBeDefined();
expect(Object.keys(dict).length).toBe(0);
done();
});
test('empty index file', (done) => {
const dir = path.join(dataDir, '2019', '05', '05');
const file = path.join(dir, testIndex);
utils.createEmptyDirs([dir]);
utils.createEmptyFiles([file]);
fw.fetchArticles((err, dict) => {
expect(err).toBeNull();
expect(dict).toBeDefined();
expect(Object.keys(dict).length).toBe(1);
expect(dict[path.join('2019', '05', '05')]).toEqual({
path: path.join('2019', '05', '05'),
realPath: dir,
year: 2019,
month: 5,
day: 5,
date : new Date(2019,5,5),
title: 'Untitled',
thumbnail: 'default.png',
escapedTitle: 'untitled',
url: '/'+path.join('2019', '05', '05', 'untitled')+'/',
});
done();
});
});
test('empty index file', (done) => {
const dir = path.join(dataDir, '2019', '05', '05');
const file = path.join(dir, testIndex);
utils.createEmptyDirs([dir]);
utils.createEmptyFiles([file]);
fw.fetchArticles((err, dict) => {
expect(err).toBeNull();
expect(dict).toBeDefined();
expect(Object.keys(dict).length).toBe(1);
expect(dict[path.join('2019', '05', '05')]).toEqual({
path: path.join('2019', '05', '05'),
realPath: dir,
year: 2019,
month: 5,
day: 5,
date: new Date(2019, 5, 5),
title: 'Untitled',
thumbnail: 'default.png',
escapedTitle: 'untitled',
url: '/' + path.join('2019', '05', '05', 'untitled') + '/',
});
done();
});
test('correct index file', (done) => {
const dir = path.join(dataDir, '2019', '05', '05');
const file = path.join(dir, testIndex);
utils.createEmptyDirs([dir]);
fs.writeFileSync(file, `
});
test('correct index file', (done) => {
const dir = path.join(dataDir, '2019', '05', '05');
const file = path.join(dir, testIndex);
utils.createEmptyDirs([dir]);
fs.writeFileSync(file, `
# Title with : info !
![thumbnail](./thumbnail.jpg)
this is some text
`);
fw.fetchArticles((err, dict) => {
expect(err).toBeNull();
expect(dict).toBeDefined();
expect(Object.keys(dict).length).toBe(1);
expect(dict[path.join('2019', '05', '05')]).toEqual({
path: path.join('2019', '05', '05'),
realPath: dir,
year: 2019,
month: 5,
day: 5,
date : new Date(2019,5,5),
title: 'Title with : info !',
thumbnail: path.join('2019', '05', '05', './thumbnail.jpg'),
escapedTitle: 'title_with___info',
url: '/'+path.join('2019', '05', '05', 'title_with___info')+'/',
});
done();
});
fw.fetchArticles((err, dict) => {
expect(err).toBeNull();
expect(dict).toBeDefined();
expect(Object.keys(dict).length).toBe(1);
expect(dict[path.join('2019', '05', '05')]).toEqual({
path: path.join('2019', '05', '05'),
realPath: dir,
year: 2019,
month: 5,
day: 5,
date: new Date(2019, 5, 5),
title: 'Title with : info !',
thumbnail: path.join('2019', '05', '05', './thumbnail.jpg'),
escapedTitle: 'title_with___info',
url: '/' + path.join('2019', '05', '05', 'title_with___info') + '/',
});
done();
});
});
});
+26 -26
View File
@@ -7,47 +7,47 @@ const dataDir = 'test_data';
const file = path.join(dataDir, 'test.md');
const config = {
'showdown': {
'simplifiedAutoLink': true,
'smartIndentationFix': true
}
'showdown': {
'simplifiedAutoLink': true,
'smartIndentationFix': true
}
};
const renderer = require('../src/renderer')(config);
beforeEach(() => {
utils.deleteFolderSync(dataDir);
fs.mkdirSync(dataDir);
utils.deleteFolderSync(dataDir);
fs.mkdirSync(dataDir);
});
afterAll(() => {
if (fs.existsSync(dataDir)) {
utils.deleteFolderSync(dataDir);
}
if (fs.existsSync(dataDir)) {
utils.deleteFolderSync(dataDir);
}
});
test('invalid file', (done) => {
renderer.render('invalid file', (err, html) => {
expect(err).not.toBeNull();
expect(html).not.toBeDefined();
done();
});
renderer.render('invalid file', (err, html) => {
expect(err).not.toBeNull();
expect(html).not.toBeDefined();
done();
});
});
test('normal file', (done) => {
fs.writeFileSync(file, `# Hello`);
renderer.render(file, (err, html) => {
expect(err).toBeNull();
expect(html).toBe('<h1 id="hello">Hello</h1>');
done();
});
fs.writeFileSync(file, `# Hello`);
renderer.render(file, (err, html) => {
expect(err).toBeNull();
expect(html).toBe('<h1 id="hello">Hello</h1>');
done();
});
});
test('custom rules', (done) => {
fs.writeFileSync(file, `www.google.com`);
renderer.render(file, (err, html) => {
expect(err).toBeNull();
expect(html).toBe('<p><a href="http://www.google.com">www.google.com</a></p>');
done();
});
fs.writeFileSync(file, `www.google.com`);
renderer.render(file, (err, html) => {
expect(err).toBeNull();
expect(html).toBe('<p><a href="http://www.google.com">www.google.com</a></p>');
done();
});
});
+12 -12
View File
@@ -2,19 +2,19 @@ const fs = require('fs');
const path = require('path');
const deleteFolderSync = (dir) => {
if (!fs.existsSync(dir))
return;
fs.readdirSync(dir, {withFileTypes: true}).forEach((item) => {
if (item.isDirectory())
deleteFolderSync(path.join(dir,item.name));
else
fs.unlinkSync(path.join(dir,item.name));
});
fs.rmdirSync(dir);
if (!fs.existsSync(dir))
return;
fs.readdirSync(dir, {withFileTypes: true}).forEach((item) => {
if (item.isDirectory())
deleteFolderSync(path.join(dir, item.name));
else
fs.unlinkSync(path.join(dir, item.name));
});
fs.rmdirSync(dir);
};
module.exports = {
deleteFolderSync: deleteFolderSync,
createEmptyDirs: (list) =>list.forEach((path) =>fs.mkdirSync(path, {recursive: true})),
createEmptyFiles: (list) =>list.forEach((file) =>fs.writeFileSync(file, '')),
deleteFolderSync: deleteFolderSync,
createEmptyDirs: (list) => list.forEach((path) => fs.mkdirSync(path, {recursive: true})),
createEmptyFiles: (list) => list.forEach((file) => fs.writeFileSync(file, '')),
};