Installation
Quick Take
Examples
- Abort an asynchronous search
- Return absolute paths
- Brace expansion and extglobs
- Match without case sensitivity
- Configure directory expansion
- Return directories
- Disable symbolic-link traversal
- Include hidden entries
- Veto matches with ignore patterns
- Combine multiple patterns
- Exclude matches with a negative pattern
- Synchronous globbing
- Use a file URL as the working directory
Idea
Find files on disk by wildcard pattern. Only one runtime dependency (picomatch) (and a Node-only API).
import { glob } from "codsen-glob";
console.log(await glob("src/**/*.ts"));
// => ["src/main.test.ts", "src/main.ts", "src/nested/deep.ts"]
Results are returned sorted, with forward slashes on every platform, and relative to cwd unless you ask for absolute paths.
API — glob()
The main function glob() is imported like this:
It’s an async function which takes two input arguments and returns a promise resolving to an array of paths:
| Input argument | Type | Obligatory | Description |
|---|---|---|---|
patternsType: String or array of strings Obligatory: yes | |||
patterns | String or array of strings | yes | One or more glob patterns to match. |
optionsInputType: Plain object Obligatory: no | |||
optionsInput | Plain object | no | Optional Options Object. |
API — globSync()
The function globSync() is imported like this:
The synchronous counterpart of glob(). Same input arguments, same results, except it returns the array directly instead of a promise:
import { globSync } from "codsen-glob";
console.log(globSync("src/**/*.ts"));
// => ["src/main.test.ts", "src/main.ts", "src/nested/deep.ts"]
Patterns
A pattern prefixed with ! is negative — it removes from the results anything the positive patterns matched:
import { glob } from "codsen-glob";
console.log(await glob(["src/**/*.ts", "!src/**/*.test.ts"]));
// => ["src/main.ts", "src/nested/deep.ts"]
A pattern which names a directory is expanded to match everything inside it. See expandDirectories to control or switch off that behaviour.
The Optional Options Object
| Key | Type | Default | Description |
|---|---|---|---|
absoluteType: Boolean Default: false | |||
absolute | Boolean | false | Set to true to return absolute paths instead of paths relative to cwd. |
caseSensitiveMatchType: Boolean Default: true | |||
caseSensitiveMatch | Boolean | true | Set to false to match patterns regardless of letter case. |
cwdType: String or URLDefault: process.cwd() | |||
cwd | String or URL | process.cwd() | Directory to resolve patterns and results against. A file: URL is accepted and converted to a path. |
dotType: Boolean Default: false | |||
dot | Boolean | false | Set to true to also match files and directories whose name starts with a dot. |
expandDirectoriesType: Boolean, array of strings or object Default: true | |||
expandDirectories | Boolean, array of strings or object | true | Controls how a pattern naming a directory is expanded, see below. |
followSymbolicLinksType: Boolean Default: true | |||
followSymbolicLinks | Boolean | true | Set to false to leave symlinked entries unresolved. |
ignoreType: String or array of strings Default: [] | |||
ignore | String or array of strings | [] | Patterns to exclude from the results. Equivalent to passing negative patterns, but kept separate for clarity. |
onlyDirectoriesType: Boolean Default: false | |||
onlyDirectories | Boolean | false | Set to true to return only directories. Enabling it forces onlyFiles to false. |
onlyFilesType: Boolean Default: true | |||
onlyFiles | Boolean | true | Set to false to return directories alongside files. |
signalType: AbortSignalDefault: undefined | |||
signal | AbortSignal | undefined | Abort a traversal in progress. |
The program throws if cwd is neither a string nor a URL, or if ignore is neither a string nor an array of strings.
opts.expandDirectories
When a pattern points at a directory rather than at files, the program appends **/* to it, so "src" behaves like "src/**/*":
import { glob } from "codsen-glob";
console.log(await glob("src"));
// => ["src/main.test.ts", "src/main.ts", "src/util.js", "src/nested/deep.ts"]
Set expandDirectories to false to switch this off — a bare directory name then matches nothing:
console.log(await glob("src", { expandDirectories: false }));
// => []
Pass an array of strings to name the files to look for inside the directory, or an object to be specific:
| Value | Expands "src" into |
|---|---|
true (default) | |
true (default) | src/**/* |
false | |
false | nothing — the pattern is used as-is |
["index.js"] | |
["index.js"] | src/**/index.js |
{ extensions: ["ts"] } | |
{ extensions: ["ts"] } | src/**/*.ts |
{ files: ["index"], extensions: ["ts", "js"] } | |
{ files: ["index"], extensions: ["ts", "js"] } | src/**/index.ts and src/**/index.js |
console.log(await glob("src", { expandDirectories: { extensions: ["ts"] } }));
// => ["src/main.test.ts", "src/main.ts", "src/nested/deep.ts"]
API — version
You can import version:
API — types
This package is written in TypeScript and exports the following types:
| Type | Description |
|---|---|
GlobOptionsType: GlobOptions | |
GlobOptions | The Optional Options Object of glob() and globSync(), shown above. |
ExpandDirectoriesType: ExpandDirectories | |
ExpandDirectories | The accepted values of opts.expandDirectories, shown above. |
import type { ExpandDirectories, GlobOptions } from "codsen-glob";