[d565449] | 1 | <p align="center">
|
---|
| 2 | <a href="https://gulpjs.com">
|
---|
| 3 | <img height="257" width="114" src="https://raw.githubusercontent.com/gulpjs/artwork/master/gulp-2x.png">
|
---|
| 4 | </a>
|
---|
| 5 | </p>
|
---|
| 6 |
|
---|
| 7 | # glob-parent
|
---|
| 8 |
|
---|
| 9 | [![NPM version][npm-image]][npm-url] [![Downloads][downloads-image]][npm-url] [![Build Status][ci-image]][ci-url] [![Coveralls Status][coveralls-image]][coveralls-url]
|
---|
| 10 |
|
---|
| 11 | Extract the non-magic parent path from a glob string.
|
---|
| 12 |
|
---|
| 13 | ## Usage
|
---|
| 14 |
|
---|
| 15 | ```js
|
---|
| 16 | var globParent = require('glob-parent');
|
---|
| 17 |
|
---|
| 18 | globParent('path/to/*.js'); // 'path/to'
|
---|
| 19 | globParent('/root/path/to/*.js'); // '/root/path/to'
|
---|
| 20 | globParent('/*.js'); // '/'
|
---|
| 21 | globParent('*.js'); // '.'
|
---|
| 22 | globParent('**/*.js'); // '.'
|
---|
| 23 | globParent('path/{to,from}'); // 'path'
|
---|
| 24 | globParent('path/!(to|from)'); // 'path'
|
---|
| 25 | globParent('path/?(to|from)'); // 'path'
|
---|
| 26 | globParent('path/+(to|from)'); // 'path'
|
---|
| 27 | globParent('path/*(to|from)'); // 'path'
|
---|
| 28 | globParent('path/@(to|from)'); // 'path'
|
---|
| 29 | globParent('path/**/*'); // 'path'
|
---|
| 30 |
|
---|
| 31 | // if provided a non-glob path, returns the nearest dir
|
---|
| 32 | globParent('path/foo/bar.js'); // 'path/foo'
|
---|
| 33 | globParent('path/foo/'); // 'path/foo'
|
---|
| 34 | globParent('path/foo'); // 'path' (see issue #3 for details)
|
---|
| 35 | ```
|
---|
| 36 |
|
---|
| 37 | ## API
|
---|
| 38 |
|
---|
| 39 | ### `globParent(maybeGlobString, [options])`
|
---|
| 40 |
|
---|
| 41 | Takes a string and returns the part of the path before the glob begins. Be aware of Escaping rules and Limitations below.
|
---|
| 42 |
|
---|
| 43 | #### options
|
---|
| 44 |
|
---|
| 45 | ```js
|
---|
| 46 | {
|
---|
| 47 | // Disables the automatic conversion of slashes for Windows
|
---|
| 48 | flipBackslashes: true;
|
---|
| 49 | }
|
---|
| 50 | ```
|
---|
| 51 |
|
---|
| 52 | ## Escaping
|
---|
| 53 |
|
---|
| 54 | The following characters have special significance in glob patterns and must be escaped if you want them to be treated as regular path characters:
|
---|
| 55 |
|
---|
| 56 | - `?` (question mark) unless used as a path segment alone
|
---|
| 57 | - `*` (asterisk)
|
---|
| 58 | - `|` (pipe)
|
---|
| 59 | - `(` (opening parenthesis)
|
---|
| 60 | - `)` (closing parenthesis)
|
---|
| 61 | - `{` (opening curly brace)
|
---|
| 62 | - `}` (closing curly brace)
|
---|
| 63 | - `[` (opening bracket)
|
---|
| 64 | - `]` (closing bracket)
|
---|
| 65 |
|
---|
| 66 | **Example**
|
---|
| 67 |
|
---|
| 68 | ```js
|
---|
| 69 | globParent('foo/[bar]/'); // 'foo'
|
---|
| 70 | globParent('foo/\\[bar]/'); // 'foo/[bar]'
|
---|
| 71 | ```
|
---|
| 72 |
|
---|
| 73 | ## Limitations
|
---|
| 74 |
|
---|
| 75 | ### Braces & Brackets
|
---|
| 76 |
|
---|
| 77 | This library attempts a quick and imperfect method of determining which path
|
---|
| 78 | parts have glob magic without fully parsing/lexing the pattern. There are some
|
---|
| 79 | advanced use cases that can trip it up, such as nested braces where the outer
|
---|
| 80 | pair is escaped and the inner one contains a path separator. If you find
|
---|
| 81 | yourself in the unlikely circumstance of being affected by this or need to
|
---|
| 82 | ensure higher-fidelity glob handling in your library, it is recommended that you
|
---|
| 83 | pre-process your input with [expand-braces] and/or [expand-brackets].
|
---|
| 84 |
|
---|
| 85 | ### Windows
|
---|
| 86 |
|
---|
| 87 | Backslashes are not valid path separators for globs. If a path with backslashes
|
---|
| 88 | is provided anyway, for simple cases, glob-parent will replace the path
|
---|
| 89 | separator for you and return the non-glob parent path (now with
|
---|
| 90 | forward-slashes, which are still valid as Windows path separators).
|
---|
| 91 |
|
---|
| 92 | This cannot be used in conjunction with escape characters.
|
---|
| 93 |
|
---|
| 94 | ```js
|
---|
| 95 | // BAD
|
---|
| 96 | globParent('C:\\Program Files \\(x86\\)\\*.ext'); // 'C:/Program Files /(x86/)'
|
---|
| 97 |
|
---|
| 98 | // GOOD
|
---|
| 99 | globParent('C:/Program Files\\(x86\\)/*.ext'); // 'C:/Program Files (x86)'
|
---|
| 100 | ```
|
---|
| 101 |
|
---|
| 102 | If you are using escape characters for a pattern without path parts (i.e.
|
---|
| 103 | relative to `cwd`), prefix with `./` to avoid confusing glob-parent.
|
---|
| 104 |
|
---|
| 105 | ```js
|
---|
| 106 | // BAD
|
---|
| 107 | globParent('foo \\[bar]'); // 'foo '
|
---|
| 108 | globParent('foo \\[bar]*'); // 'foo '
|
---|
| 109 |
|
---|
| 110 | // GOOD
|
---|
| 111 | globParent('./foo \\[bar]'); // 'foo [bar]'
|
---|
| 112 | globParent('./foo \\[bar]*'); // '.'
|
---|
| 113 | ```
|
---|
| 114 |
|
---|
| 115 | ## License
|
---|
| 116 |
|
---|
| 117 | ISC
|
---|
| 118 |
|
---|
| 119 | <!-- prettier-ignore-start -->
|
---|
| 120 | [downloads-image]: https://img.shields.io/npm/dm/glob-parent.svg?style=flat-square
|
---|
| 121 | [npm-url]: https://www.npmjs.com/package/glob-parent
|
---|
| 122 | [npm-image]: https://img.shields.io/npm/v/glob-parent.svg?style=flat-square
|
---|
| 123 |
|
---|
| 124 | [ci-url]: https://github.com/gulpjs/glob-parent/actions?query=workflow:dev
|
---|
| 125 | [ci-image]: https://img.shields.io/github/workflow/status/gulpjs/glob-parent/dev?style=flat-square
|
---|
| 126 |
|
---|
| 127 | [coveralls-url]: https://coveralls.io/r/gulpjs/glob-parent
|
---|
| 128 | [coveralls-image]: https://img.shields.io/coveralls/gulpjs/glob-parent/master.svg?style=flat-square
|
---|
| 129 | <!-- prettier-ignore-end -->
|
---|
| 130 |
|
---|
| 131 | <!-- prettier-ignore-start -->
|
---|
| 132 | [expand-braces]: https://github.com/jonschlinkert/expand-braces
|
---|
| 133 | [expand-brackets]: https://github.com/jonschlinkert/expand-brackets
|
---|
| 134 | <!-- prettier-ignore-end -->
|
---|