Skip to content
This repository has been archived by the owner on Nov 27, 2018. It is now read-only.

Fixing windows path matching #82

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/site_generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ module.exports = {

_.each(self.pathsToSkip, function(skip_pattern) {
_.each(from, function(path) {
if (path.match(skip_pattern) !== null) {
if (path_utils.matchPath(path, skip_pattern) !== null) {
paths_to_skip.push(path);
}
});
Expand Down
21 changes: 20 additions & 1 deletion lib/utils/path_utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
var path = require("path");
var mime = require("mime");
var os = require("os");
var _ = require("underscore");

module.exports = {

Expand Down Expand Up @@ -28,6 +30,23 @@ module.exports = {
} else {
return request_path;
}
}
},

matchPath: function(basepath, patterns) {
function match(file_path, regex)
{
if (os.platform() === "win32") {
return file_path.replace(/\\/g, "/").match( new RegExp(regex) );
}
return file_path.match( new RegExp(regex) );
}
if (Array.isArray(patterns)) {
return _.any(patterns, function(pattern) {
return match(basepath, pattern) != null;
});
} else {
// assume a single pattern given and match directly
return match(basepath, patterns);
}
}
};
44 changes: 44 additions & 0 deletions spec/utils/path_utils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,47 @@ describe("get the basename", function() {
});

});

describe("match a path", function() {

it("matches a single path", function() {
var matches = path_utils.matchPath("/css/less/main.less", "/css/less/*");
expect(matches[0]).toEqual("/css/less/");
});

it("matches a single path on windows", function() {
var matches = path_utils.matchPath("\\css\\less\\main.less", "/css/less/*");
expect(matches[0]).toEqual("/css/less/");
});

it("matches multiple paths", function() {
var matches = path_utils.matchPath("/css/less/main.less", ["/css/less/*", "/css/sass/*"]);
expect(matches).toBeTruthy();
});

it("matches multiple paths on windows", function() {
var matches = path_utils.matchPath("\\css\\less\\main.less", ["/css/less/*", "/css/sass/*"]);
expect(matches).toBeTruthy();
});

it("fails a match on single path", function() {
var matches = path_utils.matchPath("/css/less/main.less", "/css/sass/*");
expect(matches).toBeFalsy();
});

it("fails a match on single path on windows", function() {
var matches = path_utils.matchPath("\\css\\less\\main.less", "/css/sass/*");
expect(matches).toBeFalsy();
});

it("fails a match on multiple paths", function() {
var matches = path_utils.matchPath("/css/less/main.less", ["/css/les/*", "/css/sass/*"]);
expect(matches).toBeTruthy();
});

it("fails a match on multiple paths on windows", function() {
var matches = path_utils.matchPath("\\css\\less\\main.less", ["/css/les/*", "/css/sass/*"]);
expect(matches).toBeTruthy();
});

});