# 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:
没有合适的资源?快使用搜索试试~ 我知道了~
node.js源码 node-v21.0.0.tar.gz
需积分: 5 0 下载量 115 浏览量
2023-11-07
14:54:38
上传
评论
收藏 84.82MB GZ 举报
温馨提示
共2000个文件
c:1188个
h:629个
md:148个
node.js源码 node-v21.0.0.tar.gz
资源推荐
资源详情
资源评论
收起资源包目录
node.js源码 node-v21.0.0.tar.gz (2000个子文件)
ecp_nistz256_table.c 603KB
ares_platform.c 482KB
curve25519.c 198KB
ssl_lib.c 169KB
ec_curve.c 142KB
statem_srvr.c 134KB
e_aes.c 132KB
s3_lib.c 128KB
speed.c 125KB
s_server.c 120KB
statem_clnt.c 119KB
s_client.c 115KB
x509_vfy.c 112KB
t1_lib.c 111KB
ctrl_params_translate.c 106KB
cmp.c 105KB
curve448_tables.c 97KB
apps.c 96KB
ca.c 88KB
aes_core.c 80KB
statem_lib.c 79KB
ecp_nistp256.c 74KB
ecp_nistp521.c 73KB
ssl_ciph.c 71KB
p_lib.c 70KB
pmeth_lib.c 69KB
provider_core.c 68KB
extensions_srvr.c 68KB
extensions_clnt.c 68KB
ssl3_record.c 66KB
ares_init.c 65KB
extensions.c 63KB
rec_layer_s3.c 62KB
bn_dh.c 61KB
ecp_nistp224.c 59KB
bss_dgram.c 56KB
t1_trce.c 56KB
e_loader_attic.c 55KB
e_capi.c 55KB
list.c 54KB
gcm128.c 54KB
req.c 54KB
encode_key2any.c 53KB
ecp_nistz256.c 51KB
evp_enc.c 50KB
cms.c 50KB
ec_lib.c 49KB
ares_process.c 49KB
ec_kmgmt.c 49KB
rsa_sig.c 49KB
ecp_smpl.c 49KB
s_cb.c 48KB
bn_exp.c 48KB
http_client.c 48KB
aria.c 44KB
statem_dtls.c 44KB
e_devcrypto.c 43KB
tls13_enc.c 43KB
x509.c 42KB
v3_addr.c 42KB
keccak1600.c 41KB
ocsp.c 41KB
ssl_sess.c 41KB
aes_x86core.c 40KB
pkcs12.c 40KB
tasn_dec.c 39KB
ec_asn1.c 38KB
v3_utl.c 38KB
params.c 38KB
ecx_meth.c 37KB
bn_nist.c 37KB
pk7_doit.c 37KB
server.c 36KB
cms_env.c 36KB
cmp_msg.c 36KB
rsa_lib.c 35KB
wp_block.c 34KB
cmp_ctx.c 34KB
decoder_lib.c 34KB
v3_purp.c 34KB
ssl_conf.c 33KB
rec_layer_d1.c 33KB
fipsprov.c 33KB
e_dasync.c 33KB
cmp_client.c 33KB
evp_lib.c 33KB
digest.c 32KB
ecx_kmgmt.c 32KB
ffc_params_generate.c 32KB
cms_sd.c 32KB
ts.c 32KB
decode_der2key.c 31KB
store_lib.c 31KB
e_aes_cbc_hmac_sha1.c 31KB
e_aes_cbc_hmac_sha256.c 31KB
statem.c 31KB
ssl_cert.c 31KB
opt.c 31KB
ec_mult.c 31KB
ssl_rsa.c 31KB
共 2000 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20
资源评论
李小白20200202
- 粉丝: 3541
- 资源: 70
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 解决未在远程桌面会话中捕获到鼠标,vmguest.iso软件分享给大家
- JSP+SQL基于WEB的开放性实验管理系统设计与实现(源代码+论文+开题报告+中英文献+答辩PPT).rar
- log4net配置文件!!!!!!!!!!!!!!!!!
- 河南统计面板数据集(2010-2022年).xlsx
- OrcaleDBHelper帮助类!!!!!!!!!!!!
- log4net帮助类,用来写日志!!!!!!!!!!!!!
- Windows10时间同步源
- 信呼OA系统2.1.7版源码
- 3122080306 邹子轩 实验报告二.docx
- 基于STM32 NUCLEO板设计彩色LED照明灯(纯cubeMX开发)(大赛作品,文档完整,可直接运行)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功