diff options
Diffstat (limited to 'tools')
-rw-r--r-- | tools/node.js | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/tools/node.js b/tools/node.js index 179c69db..5a143759 100644 --- a/tools/node.js +++ b/tools/node.js @@ -261,3 +261,47 @@ exports.writeNameCache = function(filename, key, cache) { fs.writeFileSync(filename, JSON.stringify(data, null, 2), "utf8"); } }; + +// A file glob function that only supports "*" and "?" wildcards in the basename. +// Example: "foo/bar/*baz??.*.js" +// Argument `glob` may be a string or an array of strings. +// Returns an array of strings. Garbage in, garbage out. +exports.simple_glob = function simple_glob(glob) { + var results = []; + if (Array.isArray(glob)) { + glob.forEach(function(elem) { + results = results.concat(simple_glob(elem)); + }); + return results; + } + if (glob.match(/\*|\?/)) { + var dir = path.dirname(glob); + try { + var entries = fs.readdirSync(dir); + } catch (ex) {} + if (entries) { + var pattern = "^" + (path.basename(glob) + .replace(/\(/g, "\\(") + .replace(/\)/g, "\\)") + .replace(/\{/g, "\\{") + .replace(/\}/g, "\\}") + .replace(/\[/g, "\\[") + .replace(/\]/g, "\\]") + .replace(/\+/g, "\\+") + .replace(/\^/g, "\\^") + .replace(/\$/g, "\\$") + .replace(/\*/g, "[^/\\\\]*") + .replace(/\./g, "\\.") + .replace(/\?/g, ".")) + "$"; + var mod = process.platform === "win32" ? "i" : ""; + var rx = new RegExp(pattern, mod); + for (var i in entries) { + if (rx.test(entries[i])) + results.push(dir + "/" + entries[i]); + } + } + } + if (results.length === 0) + results = [ glob ]; + return results; +}; |