| 1 |
const path = require('path'); |
| 2 |
const CHARS = { '{': '}', '(': ')', '[': ']'}; |
| 3 |
const STRICT = /\\(.)|(^!|\*|[\].+)]\?|\[[^\\\]]+\]|\{[^\\}]+\}|\(\?[:!=][^\\)]+\)|\([^|]+\|[^\\)]+\)|(\\).|([@?!+*]\(.*\)))/; |
| 4 |
const RELAXED = /\\(.)|(^!|[*?{}()[\]]|\(\?)/; |
| 5 |
|
| 6 |
/** |
| 7 |
* Detect if a string cointains glob |
| 8 |
* @param {String} str Input string |
| 9 |
* @param {Object} [options] Configuration object |
| 10 |
* @param {Boolean} [options.strict=true] Use relaxed regex if true |
| 11 |
* @returns {Boolean} true if string contains glob |
| 12 |
*/ |
| 13 |
function isglob(str, { strict = true } = {}) { |
| 14 |
if (str === '') return false; |
| 15 |
let match, rgx = strict ? STRICT : RELAXED; |
| 16 |
|
| 17 |
while ((match = rgx.exec(str))) { |
| 18 |
if (match[2]) return true; |
| 19 |
let idx = match.index + match[0].length; |
| 20 |
|
| 21 |
// if an open bracket/brace/paren is escaped, |
| 22 |
// set the index to the next closing character |
| 23 |
let open = match[1]; |
| 24 |
let close = open ? CHARS[open] : null; |
| 25 |
if (open && close) { |
| 26 |
let n = str.indexOf(close, idx); |
| 27 |
if (n !== -1) idx = n + 1; |
| 28 |
} |
| 29 |
|
| 30 |
str = str.slice(idx); |
| 31 |
} |
| 32 |
return false; |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
/** |
| 37 |
* Find the static part of a glob-path, |
| 38 |
* split path and return path part |
| 39 |
* @param {String} str Path/glob string |
| 40 |
* @returns {String} static path section of glob |
| 41 |
*/ |
| 42 |
function parent(str, { strict = false } = {}) { |
| 43 |
str = path.normalize(str).replace(/\/|\\/, '/'); |
| 44 |
|
| 45 |
// special case for strings ending in enclosure containing path separator |
| 46 |
if (/[\{\[].*[\/]*.*[\}\]]$/.test(str)) str += '/'; |
| 47 |
|
| 48 |
// preserves full path in case of trailing path separator |
| 49 |
str += 'a'; |
| 50 |
|
| 51 |
do {str = path.dirname(str)} |
| 52 |
while (isglob(str, {strict}) || /(^|[^\\])([\{\[]|\([^\)]+$)/.test(str)); |
| 53 |
|
| 54 |
// remove escape chars and return result |
| 55 |
return str.replace(/\\([\*\?\|\[\]\(\)\{\}])/g, '$1'); |
| 56 |
}; |
| 57 |
|
| 58 |
|
| 59 |
/** |
| 60 |
* Parse a glob path, and split it by static/glob part |
| 61 |
* @param {String} pattern String path |
| 62 |
* @param {Object} [opts] Options |
| 63 |
* @param {Object} [opts.strict=false] Use strict parsing |
| 64 |
* @returns {Object} object with parsed path |
| 65 |
*/ |
| 66 |
function globalyzer(pattern, opts = {}) { |
| 67 |
let base = parent(pattern, opts); |
| 68 |
let isGlob = isglob(pattern, opts); |
| 69 |
let glob; |
| 70 |
|
| 71 |
if (base != '.') { |
| 72 |
glob = pattern.substr(base.length); |
| 73 |
if (glob.startsWith('/')) glob = glob.substr(1); |
| 74 |
} else { |
| 75 |
glob = pattern; |
| 76 |
} |
| 77 |
|
| 78 |
if (!isGlob) { |
| 79 |
base = path.dirname(pattern); |
| 80 |
glob = base !== '.' ? pattern.substr(base.length) : pattern; |
| 81 |
} |
| 82 |
|
| 83 |
if (glob.startsWith('./')) glob = glob.substr(2); |
| 84 |
if (glob.startsWith('/')) glob = glob.substr(1); |
| 85 |
|
| 86 |
return { base, glob, isGlob }; |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
module.exports = globalyzer; |