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.

109 lines
3.5 KiB

4 years ago
  1. glob-parent [![Build Status](https://travis-ci.org/es128/glob-parent.svg)](https://travis-ci.org/es128/glob-parent) [![Coverage Status](https://img.shields.io/coveralls/es128/glob-parent.svg)](https://coveralls.io/r/es128/glob-parent?branch=master)
  2. ======
  3. Javascript module to extract the non-magic parent path from a glob string.
  4. [![NPM](https://nodei.co/npm/glob-parent.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/glob-parent/)
  5. [![NPM](https://nodei.co/npm-dl/glob-parent.png?height=3&months=9)](https://nodei.co/npm-dl/glob-parent/)
  6. Usage
  7. -----
  8. ```sh
  9. npm install glob-parent --save
  10. ```
  11. **Examples**
  12. ```js
  13. var globParent = require('glob-parent');
  14. globParent('path/to/*.js'); // 'path/to'
  15. globParent('/root/path/to/*.js'); // '/root/path/to'
  16. globParent('/*.js'); // '/'
  17. globParent('*.js'); // '.'
  18. globParent('**/*.js'); // '.'
  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/*(to|from)'); // 'path'
  24. globParent('path/@(to|from)'); // 'path'
  25. globParent('path/**/*'); // 'path'
  26. // if provided a non-glob path, returns the nearest dir
  27. globParent('path/foo/bar.js'); // 'path/foo'
  28. globParent('path/foo/'); // 'path/foo'
  29. globParent('path/foo'); // 'path' (see issue #3 for details)
  30. ```
  31. ## Escaping
  32. The following characters have special significance in glob patterns and must be escaped if you want them to be treated as regular path characters:
  33. - `?` (question mark)
  34. - `*` (star)
  35. - `|` (pipe)
  36. - `(` (opening parenthesis)
  37. - `)` (closing parenthesis)
  38. - `{` (opening curly brace)
  39. - `}` (closing curly brace)
  40. - `[` (opening bracket)
  41. - `]` (closing bracket)
  42. **Example**
  43. ```js
  44. globParent('foo/[bar]/') // 'foo'
  45. globParent('foo/\\[bar]/') // 'foo/[bar]'
  46. ```
  47. ## Limitations
  48. #### Braces & Brackets
  49. This library attempts a quick and imperfect method of determining which path
  50. parts have glob magic without fully parsing/lexing the pattern. There are some
  51. advanced use cases that can trip it up, such as nested braces where the outer
  52. pair is escaped and the inner one contains a path separator. If you find
  53. yourself in the unlikely circumstance of being affected by this or need to
  54. ensure higher-fidelity glob handling in your library, it is recommended that you
  55. pre-process your input with [expand-braces] and/or [expand-brackets].
  56. #### Windows
  57. Backslashes are not valid path separators for globs. If a path with backslashes
  58. is provided anyway, for simple cases, glob-parent will replace the path
  59. separator for you and return the non-glob parent path (now with
  60. forward-slashes, which are still valid as Windows path separators).
  61. This cannot be used in conjunction with escape characters.
  62. ```js
  63. // BAD
  64. globParent('C:\\Program Files \\(x86\\)\\*.ext') // 'C:/Program Files /(x86/)'
  65. // GOOD
  66. globParent('C:/Program Files\\(x86\\)/*.ext') // 'C:/Program Files (x86)'
  67. ```
  68. If you are using escape characters for a pattern without path parts (i.e.
  69. relative to `cwd`), prefix with `./` to avoid confusing glob-parent.
  70. ```js
  71. // BAD
  72. globParent('foo \\[bar]') // 'foo '
  73. globParent('foo \\[bar]*') // 'foo '
  74. // GOOD
  75. globParent('./foo \\[bar]') // 'foo [bar]'
  76. globParent('./foo \\[bar]*') // '.'
  77. ```
  78. Change Log
  79. ----------
  80. [See release notes page on GitHub](https://github.com/es128/glob-parent/releases)
  81. License
  82. -------
  83. [ISC](https://raw.github.com/es128/glob-parent/master/LICENSE)
  84. [expand-braces]: https://github.com/jonschlinkert/expand-braces
  85. [expand-brackets]: https://github.com/jonschlinkert/expand-brackets