diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..123ae94 --- /dev/null +++ b/.gitignore @@ -0,0 +1,27 @@ +# Logs +logs +*.log + +# Runtime data +pids +*.pid +*.seed + +# Directory for instrumented libs generated by jscoverage/JSCover +lib-cov + +# Coverage directory used by tools like istanbul +coverage + +# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) +.grunt + +# node-waf configuration +.lock-wscript + +# Compiled binary addons (http://nodejs.org/api/addons.html) +build/Release + +# Dependency directory +# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git +node_modules diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..d84d888 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,10 @@ +language: node_js +node_js: + - '0.12' + - 'iojs' +sudo: false +cache: + directories: + - node_modules +script: + - npm test diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..c606a74 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,7 @@ +# deglob change log + +All notable changes to this project will be documented in this file. +This project adheres to [Semantic Versioning](http://semver.org/). + +## Unreleased +* engage diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..7ec8e81 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,61 @@ +# Contributing Guidelines + +Contributions welcome! + +**Before spending lots of time on something, ask for feedback on your idea first!** + +Please search issues and pull requests before adding something new to avoid duplicating efforts and conversations. + +In addition to improving the project by refactoring code and and implementing relevant features, this project welcomes the following types of contributions: + +- **Ideas**: participate in an issue thread or start your own to have your voice heard. +- **Writing**: contribute your expertise in an area by helping expand the included content. +- **Copy editing**: fix typos, clarify language, and generally improve the quality of the content. +- **Formatting**: help keep content easy to read with consistent formatting. + +## Installing + +Fork and clone the repo, then `npm install` to install all dependencies. + +## Testing + +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. + +## Code Style + +[![standard][standard-image]][standard-url] + +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! + +[standard-image]: https://cdn.rawgit.com/feross/standard/master/badge.svg +[standard-url]: https://github.com/feross/standard +[semistandard-image]: https://cdn.rawgit.com/flet/semistandard/master/badge.svg +[semistandard-url]: https://github.com/Flet/semistandard + +--- + +# Collaborating Guidelines + +**This is an OPEN Open Source Project.** + +## What? + +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. + +## Rules + +There are a few basic ground rules for collaborators: + +1. **No `--force` pushes** or modifying the Git history in any way. +1. **Non-master branches** ought to be used for ongoing work. +1. **External API changes and significant modifications** ought to be subject to an **internal pull request** to solicit feedback from other collaborators. +1. Internal pull requests to solicit feedback are *encouraged* for any other non-trivial contribution but left to the discretion of the contributor. +1. Contributors should attempt to adhere to the prevailing code style. + +## Releases + +Declaring formal releases remains the prerogative of the project maintainer. + +## Changes to this arrangement + +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. diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..e41f2c5 --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,6 @@ +Copyright (c) 2015, Dan Flettre + +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. + +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 +ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..d422a82 --- /dev/null +++ b/README.md @@ -0,0 +1,75 @@ +# deglob + +[![npm][npm-image]][npm-url] +[![travis][travis-image]][travis-url] + +[npm-image]: https://img.shields.io/npm/v/deglob.svg?style=flat-square +[npm-url]: https://www.npmjs.com/package/deglob +[travis-image]: https://img.shields.io/travis/flet/deglob.svg?style=flat-square +[travis-url]: https://travis-ci.org/flet/deglob + +Take a list of glob patterns and return an array of file locations, respecting `.gitignore` and allowing for ignore patterns via `package.json`. + +Giant swaths of this code were extracted from [standard](https://github.com/feross/standard). It seems useful outside of that tool, so I've attempted to extract it! :) + +## Install + +``` +npm install --save deglob +``` + +## Usage + +```js +var deglob = require('deglob') + +deglob([**/*.js], function(err, files) { + files.forEach(function(file) { + console.log('found file ' + file) + }) +}) + +// pass in some options to customize! + +var path = require('path') +var opts = { + cwd: path.join(__dirname, 'someDir'), + useGitIgnore: false, + usePackageJson: false +} + +deglob([**/*.js], opts function(err, files) { + files.forEach(function(file) { + console.log('found file ' + file) + }) +}) +``` + +## Ignoring files in package.json +`deglob` will look for a `package.json` file by default and use any ignore patterns defined. + +To define patterns in package.json add somthing like this: +```js +"config": { + "ignore": ['**/*.bad'] +} +``` +If you do not fancy the `config` key, provide a different one using the `configKey` option. + + +## Options +Option | Default | Description +-------------- | -------- | ------- +useGitIgnore | true | Turn on/off allowing ignore patterns via `.gitignore` +usePackageJson | true | Turn on/off allowing ignore patterns via `package.json` config. +configKey | 'config' | This is the parent key in `package.json to look for the `ignore` attribute. +gitIgnoreFile | '.gitignore' | Name of the `.gitignore` file look for (probably best to leave it default) +cwd | process.cwd() | This is the working directory to start the deglobbing + +## Contributing + +Contributions welcome! Please read the [contributing guidelines](CONTRIBUTING.md) first. + +## License + +[ISC](LICENSE.md) diff --git a/index.js b/index.js new file mode 100644 index 0000000..bfd9bc6 --- /dev/null +++ b/index.js @@ -0,0 +1,138 @@ +module.exports = deglob + +var dezalgo = require('dezalgo') +var extend = require('xtend') +var findRoot = require('find-root') +var fs = require('fs') +var glob = require('glob') +var ignorePkg = require('ignore') +var os = require('os') +var parallel = require('run-parallel') +var path = require('path') +var pkgConfig = require('pkg-config') +var uniq = require('uniq') + +var DEFAULT_OPTIONS = { + useGitIgnore: true, + usePackageJson: true, + configKey: 'config', + gitIgnoreFile: '.gitignore' +} + +function deglob (files, opts, cb) { + if (typeof opts === 'function') { + cb = opts + opts = {} + } + opts = parseOpts(opts) + cb = dezalgo(cb) + + if (typeof files === 'string') files = [ files ] + if (files.length === 0) return cb(null, []) + + // traverse filesystem + parallel(files.map(function (pattern) { + return function (callback) { + glob(pattern, { + cwd: opts.cwd, + ignore: opts._ignore, + nodir: true + }, callback) + } + }), function (err, results) { + if (err) return cb(err) + + // flatten nested arrays + var files = results.reduce(function (files, result) { + result.forEach(function (file) { + files.push(path.resolve(opts.cwd, file)) + }) + return files + }, []) + + // de-dupe + files = uniq(files) + + if (opts._gitignore) { + files = toRelative(opts.cwd, files) + if (os.platform() === 'win32') files = toUnix(files) + files = opts._gitignore.filter(files) + files = toAbsolute(opts.cwd, files) + if (os.platform() === 'win32') files = toWin32(files) + } + + return cb(null, files) + }) +} + +function parseOpts (opts) { + if (!opts) opts = {} + opts = extend(DEFAULT_OPTIONS, opts) + + if (!opts.cwd) opts.cwd = process.cwd() + + opts._ignore = [] + opts._gitignore = ignorePkg() + + function addIgnorePattern (patterns) { + opts._ignore = opts._ignore.concat(patterns) + opts._gitignore.addPattern(patterns) + } + + if (opts.ignore) addIgnorePattern(opts.ignore) + + // return if we're not looking for packageJson or gitIgnore + if (!opts.useGitIgnore && !opts.usePackageJson) { + return opts + } + + // Find package.json in the project root + var root + try { + root = findRoot(opts.cwd) + } catch (e) {} + + if (root) { + var packageOpts = pkgConfig(opts.configKey, { root: false, cwd: opts.cwd }) + + if (packageOpts && packageOpts.ignore) { + // Use ignore patterns from package.json ("config.ignore" property) + addIgnorePattern(packageOpts.ignore) + } + + if (opts.useGitIgnore) { + // Use ignore patterns from project root .gitignore + var gitignore + try { + gitignore = fs.readFileSync(path.join(root, opts.gitIgnoreFile), 'utf8') + } catch (e) {} + if (gitignore) opts._gitignore.addPattern(gitignore.split(/\r?\n/)) + } + } + + return opts +} + +function toAbsolute (cwd, files) { + return files.map(function (file) { + return path.join(cwd, file) + }) +} + +function toRelative (cwd, files) { + return files.map(function (file) { + return path.relative(cwd, file) + }) +} + +function toUnix (files) { + return files.map(function (file) { + return file.replace(/\\/g, '/') + }) +} + +function toWin32 (files) { + return files.map(function (file) { + return file.replace(/\//g, '\\') + }) +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..c670ac3 --- /dev/null +++ b/package.json @@ -0,0 +1,43 @@ +{ + "name": "deglob", + "description": "Take a list of glob patterns and return an array of file locations, respecting `.gitignore` and allowing for ignore patterns via `package.json`.", + "version": "1.0.0", + "author": "Dan Flettre ", + "bugs": { + "url": "https://github.com/flet/deglob/issues" + }, + "devDependencies": { + "standard": "*", + "tap-spec": "^4.0.2", + "tape": "^4.0.0" + }, + "homepage": "https://github.com/flet/deglob", + "keywords": [ + "cli", + "command", + "deglob", + "files", + "glob", + "unglob" + ], + "license": "ISC", + "main": "index.js", + "repository": { + "type": "git", + "url": "https://github.com/flet/deglob.git" + }, + "scripts": { + "test": "standard && tape test/*.js | tap-spec" + }, + "dependencies": { + "dezalgo": "^1.0.3", + "find-root": "^0.1.1", + "glob": "^5.0.13", + "ignore": "^2.2.15", + "path": "^0.11.14", + "pkg-config": "^1.1.0", + "run-parallel": "^1.1.2", + "uniq": "^1.0.1", + "xtend": "^4.0.0" + } +} diff --git a/test/index.js b/test/index.js new file mode 100644 index 0000000..24e6541 --- /dev/null +++ b/test/index.js @@ -0,0 +1,63 @@ +var path = require('path') +var test = require('tape') +var extend = require('xtend') +var deglob = require('../') + +var playground = path.join(__dirname, 'playground') +var opts = {cwd: playground, gitIgnoreFile: 'custom-gitignore'} + +test('all of the things', function (t) { + + globbies.forEach(function (obj) { + deglob(obj.globs, obj.opts, checkEm) + + function checkEm (err, files) { + if (err) throw err + var testName = obj.name + ' -- matches ' + obj.expectedFiles.length + ' files' + t.equals(files.length, obj.expectedFiles.length, testName) + obj.expectedFiles.forEach(function (expectedFile) { + t.ok(files.indexOf(path.join(playground, expectedFile)) > -1, 'File in Result: ' + expectedFile) + }) + } + }) + + t.end() +}) + +var globbies = [ + { + name: '*.txt useGitIgnore: true, usePackageJson: true', + globs: '*.txt', + opts: extend(opts), + expectedFiles: ['blah.txt'] + }, + { + name: '*.txt useGitIgnore: false, usePackageJson: true', + globs: '*.txt', + opts: extend(opts, {useGitIgnore: false}), + expectedFiles: [ + 'ignored-by-git.txt', + 'blah.txt'] + }, + { + name: '*.txt useGitIgnore: false, usePackageJson: false', + globs: '*.txt', + opts: extend(opts, {useGitIgnore: false, usePackageJson: false}), + expectedFiles: [ + 'ignored-by-git.txt', + 'ignored-by-package-json.txt', + 'blah.txt'] + }, + { + name: '*.txt and *.json useGitIgnore: true, usePackageJson: false', + globs: ['*.txt', '*.json'], + opts: extend(opts), + expectedFiles: ['blah.txt', 'package.json'] + }, + { + name: '*.txt and *.json useGitIgnore: true, usePackageJson: true, configKey: custom-ignore-blah', + globs: ['*.txt'], + opts: extend(opts, {configKey: 'custom-ignore-blah'}), + expectedFiles: ['ignored-by-package-json.txt'] + } +] diff --git a/test/playground/blah.txt b/test/playground/blah.txt new file mode 100644 index 0000000..e69de29 diff --git a/test/playground/custom-gitignore b/test/playground/custom-gitignore new file mode 100644 index 0000000..601aa49 --- /dev/null +++ b/test/playground/custom-gitignore @@ -0,0 +1 @@ +ignored-by-git.txt \ No newline at end of file diff --git a/test/playground/ignored-by-git.txt b/test/playground/ignored-by-git.txt new file mode 100644 index 0000000..e69de29 diff --git a/test/playground/ignored-by-package-json.txt b/test/playground/ignored-by-package-json.txt new file mode 100644 index 0000000..0951312 --- /dev/null +++ b/test/playground/ignored-by-package-json.txt @@ -0,0 +1 @@ +ignored-by-package-json.txt \ No newline at end of file diff --git a/test/playground/package.json b/test/playground/package.json new file mode 100644 index 0000000..61364dd --- /dev/null +++ b/test/playground/package.json @@ -0,0 +1,8 @@ +{ + "config": { + "ignore": "ignored-by-package-json.txt" + }, + "custom-ignore-blah": { + "ignore": "blah.txt" + } +} \ No newline at end of file