├── .gitignore ├── test ├── playground │ ├── blah.txt │ ├── ignored-by-git.txt │ ├── custom-gitignore │ ├── ignored-by-package-json.txt │ └── package.json └── index.js ├── .npmignore ├── .travis.yml ├── .github └── workflows │ └── add-to-project.yaml ├── LICENSE.md ├── package.json ├── README.md ├── CONTRIBUTING.md └── index.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /test/playground/blah.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /test/playground/ignored-by-git.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /test/playground/custom-gitignore: -------------------------------------------------------------------------------- 1 | ignored-by-git.txt -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | .travis.yml 2 | CONTRIBUTING.md 3 | test/ 4 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - lts/* 4 | -------------------------------------------------------------------------------- /test/playground/ignored-by-package-json.txt: -------------------------------------------------------------------------------- 1 | ignored-by-package-json.txt -------------------------------------------------------------------------------- /test/playground/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "config": { 3 | "ignore": "ignored-by-package-json.txt" 4 | }, 5 | "custom-ignore-blah": { 6 | "ignore": "blah.txt" 7 | } 8 | } -------------------------------------------------------------------------------- /.github/workflows/add-to-project.yaml: -------------------------------------------------------------------------------- 1 | on: 2 | pull_request: 3 | types: [opened] 4 | issues: 5 | types: [opened] 6 | 7 | jobs: 8 | add-to-project: 9 | uses: standard/.github/.github/workflows/add-to-project.yaml@master 10 | secrets: inherit 11 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | Copyright (c) 2015, Dan Flettre 2 | 3 | Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. 4 | 5 | THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 6 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 7 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "deglob", 3 | "description": "Take a list of glob patterns and return an array of file locations, respecting `.gitignore` and allowing for ignore patterns via `package.json`.", 4 | "version": "4.0.1", 5 | "author": "Dan Flettre ", 6 | "bugs": { 7 | "url": "https://github.com/flet/deglob/issues" 8 | }, 9 | "devDependencies": { 10 | "standard": "*", 11 | "tap-spec": "^5.0.0", 12 | "tape": "^5.0.0" 13 | }, 14 | "homepage": "https://github.com/flet/deglob", 15 | "keywords": [ 16 | "cli", 17 | "command", 18 | "deglob", 19 | "files", 20 | "glob", 21 | "unglob", 22 | "gitignore", 23 | "ignore", 24 | "file" 25 | ], 26 | "license": "ISC", 27 | "main": "index.js", 28 | "repository": { 29 | "type": "git", 30 | "url": "https://github.com/flet/deglob.git" 31 | }, 32 | "scripts": { 33 | "test": "standard && tape test/*.js | tap-spec" 34 | }, 35 | "dependencies": { 36 | "find-root": "^1.0.0", 37 | "glob": "^7.0.5", 38 | "ignore": "^5.0.0", 39 | "pkg-config": "^1.1.0", 40 | "run-parallel": "^1.1.2", 41 | "uniq": "^1.0.1" 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | const path = require('path') 2 | const test = require('tape') 3 | const deglob = require('../') 4 | 5 | const playground = path.join(__dirname, 'playground') 6 | const opts = { cwd: playground, gitIgnoreFile: 'custom-gitignore' } 7 | 8 | const globbies = [ 9 | { 10 | name: '*.txt useGitIgnore: default, usePackageJson: default', 11 | globs: '*.txt', 12 | opts: Object.assign({}, opts), 13 | expectedFiles: ['blah.txt'] 14 | }, 15 | { 16 | name: '*.txt useGitIgnore: false, usePackageJson: default', 17 | globs: '*.txt', 18 | opts: Object.assign({}, opts, { useGitIgnore: false }), 19 | expectedFiles: [ 20 | 'ignored-by-git.txt', 21 | 'blah.txt'] 22 | }, 23 | { 24 | name: '*.txt useGitIgnore: false, usePackageJson: false', 25 | globs: '*.txt', 26 | opts: Object.assign({}, opts, { useGitIgnore: false, usePackageJson: false }), 27 | expectedFiles: [ 28 | 'ignored-by-git.txt', 29 | 'ignored-by-package-json.txt', 30 | 'blah.txt'] 31 | }, 32 | { 33 | name: '*.txt and *.json useGitIgnore: default, usePackageJson: false', 34 | globs: ['*.txt', '*.json'], 35 | opts: Object.assign({}, opts, { usePackageJson: false }), 36 | expectedFiles: [ 37 | 'ignored-by-package-json.txt', 38 | 'blah.txt', 39 | 'package.json'] 40 | }, 41 | { 42 | name: '*.txt and *.json useGitIgnore: default, usePackageJson: default, configKey: custom-ignore-blah', 43 | globs: ['*.txt'], 44 | opts: Object.assign({}, opts, { configKey: 'custom-ignore-blah' }), 45 | expectedFiles: ['ignored-by-package-json.txt'] 46 | } 47 | ] 48 | 49 | globbies.forEach(function (obj) { 50 | test('Testing ' + obj.name, function (t) { 51 | deglob(obj.globs, obj.opts, checkEm) 52 | 53 | function checkEm (err, files) { 54 | if (err) throw err 55 | const testName = obj.name + ' -- matches ' + obj.expectedFiles.length + ' files' 56 | t.equals(files.length, obj.expectedFiles.length, testName) 57 | obj.expectedFiles.forEach(function (expectedFile) { 58 | t.ok(files.indexOf(path.join(playground, expectedFile)) > -1, 'File in Result: ' + expectedFile) 59 | }) 60 | t.end() 61 | } 62 | }) 63 | }) 64 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # deglob [![travis][travis-image]][travis-url] [![npm][npm-image]][npm-url] [![downloads][downloads-image]][downloads-url] [![javascript style guide][standard-image]][standard-url] 2 | 3 | [travis-image]: https://img.shields.io/travis/standard/deglob/master.svg 4 | [travis-url]: https://travis-ci.org/standard/deglob 5 | [npm-image]: https://img.shields.io/npm/v/deglob.svg 6 | [npm-url]: https://npmjs.org/package/deglob 7 | [downloads-image]: https://img.shields.io/npm/dm/deglob.svg 8 | [downloads-url]: https://npmjs.org/package/deglob 9 | [standard-image]: https://img.shields.io/badge/code_style-standard-brightgreen.svg 10 | [standard-url]: https://standardjs.com 11 | 12 | Take a list of glob patterns and return an array of file locations, respecting `.gitignore` and allowing for ignore patterns via `package.json`. 13 | 14 | Giant swaths of this code were extracted from [standard](https://standardjs.com). It seems useful outside of that tool, so I've attempted to extract it! :) 15 | 16 | ## Install 17 | 18 | ``` 19 | npm install --save deglob 20 | ``` 21 | 22 | ## Usage 23 | 24 | ```js 25 | var deglob = require('deglob') 26 | 27 | deglob(['**/*.js'], function(err, files) { 28 | files.forEach(function(file) { 29 | console.log('found file ' + file) 30 | }) 31 | }) 32 | 33 | // pass in some options to customize! 34 | 35 | var path = require('path') 36 | var opts = { 37 | cwd: path.join(__dirname, 'someDir'), 38 | useGitIgnore: false, 39 | usePackageJson: false 40 | } 41 | 42 | deglob(['**/*.js'], opts, function(err, files) { 43 | files.forEach(function(file) { 44 | console.log('found file ' + file) 45 | }) 46 | }) 47 | ``` 48 | 49 | ## Ignoring files in package.json 50 | `deglob` will look for a `package.json` file by default and use any ignore patterns defined. 51 | 52 | To define patterns in package.json add somthing like this: 53 | ```js 54 | "config": { 55 | "ignore": ['**/*.bad'] 56 | } 57 | ``` 58 | If you do not fancy the `config` key, provide a different one using the `configKey` option. 59 | 60 | 61 | ## Options 62 | Option | Default | Description 63 | -------------- | -------- | ------- 64 | useGitIgnore | true | Turn on/off allowing ignore patterns via `.gitignore` 65 | usePackageJson | true | Turn on/off allowing ignore patterns via `package.json` config. 66 | configKey | 'config' | This is the parent key in `package.json` to look for the `ignore` attribute. 67 | gitIgnoreFile | '.gitignore' | Name of the `.gitignore` file look for (probably best to leave it default) 68 | ignore | [] | List of additional ignore patterns to use 69 | cwd | process.cwd() | This is the working directory to start the deglobbing 70 | 71 | ## Contributing 72 | 73 | Contributions welcome! Please read the [contributing guidelines](CONTRIBUTING.md) first. 74 | 75 | ## License 76 | 77 | [ISC](LICENSE.md) 78 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing Guidelines 2 | 3 | Contributions welcome! 4 | 5 | **Before spending lots of time on something, ask for feedback on your idea first!** 6 | 7 | Please search issues and pull requests before adding something new to avoid duplicating efforts and conversations. 8 | 9 | In addition to improving the project by refactoring code and and implementing relevant features, this project welcomes the following types of contributions: 10 | 11 | - **Ideas**: participate in an issue thread or start your own to have your voice heard. 12 | - **Writing**: contribute your expertise in an area by helping expand the included content. 13 | - **Copy editing**: fix typos, clarify language, and generally improve the quality of the content. 14 | - **Formatting**: help keep content easy to read with consistent formatting. 15 | 16 | ## Installing 17 | 18 | Fork and clone the repo, then `npm install` to install all dependencies. 19 | 20 | ## Testing 21 | 22 | Tests are run with `npm test`. Unless you're creating a failing test to increase test coverage or show a problem, please make sure all tests are passing before submitting a pull request. 23 | 24 | ## Code Style 25 | 26 | [![standard][standard-image]][standard-url] 27 | 28 | This repository uses [`standard`][standard-url] to maintain code style and consistency and avoid style arguments. `npm test` runs `standard` so you don't have to! 29 | 30 | [standard-image]: https://cdn.rawgit.com/feross/standard/master/badge.svg 31 | [standard-url]: https://github.com/feross/standard 32 | [semistandard-image]: https://cdn.rawgit.com/flet/semistandard/master/badge.svg 33 | [semistandard-url]: https://github.com/Flet/semistandard 34 | 35 | --- 36 | 37 | # Collaborating Guidelines 38 | 39 | **This is an OPEN Open Source Project.** 40 | 41 | ## What? 42 | 43 | Individuals making significant and valuable contributions are given commit access to the project to contribute as they see fit. This project is more like an open wiki than a standard guarded open source project. 44 | 45 | ## Rules 46 | 47 | There are a few basic ground rules for collaborators: 48 | 49 | 1. **No `--force` pushes** or modifying the Git history in any way. 50 | 1. **Non-master branches** ought to be used for ongoing work. 51 | 1. **External API changes and significant modifications** ought to be subject to an **internal pull request** to solicit feedback from other collaborators. 52 | 1. Internal pull requests to solicit feedback are *encouraged* for any other non-trivial contribution but left to the discretion of the contributor. 53 | 1. Contributors should attempt to adhere to the prevailing code style. 54 | 55 | ## Releases 56 | 57 | Declaring formal releases remains the prerogative of the project maintainer. 58 | 59 | ## Changes to this arrangement 60 | 61 | This is an experiment and feedback is welcome! This document may also be subject to pull requests or changes by collaborators where you believe you have something valuable to add or change. 62 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | module.exports = deglob 2 | 3 | const findRoot = require('find-root') 4 | const fs = require('fs') 5 | const glob = require('glob') 6 | const ignorePkg = require('ignore') 7 | const os = require('os') 8 | const parallel = require('run-parallel') 9 | const path = require('path') 10 | const pkgConfig = require('pkg-config') 11 | const uniq = require('uniq') 12 | 13 | function deglob (files, opts, cb) { 14 | if (typeof opts === 'function') return deglob(files, null, opts) 15 | opts = parseOpts(opts) 16 | 17 | if (typeof files === 'string') files = [files] 18 | if (files.length === 0) return nextTick(cb, null, []) 19 | 20 | // traverse filesystem 21 | parallel(files.map(function (pattern) { 22 | return function (callback) { 23 | glob(pattern, { 24 | cwd: opts.cwd, 25 | ignore: opts._ignore, 26 | nodir: true 27 | }, callback) 28 | } 29 | }), function (err, results) { 30 | if (err) return cb(err) 31 | 32 | // flatten nested arrays 33 | let files = results.reduce(function (files, result) { 34 | result.forEach(function (file) { 35 | files.push(path.resolve(opts.cwd, file)) 36 | }) 37 | return files 38 | }, []) 39 | 40 | // de-dupe 41 | files = uniq(files) 42 | 43 | if (opts._gitignore) { 44 | files = toRelative(opts.cwd, files) 45 | if (os.platform() === 'win32') files = toUnix(files) 46 | files = opts._gitignore.filter(files) 47 | files = toAbsolute(opts.cwd, files) 48 | if (os.platform() === 'win32') files = toWin32(files) 49 | } 50 | 51 | return cb(null, files) 52 | }) 53 | } 54 | 55 | function parseOpts (opts) { 56 | if (!opts) opts = {} 57 | opts = Object.assign({ 58 | useGitIgnore: true, 59 | usePackageJson: true, 60 | configKey: 'config', 61 | gitIgnoreFile: ['.gitignore', '.git/info/exclude'] 62 | }, opts) 63 | 64 | if (!opts.cwd) opts.cwd = process.cwd() 65 | if (!Array.isArray(opts.gitIgnoreFile)) opts.gitIgnoreFile = [opts.gitIgnoreFile] 66 | 67 | opts._ignore = [] 68 | opts._gitignore = ignorePkg() 69 | 70 | function addIgnorePattern (patterns) { 71 | opts._ignore = opts._ignore.concat(patterns) 72 | opts._gitignore.addPattern(patterns) 73 | } 74 | 75 | if (opts.ignore) addIgnorePattern(opts.ignore) 76 | 77 | // return if we're not looking for packageJson or gitIgnore 78 | if (!opts.useGitIgnore && !opts.usePackageJson) { 79 | return opts 80 | } 81 | 82 | // Find package.json in the project root 83 | let root 84 | try { 85 | root = findRoot(opts.cwd) 86 | } catch (e) {} 87 | 88 | if (root) { 89 | if (opts.usePackageJson) { 90 | const packageOpts = pkgConfig(opts.configKey, { root: false, cwd: opts.cwd }) 91 | if (packageOpts && packageOpts.ignore) { 92 | // Use ignore patterns from package.json ("config.ignore" property) 93 | addIgnorePattern(packageOpts.ignore) 94 | } 95 | } 96 | 97 | if (opts.useGitIgnore) { 98 | // Use ignore patterns from project root .gitignore 99 | let gitignores = [] 100 | gitignores = opts.gitIgnoreFile 101 | .map(function (f) { 102 | try { 103 | return fs.readFileSync(path.join(root, f), 'utf8') 104 | } catch (err) { 105 | return null 106 | } 107 | }) 108 | .filter(Boolean) 109 | 110 | gitignores.forEach(function (gitignore) { 111 | opts._gitignore.addPattern(gitignore.split(/\r?\n/)) 112 | }) 113 | } 114 | } 115 | 116 | return opts 117 | } 118 | 119 | function toAbsolute (cwd, files) { 120 | return files.map(function (file) { 121 | return path.join(cwd, file) 122 | }) 123 | } 124 | 125 | function toRelative (cwd, files) { 126 | return files.map(function (file) { 127 | return path.relative(cwd, file) 128 | }) 129 | } 130 | 131 | function toUnix (files) { 132 | return files.map(function (file) { 133 | return file.replace(/\\/g, '/') 134 | }) 135 | } 136 | 137 | function toWin32 (files) { 138 | return files.map(function (file) { 139 | return file.replace(/\//g, '\\') 140 | }) 141 | } 142 | 143 | function nextTick (cb, err, val) { 144 | process.nextTick(function () { 145 | cb(err, val) 146 | }) 147 | } 148 | --------------------------------------------------------------------------------