基础学习: 更快的 glob/globby,fast-glob/fg 常用功能

记录一下 fast-glob 的常用功能
更新于: 2023-11-24 10:01:03

基本介绍

  1. 号称更快的 glob
  2. gulp 用到了 globby
  3. 而 globby 的底层就是 fg(fast-glob) 了

安装

npm i fast-glob

基本使用

const fg = require('fast-glob');
# 导步
const entries = await fg(['.editorconfig', '**/index.js'], { dot: true });
# 同步
const entries = fg.sync(['.editorconfig', '**/index.js'], { dot: true });

示例目录

.
├── .editorconfig
├── .eslintrc.json
├── .git
├── .gitattributes
├── .github
├── .gitignore
├── .npmrc
├── LICENSE
├── README.md
├── fixtures
├── package.json
├── src
└── tsconfig.json

选项及配套用法

选项 代码备注
concurrencycpu.length-最大并发请求数
cwd当前
const fg = require("fast-glob");
const entries = fg.sync(["*", ".*"], { cwd: "./ellis_error" });
console.log(entries);
Current Work Dir
deep1
dir/
└── one/            // 1
    └── two/        // 2
        └── file.js // 3
// With base directory
fg.sync('dir/**', { onlyFiles: false, deep: 1 }); // ['dir/one']
fg.sync('dir/**', { onlyFiles: false, deep: 2 }); // ['dir/one', 'dir/one/two']

// With cwd option
fg.sync('**', { onlyFiles: false, cwd: 'dir', deep: 1 }); // ['one']
fg.sync('**', { onlyFiles: false, cwd: 'dir', deep: 2 }); // ['one', 'one/two']
深度
followSymbolicLinks-  
fs- 默认 fs 实现
absolute 
fg.sync('*.js', { absolute: false }); // ['index.js']
fg.sync('*.js', { absolute: true });  // ['/home/user/index.js']
 
markDirectories 
fg.sync('*', { onlyFiles: false, markDirectories: false }); // ['index.js', 'controllers']
fg.sync('*', { onlyFiles: false, markDirectories: true });  // ['index.js', 'controllers/']
 
objectMode 
fg.sync('*', { objectMode: false }); // ['src/index.js']
fg.sync('*', { objectMode: true });  // [{ name: 'index.js', path: 'src/index.js', dirent: <fs.Dirent> }]
 
onlyDirectories 
fg.sync('*', { onlyDirectories: false }); // ['index.js', 'src']
fg.sync('*', { onlyDirectories: true });  // ['src']
仅保存目录
onlyFiles 
fg.sync('*', { onlyFiles: false }); // ['index.js', 'src']
fg.sync('*', { onlyFiles: true });  // ['index.js']
 
statsfalse 将文件 信息 添加进来
uniquetrue
fg.sync(['*.json', 'package.json'], { unique: false }); // ['package.json', 'package.json']
fg.sync(['*.json', 'package.json'], { unique: true });  // ['package.json']
 
caseSensitiveMatch 
dir/
├── file.txt
└── File.txt
fg.sync('file.txt', { caseSensitiveMatch: false }); // ['file.txt', 'File.txt']
fg.sync('file.txt', { caseSensitiveMatch: true });  // ['file.txt']
大小写敏感
dotfalse
dir/
├── .editorconfig
└── package.json
fg.sync('*', { dot: false }); // ['package.json']
fg.sync('*', { dot: true });  // ['.editorconfig', 'package.json']
对点文件处理
globstarfalse
dir/
└── a
    └── b
fg.sync('**', { onlyFiles: false, globstar: false }); // ['a']
fg.sync('**', { onlyFiles: false, globstar: true });  // ['a', 'a/b']
**情况的递归

参考