Skip to content

Commit 968a9b1

Browse files
jonschlinkertphated
authored andcommitted
Breaking: Massive refactor & now uses micromatch (closes #26, #25, #15, #18, #22, #23, #24)
1 parent 960c2a5 commit 968a9b1

32 files changed

+474
-165
lines changed

.gitattributes

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Enforce Unix newlines
2+
*.* text eol=lf
3+
4+
*.jpg binary
5+
*.gif binary
6+
*.png binary
7+
*.jpeg binary

.gitignore

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
/node_modules/
1+
*.DS_Store
2+
*.sublime-*
3+
node_modules
4+
npm-debug.log
5+
test/actual

.jshintrc

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
{
2-
"loopfunc": true,
2+
"asi": false,
3+
"boss": true,
34
"curly": true,
45
"eqeqeq": true,
6+
"eqnull": true,
7+
"esnext": true,
58
"immed": true,
6-
"latedef": true,
9+
"latedef": false,
10+
"laxbreak": true,
11+
"laxcomma": false,
712
"newcap": true,
813
"noarg": true,
14+
"node": true,
915
"sub": true,
1016
"undef": true,
1117
"unused": true,
12-
"boss": true,
13-
"eqnull": true,
14-
"node": true
15-
}
18+
"mocha": true
19+
}

.npmignore

Lines changed: 0 additions & 4 deletions
This file was deleted.

.travis.yml

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1+
sudo: false
12
language: node_js
23
node_js:
3-
- 0.8
4-
- '0.10'
5-
before_install:
6-
- npm update -g npm
7-
- npm install -g grunt-cli
4+
- "stable"
5+
- "5"
6+
- "4"
7+
- "0.12"
8+
- "0.10"
9+
matrix:
10+
fast_finish: true
11+
allow_failures:
12+
- node_js: "0.10"

Gruntfile.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,17 @@ module.exports = function(grunt) {
44

55
// Project configuration.
66
grunt.initConfig({
7-
nodeunit: {
8-
files: ['test/**/*_test.js'],
9-
},
107
jshint: {
118
options: {
129
jshintrc: '.jshintrc'
1310
},
14-
all: ['Gruntfile.js', 'lib/**/*.js', 'test/**/*.js']
11+
all: ['Gruntfile.js', '*.js']
1512
}
1613
});
1714

1815
// Load plugins.
1916
grunt.loadNpmTasks('grunt-contrib-jshint');
20-
grunt.loadNpmTasks('grunt-contrib-nodeunit');
2117

2218
// Default task.
23-
grunt.registerTask('default', ['jshint', 'nodeunit']);
24-
19+
grunt.registerTask('default', ['jshint']);
2520
};

LICENSE-MIT

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
Copyright (c) 2013 "Cowboy" Ben Alman
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2013-2016, "Cowboy" Ben Alman.
24

35
Permission is hereby granted, free of charge, to any person
46
obtaining a copy of this software and associated documentation

README.md

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
1-
# findup-sync [![Build Status](https://secure.travis-ci.org/cowboy/node-findup-sync.png?branch=master)](http://travis-ci.org/cowboy/node-findup-sync)
1+
# 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)
22

3-
Find the first file matching a given pattern in the current directory or the nearest ancestor directory.
3+
> Find the first file matching a given pattern in the current directory or the nearest ancestor directory.
44
5-
## Getting Started
6-
Install the module with: `npm install findup-sync`
5+
Matching is done with [micromatch][], please report any matching related issues on that repository.
6+
7+
## Install with [npm](npmjs.org)
8+
9+
```bash
10+
npm i findup-sync --save
11+
```
12+
13+
## Usage
714

815
```js
916
var findup = require('findup-sync');
17+
findup(patternOrPatterns [, micromatchOptions]);
1018

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

18-
## Usage
19-
20-
```js
21-
findup(patternOrPatterns [, minimatchOptions])
22-
```
26+
* `patterns` **{String|Array}**: Glob pattern(s) or file path(s) to match against.
27+
* `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.
28+
* `returns` **{String}**: Returns the first matching file.
2329

24-
### patternOrPatterns
25-
Type: `String` or `Array`
26-
Default: none
30+
## Running tests
2731

28-
One or more wildcard glob patterns. Or just filenames.
32+
Install dev dependencies:
2933

30-
### minimatchOptions
31-
Type: `Object`
32-
Default: `{}`
34+
```bash
35+
npm i -d && npm test
36+
```
3337

34-
Options to be passed to [minimatch](https://github.com/isaacs/minimatch).
38+
## Contributing
3539

36-
Note that if you want to start in a different directory than the current working directory, specify a `cwd` property here.
40+
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/)
3741

