Socket
Socket
Sign inDemoInstall

fast-glob

Package Overview
Dependencies
80
Maintainers
1
Versions
43
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    fast-glob

Is a faster `node-glob` alternative


Version published
Weekly downloads
47M
decreased by-0.06%
Maintainers
1
Install size
2.46 MB
Created
Weekly downloads
 

Package description

What is fast-glob?

The fast-glob package is a Node.js library that provides a fast and efficient way to match file paths against specified patterns. It uses the glob syntax, which is a way of filtering files in file systems using wildcard characters.

What are fast-glob's main functionalities?

Synchronous file searching

This feature allows you to perform synchronous file searches, returning an array of paths that match the specified patterns. The example code searches for all files except markdown files.

const fg = require('fast-glob');
const paths = fg.sync(['**/*', '!**/*.md']);

Asynchronous file searching

This feature allows you to perform asynchronous file searches, returning a promise that resolves with an array of paths that match the specified patterns. The example code searches for all files except markdown files and logs the result.

const fg = require('fast-glob');
fg.async(['**/*', '!**/*.md']).then(paths => {
  console.log(paths);
});

Stream interface for file searching

This feature provides a stream interface for file searching, emitting each matching path as a 'data' event. The example code searches for all files except markdown files and logs each matching path as it's found.

const fg = require('fast-glob');
const stream = fg.stream(['**/*', '!**/*.md']);
stream.on('data', (entry) => console.log(entry));

Other packages similar to fast-glob

Readme

Source

:rocket: fast-glob

Is a faster node-glob alternative.

:bulb: Highlights

  • :rocket: Fast by using Streams and Promises. Used readdir-enhanced and micromatch.
  • :beginner: User-friendly, since it supports multiple and negated patterns (['*', '!*.md']).
  • :vertical_traffic_light: Rational, because it doesn't read excluded directories (!**/node_modules/**).
  • :gear: Universal, because it supports Synchronous, Promise and Stream API.
  • :money_with_wings: Economy, because it provides fs.Stats for matched path if you wanted.

Donate

If you want to thank me, or promote your Issue.

Donate

Sorry, but I have work and support for packages requires some time after work. I will be glad of your support and PR's.

Install

$ npm install --save fast-glob

Usage

Asynchronous
const fg = require('fast-glob');

fg(['src/**/*.js', '!src/**/*.spec.js']).then((entries) => console.log(entries));
fg.async(['src/**/*.js', '!src/**/*.spec.js']).then((entries) => console.log(entries));
Synchronous
const fg = require('fast-glob');

const entries = fg.sync(['src/**/*.js', '!src/**/*.spec.js']);
console.log(entries);
Stream
const fg = require('fast-glob');

const stream = fg.stream(['src/**/*.js', '!src/**/*.spec.js']);

const entries = [];

stream.on('data', (entry) => entries.push(entry));
stream.once('error', console.log);
stream.once('end', () => console.log(entries));

API

fg(patterns, [options])

fg.async(patterns, [options])

Returns a Promise with an array of matching entries.

fg.sync(patterns, [options])

Returns an array of matching entries.

fg.stream(patterns, [options])

Returns a ReadableStream when the data event will be emitted with Entry.

patterns
  • Type: string|string[]

This package does not respect the order of patterns. First, all the negative patterns are applied, and only then the positive patterns.

options
  • Type: Object

See options section for more detailed information.

fg.generateTasks(patterns, [options])

Return a set of tasks based on provided patterns. All tasks satisfy the Task interface:

interface Task {
  /**
   * Parent directory for all patterns inside this task.
   */
  base: string;
  /**
   * Dynamic or static patterns are in this task.
   */
  dynamic: boolean;
  /**
   * All patterns.
   */
  patterns: string[];
  /**
   * Only positive patterns.
   */
  positive: string[];
  /**
   * Only negative patterns without ! symbol.
   */
  negative: string[];
}

Entry

The entry which can be a string if the stats option is disabled, otherwise fs.Stats with two additional path and depth properties.

Options

cwd
  • Type: string
  • Default: process.cwd()

The current working directory in which to search.

deep
  • Type: number|boolean
  • Default: true

The deep option can be set to true to traverse the entire directory structure, or it can be set to a number to only traverse that many levels deep.

For example, you have the following tree:

test
└── one
    └── two
        └── index.js

:book: If you specify a pattern with some base directory, this directory will not participate in the calculation of the depth of the found directories. Think of it as a cwd option.

fg('test/**', { onlyFiles: false, deep: 0 });
// -> ['test/one']
fg('test/**', { onlyFiles: false, deep: 1 });
// -> ['test/one', 'test/one/two']

fg('**', { onlyFiles: false, cwd: 'test', deep: 0 });
// -> ['one']
fg('**', { onlyFiles: false, cwd: 'test', deep: 1 });
// -> ['one', 'one/two']
ignore
  • Type: string[]
  • Default: []

An array of glob patterns to exclude matches.

dot
  • Type: boolean
  • Default: false

Allow patterns to match filenames starting with a period (files & directories), even if the pattern does not explicitly have a period in that spot.

