Skip to content

Commit

Permalink
fix: support join a absolute path
Browse files Browse the repository at this point in the history
  • Loading branch information
popomore committed Mar 31, 2017
1 parent 91cc636 commit f0ae61f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 20 deletions.
39 changes: 19 additions & 20 deletions index.js
Expand Up @@ -72,29 +72,23 @@ proto.getSource = function (name) {
var paths = this.searchPaths;
var charset = '';

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;
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;
}
}

if (!fullpath) {
debug('view %s not found in %j', name, this.searchPaths);
return null;
}
} else {
if (!fs.existsSync(name)) return null;
if (!fullpath && path.isAbsolute(name) && fs.existsSync(name)) {
fullpath = name;
var roots = Object.keys(this.charsets);
for (var i = 0, j = roots.length; i < j; i++) {
Expand All @@ -105,6 +99,11 @@ proto.getSource = function (name) {
}
}

if (!fullpath) {
debug('view %s not found in %j', name, this.searchPaths);
return null;
}

this.pathsToNames[fullpath] = name;
var content = fs.readFileSync(fullpath);
debug('view %s mapping to %s, charset: %s, size: %d', name, fullpath, charset, content.length);
Expand Down
6 changes: 6 additions & 0 deletions test/fileloader.test.js
Expand Up @@ -159,6 +159,12 @@ describe('fileloader.test.js', function () {
info.path.should.equal(filepath);
info.src.should.equal('bar\n');

info = loader.getSource('/foo.txt');
info.path.should.equal(filepath);

info = loader.getSource('/home');
info.path.should.equal(path.join(dirs[0], 'home'));

filepath = path.join(dirs[0], 'noexist.txt');
info = loader.getSource(filepath);
assert(info === null);
Expand Down
Empty file added test/fixtures/dir1/home
Empty file.

0 comments on commit f0ae61f

Please sign in to comment.