Allen's blog Allen's blog
首页
面经
算法 (opens new window)
分类

Allen

前端CV工程师
首页
面经
算法 (opens new window)
分类
  • nodejs前言
  • glob模块使用
    • glob.sync/globSync
  • nodejs
Allen
2023-04-25
目录

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

匹配结果为:

;[
    '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

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

当我们读取文件的时候,不希望得到含有.vscode的文件名,加上 ignore 参数就好了

后面看了看参数类型,有个参数nodir可以做到上面的忽略文件夹的功能

上次更新: 2023/12/16, 09:22:46
nodejs前言

← nodejs前言

最近更新
01
rollup使用配置文件rollup.config.ts打包
12-08
02
package.json导出类型
12-08
03
关键问题方案
11-17
更多文章>
Theme by Vdoing | Copyright © 2023-2023 Allen | Github
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式