blob: 3a14a539f04754e1a49e08f5524c572c874d8647 [file] [log] [blame]
Leo Repp58b9f112021-11-22 11:57:47 +01001'use strict';
2
3var path = require('path');
4var isglob = require('is-glob');
5var pathDirname = require('path-dirname');
6var isWin32 = require('os').platform() === 'win32';
7
8module.exports = function globParent(str) {
9 // flip windows path separators
10 if (isWin32 && str.indexOf('/') < 0) str = str.split('\\').join('/');
11
12 // special case for strings ending in enclosure containing path separator
13 if (/[\{\[].*[\/]*.*[\}\]]$/.test(str)) str += '/';
14
15 // preserves full path in case of trailing path separator
16 str += 'a';
17
18 // remove path parts that are globby
19 do {str = pathDirname.posix(str)}
20 while (isglob(str) || /(^|[^\\])([\{\[]|\([^\)]+$)/.test(str));
21
22 // remove escape chars and return result
23 return str.replace(/\\([\*\?\|\[\]\(\)\{\}])/g, '$1');
24};