# Glob
Match files using the patterns the shell uses.
The most correct and second fastest glob implementation in
JavaScript. (See **Comparison to Other JavaScript Glob
Implementations** at the bottom of this readme.)
![a fun cartoon logo made of glob characters](https://github.com/isaacs/node-glob/raw/main/logo/glob.png)
## Usage
Install with npm
```
npm i glob
```
**Note** the npm package name is _not_ `node-glob` that's a
different thing that was abandoned years ago. Just `glob`.
```js
// load using import
import { glob, globSync, globStream, globStreamSync, Glob } from 'glob'
// or using commonjs, that's fine, too
const {
glob,
globSync,
globStream,
globStreamSync,
Glob,
} = require('glob')
// the main glob() and globSync() resolve/return array of filenames
// all js files, but don't look in node_modules
const jsfiles = await glob('**/*.js', { ignore: 'node_modules/**' })
// pass in a signal to cancel the glob walk
const stopAfter100ms = await glob('**/*.css', {
signal: AbortSignal.timeout(100),
})
// multiple patterns supported as well
const images = await glob(['css/*.{png,jpeg}', 'public/*.{png,jpeg}'])
// but of course you can do that with the glob pattern also
// the sync function is the same, just returns a string[] instead
// of Promise<string[]>
const imagesAlt = globSync('{css,public}/*.{png,jpeg}')
// you can also stream them, this is a Minipass stream
const filesStream = globStream(['**/*.dat', 'logs/**/*.log'])
// construct a Glob object if you wanna do it that way, which
// allows for much faster walks if you have to look in the same
// folder multiple times.
const g = new Glob('**/foo', {})
// glob objects are async iterators, can also do globIterate() or
// g.iterate(), same deal
for await (const file of g) {
console.log('found a foo file:', file)
}
// pass a glob as the glob options to reuse its settings and caches
const g2 = new Glob('**/bar', g)
// sync iteration works as well
for (const file of g2) {
console.log('found a bar file:', file)
}
// you can also pass withFileTypes: true to get Path objects
// these are like a Dirent, but with some more added powers
// check out http://npm.im/path-scurry for more info on their API
const g3 = new Glob('**/baz/**', { withFileTypes: true })
g3.stream().on('data', path => {
console.log(
'got a path object',
path.fullpath(),
path.isDirectory(),
path.readdirSync().map(e => e.name)
)
})
// if you use stat:true and withFileTypes, you can sort results
// by things like modified time, filter by permission mode, etc.
// All Stats fields will be available in that case. Slightly
// slower, though.
// For example:
const results = await glob('**', { stat: true, withFileTypes: true })
const timeSortedFiles = results
.sort((a, b) => a.mtimeMs - b.mtimeMs)
.map(path => path.fullpath())
const groupReadableFiles = results
.filter(path => path.mode & 0o040)
.map(path => path.fullpath())
// custom ignores can be done like this, for example by saying
// you'll ignore all markdown files, and all folders named 'docs'
const customIgnoreResults = await glob('**', {
ignore: {
ignored: p => /\.md$/.test(p.name),
childrenIgnored: p => p.isNamed('docs'),
},
})
// another fun use case, only return files with the same name as
// their parent folder, plus either `.ts` or `.js`
const folderNamedModules = await glob('**/*.{ts,js}', {
ignore: {
ignored: p => {
const pp = p.parent
return !(p.isNamed(pp.name + '.ts') || p.isNamed(pp.name + '.js'))
},
},
})
// find all files edited in the last hour, to do this, we ignore
// all of them that are more than an hour old
const newFiles = await glob('**', {
// need stat so we have mtime
stat: true,
// only want the files, not the dirs
nodir: true,
ignore: {
ignored: p => {
return new Date() - p.mtime > 60 * 60 * 1000
},
// could add similar childrenIgnored here as well, but
// directory mtime is inconsistent across platforms, so
// probably better not to, unless you know the system
// tracks this reliably.
},
})
```
**Note** Glob patterns should always use `/` as a path separator,
even on Windows systems, as `\` is used to escape glob
characters. If you wish to use `\` as a path separator _instead
of_ using it as an escape character on Windows platforms, you may
set `windowsPathsNoEscape:true` in the options. In this mode,
special glob characters cannot be escaped, making it impossible
to match a literal `*` `?` and so on in filenames.
## Command Line Interface
```
$ glob -h
Usage:
glob [options] [<pattern> [<pattern> ...]]
Expand the positional glob expression arguments into any matching file system
paths found.
-c<command> --cmd=<command>
Run the command provided, passing the glob expression
matches as arguments.
-A --all By default, the glob cli command will not expand any
arguments that are an exact match to a file on disk.
This prevents double-expanding, in case the shell
expands an argument whose filename is a glob
expression.
For example, if 'app/*.ts' would match 'app/[id].ts',
then on Windows powershell or cmd.exe, 'glob app/*.ts'
will expand to 'app/[id].ts', as expected. However, in
posix shells such as bash or zsh, the shell will first
expand 'app/*.ts' to a list of filenames. Then glob
will look for a file matching 'app/[id].ts' (ie,
'app/i.ts' or 'app/d.ts'), which is unexpected.
Setting '--all' prevents this behavior, causing glob to
treat ALL patterns as glob expressions to be expanded,
even if they are an exact match to a file on disk.
When setting this option, be sure to enquote arguments
so that the shell will not expand them prior to passing
them to the glob command process.
-a --absolute Expand to absolute paths
-d --dot-relative Prepend './' on relative matches
-m --mark Append a / on any directories matched
-x --posix Always resolve to posix style paths, using '/' as the
directory separator, even on Windows. Drive letter
absolute matches on Windows will be expanded to their
full resolved UNC maths, eg instead of 'C:\foo\bar', it
will expand to '//?/C:/foo/bar'.
-f --follow Follow symlinked directories when expanding '**'
-R --realpath Call 'fs.realpath' on all of the results. In the case
of an entry that cannot be resolved, the entry is
omitted. This incurs a slight performance penalty, of
course, because of the added system calls.
-s --stat Call 'fs.lstat' on all entries, whether required or not
to determine if it's a valid match.
-b --match-base Perform a basename-only match if the pattern does not
contain any slash characters. That is, '*.js' would be
treated as equivalent to '**/*.js', matching js files
in all directories.
--dot Allow patterns to match files/directories that start
with '.', even if the pattern does not start with '.'
--nobrace Do not expand {...} patterns
--nocase Perform a case-insensitive match. This defaults to
'true' on macOS and Windows platforms, and false on all
others.
Note:
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
ECharts 数据可视化大屏项目学习 (2000个子文件)
preflight.css 8KB
preflight.css 8KB
tailwind.css 61B
components.css 22B
utilities.css 21B
variants.css 20B
screens.css 19B
base.css 16B
index.html 7KB
index.js 4.1MB
echarts.js 3.18MB
echarts.esm.js 2.86MB
echarts.common.js 2.09MB
echarts.simple.js 1.51MB
echarts.min.js 999KB
echarts.esm.min.js 999KB
rollup.js 794KB
node-entry.js 793KB
echarts.common.min.js 642KB
vue.global.js 509KB
vue.esm-browser.js 478KB
echarts.simple.min.js 454KB
vue.runtime.global.js 347KB
vue.runtime.esm-browser.js 322KB
vue.esm-browser.prod.js 147KB
vue.global.prod.js 144KB
corePlugins.js 131KB
watch.js 128KB
index.js 119KB
axios.js 94KB
parseAst.js 92KB
vue.runtime.esm-browser.prod.js 89KB
corePlugins.js 89KB
vue.runtime.global.prod.js 89KB
parseAst.js 84KB
axios.js 82KB
echarts.js 82KB
typescript.js 60KB
setupContextUtils.js 57KB
typescript.js 46KB
readWordTree.js 43KB
statement.js 42KB
readWordTree.js 42KB
index.js 42KB
setupContextUtils.js 42KB
LineView.js 40KB
CustomView.js 39KB
flow.js 38KB
generateRules.js 37KB
SeriesData.js 36KB
expression.js 35KB
object_hash.js 34KB
CJSImportTransformer.js 34KB
index.js 34KB
statement.js 33KB
TooltipView.js 33KB
index.js 33KB
BarView.js 33KB
index.js 33KB
DataStore.js 33KB
index.js 32KB
TreemapView.js 32KB
SliderZoomView.js 31KB
axios.min.js 31KB
CJSImportTransformer.js 31KB
index.js 29KB
grid-utils.js 29KB
axios.min.js 28KB
Global.js 28KB
expression.js 28KB
generateRules.js 28KB
index.js 28KB
ContinuousView.js 28KB
MapDraw.js 27KB
parse.js 27KB
flow.js 26KB
JSXTransformer.js 26KB
model.js 26KB
states.js 25KB
JSXTransformer.js 25KB
BrushController.js 25KB
PictorialBarView.js 25KB
SliderTimelineView.js 25KB
config.full.js 24KB
prefixes.js 23KB
expandApplyAtRules.js 23KB
GaugeView.js 22KB
TreeView.js 22KB
AxisBuilder.js 21KB
processor.js 21KB
universalTransition.js 21KB
http.js 21KB
loadConfigFile.js 21KB
bigfraction.js 21KB
resolver_sync.js 20KB
Scheduler.js 20KB
LegendView.js 20KB
nodefs-handler.js 20KB
resolver.js 20KB
Series.js 20KB
共 2000 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20
资源评论
呆呆小板栗
- 粉丝: 450
- 资源: 89
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功