stats
  • Type: boolean
  • Default: false

Return fs.Stats with two additional path and depth properties instead of a string.

onlyFiles
  • Type: boolean
  • Default: true

Return only files.

onlyDirectories
  • Type: boolean
  • Default: false

Return only directories.

followSymlinkedDirectories
  • Type: boolean
  • Default: true

Follow symlinked directories when expanding ** patterns.

unique
  • Type: boolean
  • Default: true

Prevent duplicate results.

markDirectories
  • Type: boolean
  • Default: false

Add a / character to directory entries.

absolute
  • Type: boolean
  • Default: false

Return absolute paths for matched entries.

:book: Note that you need to use this option if you want to use absolute negative patterns like ${__dirname}/*.md.

nobrace
  • Type: boolean
  • Default: false

Disable expansion of brace patterns ({a,b}, {1..3}).

brace
  • Type: boolean
  • Default: true

The nobrace option without double-negation. This option has a higher priority then nobrace.

noglobstar
  • Type: boolean
  • Default: false

Disable matching with globstars (**).

globstar
  • Type: boolean
  • Default: true

The noglobstar option without double-negation. This option has a higher priority then noglobstar.

noext
  • Type: boolean
  • Default: false

Disable extglob support (patterns like +(a|b)), so that extglobs are regarded as literal characters.

extension
  • Type: boolean
  • Default: true

The noext option without double-negation. This option has a higher priority then noext.

nocase
  • Type: boolean
  • Default: false

Disable a case-sensitive mode for matching files.

Examples
  • File System: test/file.md, test/File.md
  • Case-sensitive for test/file.* pattern (false): test/file.md
  • Case-insensitive for test/file.* pattern (true): test/file.md, test/File.md
case
  • Type: boolean
  • Default: true

The nocase option without double-negation. This option has a higher priority then nocase.

matchBase
  • Type: boolean
  • Default: false

Allow glob patterns without slashes to match a file path based on its basename. For example, a?b would match the path /xyz/123/acb, but not /xyz/acb/123.

transform
  • Type: Function
  • Default: null

Allows you to transform a path or fs.Stats object before sending to the array.

const fg = require('fast-glob');

const entries1 = fg.sync(['**/*.scss']);
const entries2 = fg.sync(['**/*.scss'], { transform: (entry) => '_' + entry });

console.log(entries1); // ['a.scss', 'b.scss']
console.log(entries2); // ['_a.scss', '_b.scss']

If you are using TypeScript, you probably want to specify your own type of the returned array.

import * as fg from 'fast-glob';

interface IMyOwnEntry {
	path: string;
}

const entries: IMyOwnEntry[] = fg.sync<IMyOwnEntry>(['*.md'], {
	transform: (entry) => typeof entry === 'string' ? { path: entry } : { path: entry.path }
	// Will throw compilation error for non-IMyOwnEntry types (boolean, for example)
});

How to exclude directory from reading?

You can use a negative pattern like this: !**/node_modules or !**/node_modules/**. Also you can use ignore option. Just look at the example below.

first/
├── file.md
└── second
    └── file.txt

If you don't want to read the second directory, you must write the following pattern: !**/second or !**/second/**.

fg.sync(['**/*.md', '!**/second']); // ['first/file.txt']
fg.sync(['**/*.md'], { ignore: '**/second/**' }); // ['first/file.txt']

:warning: When you write !**/second/**/* it means that the directory will be read, but all the entries will not be included in the results.

You have to understand that if you write the pattern to exclude directories, then the directory will not be read under any circumstances.

How to use UNC path?

You cannot use UNC paths as patterns (due to syntax), but you can use them as cwd directory.

fg.sync('*', { cwd: '\\\\?\\C:\\Python27' /* or //?/C:/Python27 */ });
fg.sync('Python27/*', { cwd: '\\\\?\\C:\\' /* or //?/C:/ */ });

Compatible with node-glob?

Not fully, because fast-glob does not implement all options of node-glob. See table below.

node-globfast-glob
cwdcwd
root
dotdot
nomount
markmarkDirectories
nosort
nouniqueunique
nobracenobrace or brace
noglobstarnoglobstar or globstar
noextnoext or extension
nocasenocase or case
matchBasematchbase
nodironlyFiles
ignoreignore
followfollowSymlinkedDirectories
realpath
absoluteabsolute

Benchmarks

Tech specs:

Server: Vultr Bare Metal

  • Processor: E3-1270v6 (8 CPU)
  • RAM: 32GB
  • Disk: SSD

You can see results here for latest release.

  • readdir-enhanced – Fast functional replacement for fs.readdir().
  • globby – User-friendly glob matching.
  • node-glob – «Standard» glob functionality for Node.js
  • bash-glob – Bash-powered globbing for node.js.
  • glob-stream – A Readable Stream interface over node-glob that used in the gulpjs.
  • tiny-glob – Tiny and extremely fast library to match files and folders using glob patterns.

Changelog

See the Releases section of our GitHub project for changelogs for each release version.

License

This software is released under the terms of the MIT license.

Keywords

FAQs

Last updated on 18 May 2019

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc