From d159368153e39946c4b78b2da05a3e14ecfac34f Mon Sep 17 00:00:00 2001 From: Haoliang Gao Date: Fri, 31 Mar 2017 18:34:48 +0800 Subject: [PATCH] feat: support fullpath (#5) --- .travis.yml | 5 ++--- index.js | 44 ++++++++++++++++++++++++++--------------- test/fileloader.test.js | 17 ++++++++++++++++ 3 files changed, 47 insertions(+), 19 deletions(-) diff --git a/.travis.yml b/.travis.yml index 396b793..ca27eab 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,9 +1,8 @@ language: node_js node_js: + - '7' + - '6' - '4' - - '3' - - '2' - - '1' - '0.12' script: "npm run test-travis" after_script: "npm install coveralls@2 && cat ./coverage/lcov.info | coveralls" diff --git a/index.js b/index.js index f76fd81..6ef51f5 100644 --- a/index.js +++ b/index.js @@ -72,25 +72,37 @@ proto.getSource = function (name) { var paths = this.searchPaths; var charset = ''; - for (var i = 0; i < paths.length; i++) { - var root = paths[i]; - var p = path.join(root, name); - - // Only allow the current directory and anything - // underneath it to be searched - if (fs.existsSync(p)) { - fullpath = p; - charset = this.charsets[root]; - if (charset) { - charset = charset.toLowerCase(); + if (!path.isAbsolute(name)) { + for (var i = 0; i < paths.length; i++) { + var root = paths[i]; + var p = path.join(root, name); + + // Only allow the current directory and anything + // underneath it to be searched + if (fs.existsSync(p)) { + fullpath = p; + charset = this.charsets[root]; + if (charset) { + charset = charset.toLowerCase(); + } + break; } - break; } - } - if (!fullpath) { - debug('view %s not found in %j', name, this.searchPaths); - return null; + if (!fullpath) { + debug('view %s not found in %j', name, this.searchPaths); + return null; + } + } else { + if (!fs.existsSync(name)) return null; + fullpath = name; + var roots = Object.keys(this.charsets); + for (var i = 0, j = roots.length; i < j; i++) { + if (fullpath.indexOf(roots[i]) > -1) { + charset = this.charsets[roots[i]]; + break; + } + } } this.pathsToNames[fullpath] = name; diff --git a/test/fileloader.test.js b/test/fileloader.test.js index e7055e7..dd4c120 100644 --- a/test/fileloader.test.js +++ b/test/fileloader.test.js @@ -18,6 +18,7 @@ var should = require('should'); var path = require('path'); var fs = require('fs'); var mm = require('mm'); +var assert = require('assert'); var FileLoader = require('../'); describe('fileloader.test.js', function () { @@ -151,4 +152,20 @@ describe('fileloader.test.js', function () { cb({path: path.join(dirs[0], 'subdir1', 'subdirfile.txt')}); }, 10); }); + + it('should support fullpath', function () { + var filepath = path.join(dirs[0], 'foo.txt'); + var info = loader.getSource(filepath); + info.path.should.equal(filepath); + info.src.should.equal('bar\n'); + + filepath = path.join(dirs[0], 'noexist.txt'); + info = loader.getSource(filepath); + assert(info === null); + + filepath = path.join(dirs[1], 'dir2file.txt'); + info = loader.getSource(filepath); + info.path.should.equal(filepath); + info.src.should.containEql('知道'); + }); });