What is @humanwhocodes/gitignore-to-minimatch?
@humanwhocodes/gitignore-to-minimatch is an npm package that converts .gitignore patterns to minimatch patterns. This allows developers to use .gitignore syntax for file matching in other contexts, such as build tools or custom scripts.
What are @humanwhocodes/gitignore-to-minimatch's main functionalities?
Convert .gitignore patterns to minimatch patterns
This feature allows you to convert an array of .gitignore patterns into an array of minimatch patterns. This is useful for reusing .gitignore patterns in other tools that use minimatch for pattern matching.
const { convert } = require('@humanwhocodes/gitignore-to-minimatch');
const gitignorePatterns = [
'*.log',
'node_modules/',
'dist/'
];
const minimatchPatterns = convert(gitignorePatterns);
console.log(minimatchPatterns);
Convert .gitignore file to minimatch patterns
This feature allows you to convert the patterns in a .gitignore file directly into minimatch patterns. This is useful for dynamically loading and converting patterns from a .gitignore file.
const { convertFile } = require('@humanwhocodes/gitignore-to-minimatch');
const minimatchPatterns = convertFile('.gitignore');
console.log(minimatchPatterns);
Other packages similar to @humanwhocodes/gitignore-to-minimatch
ignore
The 'ignore' package is a JavaScript implementation of the .gitignore file format. It provides a way to filter file paths based on .gitignore rules. Unlike @humanwhocodes/gitignore-to-minimatch, it does not convert patterns to minimatch but directly uses .gitignore syntax for filtering.
minimatch
The 'minimatch' package is a glob matcher in JavaScript. It is used to match file paths against glob patterns. While it does not directly handle .gitignore patterns, it is often used in conjunction with other tools to perform pattern matching. @humanwhocodes/gitignore-to-minimatch can convert .gitignore patterns to minimatch patterns, making them compatible.
micromatch
The 'micromatch' package is a powerful globbing library that supports advanced glob patterns and matching features. It can be used as an alternative to minimatch with more features and better performance. However, it does not directly convert .gitignore patterns, which is a specific feature of @humanwhocodes/gitignore-to-minimatch.
Gitignore to Minimatch Utility
by Nicholas C. Zakas
If you find this useful, please consider supporting my work with a donation.
Description
A function that converts a gitignore pattern into a minimatch pattern. There are subtle differences between these two formats, and depending on the utilities you're using, you may need one or the other.
Usage
Node.js
Install using npm or yarn:
npm install @humanwhocodes/gitignore-to-minimatch --save
# or
yarn add @humanwhocodes/gitignore-to-minimatch
Import into your Node.js project:
const { gitignoreToMinimatch } = require("@humanwhocodes/gitignore-to-minimatch");
import { gitignoreToMinimatch } from "@humanwhocodes/gitignore-to-minimatch";
Deno
Import into your Deno project:
import { gitignoreToMinimatch } from "https://cdn.skypack.dev/@humanwhocodes/gitignore-to-minimatch?dts";
Browser
It's recommended to import the minified version to save bandwidth:
import { gitignoreToMinimatch } from "https://cdn.skypack.dev/@humanwhocodes/gitignore-to-minimatch?min";
However, you can also import the unminified version for debugging purposes:
import { gitignoreToMinimatch } from "https://cdn.skypack.dev/@humanwhocodes/gitignore-to-minimatch";
API
After importing, call gitignoreToMinimatch
with a string argument, like this:
const minimatchPattern = gitignoreToMinimatch("foo");
console.log(minimatchPattern);
Developer Setup
- Fork the repository
- Clone your fork
- Run
npm install
to setup dependencies - Run
npm test
to run tests
License
Apache 2.0