| Leo Repp | 58b9f11 | 2021-11-22 11:57:47 +0100 | [diff] [blame^] | 1 | 'use strict'; |
| 2 | |
| 3 | var path = require('path'); |
| 4 | var isglob = require('is-glob'); |
| 5 | var pathDirname = require('path-dirname'); |
| 6 | var isWin32 = require('os').platform() === 'win32'; |
| 7 | |
| 8 | module.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 | }; |