glob模块使用
glob 库是用来匹配文件的,给一个匹配模式,就可以匹配到符合该模式的所有文件名,返回文件名数组 以下笔记是 glob@^9.3.4 版本的
# glob.sync/globSync
glob.sync 和 globSync 是别名关系,二者用法一致。
glob.sync('**/*', {
cwd: path.resolve(__dirname, `../configs/${configType}/`),
dot: true,
ignore: '.vscode'
})
1
2
3
4
5
2
3
4
5
匹配结果为:
;[
'cz-customize.js',
'commitlint.config.js',
'.stylelintrc.js',
'.stylelintignore',
'.prettierrc.js',
'.gitignore',
'.gitattributes',
'.eslintrc.js',
'.eslintignore',
'.editorconfig',
'.vscode\\settings.json',
'.vscode\\extensions.json'
]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
cwd 参数表示在这个目录下进行匹配
dot 参数表示匹配"."开头的文件
ignore 参数表示忽略文件名
如果没有 ignore 参数,glob 会输出文件夹名,以下输出的.vscode实际上是一个文件夹
;[
'cz-customize.js',
'commitlint.config.js',
'.vscode',
'.stylelintrc.js',
'.stylelintignore',
'.prettierrc.js',
'.gitignore',
'.gitattributes',
'.eslintrc.js',
'.eslintignore',
'.editorconfig',
'.vscode\\settings.json',
'.vscode\\extensions.json'
]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
当我们读取文件的时候,不希望得到含有.vscode的文件名,加上 ignore 参数就好了
后面看了看参数类型,有个参数
nodir可以做到上面的忽略文件夹的功能
上次更新: 2023/12/16, 09:22:46