Convert Gitignore to Glob patterns
MIT License
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)
GlobifiedEntry
(type)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
}
globifyGitIgnoreFile
(function)Parse and globy the .gitingore
file that exists in a directory
Parameters:
string
) - The given directory that has the .gitignore
fileboolean
) - [false] If true, the glob will be absolutereturns: Promise<GlobifiedEntry[]>
globifyGitIgnore
(function)Globify the content of a gitignore string
Parameters:
string
) - The content of the gitignore filestring
) - The directory of gitignoreboolean
) - [false] If true, the glob will be absolutereturns: Promise<GlobifiedEntry[]>
globifyPath
(function)Parameters:
string
) - The given path to be globifiedstring
) - [process.cwd()] The cwd to use to resolve relative path namesboolean
) - [false] If true, the glob will be absolutereturns: Promise<[GlobifiedEntry] | [GlobifiedEntry, GlobifiedEntry]>
globifyDirectory
(function)Globifies a directory
Parameters:
string
) - The given directory to be globifiedreturns: string
globifyGitIgnoreEntry
(function)Parameters:
string
) - One git ignore entry (it expects a valid non-comment gitignore entry with nostring
) - The directory of gitignoreboolean
) - [false] If true, the glob will be absolutereturns: Promise<[GlobifiedEntry] | [GlobifiedEntry, GlobifiedEntry]>
getGlob
(function)Parameters:
GlobifiedEntry
)returns: string
globSorter
(variable)uniqueMatcher
(function)Parameters:
GlobifiedEntry
)GlobifiedEntry
)returns: boolean
uniqueGlobs
(function)Parameters:
GlobifiedEntry[]
)returns: any
uniqueSortGlobs
(function)Parameters:
GlobifiedEntry[]
)returns: any
posixifyPath
(function)Converts given path to Posix (replacing \ with /)
Parameters:
string
) - Path to convertreturns: string
posixifyPathNormalized
(function)Converts given path to Posix (replacing \ with /) and removing ending slashes
Parameters:
string
) - Path to convertreturns: string
getPathType
(function)Get the type of the given path
Parameters:
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.