Skip to content

glob

[TOC]

索引

方法

  • glob()(pattern, options?, callback),(异步),查找与指定模式匹配的文件。
  • glob.sync()(pattern, options?),(同步),查找与指定模式匹配的文件。

glob

方法

glob()

glob()(pattern, options?, callback),(异步),查找与指定模式匹配的文件。

  • patternstring | reg,需要匹配的路径模式(字符串),可以包含通配符。

  • options?{cwd,ignore,nodir,matchBase,realpath,followSymlinks},配置匹配行为。可以是对象或布尔值(true表示默认设置)。

    • cwd: 工作目录,默认为当前目录。
    • ignore: 要忽略的模式数组。
    • nodir: 如果设置为 true,不返回目录。
    • matchBase: 仅根据文件名进行匹配。
    • realpath: 如果设置为 true,返回绝对路径。
    • followSymlinks: 是否跟随符号链接。
  • callback(err, files) => void,回调函数。

    • errError,错误信息
    • filesstring[],匹配到的文件路径数组。
  • js
    const glob = require('glob');
    
    glob('src/**/*.js', { ignore: 'node_modules/**' }, (err, files) => {
        if (err) throw err;
        console.log(files);
    });

glob.sync()

glob.sync()(pattern, options?),(同步),查找与指定模式匹配的文件。

  • patternstring | reg,需要匹配的路径模式(字符串),可以包含通配符。

  • options?{cwd,ignore,nodir,matchBase,realpath,followSymlinks},配置匹配行为。可以是对象或布尔值(true表示默认设置)。

    • cwd: 工作目录,默认为当前目录。
    • ignore: 要忽略的模式数组。
    • nodir: 如果设置为 true,不返回目录。
    • matchBase: 仅根据文件名进行匹配。
    • realpath: 如果设置为 true,返回绝对路径。
    • followSymlinks: 是否跟随符号链接。
  • 返回:

  • filesarray默认:,匹配的文件路径数组。

  • js
    const glob = require('glob');
    
    const files = glob.sync('src/**/*.js', { ignore: 'node_modules/**' });
    console.log(files);