From 07dd06a338ba0e06e31e6fd6822b0262847a901c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20GOUIN?= Date: Thu, 20 Jun 2019 10:40:48 +0200 Subject: [PATCH] use of path.join everywhere --- src/app.js | 19 +++++-------- src/config.js | 2 +- src/file_walker.js | 24 ++++++++-------- src/postinstall.js | 21 +++++++------- test/app.test.js | 9 +++--- test/config.test.js | 4 +-- test/file_walker.test.js | 61 ++++++++++++++++++++-------------------- test/test_utils.js | 13 +++++---- 8 files changed, 77 insertions(+), 76 deletions(-) diff --git a/src/app.js b/src/app.js index 06308eb..e825e6d 100644 --- a/src/app.js +++ b/src/app.js @@ -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']}`); }); }); }; diff --git a/src/config.js b/src/config.js index ac75ed5..fd44034 100644 --- a/src/config.js +++ b/src/config.js @@ -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) { diff --git a/src/file_walker.js b/src/file_walker.js index 1b1766b..52ac51c 100644 --- a/src/file_walker.js +++ b/src/file_walker.js @@ -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) diff --git a/src/postinstall.js b/src/postinstall.js index 7aeedcf..8654aa5 100644 --- a/src/postinstall.js +++ b/src/postinstall.js @@ -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); } \ No newline at end of file diff --git a/test/app.test.js b/test/app.test.js index 03cf8a3..f9f2eae 100644 --- a/test/app.test.js +++ b/test/app.test.js @@ -1,9 +1,10 @@ /* jshint -W117 */ const request = require('supertest'); const fs = require('fs'); +const path = require('path'); const utils = require('./test_utils'); -const dataDir = './test_data'; +const dataDir = 'test_data'; const testIndex = 'testindex.ejs'; const testError = 'testerror.ejs'; @@ -38,7 +39,7 @@ describe('Test root path', () => { }); }); test('404 no index but error page', done => { - fs.writeFileSync(`${dataDir}/${testError}`, 'error <%= error %> at <%= path %>'); + 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 /'); @@ -46,7 +47,7 @@ describe('Test root path', () => { }); }); test('200 index page', done => { - fs.writeFileSync(`${dataDir}/${testIndex}`, 'hello there'); + fs.writeFileSync(path.join(dataDir,testIndex), 'hello there'); request(app).get('/').then(response => { expect(response.statusCode).toBe(200); expect(response.text).toBe('hello there'); @@ -64,7 +65,7 @@ describe('Test static files', () => { }); }); test('404 invalid file but error page', done => { - fs.writeFileSync(`${dataDir}/${testError}`, 'error <%= error %> at <%= path %>'); + 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'); diff --git a/test/config.test.js b/test/config.test.js index 5f61b3f..ad60ea4 100644 --- a/test/config.test.js +++ b/test/config.test.js @@ -1,8 +1,8 @@ /* jshint -W117 */ const fs = require('fs'); -const configFile = './config.json'; -const tmpConfigFile = './config.temp.json'; +const configFile = 'config.json'; +const tmpConfigFile = 'config.temp.json'; beforeAll(() => { if (fs.existsSync(configFile)) { diff --git a/test/file_walker.test.js b/test/file_walker.test.js index 4500d87..b60131a 100644 --- a/test/file_walker.test.js +++ b/test/file_walker.test.js @@ -1,8 +1,9 @@ /* jshint -W117 */ const fs = require('fs'); +const path = require('path'); const utils = require('./test_utils'); -const dataDir = './test_data'; +const dataDir = 'test_data'; const testIndex = 'testindex.md'; const config = { @@ -40,9 +41,9 @@ describe('Test function fileTree', () => { }); test('empty folders', (done) => { utils.createEmptyDirs([ - `${dataDir}/test/test`, - `${dataDir}/test/test2`, - `${dataDir}/test2` + path.join(dataDir, 'test', 'test'), + path.join(dataDir, 'test', 'test2'), + path.join(dataDir, 'test2') ]); fw.fileTree(dataDir, (err, list) => { expect(err).toBeNull(); @@ -53,8 +54,8 @@ describe('Test function fileTree', () => { }); test('simple files', (done) => { const fileList = [ - `${dataDir}/f1.txt`, - `${dataDir}/f2.txt` + path.join(dataDir, 'f1.txt'), + path.join(dataDir, 'f2.txt') ]; utils.createEmptyFiles(fileList); fw.fileTree(dataDir, (err, list) => { @@ -67,14 +68,14 @@ describe('Test function fileTree', () => { }); test('nested files', (done) => { utils.createEmptyDirs([ - `${dataDir}/test/test`, - `${dataDir}/test2` + path.join(dataDir, 'test', 'test'), + path.join(dataDir, 'test2') ]); const fileList = [ - `${dataDir}/f1.txt`, - `${dataDir}/test/f2.txt`, - `${dataDir}/test/test/f3.txt`, - `${dataDir}/test2/f4.txt` + 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) => { @@ -95,7 +96,7 @@ describe('Test function fileTree', () => { }); describe('Test index article reading', () => { - const file = `${dataDir}/${testIndex}`; + const file = path.join(dataDir, testIndex); test('invalid file', (done) => { fw.readIndexFile('invalid file', 'thumbnail', (err, info) => { @@ -199,13 +200,13 @@ describe('Test article fetching', () => { }); test('misplaced index file', (done) => { utils.createEmptyDirs([ - `${dataDir}/test/test`, - `${dataDir}/2019/05/05`, + path.join(dataDir, 'test', 'test'), + path.join(dataDir, '2019', '05', '05') ]); utils.createEmptyFiles([ - `${dataDir}/${testIndex}`, - `${dataDir}/test/test/${testIndex}`, - `${dataDir}/2019/05/${testIndex}`, + path.join(dataDir, testIndex), + path.join(dataDir, 'test', 'test', testIndex), + path.join(dataDir, '2019', '05', testIndex) ]); fw.fetchArticles((err, list) => { expect(err).toBeNull(); @@ -215,10 +216,9 @@ describe('Test article fetching', () => { }); }); test('empty index file', (done) => { - utils.createEmptyDirs([ - `${dataDir}/2019/05/05`, - ]); - const file = `${dataDir}/2019/05/05/${testIndex}`; + const dir = path.join(dataDir, '2019', '05', '05'); + const file = path.join(dir, testIndex); + utils.createEmptyDirs([dir]); utils.createEmptyFiles([file]); fw.fetchArticles((err, list) => { expect(err).toBeNull(); @@ -226,20 +226,20 @@ describe('Test article fetching', () => { expect(list.length).toBe(1); expect(list[0]).toEqual({ path: file, + parent:dir, year: 2019, month: 5, day: 5, - title:'Untitled', - thumbnail:'default.png' + title: 'Untitled', + thumbnail: 'default.png' }); done(); }); }); test('correct index file', (done) => { - utils.createEmptyDirs([ - `${dataDir}/2019/05/05`, - ]); - const file = `${dataDir}/2019/05/05/${testIndex}`; + const dir = path.join(dataDir, '2019', '05', '05'); + const file = path.join(dir, testIndex); + utils.createEmptyDirs([dir]); fs.writeFileSync(file, ` # Title ![thumbnail](./thumbnail.jpg) @@ -251,11 +251,12 @@ describe('Test article fetching', () => { expect(list.length).toBe(1); expect(list[0]).toEqual({ path: file, + parent:dir, year: 2019, month: 5, day: 5, - title:'Title', - thumbnail:`${dataDir}/2019/05/05/./thumbnail.jpg` + title: 'Title', + thumbnail: path.join(dataDir, '2019', '05', '05', './thumbnail.jpg') }); done(); }); diff --git a/test/test_utils.js b/test/test_utils.js index e03ef7a..5b39983 100644 --- a/test/test_utils.js +++ b/test/test_utils.js @@ -1,15 +1,16 @@ const fs = require('fs'); +const path = require('path'); -const deleteFolderSync = (path) => { - if (!fs.existsSync(path)) +const deleteFolderSync = (dir) => { + if (!fs.existsSync(dir)) return; - fs.readdirSync(path, {withFileTypes: true}).forEach((item) => { + fs.readdirSync(dir, {withFileTypes: true}).forEach((item) => { if (item.isDirectory()) - deleteFolderSync(`${path}/${item.name}`); + deleteFolderSync(path.join(dir,item.name)); else - fs.unlinkSync(`${path}/${item.name}`); + fs.unlinkSync(path.join(dir,item.name)); }); - fs.rmdirSync(path); + fs.rmdirSync(dir); }; module.exports = {