38-
## Contributing
39-
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/).
42+
For bugs and feature requests, [please create an issue](https://github.com/cowboy/node-findup-sync/issues).
4043

4144
## Release History
42-
2015-09-14 0 v0.3.0 - updated glob to ~5.0.
43-
2014-12-17 - v0.2.1 - updated to glob ~4.3.
45+
46+
2015-01-30 - v0.4.0 - Refactored, not also uses [micromatch][] instead of minimatch.
47+
2015-09-14 - v0.3.0 - updated glob to ~5.0. 2014-12-17 - v0.2.1 - updated to glob ~4.3.
48+
2014-12-17 - v0.2.1 - Updated to glob 4.3.
4449
2014-12-16 - v0.2.0 - Removed lodash, updated to glob 4.x.
4550
2014-03-14 - v0.1.3 - Updated dependencies.
4651
2013-03-08 - v0.1.2 - Updated dependencies. Fixed a Node 0.9.x bug. Updated unit tests to work cross-platform.
4752
2012-11-15 - v0.1.1 - Now works without an options object.
48-
2012-11-01 - v0.1.0 - Initial release.
53+
2012-11-01 - v0.1.0 - Initial release.
54+
55+
## Authors
56+
57+
**"Cowboy" Ben Alman**
58+
59+
+ [github/cowboy](https://github.com/cowboy)
60+
+ [twitter/cowboy](http://twitter.com/cowboy)
61+
62+
## License
63+
64+
Copyright (c) 2012-2016 "Cowboy" Ben Alman
65+
Released under the MIT license
66+
67+
[micromatch]: http://github.com/jonschlinkert/micromatch

index.js

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
'use strict';
2+
3+
/**
4+
* Module dependencies
5+
*/
6+
7+
var fs = require('fs');
8+
var path = require('path');
9+
var isGlob = require('is-glob');
10+
var resolveDir = require('resolve-dir');
11+
var mm = require('micromatch');
12+
13+
/**
14+
* @param {String|Array} `pattern` Glob pattern or file path(s) to match against.
15+
* @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.
16+
* @return {String} Returns the first matching file.
17+
* @api public
18+
*/
19+
20+
module.exports = function(patterns, options) {
21+
if (typeof patterns === 'string') {
22+
return lookup(patterns, options);
23+
}
24+
25+
if (!Array.isArray(patterns)) {
26+
throw new TypeError('findup-sync expects a string or array as the first argument.');
27+
}
28+
29+
var len = patterns.length, i = -1;
30+
while (++i < len) {
31+
var res = lookup(patterns[i], options);
32+
if (res) {
33+
return res;
34+
}
35+
}
36+
37+
return null;
38+
};
39+
40+
function lookup(pattern, options) {
41+
options = options || {};
42+
var cwd = resolveDir(options.cwd || '');
43+
if (isGlob(pattern)) {
44+
return matchFile(cwd, pattern, options);
45+
} else {
46+
return findFile(cwd, pattern);
47+
}
48+
}
49+
50+
function matchFile(cwd, pattern, opts) {
51+
var isMatch = mm.matcher(pattern, opts);
52+
var files = fs.readdirSync(cwd);
53+
var len = files.length, i = -1;
54+
55+
while (++i < len) {
56+
var name = files[i];
57+
var fp = path.join(cwd, name);
58+
if (isMatch(name) || isMatch(fp)) {
59+
return fp;
60+
}
61+
}
62+
63+
var dir = path.dirname(cwd);
64+
if (dir === cwd) {
65+
return null;
66+
}
67+
return matchFile(dir, pattern, opts);
68+
}
69+
70+
function findFile(cwd, filename) {
71+
var fp = cwd ? (cwd + '/' + filename) : filename;
72+
if (fs.existsSync(fp)) {
73+
return fp;
74+
}
75+
76+
var segs = cwd.split(path.sep);
77+
var len = segs.length;
78+
79+
while (len--) {
80+
cwd = segs.slice(0, len).join('/');
81+
fp = cwd + '/' + filename;
82+
if (fs.existsSync(fp)) {
83+
return fp;
84+
}
85+
}
86+
return null;
87+
}

lib/findup-sync.js

Lines changed: 0 additions & 49 deletions
This file was deleted.

package.json

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,46 @@
33
"description": "Find the first file matching a given pattern in the current directory or the nearest ancestor directory.",
44
"version": "0.3.0",
55
"homepage": "https://github.com/cowboy/node-findup-sync",
6-
"author": {
7-
"name": "\"Cowboy\" Ben Alman",
8-
"url": "http://benalman.com/"
9-
},
10-
"repository": {
11-
"type": "git",
12-
"url": "git://github.com/cowboy/node-findup-sync.git"
13-
},
6+
"author": "\"Cowboy\" Ben Alman (http://benalman.com)",
7+
"repository": "cowboy/node-findup-sync",
148
"bugs": {
159
"url": "https://github.com/cowboy/node-findup-sync/issues"
1610
},
17-
"licenses": [
18-
{
19-
"type": "MIT",
20-
"url": "https://github.com/cowboy/node-findup-sync/blob/master/LICENSE-MIT"
21-
}
11+
"license": "MIT",
12+
"files": [
13+
"index.js"
2214
],
23-
"main": "lib/findup-sync",
15+
"main": "index.js",
2416
"engines": {
25-
"node": ">= 0.6.0"
17+
"node": ">= 0.8.0"
2618
},
2719
"scripts": {
28-
"test": "grunt nodeunit"
20+
"test": "grunt && mocha"
2921
},
3022
"dependencies": {
31-
"glob": "~5.0.0"
23+
"is-glob": "^2.0.1",
24+
"micromatch": "^2.3.7",
25+
"resolve-dir": "^0.1.0"
3226
},
3327
"devDependencies": {
34-
"grunt": "~0.4.5",
35-
"grunt-contrib-jshint": "~0.11.3",
36-
"grunt-contrib-nodeunit": "~0.3.3"
28+
"grunt": "^0.4.5",
29+
"grunt-contrib-jshint": "^0.12.0",
30+
"is-absolute": "^0.2.3",
31+
"minimist": "^1.2.0",
32+
"mocha": "^2.4.5",
33+
"normalize-path": "^2.0.1",
34+
"resolve": "^1.1.7",
35+
"user-home": "^2.0.0"
3736
},
3837
"keywords": [
38+
"file",
3939
"find",
40+
"find-up",
41+
"findup",
4042
"glob",
41-
"file"
43+
"match",
44+
"pattern",
45+
"resolve",
46+
"search"
4247
]
4348
}

0 commit comments

Comments
 (0)