|
| 1 | +/** |
| 2 | + * (C) Microsoft Open Technologies, Inc. All rights reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +var PATH = require('path'); |
| 17 | +var fs = require('fs'); |
| 18 | + |
| 19 | +/** |
| 20 | + * @type {Function} wildcard(pattern) |
| 21 | + * wildcard searches for files matching pattern. |
| 22 | + * |
| 23 | + * @pattern {String} search pattern with optional * wildcards |
| 24 | + * @return an array containing the paths to the matching files |
| 25 | + */ |
| 26 | +var wildcard = exports.wildcard = function(pattern) { |
| 27 | + |
| 28 | + // process pattern string for * wildcard |
| 29 | + var matchers = [], |
| 30 | + tokens, |
| 31 | + path; |
| 32 | + |
| 33 | + var index = pattern.indexOf('*'); |
| 34 | + if(index === -1) { |
| 35 | + return [pattern]; |
| 36 | + } |
| 37 | + |
| 38 | + path = pattern.substr(0, index-1); |
| 39 | + pattern = pattern.substr(index); |
| 40 | + pattern = pattern.replace(/\*/g, '(?=.*)'); |
| 41 | + tokens = pattern.split(PATH.sep); |
| 42 | + |
| 43 | + // create matcher regex for each path component in pattern |
| 44 | + tokens.forEach(function(token, index, array) { |
| 45 | + var matcher = {}; |
| 46 | + matcher.index = index; |
| 47 | + matcher.isDir = index < array.length -1; |
| 48 | + matcher.regex = new RegExp(token); |
| 49 | + matchers.push(matcher); |
| 50 | + }); |
| 51 | + |
| 52 | + return process(path, matchers); |
| 53 | +}; |
| 54 | + |
| 55 | +// searches starting from the path directory and returns files matching wildcard pattern |
| 56 | +// search only proceeds to directory depth equal to the numbe rof matchers. |
| 57 | +var process = function(path, matchers) { |
| 58 | + |
| 59 | + var files = []; |
| 60 | + var traverse = function(path, level) { |
| 61 | + var dirs, |
| 62 | + matcher; |
| 63 | + |
| 64 | + // check we have not exceeded search directory depth |
| 65 | + if(level >= matchers.length) { |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + // read the dirs and files from the current path |
| 70 | + dirs = fs.readdirSync(path); |
| 71 | + matcher = matchers[level]; |
| 72 | + |
| 73 | + // check if each dir or file matches the matcher for the current directory level |
| 74 | + for(var i = 0; i < dirs.length; i++) { |
| 75 | + var dir = dirs[i]; |
| 76 | + |
| 77 | + if(dir.match(matcher.regex) === null) { |
| 78 | + continue; |
| 79 | + } |
| 80 | + |
| 81 | + var pathName = PATH.join(path,dir); |
| 82 | + |
| 83 | + var stats = fs.statSync(pathName); |
| 84 | + if(stats.isDirectory()) { |
| 85 | + if(matcher.isDir) { |
| 86 | + traverse(pathName, level + 1); |
| 87 | + } else { |
| 88 | + continue; |
| 89 | + } |
| 90 | + } |
| 91 | + else if(level === matchers.length - 1) { |
| 92 | + // found a matching file |
| 93 | + if(stats.isFile()) { |
| 94 | + files.push(pathName); |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + }; |
| 99 | + |
| 100 | + traverse(path, 0); |
| 101 | + return files; |
| 102 | +}; |
| 103 | + |
| 104 | + |
| 105 | + |
| 106 | +//var pattern = '/Users/stammen/dev/microsoft/pkgcloud/test/*/*/*-test.js'; |
| 107 | +// |
| 108 | +//var result = wildcard(pattern); |
| 109 | +//console.log(result); |
| 110 | +//console.log(result.length); |
| 111 | + |
| 112 | + |
| 113 | + |
| 114 | + |
0 commit comments