Skip to content

Commit

Permalink
Rename exported methods, add TypeScript definition (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
BendingBender authored and sindresorhus committed Mar 12, 2019
1 parent 7b3755c commit f6de669
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 48 deletions.
3 changes: 1 addition & 2 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
* text=auto
*.js text eol=lf
* text=auto eol=lf
51 changes: 51 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
export type PathTypeFunction = (path: string) => Promise<boolean>;

/**
* Checks whether the passed `path` is a file.
*
* @param path - The path to check.
* @returns Whether the `path` is a file.
*/
export const isFile: PathTypeFunction;

/**
* Checks whether the passed `path` is a directory.
*
* @param path - The path to check.
* @returns Whether the `path` is a directory.
*/
export const isDirectory: PathTypeFunction;

/**
* Checks whether the passed `path` is a symlink.
*
* @param path - The path to check.
* @returns Whether the `path` is a symlink.
*/
export const isSymlink: PathTypeFunction;

export type PathTypeSyncFunction = (path: string) => boolean;

/**
* Synchronously checks whether the passed `path` is a file.
*
* @param path - The path to check.
* @returns Whether the `path` is a file.
*/
export const isFileSync: PathTypeSyncFunction;

/**
* Synchronously checks whether the passed `path` is a directory.
*
* @param path - The path to check.
* @returns Whether the `path` is a directory.
*/
export const isDirectorySync: PathTypeSyncFunction;

/**
* Synchronously checks whether the passed `path` is a symlink.
*
* @param path - The path to check.
* @returns Whether the `path` is a directory.
*/
export const isSymlinkSync: PathTypeSyncFunction;
33 changes: 17 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
'use strict';
const fs = require('fs');

const {promisify} = require('util');
const fs = require('fs');

async function type(fn, fn2, fp) {
if (typeof fp !== 'string') {
throw new TypeError(`Expected a string, got ${typeof fp}`);
async function isType(fsStatType, statsMethodName, filePath) {
if (typeof filePath !== 'string') {
return Promise.reject(new TypeError(`Expected a string, got ${typeof filePath}`));
}

try {
const stats = await promisify(fs[fn])(fp);
const stats = await promisify(fs[fsStatType])(filePath);

return stats[fn2]();
return stats[statsMethodName]();
} catch (error) {
if (error.code === 'ENOENT') {
return false;
Expand All @@ -20,13 +21,13 @@ async function type(fn, fn2, fp) {
}
}

function typeSync(fn, fn2, fp) {
if (typeof fp !== 'string') {
throw new TypeError(`Expected a string, got ${typeof fp}`);
function isTypeSync(fsStatType, statsMethodName, filePath) {
if (typeof filePath !== 'string') {
throw new TypeError(`Expected a string, got ${typeof filePath}`);
}

try {
return fs[fn](fp)[fn2]();
return fs[fsStatType](filePath)[statsMethodName]();
} catch (error) {
if (error.code === 'ENOENT') {
return false;
Expand All @@ -36,9 +37,9 @@ function typeSync(fn, fn2, fp) {
}
}

exports.file = type.bind(null, 'stat', 'isFile');
exports.dir = type.bind(null, 'stat', 'isDirectory');
exports.symlink = type.bind(null, 'lstat', 'isSymbolicLink');
exports.fileSync = typeSync.bind(null, 'statSync', 'isFile');
exports.dirSync = typeSync.bind(null, 'statSync', 'isDirectory');
exports.symlinkSync = typeSync.bind(null, 'lstatSync', 'isSymbolicLink');
exports.isFile = isType.bind(null, 'stat', 'isFile');
exports.isDirectory = isType.bind(null, 'stat', 'isDirectory');
exports.isSymlink = isType.bind(null, 'lstat', 'isSymbolicLink');
exports.isFileSync = isTypeSync.bind(null, 'statSync', 'isFile');
exports.isDirectorySync = isTypeSync.bind(null, 'statSync', 'isDirectory');
exports.isSymlinkSync = isTypeSync.bind(null, 'lstatSync', 'isSymbolicLink');
17 changes: 17 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import {expectType} from 'tsd-check';
import {
isFile,
isDirectory,
isSymlink,
isFileSync,
isDirectorySync,
isSymlinkSync
} from '.';

expectType<Promise<boolean>>(isFile('package.json'));
expectType<Promise<boolean>>(isDirectory('package.json'));
expectType<Promise<boolean>>(isSymlink('package.json'));

expectType<boolean>(isFileSync('package.json'));
expectType<boolean>(isDirectorySync('package.json'));
expectType<boolean>(isSymlinkSync('package.json'));
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
"node": ">=8"
},
"scripts": {
"test": "xo && nyc ava"
"test": "xo && nyc ava && tsd-check"
},
"files": [
"index.js"
"index.js",
"index.d.ts"
],
"keywords": [
"path",
Expand All @@ -36,8 +37,9 @@
"filesystem"
],
"devDependencies": {
"ava": "*",
"ava": "^1.3.1",
"nyc": "^13.3.0",
"xo": "*"
"tsd-check": "^0.3.0",
"xo": "^0.24.0"
}
}
47 changes: 38 additions & 9 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ $ npm install path-type
```js
const pathType = require('path-type');

pathType.file('package.json').then(isFile => {
pathType.isFile('package.json').then(isFile => {
console.log(isFile);
//=> true
})
Expand All @@ -24,18 +24,47 @@ pathType.file('package.json').then(isFile => {

## API

### .file(path)
### .dir(path)
### .symlink(path)
### .isFile(path)

Returns a `Promise` for a `boolean` of whether the path is the checked type.
Checks whether the passed `path` is a file.

### .fileSync(path)
### .dirSync(path)
### .symlinkSync(path)
Returns `Promise` for a `boolean`.

Returns a `boolean` of whether the path is the checked type.
### .isDirectory(path)

Checks whether the passed `path` is a directory.

Returns `Promise` for a `boolean`.

### .isSymlink(path)

Checks whether the passed `path` is a symlink.

Returns `Promise` for a `boolean`.

#### path

Type: `string`

The path to check.

### .isFileSync(path)

Synchronously checks whether the passed `path` is a file.

Returns a `boolean`.

### .isDirectorySync(path)

Synchronously checks whether the passed `path` is a directory.

Returns a `boolean`.

### .isSymlinkSync(path)

Synchronously checks whether the passed `path` is a symlink.

Returns a `boolean`.

## License

Expand Down
6 changes: 3 additions & 3 deletions test/eacces.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fs from 'fs';
import test from 'ava';
import m from '..';
import pathType from '..';

function fakeError(fp) {
const error = new Error(`EACCES: permission denied, stat '${fp}'`);
Expand All @@ -22,9 +22,9 @@ Object.defineProperties(fs, {
});

test('throws on EACCES error - async', async t => {
await t.throwsAsync(m.file('/root/private'));
await t.throwsAsync(pathType.isFile('/root/private'));
});

test('throws on EACCES error - sync', t => {
t.throws(() => m.fileSync('/root/private'));
t.throws(() => pathType.isFileSync('/root/private'));
});
28 changes: 14 additions & 14 deletions test/nominal.js
Original file line number Diff line number Diff line change
@@ -1,49 +1,49 @@
import test from 'ava';
import m from '..';
import pathType from '..';

test('.file()', async t => {
t.true(await m.file('package.json'));
await t.throwsAsync(m.file(false));
t.true(await pathType.isFile('package.json'));
await t.throwsAsync(pathType.isFile(false));
});

test('.dir()', async t => {
t.true(await m.dir('.'));
await t.throwsAsync(m.dir(false));
t.true(await pathType.isDirectory('.'));
await t.throwsAsync(pathType.isDirectory(false));
});

if (process.platform !== 'win32') {
test('.symlink()', async t => {
t.true(await m.symlink('symlink'));
await t.throwsAsync(m.symlink(false));
t.true(await pathType.isSymlink('symlink'));
await t.throwsAsync(pathType.isSymlink(false));
});
}

test('.fileSync()', t => {
t.true(m.fileSync('package.json'));
t.true(pathType.isFileSync('package.json'));
});

test('.dirSync()', t => {
t.true(m.dirSync('.'));
t.true(pathType.isDirectorySync('.'));
});

if (process.platform !== 'win32') {
test('.symlinkSync()', t => {
t.true(m.symlinkSync('symlink'));
t.true(pathType.isSymlinkSync('symlink'));
});
}

test('return false if path doesn\'t exist - async', async t => {
t.false(await m.file('unicorn'));
t.false(await pathType.isFile('unicorn'));
});

test('return false if path doesn\'t exist - sync', t => {
t.false(m.fileSync('unicorn'));
t.false(pathType.isFileSync('unicorn'));
});

test('throws invalid argument - async', async t => {
await t.throwsAsync(m.file(false));
await t.throwsAsync(pathType.isFile(false));
});

test('throws on invalid argument - sync', t => {
t.throws(() => m.fileSync(false));
t.throws(() => pathType.isFileSync(false));
});

0 comments on commit f6de669

Please sign in to comment.