You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

134 lines
4.1 KiB

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