Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TypeScript definition #35

Merged
merged 1 commit into from Apr 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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"
}
}