path-type
Advanced tools
Comparing version 3.0.0 to 4.0.0
51
index.js
'use strict'; | ||
const {promisify} = require('util'); | ||
const fs = require('fs'); | ||
const pify = require('pify'); | ||
function type(fn, fn2, fp) { | ||
if (typeof fp !== 'string') { | ||
return Promise.reject(new TypeError(`Expected a string, got ${typeof fp}`)); | ||
async function isType(fsStatType, statsMethodName, filePath) { | ||
if (typeof filePath !== 'string') { | ||
throw new TypeError(`Expected a string, got ${typeof filePath}`); | ||
} | ||
return pify(fs[fn])(fp) | ||
.then(stats => stats[fn2]()) | ||
.catch(err => { | ||
if (err.code === 'ENOENT') { | ||
return false; | ||
} | ||
try { | ||
const stats = await promisify(fs[fsStatType])(filePath); | ||
return stats[statsMethodName](); | ||
} catch (error) { | ||
if (error.code === 'ENOENT') { | ||
return false; | ||
} | ||
throw err; | ||
}); | ||
throw error; | ||
} | ||
} | ||
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](); | ||
} catch (err) { | ||
if (err.code === 'ENOENT') { | ||
return fs[fsStatType](filePath)[statsMethodName](); | ||
} catch (error) { | ||
if (error.code === 'ENOENT') { | ||
return false; | ||
} | ||
throw err; | ||
throw error; | ||
} | ||
} | ||
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'); |
{ | ||
"name": "path-type", | ||
"version": "3.0.0", | ||
"version": "4.0.0", | ||
"description": "Check if a path is a file, directory, or symlink", | ||
@@ -13,9 +13,10 @@ "license": "MIT", | ||
"engines": { | ||
"node": ">=4" | ||
"node": ">=8" | ||
}, | ||
"scripts": { | ||
"test": "xo && ava" | ||
"test": "xo && nyc ava && tsd-check" | ||
}, | ||
"files": [ | ||
"index.js" | ||
"index.js", | ||
"index.d.ts" | ||
], | ||
@@ -39,9 +40,8 @@ "keywords": [ | ||
], | ||
"dependencies": { | ||
"pify": "^3.0.0" | ||
}, | ||
"devDependencies": { | ||
"ava": "*", | ||
"xo": "*" | ||
"ava": "^1.3.1", | ||
"nyc": "^13.3.0", | ||
"tsd-check": "^0.3.0", | ||
"xo": "^0.24.0" | ||
} | ||
} |
@@ -16,8 +16,8 @@ # path-type [![Build Status](https://travis-ci.org/sindresorhus/path-type.svg?branch=master)](https://travis-ci.org/sindresorhus/path-type) | ||
```js | ||
const pathType = require('path-type'); | ||
const {isFile} = require('path-type'); | ||
pathType.file('package.json').then(isFile => { | ||
console.log(isFile); | ||
(async () => { | ||
console.log(await isFile('package.json')); | ||
//=> true | ||
}) | ||
})(); | ||
``` | ||
@@ -28,17 +28,47 @@ | ||
### .file(path) | ||
### .dir(path) | ||
### .symlink(path) | ||
### isFile(path) | ||
Returns a `Promise` for a `boolean` of whether the path is the checked type. | ||
Check whether the passed `path` is a file. | ||
### .fileSync(path) | ||
### .dirSync(path) | ||
### .symlinkSync(path) | ||
Returns a `Promise<boolean>`. | ||
Returns a `boolean` of whether the path is the checked type. | ||
#### path | ||
Type: `string` | ||
The path to check. | ||
### isDirectory(path) | ||
Check whether the passed `path` is a directory. | ||
Returns a `Promise<boolean>`. | ||
### isSymlink(path) | ||
Check whether the passed `path` is a symlink. | ||
Returns a `Promise<boolean>`. | ||
### isFileSync(path) | ||
Synchronously check whether the passed `path` is a file. | ||
Returns a `boolean`. | ||
### isDirectorySync(path) | ||
Synchronously check whether the passed `path` is a directory. | ||
Returns a `boolean`. | ||
### isSymlinkSync(path) | ||
Synchronously check whether the passed `path` is a symlink. | ||
Returns a `boolean`. | ||
## License | ||
MIT © [Sindre Sorhus](https://sindresorhus.com) |
5407
0
5
80
73
4
- Removedpify@^3.0.0
- Removedpify@3.0.0(transitive)