flatten-gitignores
v1.0.2
Published
A simple Node.js tool for flattening nested `.gitignore` files into a single file.
Readme
flatten-gitignores
A simple Node.js tool for flattening nested .gitignore files into a single file.
Useful for supporting tools that expect a single ignore file and don't resolve nested ignore files (e.g. EAS expects .easignore and Prettier expects .prettierignore).
Usage
Run the script with Node, passing optional flags to customize input, output, additions, and excludes.
npx flatten-gitignores [--ignore-file] [--cwd <path>] [--output <file>] [--prepend <path>] [--append <path>] [--exclude <glob> ...] [--no-default-excludes] [--help]Flags
--ignore-file <file>: The ignore file name to search for. Defaults to.gitignore.--cwd <path>: The search root / working directory; defaults to current CWD (.).--output <file>: Output file path; defaults to<cwd>/.gitignore-collated.--prepend <path>: Path to an additional ignore file to prepend. Resolved relative to CWD.--append <path>: Path to an additional ignore file to append. Resolved relative to CWD.--exclude <glob>: Glob pattern(s) to exclude from the search for ignore files. Defaults to**/node_modules/**. Pass one or more--excludeflags to add more. To omit the defaults, pass--no-default-excludes.--no-default-excludes: Omit the default excludes (**/node_modules/**).--help: Show help and exit.
Examples
Default behavior:
npx flatten-gitignores- Collates all
.gitignorefiles found into a.gitignore-collatedfile in the current directory.
- Collates all
Specify output file (
--output):npx flatten-gitignores --output ./.easignore- Collates all
.gitignorefiles found into a.easignorefile in the current directory.
- Collates all
Exclude certain files from the search for ignore files (
--exclude):npx flatten-gitignores --exclude **/dist/** --exclude **/build/**- Collates all
.gitignorefiles found into a.easignorefile in the current directory. --exclude: Excludes**/dist/**and**/build/**from the search. Useful for reducing needless search time.
- Collates all
Stop excluding
node_modulesfrom the search (--no-default-excludes):npx flatten-gitignores --no-default-excludes- Collates all
.gitignorefiles found into a.easignorefile in the current directory. --no-default-excludes: Stops excluding**/node_modules/**(I can't think of a practical use-case for this, but just in case you need it!).
- Collates all
Prepend (
--prepend) or append (--append) extra rules:npx flatten-gitignores --output ./.easignore --prepend .easignore-prepend --append .easignore-append- Collates all
.gitignorefiles found into a.easignorefile in the current directory. --prepend: Prepends the contents of.easignore-prependinto that.easignorefile.--append: Appends the contents of.easignore-appendinto that.easignorefile.
- Collates all
Collate a file other than
.gitignore(--ignore-file):npx flatten-gitignores --ignore-file _gitignore- Collates all
_gitignorefiles found into a.gitignore-collatedfile in the current directory (possibly useful for ecosystems like npm, which do not support bundling a.gitignorefile into your npm package, so people have to work around it with a_gitignorefile or similar that they rename to.gitignoreafter unpacking).
- Collates all
Example output
If you run the following command:
npx flatten-gitignores --output .easignore --prepend .easignore-prependThen, given:
- …a typical monorepo that looks like this:
.
├── .gitignore
├── package.json
├── apps
│ ├── desktop
│ ├── docs
│ └── mobile
│ ├── .gitignore
│ ├── package.json
│ ├── ios
│ └── android
└── packages
└── react-native-confetti
├── .gitignore
├── android
├── ios
├── example
└── package.json
- …the following
.gitignorefile at the root of your monorepo:
.DS_Store- …the following
.easignore-prependfile:
# Ignore all non-Expo apps:
apps/desktop
apps/docs
# Ignore all example React Native apps:
packages/*/example- …and the following
apps/mobile/.gitignorefile:
node_modules
ios
android… it will produce the following .easignore file:
################################################################################
### This is a flattened ignore pattern list generated by flatten-gitignores. ###
### See https://shirakaba/flatten-gitignores for documentation. ###
################################################################################
### START /.easignore-prepend ###
# Ignore all non-Expo apps:
/**/apps/desktop
/**/apps/docs
# Ignore all example React Native apps:
/**/packages/*/example
### END /.easignore-prepend ###
### START /.gitignore ###
/**/.DS_Store
### END /.gitignore ###
### START /apps/mobile/.gitignore ###
/apps/mobile/**/node_modules
/apps/mobile/**/ios
/apps/mobile/**/android
### END /apps/mobile/.gitignore ###Note how all rules have been normalised with /, /**, and subpaths, as necessary. This prevents nested .gitignore files accidentally applying rules across the whole tree. The collated .easignore should behave equivalently to the multiple .gitignore files, though, to the best of my understanding!
Does it, like, work?
I implemented this in a hurry one day with reference to the .gitignore pattern format docs. I think I've probably covered all cases, i.e. comments, backslashes, **, etc. – but as you can see, there are no tests, so this is all just talk.
All I can say is that this works well enough that it helped me cut 4.55 GB off my EAS archive!
