From e38f3bb88423a17a7d0b292d86805a37f72cbff2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samy=20Pess=C3=A9?= Date: Mon, 16 Nov 2015 10:31:24 +0100 Subject: [PATCH] Improve book.findFile to be case incensitive --- lib/book.js | 9 +++++---- lib/utils/fs.js | 12 ++++++++++++ 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/lib/book.js b/lib/book.js index cb00ab17b..93c9486e3 100644 --- a/lib/book.js +++ b/lib/book.js @@ -614,12 +614,13 @@ Book.prototype.findFile = function(filename) { var filepath = basename+ext; - return that.fileExists(filepath) - .then(function(exists) { - if (!exists) return null; + return fs.findFile(that.root, filepath) + .then(function(realFilepath) { + if (!realFilepath) return null; + return { parser: parsers.get(ext), - path: filepath + path: realFilepath }; }); }); diff --git a/lib/utils/fs.js b/lib/utils/fs.js index 716e3a00b..b82701f98 100644 --- a/lib/utils/fs.js +++ b/lib/utils/fs.js @@ -30,6 +30,7 @@ var fsUtils = { fs.exists(path, d.resolve); return d.promise; }, + findFile: findFile, existsSync: fs.existsSync.bind(fs), readFileSync: fs.readFileSync.bind(fs), clean: cleanFolder, @@ -178,4 +179,15 @@ function cleanFolder(root) { }); } +// Find a file in a folder (case incensitive) +// Return the real filename +function findFile(root, filename) { + return Q.nfcall(fs.readdir, root) + .then(function(files) { + return _.find(files, function(file) { + return (file.toLowerCase() == filename.toLowerCase()); + }); + }); +} + module.exports = fsUtils;