Skip to content

Commit

Permalink
Add TypeScript definition (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
BendingBender authored and sindresorhus committed Apr 18, 2019
1 parent ce26654 commit 905e612
Show file tree
Hide file tree
Showing 5 changed files with 180 additions and 104 deletions.
55 changes: 55 additions & 0 deletions index.d.ts
@@ -0,0 +1,55 @@
declare namespace findUp {
interface Options {
/**
Directory to start from.
@default process.cwd()
*/
readonly cwd?: string;
}
}

declare const findUp: {
/**
Find a file or directory by walking up parent directories.
@param filename - Filename of the file or an array of files to find.
@returns Either the first filepath found (by respecting the order of `filename`s) or `null`.
@example
```
// /
// └── Users
// └── sindresorhus
// ├── unicorn.png
// └── foo
// └── bar
// ├── baz
// └── example.js
// example.js
import findUp = require('find-up');
(async () => {
console.log(await findUp('unicorn.png'));
//=> '/Users/sindresorhus/unicorn.png'
console.log(await findUp(['rainbow.png', 'unicorn.png']));
//=> '/Users/sindresorhus/unicorn.png'
})();
```
*/
(filename: string | string[], options?: findUp.Options): Promise<
string | null
>;

/**
Synchronously find a file or directory by walking up parent directories.
@param filename - Filename of the file or an array of files to find.
@returns Either the first filepath found (by respecting the order of `filename`s) or `null`.
*/
sync(filename: string | string[], options?: findUp.Options): string | null;
};

export = findUp;
32 changes: 16 additions & 16 deletions index.js
Expand Up @@ -2,45 +2,45 @@
const path = require('path');
const locatePath = require('locate-path');

module.exports = (filename, opts = {}) => {
const startDir = path.resolve(opts.cwd || '');
const {root} = path.parse(startDir);
module.exports = (filename, options = {}) => {
const startDirectory = path.resolve(options.cwd || '');
const {root} = path.parse(startDirectory);

const filenames = [].concat(filename);

return new Promise(resolve => {
(function find(dir) {
locatePath(filenames, {cwd: dir}).then(file => {
(function find(directory) {
locatePath(filenames, {cwd: directory}).then(file => {
if (file) {
resolve(path.join(dir, file));
} else if (dir === root) {
resolve(path.join(directory, file));
} else if (directory === root) {
resolve(null);
} else {
find(path.dirname(dir));
find(path.dirname(directory));
}
});
})(startDir);
})(startDirectory);
});
};

module.exports.sync = (filename, opts = {}) => {
let dir = path.resolve(opts.cwd || '');
const {root} = path.parse(dir);
module.exports.sync = (filename, options = {}) => {
let directory = path.resolve(options.cwd || '');
const {root} = path.parse(directory);

const filenames = [].concat(filename);

// eslint-disable-next-line no-constant-condition
while (true) {
const file = locatePath.sync(filenames, {cwd: dir});
const file = locatePath.sync(filenames, {cwd: directory});

if (file) {
return path.join(dir, file);
return path.join(directory, file);
}

if (dir === root) {
if (directory === root) {
return null;
}

dir = path.dirname(dir);
directory = path.dirname(directory);
}
};
16 changes: 16 additions & 0 deletions index.test-d.ts
@@ -0,0 +1,16 @@
import {expectType} from 'tsd';
import findUp = require('.');

expectType<Promise<string | null>>(findUp('unicorn.png'));
expectType<Promise<string | null>>(findUp('unicorn.png', {cwd: ''}));
expectType<Promise<string | null>>(findUp(['rainbow.png', 'unicorn.png']));
expectType<Promise<string | null>>(
findUp(['rainbow.png', 'unicorn.png'], {cwd: ''})
);

expectType<string | null>(findUp.sync('unicorn.png'));
expectType<string | null>(findUp.sync('unicorn.png', {cwd: ''}));
expectType<string | null>(findUp.sync(['rainbow.png', 'unicorn.png']));
expectType<string | null>(
findUp.sync(['rainbow.png', 'unicorn.png'], {cwd: ''})
);
10 changes: 6 additions & 4 deletions package.json
Expand Up @@ -13,10 +13,11 @@
"node": ">=6"
},
"scripts": {
"test": "xo && ava"
"test": "xo && ava && tsd"
},
"files": [
"index.js"
"index.js",
"index.d.ts"
],
"keywords": [
"find",
Expand All @@ -43,8 +44,9 @@
"locate-path": "^3.0.0"
},
"devDependencies": {
"ava": "*",
"ava": "^1.4.1",
"tempy": "^0.2.1",
"xo": "*"
"tsd": "^0.7.2",
"xo": "^0.24.0"
}
}

0 comments on commit 905e612

Please sign in to comment.