Skip to content

Commit

Permalink
oh my glob
Browse files Browse the repository at this point in the history
  • Loading branch information
Flet committed Jul 10, 2015
0 parents commit e5bc0f6
Show file tree
Hide file tree
Showing 14 changed files with 440 additions and 0 deletions.
27 changes: 27 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
language: node_js
node_js:
- '0.12'
- 'iojs'
sudo: false
cache:
directories:
- node_modules
script:
- npm test
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
61 changes: 61 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -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.
6 changes: 6 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Copyright (c) 2015, Dan Flettre <[email protected]>

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.
75 changes: 75 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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)
138 changes: 138 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -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, '\\')
})
}
43 changes: 43 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>",
"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"
}
}
Loading

0 comments on commit e5bc0f6

Please sign in to comment.