Skip to content

Commit

Permalink
feat: support fullpath (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
popomore committed Mar 31, 2017
1 parent d9dd2d3 commit d159368
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 19 deletions.
5 changes: 2 additions & 3 deletions .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"
44 changes: 28 additions & 16 deletions index.js
Expand Up @@ -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;
Expand Down
17 changes: 17 additions & 0 deletions test/fileloader.test.js
Expand Up @@ -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 () {
Expand Down Expand Up @@ -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('知道');
});
});

0 comments on commit d159368

Please sign in to comment.