2020-08-25 23:57:08 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var isGlob = require('is-glob');
|
|
|
|
var pathPosixDirname = require('path').posix.dirname;
|
|
|
|
var isWin32 = require('os').platform() === 'win32';
|
|
|
|
|
|
|
|
var slash = '/';
|
|
|
|
var backslash = /\\/g;
|
2022-11-10 10:43:16 +00:00
|
|
|
var escaped = /\\([!*?|[\](){}])/g;
|
2020-08-25 23:57:08 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} str
|
|
|
|
* @param {Object} opts
|
|
|
|
* @param {boolean} [opts.flipBackslashes=true]
|
|
|
|
*/
|
|
|
|
module.exports = function globParent(str, opts) {
|
|
|
|
var options = Object.assign({ flipBackslashes: true }, opts);
|
|
|
|
|
|
|
|
// flip windows path separators
|
|
|
|
if (options.flipBackslashes && isWin32 && str.indexOf(slash) < 0) {
|
|
|
|
str = str.replace(backslash, slash);
|
|
|
|
}
|
|
|
|
|
|
|
|
// special case for strings ending in enclosure containing path separator
|
2022-11-10 10:43:16 +00:00
|
|
|
if (isEnclosure(str)) {
|
2020-08-25 23:57:08 +00:00
|
|
|
str += slash;
|
|
|
|
}
|
|
|
|
|
|
|
|
// preserves full path in case of trailing path separator
|
|
|
|
str += 'a';
|
|
|
|
|
|
|
|
// remove path parts that are globby
|
|
|
|
do {
|
|
|
|
str = pathPosixDirname(str);
|
2022-11-10 10:43:16 +00:00
|
|
|
} while (isGlobby(str));
|
2020-08-25 23:57:08 +00:00
|
|
|
|
|
|
|
// remove escape chars and return result
|
|
|
|
return str.replace(escaped, '$1');
|
|
|
|
};
|
2022-11-10 10:43:16 +00:00
|
|
|
|
|
|
|
function isEnclosure(str) {
|
|
|
|
var lastChar = str.slice(-1);
|
|
|
|
|
|
|
|
var enclosureStart;
|
|
|
|
switch (lastChar) {
|
|
|
|
case '}':
|
|
|
|
enclosureStart = '{';
|
|
|
|
break;
|
|
|
|
case ']':
|
|
|
|
enclosureStart = '[';
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
var foundIndex = str.indexOf(enclosureStart);
|
|
|
|
if (foundIndex < 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return str.slice(foundIndex + 1, -1).includes(slash);
|
|
|
|
}
|
|
|
|
|
|
|
|
function isGlobby(str) {
|
|
|
|
if (/\([^()]+$/.test(str)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (str[0] === '{' || str[0] === '[') {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (/[^\\][{[]/.test(str)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return isGlob(str);
|
|
|
|
}
|