Skip to content

Commit d5b8a65

Browse files
jonschlinkertphated
authored andcommitted
Update: Cleanup and small improvements
1 parent 62d4f4b commit d5b8a65

27 files changed

+141
-75
lines changed

Gruntfile.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module.exports = function(grunt) {
88
options: {
99
jshintrc: '.jshintrc'
1010
},
11-
all: ['Gruntfile.js', '*.js']
11+
all: ['*.js', 'test/{,support/}*.js']
1212
}
1313
});
1414

index.js

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ var fs = require('fs');
88
var path = require('path');
99
var isGlob = require('is-glob');
1010
var resolveDir = require('resolve-dir');
11+
var exists = require('fs-exists-sync');
1112
var mm = require('micromatch');
1213

1314
/**
@@ -26,9 +27,11 @@ module.exports = function(patterns, options) {
2627
throw new TypeError('findup-sync expects a string or array as the first argument.');
2728
}
2829

29-
var len = patterns.length, i = -1;
30-
while (++i < len) {
31-
var res = lookup(patterns[i], options);
30+
var len = patterns.length;
31+
var idx = -1;
32+
33+
while (++idx < len) {
34+
var res = lookup(patterns[idx], options);
3235
if (res) {
3336
return res;
3437
}
@@ -39,7 +42,7 @@ module.exports = function(patterns, options) {
3942

4043
function lookup(pattern, options) {
4144
options = options || {};
42-
var cwd = resolveDir(options.cwd || '');
45+
var cwd = path.resolve(resolveDir(options.cwd || ''));
4346
if (isGlob(pattern)) {
4447
return matchFile(cwd, pattern, options);
4548
} else {
@@ -50,10 +53,11 @@ function lookup(pattern, options) {
5053
function matchFile(cwd, pattern, opts) {
5154
var isMatch = mm.matcher(pattern, opts);
5255
var files = fs.readdirSync(cwd);
53-
var len = files.length, i = -1;
56+
var len = files.length;
57+
var idx = -1;
5458

55-
while (++i < len) {
56-
var name = files[i];
59+
while (++idx < len) {
60+
var name = files[idx];
5761
var fp = path.join(cwd, name);
5862
if (isMatch(name) || isMatch(fp)) {
5963
return fp;
@@ -68,18 +72,18 @@ function matchFile(cwd, pattern, opts) {
6872
}
6973

7074
function findFile(cwd, filename) {
71-
var fp = cwd ? (cwd + '/' + filename) : filename;
72-
if (fs.existsSync(fp)) {
75+
var fp = cwd ? path.resolve(cwd, filename) : filename;
76+
if (exists(fp)) {
7377
return fp;
7478
}
7579

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

7983
while (len--) {
80-
cwd = segs.slice(0, len).join('/');
81-
fp = cwd + '/' + filename;
82-
if (fs.existsSync(fp)) {
84+
cwd = segs.slice(0, len).join(path.sep);
85+
fp = path.resolve(cwd, filename);
86+
if (exists(fp)) {
8387
return fp;
8488
}
8589
}

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,20 @@
2020
"test": "grunt && mocha"
2121
},
2222
"dependencies": {
23+
"fs-exists-sync": "^0.1.0",
2324
"is-glob": "^2.0.1",
2425
"micromatch": "^2.3.7",
2526
"resolve-dir": "^0.1.0"
2627
},
2728
"devDependencies": {
28-
"grunt": "^0.4.5",
29+
"grunt": "^1.0.1",
2930
"grunt-contrib-jshint": "^0.12.0",
3031
"is-absolute": "^0.2.3",
3132
"minimist": "^1.2.0",
3233
"mocha": "^2.4.5",
3334
"normalize-path": "^2.0.1",
34-
"resolve": "^1.1.7",
35-
"user-home": "^2.0.0"
35+
"os-homedir": "^1.0.1",
36+
"resolve": "^1.1.7"
3637
},
3738
"keywords": [
3839
"file",

test/fixtures/a/a.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
a

test/fixtures/a/b/b.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
b

test/fixtures/a/b/c/c.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
c

test/fixtures/a/b/c/d/d.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
d

test/fixtures/a/b/c/d/e/e.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
e

test/fixtures/a/b/c/d/e/f/f.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
f

test/fixtures/a/b/c/d/e/f/file.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
f

test/fixtures/a/b/c/d/e/f/g/file.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
g

test/fixtures/a/b/c/d/e/f/g/g.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
g
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
h

test/fixtures/a/b/c/d/e/f/g/h/h.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
h
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
i

test/fixtures/a/b/c/d/e/f/g/h/i/i.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
i
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
j
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
j

test/fixtures/a/b/c/d/e/file.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
e

test/fixtures/a/b/c/d/file.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
d

test/fixtures/a/b/c/file.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
c

test/fixtures/a/b/file.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
b

test/fixtures/a/file.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
a

test/fixtures/file.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
root

test/fixtures/root.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
root

test/support/index.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
'use strict';
2+
3+
var path = require('path');
4+
var normalizePath = require('normalize-path');
5+
var isAbsolute = require('is-absolute');
6+
var resolve = require('resolve');
7+
8+
exports.normalize = function(filepath) {
9+
return filepath ? normalizePath(path.relative('.', filepath)) : null;
10+
};
11+
12+
exports.chdir = function(dir) {
13+
// store current cwd
14+
var orig = process.cwd();
15+
// set cwd to the given `dir`
16+
process.chdir(dir);
17+
return function() {
18+
// restore original `cwd`
19+
process.chdir(orig);
20+
};
21+
};
22+
23+
exports.npm = function npm(name) {
24+
return path.dirname(resolve.sync(name));
25+
};
26+
27+
exports.assert = function(assert) {
28+
assert.isPath = function (filepath) {
29+
assert(filepath);
30+
assert.equal(typeof filepath, 'string');
31+
};
32+
33+
assert.isAbsolute = function (filepath) {
34+
assert(filepath);
35+
assert.equal(typeof filepath, 'string');
36+
assert(isAbsolute(filepath));
37+
};
38+
39+
assert.basename = function (filepath, basename) {
40+
assert(filepath);
41+
assert.equal(typeof filepath, 'string');
42+
assert.equal(path.basename(filepath), basename);
43+
};
44+
45+
assert.dirname = function (filepath, dirname) {
46+
assert(filepath);
47+
assert.equal(typeof filepath, 'string');
48+
assert.equal(path.dirname(path.resolve(filepath)), path.resolve(dirname));
49+
};
50+
};
51+

test/test.js

Lines changed: 47 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,30 @@
11
'use strict';
22

33
require('mocha');
4-
var argv = require('minimist')(process.argv.slice(2));
54
var fs = require('fs');
65
var path = require('path');
76
var assert = require('assert');
8-
var expand = require('resolve-dir');
9-
var norm = require('normalize-path');
10-
var home = require('user-home');
11-
var isAbsolute = require('is-absolute');
7+
var support = require('./support');
8+
support.assert(assert);
9+
var home = require('os-homedir');
10+
var exists = require('fs-exists-sync');
1211
var resolve = require('resolve');
13-
var cwd, actual, opts;
14-
1512
var findup = require('../');
13+
var normalize = support.normalize;
14+
var chdir = support.chdir;
15+
var npm = support.npm;
16+
var cwd;
17+
var actual;
1618

17-
function normalize(fp) {
18-
return fp ? norm(path.relative('.', fp)) : null;
19-
}
20-
21-
if (argv.bench) {
22-
var b = path.join(__dirname, 'benchmark/code', argv.bench);
23-
console.log(b);
24-
findup = require(b);
25-
}
26-
27-
assert.isPath = function (fp, basename) {
28-
assert(fp);
29-
assert.equal(typeof fp, 'string');
30-
};
31-
32-
assert.isAbsolute = function (fp) {
33-
assert(fp);
34-
assert(isAbsolute(fp));
35-
};
36-
37-
assert.exists = function (fp) {
38-
assert(fp);
39-
try {
40-
fs.statSync(fp);
41-
} catch(err) {
42-
assert(fp, err);
43-
}
44-
};
45-
46-
assert.basename = function (fp, basename) {
47-
assert(fp);
48-
assert.equal(path.basename(fp), basename);
49-
};
50-
51-
assert.dirname = function (fp, dirname) {
52-
assert(fp);
53-
assert.equal(path.dirname(path.resolve(fp)), path.resolve(dirname));
54-
};
55-
56-
function npm(name) {
57-
return path.dirname(resolve.sync(name));
58-
}
5919

6020
describe('findup-sync', function () {
6121
before(function () {
62-
fs.writeFileSync(home + '/_aaa.txt', '');
63-
fs.writeFileSync(home + '/_bbb.txt', '');
22+
fs.writeFileSync(home() + '/_aaa.txt', '');
23+
fs.writeFileSync(home() + '/_bbb.txt', '');
6424
});
6525
after(function () {
66-
fs.unlinkSync(home + '/_aaa.txt');
67-
fs.unlinkSync(home + '/_bbb.txt');
26+
fs.unlinkSync(home() + '/_aaa.txt');
27+
fs.unlinkSync(home() + '/_bbb.txt');
6828
});
6929

7030
it('should throw when the first arg is not a string or array:', function(cb) {
@@ -85,6 +45,34 @@ describe('findup-sync', function () {
8545
assert.equal(normalize(findup('package.json')), 'package.json');
8646
});
8747

48+
it('should find files in a child directory', function () {
49+
var expected = path.resolve(__dirname, 'fixtures/a/b/file.txt');
50+
var restore = chdir(path.resolve(__dirname, 'fixtures/a/b/c/d/e/f/g/h'));
51+
52+
var actual = findup('a/b/file.txt');
53+
assert(actual);
54+
assert(exists(actual));
55+
assert.equal(actual, expected);
56+
restore();
57+
});
58+
59+
it('should find files in a child directory relative to a cwd', function () {
60+
var expectedFile = path.resolve(__dirname, 'fixtures/a/b/file.txt');
61+
var expectedA = path.resolve(__dirname, 'fixtures/a/a.txt');
62+
var tempDir = chdir(path.resolve(__dirname, 'fixtures'));
63+
64+
var actualFile = findup('a/b/file.txt', {cwd: 'a/b/c/d'});
65+
assert(actualFile);
66+
assert(exists(actualFile));
67+
assert.equal(actualFile, expectedFile);
68+
69+
var actualA = findup('a.txt', {cwd: 'a/b/c/d/e/f'});
70+
assert(actualA);
71+
assert(exists(actualA));
72+
assert.equal(actualA, expectedA);
73+
tempDir();
74+
});
75+
8876
it('should support normal (non-glob) file paths:', function () {
8977
var normPath = normalize(findup('package.json', {cwd: path.dirname(resolve.sync('normalize-path'))}));
9078
assert.equal(normPath, 'node_modules/normalize-path/package.json');
@@ -216,7 +204,7 @@ describe('findup-sync', function () {
216204
});
217205

218206
it('should find files from absolute paths:', function () {
219-
var actual = findup('package.json', { cwd: __dirname })
207+
var actual = findup('package.json', { cwd: __dirname });
220208

221209
assert.basename(actual, 'package.json');
222210
assert.dirname(actual, path.resolve(__dirname, '..'));
@@ -231,16 +219,16 @@ describe('findup-sync', function () {
231219
});
232220

233221
it('should find files in user home:', function () {
234-
var actual = findup('*', { cwd: home });
222+
var actual = findup('*', { cwd: home() });
235223
assert.isPath(actual);
236-
assert.exists(actual);
237-
assert.dirname(actual, home);
224+
assert(exists(actual));
225+
assert.dirname(actual, home());
238226
});
239227

240228
it('should find files in user home using tilde expansion:', function () {
241229
var actual = findup('*', { cwd: '~' });
242230
assert.isPath(actual);
243-
assert.exists(actual);
244-
assert.dirname(actual, home);
231+
assert(exists(actual));
232+
assert.dirname(actual, home());
245233
});
246234
});

0 commit comments

Comments
 (0)