Skip to content

Commit

Permalink
refactored, now uses micromatch
Browse files Browse the repository at this point in the history
also:

- resolves #26
- resolves #25
- closes #15
- closes #18
- closes #22
- closes #23
- closes #24
  • Loading branch information
jonschlinkert committed Feb 9, 2016
1 parent 3d73218 commit 93cf4a6
Show file tree
Hide file tree
Showing 32 changed files with 474 additions and 165 deletions.
7 changes: 7 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Enforce Unix newlines
*.* text eol=lf

*.jpg binary
*.gif binary
*.png binary
*.jpeg binary
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
/node_modules/
*.DS_Store
*.sublime-*
node_modules
npm-debug.log
test/actual
16 changes: 10 additions & 6 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
{
"loopfunc": true,
"asi": false,
"boss": true,
"curly": true,
"eqeqeq": true,
"eqnull": true,
"esnext": true,
"immed": true,
"latedef": true,
"latedef": false,
"laxbreak": true,
"laxcomma": false,
"newcap": true,
"noarg": true,
"node": true,
"sub": true,
"undef": true,
"unused": true,
"boss": true,
"eqnull": true,
"node": true
}
"mocha": true
}
4 changes: 0 additions & 4 deletions .npmignore

This file was deleted.

15 changes: 10 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
sudo: false
language: node_js
node_js:
- 0.8
- '0.10'
before_install:
- npm update -g npm
- npm install -g grunt-cli
- "stable"
- "5"
- "4"
- "0.12"
- "0.10"
matrix:
fast_finish: true
allow_failures:
- node_js: "0.10"
9 changes: 2 additions & 7 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,17 @@ module.exports = function(grunt) {

// Project configuration.
grunt.initConfig({
nodeunit: {
files: ['test/**/*_test.js'],
},
jshint: {
options: {
jshintrc: '.jshintrc'
},
all: ['Gruntfile.js', 'lib/**/*.js', 'test/**/*.js']
all: ['Gruntfile.js', '*.js']
}
});

// Load plugins.
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-nodeunit');

// Default task.
grunt.registerTask('default', ['jshint', 'nodeunit']);

grunt.registerTask('default', ['jshint']);
};
4 changes: 3 additions & 1 deletion LICENSE-MIT
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
Copyright (c) 2013 "Cowboy" Ben Alman
The MIT License (MIT)

Copyright (c) 2013-2016, "Cowboy" Ben Alman.

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
Expand Down
65 changes: 42 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
# findup-sync [![Build Status](https://secure.travis-ci.org/cowboy/node-findup-sync.png?branch=master)](http://travis-ci.org/cowboy/node-findup-sync)
# findup-sync [![Build Status](https://travis-ci.org/cowboy/node-findup-sync.svg)](https://travis-ci.org/cowboy/node-findup-sync) [![NPM version](https://badge.fury.io/js/findup-sync.svg)](http://badge.fury.io/js/findup-sync)

Find the first file matching a given pattern in the current directory or the nearest ancestor directory.
> Find the first file matching a given pattern in the current directory or the nearest ancestor directory.
## Getting Started
Install the module with: `npm install findup-sync`
Matching is done with [micromatch][], please report any matching related issues on that repository.

## Install with [npm](npmjs.org)

```bash
npm i findup-sync --save
```

## Usage

```js
var findup = require('findup-sync');
findup(patternOrPatterns [, micromatchOptions]);

// Start looking in the CWD.
var filepath1 = findup('{a,b}*.txt');
Expand All @@ -15,34 +23,45 @@ var filepath1 = findup('{a,b}*.txt');
var filepath2 = findup('{a,b}*.txt', {cwd: '/some/path', nocase: true});
```

## Usage

```js
findup(patternOrPatterns [, minimatchOptions])
```
* `patterns` **{String|Array}**: Glob pattern(s) or file path(s) to match against.
* `options` **{Object}**: Options to pass to [micromatch]. Note that if you want to start in a different directory than the current working directory, specify a `cwd` property here.
* `returns` **{String}**: Returns the first matching file.

### patternOrPatterns
Type: `String` or `Array`
Default: none
## Running tests

One or more wildcard glob patterns. Or just filenames.
Install dev dependencies:

### minimatchOptions
Type: `Object`
Default: `{}`
```bash
npm i -d && npm test
```

