Convert Gitignore to Glob patterns
npm install --save globify-gitignore
globify-gitignore
is tiny and treeshakable.
Use globifyGitIgnoreFile
to pass the path to a directory that has a .gitignore
file.
import { globifyGitIgnoreFile, globifyGitIgnore } from "globify-gitignore"
// ./ is the path that has a .gitignore
globifyGitIgnoreFile("./")
Use globifyGitIgnore
to directly globify the gitignore content
import { globifyGitIgnore } from "globify-gitignore"
const gitignoreContent = `# OS metadata
.DS_Store
Thumbs.db
# Node
node_modules
package-lock.json
# TypeScript
*.tsbuildinfo
# Build directories
dist
`
const gitignoreDirectory = __dirname
// An array of `GlobifiedEntry` objects. Each object has a `glob` property and an `included` property
const globifiedEntries = await globifyGitIgnore(gitignoreContent, gitignoreDirectory)
The result of a globified gitignore entry. The glob pattern is in the glob
property, and the included
property tells if the pattern is an included file or an excluded file
export type GlobifiedEntry = {
/** The glob pattern calculated from the gitignore pattern */
glob: string
/**
* If `true`, this means that the pattern was prepended by `!` in the gitignore file, and so it is an included file
* Otherwise, it is an excluded file
*/
included: boolean
}
Parse and globy the .gitingore
file that exists in a directory
Parameters:
- gitIgnoreDirectory (
string
) - The given directory that has the.gitignore
file - absolute (
boolean
) - [false] If true, the glob will be absolute
returns: Promise<GlobifiedEntry[]>
Globify the content of a gitignore string
Parameters:
- gitIgnoreContent (
string
) - The content of the gitignore file - gitIgnoreDirectory (
string
) - The directory of gitignore - absolute (
boolean
) - [false] If true, the glob will be absolute
returns: Promise<GlobifiedEntry[]>
Parameters:
- givenPath (
string
) - The given path to be globified - givenDirectory (
string
) - [process.cwd()] The cwd to use to resolve relative path names - absolute (
boolean
) - [false] If true, the glob will be absolute
returns: Promise<[GlobifiedEntry] | [GlobifiedEntry, GlobifiedEntry]>
Globifies a directory
Parameters:
- givenDirectory (
string
) - The given directory to be globified
returns: string
Parameters:
- gitIgnoreEntry (
string
) - One git ignore entry (it expects a valid non-comment gitignore entry with no surrounding whitespace) - gitIgnoreDirectory (
string
) - The directory of gitignore - absolute (
boolean
) - [false] If true, the glob will be absolute
returns: Promise<[GlobifiedEntry] | [GlobifiedEntry, GlobifiedEntry]>
Parameters:
- g (
GlobifiedEntry
)
returns: string
Parameters:
- a (
GlobifiedEntry
) - b (
GlobifiedEntry
)
returns: boolean
Parameters:
- globs (
GlobifiedEntry[]
)
returns: any
Parameters:
- globs (
GlobifiedEntry[]
)
returns: any
Converts given path to Posix (replacing \ with /)
Parameters:
- givenPath (
string
) - Path to convert
returns: string
Converts given path to Posix (replacing \ with /) and removing ending slashes
Parameters:
- givenPath (
string
) - Path to convert
returns: string
Get the type of the given path
Parameters:
- givenPath - Absolute path
- filepath (
string
)
returns: Promise<PATH_TYPE>
You can sponsor my work here:
https://github.com/sponsors/aminya
Pull requests, issues and feature requests are welcome. See the Contributing guide.