From f9a2db38d2210454f7665123309701f41e5fc642 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20GOUIN?= Date: Thu, 20 Jun 2019 10:49:51 +0200 Subject: [PATCH] code convention --- src/app.js | 21 ++++++++++++--------- src/config.js | 2 +- src/file_walker.js | 12 ++++++------ src/postinstall.js | 2 +- test/app.test.js | 36 ++++++++++++++++++------------------ test/test_utils.js | 4 ++-- 6 files changed, 40 insertions(+), 37 deletions(-) diff --git a/src/app.js b/src/app.js index e825e6d..e51e450 100644 --- a/src/app.js +++ b/src/app.js @@ -45,19 +45,22 @@ module.exports = (config) => { 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: resPath}, code); - else - res.sendStatus(code); + fs.access(errorPath, (err) => { + if(err) + res.sendStatus(code); + else + render(res, errorPath, {error: code, path: resPath}, code); + }); }; app.get('/', (req, res) => { const homePath = `${config['data_dir']}/${config['home']['index']}`; - if (fs.existsSync(homePath)) - render(res, homePath, {articles: articles}); - else { - showError(req.path, 404, res); - } + fs.access(homePath,(err)=>{ + if(err) + showError(req.path, 404, res); + else + render(res, homePath, {articles: articles}); + }); }); app.get('*', express.static(config['data_dir'])); diff --git a/src/config.js b/src/config.js index fd44034..143af88 100644 --- a/src/config.js +++ b/src/config.js @@ -6,7 +6,7 @@ const merge = (ref, src) => { return ref; } else if (typeof ref === 'object') { const out = {}; - Object.keys(ref).forEach(key => out[key] = merge(ref[key], src[key])); + Object.keys(ref).forEach((key) =>out[key] = merge(ref[key], src[key])); return out; } else { return src; diff --git a/src/file_walker.js b/src/file_walker.js index 52ac51c..5ce2e57 100644 --- a/src/file_walker.js +++ b/src/file_walker.js @@ -54,16 +54,16 @@ module.exports = (config) => { 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); + .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 list = []; let remaining = 0; - paths.forEach(matches => { + paths.forEach((matches) =>{ const article = { 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]), diff --git a/src/postinstall.js b/src/postinstall.js index 8654aa5..e9acd6e 100644 --- a/src/postinstall.js +++ b/src/postinstall.js @@ -18,7 +18,7 @@ if (!fs.existsSync('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())); diff --git a/test/app.test.js b/test/app.test.js index f9f2eae..62e4832 100644 --- a/test/app.test.js +++ b/test/app.test.js @@ -32,23 +32,23 @@ afterAll(() => { }); describe('Test root path', () => { - test('404 no index no error', done => { - request(app).get('/').then(response => { + 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 => { + test('404 no index but error page', (done) =>{ fs.writeFileSync(path.join(dataDir,testError), 'error <%= error %> at <%= path %>'); - request(app).get('/').then(response => { + request(app).get('/').then((response) =>{ expect(response.statusCode).toBe(404); expect(response.text).toBe('error 404 at /'); done(); }); }); - test('200 index page', done => { + test('200 index page', (done) =>{ fs.writeFileSync(path.join(dataDir,testIndex), 'hello there'); - request(app).get('/').then(response => { + request(app).get('/').then((response) =>{ expect(response.statusCode).toBe(200); expect(response.text).toBe('hello there'); done(); @@ -58,23 +58,23 @@ describe('Test root path', () => { }); describe('Test static files', () => { - test('404 invalid file no error page', done => { - request(app).get('/somefile.txt').then(response => { + 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 => { + 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 => { + request(app).get('/somefile.txt').then((response) =>{ expect(response.statusCode).toBe(404); expect(response.text).toBe('error 404 at /somefile.txt'); done(); }); }); - test('200 valid file', done => { + test('200 valid file', (done) =>{ fs.writeFileSync(`${dataDir}/somefile.txt`, 'filecontent'); - request(app).get('/somefile.txt').then(response => { + request(app).get('/somefile.txt').then((response) =>{ expect(response.statusCode).toBe(200); expect(response.text).toBe('filecontent'); done(); @@ -83,20 +83,20 @@ describe('Test static files', () => { }); describe('Test other requests', () => { - test('400 POST', done => { - request(app).post('/').then(response => { + test('400 POST', (done) =>{ + request(app).post('/').then((response) =>{ expect(response.statusCode).toBe(400); done(); }); }); - test('400 PUT', done => { - request(app).put('/').then(response => { + test('400 PUT', (done) =>{ + request(app).put('/').then((response) =>{ expect(response.statusCode).toBe(400); done(); }); }); - test('400 DELETE', done => { - request(app).delete('/').then(response => { + test('400 DELETE', (done) =>{ + request(app).delete('/').then((response) =>{ expect(response.statusCode).toBe(400); done(); }); diff --git a/test/test_utils.js b/test/test_utils.js index 5b39983..a80a886 100644 --- a/test/test_utils.js +++ b/test/test_utils.js @@ -15,6 +15,6 @@ const deleteFolderSync = (dir) => { module.exports = { deleteFolderSync: deleteFolderSync, - createEmptyDirs: list => list.forEach(path => fs.mkdirSync(path, {recursive: true})), - createEmptyFiles: list => list.forEach(file => fs.writeFileSync(file, '')), + createEmptyDirs: (list) =>list.forEach((path) =>fs.mkdirSync(path, {recursive: true})), + createEmptyFiles: (list) =>list.forEach((file) =>fs.writeFileSync(file, '')), }; \ No newline at end of file