Options to be passed to [minimatch](https://github.com/isaacs/minimatch).
## Contributing

Note that if you want to start in a different directory than the current working directory, specify a `cwd` property here.
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using [Grunt](http://gruntjs.com/)

## Contributing
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using [Grunt](http://gruntjs.com/).
For bugs and feature requests, [please create an issue](https://github.com/cowboy/node-findup-sync/issues).

## Release History
2015-09-14 0 v0.3.0 - updated glob to ~5.0.
2014-12-17 - v0.2.1 - updated to glob ~4.3.

2015-01-30 - v0.4.0 - Refactored, not also uses [micromatch][] instead of minimatch.
2015-09-14 - v0.3.0 - updated glob to ~5.0. 2014-12-17 - v0.2.1 - updated to glob ~4.3.
2014-12-17 - v0.2.1 - Updated to glob 4.3.
2014-12-16 - v0.2.0 - Removed lodash, updated to glob 4.x.
2014-03-14 - v0.1.3 - Updated dependencies.
2013-03-08 - v0.1.2 - Updated dependencies. Fixed a Node 0.9.x bug. Updated unit tests to work cross-platform.
2012-11-15 - v0.1.1 - Now works without an options object.
2012-11-01 - v0.1.0 - Initial release.
2012-11-01 - v0.1.0 - Initial release.

## Authors

**"Cowboy" Ben Alman**

+ [github/cowboy](https://github.com/cowboy)
+ [twitter/cowboy](http://twitter.com/cowboy)

## License

Copyright (c) 2012-2016 "Cowboy" Ben Alman
Released under the MIT license

[micromatch]: http://github.com/jonschlinkert/micromatch
87 changes: 87 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
'use strict';

/**
* Module dependencies
*/

var fs = require('fs');
var path = require('path');
var isGlob = require('is-glob');
var resolveDir = require('resolve-dir');
var mm = require('micromatch');

/**
* @param {String|Array} `pattern` Glob pattern or file path(s) to match against.
* @param {Object} `options` Options to pass to [micromatch]. Note that if you want to start in a different directory than the current working directory, specify the `options.cwd` property here.
* @return {String} Returns the first matching file.
* @api public
*/

module.exports = function(patterns, options) {
if (typeof patterns === 'string') {
return lookup(patterns, options);
}

if (!Array.isArray(patterns)) {
throw new TypeError('findup-sync expects a string or array as the first argument.');
}

var len = patterns.length, i = -1;
while (++i < len) {
var res = lookup(patterns[i], options);
if (res) {
return res;
}
}

return null;
};

function lookup(pattern, options) {
options = options || {};
var cwd = resolveDir(options.cwd || '');
if (isGlob(pattern)) {
return matchFile(cwd, pattern, options);
} else {
return findFile(cwd, pattern);
}
}

function matchFile(cwd, pattern, opts) {
var isMatch = mm.matcher(pattern, opts);
var files = fs.readdirSync(cwd);
var len = files.length, i = -1;

while (++i < len) {
var name = files[i];
var fp = path.join(cwd, name);
if (isMatch(name) || isMatch(fp)) {
return fp;
}
}

var dir = path.dirname(cwd);
if (dir === cwd) {
return null;
}
return matchFile(dir, pattern, opts);
}

function findFile(cwd, filename) {
var fp = cwd ? (cwd + '/' + filename) : filename;
if (fs.existsSync(fp)) {
return fp;
}

var segs = cwd.split(path.sep);
var len = segs.length;

while (len--) {
cwd = segs.slice(0, len).join('/');
fp = cwd + '/' + filename;
if (fs.existsSync(fp)) {
return fp;
}
}
return null;
}
49 changes: 0 additions & 49 deletions lib/findup-sync.js

This file was deleted.

47 changes: 26 additions & 21 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,46 @@
"description": "Find the first file matching a given pattern in the current directory or the nearest ancestor directory.",
"version": "0.3.0",
"homepage": "https://github.com/cowboy/node-findup-sync",
"author": {
"name": "\"Cowboy\" Ben Alman",
"url": "http://benalman.com/"
},
"repository": {
"type": "git",
"url": "git://github.com/cowboy/node-findup-sync.git"
},
"author": "\"Cowboy\" Ben Alman (http://benalman.com)",
"repository": "cowboy/node-findup-sync",
"bugs": {
"url": "https://github.com/cowboy/node-findup-sync/issues"
},
"licenses": [
{
"type": "MIT",
"url": "https://github.com/cowboy/node-findup-sync/blob/master/LICENSE-MIT"
}
"license": "MIT",
"files": [
"index.js"
],
"main": "lib/findup-sync",
"main": "index.js",
"engines": {
"node": ">= 0.6.0"
"node": ">= 0.8.0"
},
"scripts": {
"test": "grunt nodeunit"
"test": "grunt && mocha"
},
"dependencies": {
"glob": "~5.0.0"
"is-glob": "^2.0.1",
"micromatch": "^2.3.7",
"resolve-dir": "^0.1.0"
},
"devDependencies": {
"grunt": "~0.4.5",
"grunt-contrib-jshint": "~0.11.3",
"grunt-contrib-nodeunit": "~0.3.3"
"grunt": "^0.4.5",
"grunt-contrib-jshint": "^0.12.0",
"is-absolute": "^0.2.3",
"minimist": "^1.2.0",
"mocha": "^2.4.5",
"normalize-path": "^2.0.1",
"resolve": "^1.1.7",
"user-home": "^2.0.0"
},
"keywords": [
"file",
"find",
"find-up",
"findup",
"glob",
"file"
"match",
"pattern",
"resolve",
"search"
]
}
Loading

0 comments on commit 93cf4a6

Please sign in to comment.