8889841cindex.js000066600000002230150514465400006216 0ustar00'use strict'; const {promisify} = require('util'); const fs = require('fs'); async function isType(fsStatType, statsMethodName, filePath) { if (typeof filePath !== 'string') { throw new TypeError(`Expected a string, got ${typeof filePath}`); } try { const stats = await promisify(fs[fsStatType])(filePath); return stats[statsMethodName](); } catch (error) { if (error.code === 'ENOENT') { return false; } throw error; } } function isTypeSync(fsStatType, statsMethodName, filePath) { if (typeof filePath !== 'string') { throw new TypeError(`Expected a string, got ${typeof filePath}`); } try { return fs[fsStatType](filePath)[statsMethodName](); } catch (error) { if (error.code === 'ENOENT') { return false; } throw error; } } 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'); readme.md000066600000002130150514465400006327 0ustar00# path-type [![Build Status](https://travis-ci.org/sindresorhus/path-type.svg?branch=master)](https://travis-ci.org/sindresorhus/path-type) > Check if a path is a file, directory, or symlink ## Install ``` $ npm install path-type ``` ## Usage ```js const {isFile} = require('path-type'); (async () => { console.log(await isFile('package.json')); //=> true })(); ``` ## API ### isFile(path) Check whether the passed `path` is a file. Returns a `Promise`. #### path Type: `string` The path to check. ### isDirectory(path) Check whether the passed `path` is a directory. Returns a `Promise`. ### isSymlink(path) Check whether the passed `path` is a symlink. Returns a `Promise`. ### 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) license000066600000002125150514465400006121 0ustar00MIT License Copyright (c) Sindre Sorhus (sindresorhus.com) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. index.d.ts000066600000002420150514465400006453 0ustar00export type PathTypeFunction = (path: string) => Promise; /** * Check whether the passed `path` is a file. * * @param path - The path to check. * @returns Whether the `path` is a file. */ export const isFile: PathTypeFunction; /** * Check whether the passed `path` is a directory. * * @param path - The path to check. * @returns Whether the `path` is a directory. */ export const isDirectory: PathTypeFunction; /** * Check 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 check 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 check 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 check whether the passed `path` is a symlink. * * @param path - The path to check. * @returns Whether the `path` is a directory. */ export const isSymlinkSync: PathTypeSyncFunction; package.json000066600000001312150514465400007037 0ustar00{ "name": "path-type", "version": "4.0.0", "description": "Check if a path is a file, directory, or symlink", "license": "MIT", "repository": "sindresorhus/path-type", "author": { "name": "Sindre Sorhus", "email": "sindresorhus@gmail.com", "url": "sindresorhus.com" }, "engines": { "node": ">=8" }, "scripts": { "test": "xo && nyc ava && tsd-check" }, "files": [ "index.js", "index.d.ts" ], "keywords": [ "path", "fs", "type", "is", "check", "directory", "dir", "file", "filepath", "symlink", "symbolic", "link", "stat", "stats", "filesystem" ], "devDependencies": { "ava": "^1.3.1", "nyc": "^13.3.0", "tsd-check": "^0.3.0", "xo": "^0.24.0" } }