From ee2d76216d66bfc2b6a536c0ce8780dd3225decb Mon Sep 17 00:00:00 2001 From: Dimitri Benin Date: Thu, 4 Apr 2019 03:24:41 +0000 Subject: [PATCH] Require Node.js 8, add TypeScript definition (#10) --- .editorconfig | 2 +- .gitattributes | 2 +- .gitignore | 1 + .npmrc | 1 + .travis.yml | 5 ++-- index.d.ts | 28 ++++++++++++++++++ index.js | 22 +++++++++------ index.test-d.ts | 5 ++++ license | 20 +++---------- package.json | 75 ++++++++++++++++++++++++------------------------- readme.md | 8 +++--- test.js | 10 +++---- 12 files changed, 103 insertions(+), 76 deletions(-) create mode 100644 .npmrc create mode 100644 index.d.ts create mode 100644 index.test-d.ts diff --git a/.editorconfig b/.editorconfig index 98a761d..1c6314a 100644 --- a/.editorconfig +++ b/.editorconfig @@ -7,6 +7,6 @@ charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true -[{package.json,*.yml}] +[*.yml] indent_style = space indent_size = 2 diff --git a/.gitattributes b/.gitattributes index 176a458..6313b56 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1 +1 @@ -* text=auto +* text=auto eol=lf diff --git a/.gitignore b/.gitignore index 3c3629e..239ecff 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ node_modules +yarn.lock diff --git a/.npmrc b/.npmrc new file mode 100644 index 0000000..43c97e7 --- /dev/null +++ b/.npmrc @@ -0,0 +1 @@ +package-lock=false diff --git a/.travis.yml b/.travis.yml index b18bae5..f3fa8cd 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,4 @@ -sudo: false language: node_js node_js: - - '6' - - '4' + - '10' + - '8' diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..54b7ab8 --- /dev/null +++ b/index.d.ts @@ -0,0 +1,28 @@ +declare const pathExists: { + /** + Check if a path exists. + + @returns Whether the path exists. + + @example + ``` + // foo.ts + import pathExists = require('path-exists'); + + (async () => { + console.log(await pathExists('foo.ts')); + //=> true + })(); + ``` + */ + (path: string): Promise; + + /** + Synchronously check if a path exists. + + @returns Whether the path exists. + */ + sync(path: string): boolean; +}; + +export = pathExists; diff --git a/index.js b/index.js index 16ae60a..4258133 100644 --- a/index.js +++ b/index.js @@ -1,17 +1,23 @@ 'use strict'; const fs = require('fs'); +const {promisify} = require('util'); -module.exports = fp => new Promise(resolve => { - fs.access(fp, err => { - resolve(!err); - }); -}); +const pAccess = promisify(fs.access); -module.exports.sync = fp => { +module.exports = async path => { try { - fs.accessSync(fp); + await pAccess(path); return true; - } catch (err) { + } catch (error) { + return false; + } +}; + +module.exports.sync = path => { + try { + fs.accessSync(path); + return true; + } catch (error) { return false; } }; diff --git a/index.test-d.ts b/index.test-d.ts new file mode 100644 index 0000000..b80976b --- /dev/null +++ b/index.test-d.ts @@ -0,0 +1,5 @@ +import {expectType} from 'tsd'; +import pathExists = require('.'); + +expectType>(pathExists('foo.ts')); +expectType(pathExists.sync('foo.ts')); diff --git a/license b/license index 654d0bf..e7af2f7 100644 --- a/license +++ b/license @@ -1,21 +1,9 @@ -The MIT License (MIT) +MIT 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: +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 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. +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. diff --git a/package.json b/package.json index efd5626..da64fee 100644 --- a/package.json +++ b/package.json @@ -1,40 +1,39 @@ { - "name": "path-exists", - "version": "3.0.0", - "description": "Check if a path exists", - "license": "MIT", - "repository": "sindresorhus/path-exists", - "author": { - "name": "Sindre Sorhus", - "email": "sindresorhus@gmail.com", - "url": "sindresorhus.com" - }, - "engines": { - "node": ">=4" - }, - "scripts": { - "test": "xo && ava" - }, - "files": [ - "index.js" - ], - "keywords": [ - "path", - "exists", - "exist", - "file", - "filepath", - "fs", - "filesystem", - "file-system", - "access", - "stat" - ], - "devDependencies": { - "ava": "*", - "xo": "*" - }, - "xo": { - "esnext": true - } + "name": "path-exists", + "version": "3.0.0", + "description": "Check if a path exists", + "license": "MIT", + "repository": "sindresorhus/path-exists", + "author": { + "name": "Sindre Sorhus", + "email": "sindresorhus@gmail.com", + "url": "sindresorhus.com" + }, + "engines": { + "node": ">=8" + }, + "scripts": { + "test": "xo && ava && tsd" + }, + "files": [ + "index.js", + "index.d.ts" + ], + "keywords": [ + "path", + "exists", + "exist", + "file", + "filepath", + "fs", + "filesystem", + "file-system", + "access", + "stat" + ], + "devDependencies": { + "ava": "^1.4.1", + "tsd": "^0.7.2", + "xo": "^0.24.0" + } } diff --git a/readme.md b/readme.md index bc51727..25e8940 100644 --- a/readme.md +++ b/readme.md @@ -14,7 +14,7 @@ Never use this before handling a file though: ## Install ``` -$ npm install --save path-exists +$ npm install path-exists ``` @@ -24,10 +24,10 @@ $ npm install --save path-exists // foo.js const pathExists = require('path-exists'); -pathExists('foo.js').then(exists => { - console.log(exists); +(async () => { + console.log(await pathExists('foo.js')); //=> true -}); +})(); ``` diff --git a/test.js b/test.js index e726a11..762aad6 100644 --- a/test.js +++ b/test.js @@ -1,12 +1,12 @@ import test from 'ava'; -import m from './'; +import pathExists from '.'; test('async', async t => { - t.true(await m('test.js')); - t.false(await m('fail')); + t.true(await pathExists('test.js')); + t.false(await pathExists('fail')); }); test('sync', t => { - t.true(m.sync('test.js')); - t.false(m.sync('fail')); + t.true(pathExists.sync('test.js')); + t.false(pathExists.sync('fail')); });