Skip to content

Commit

Permalink
Require Node.js 8, add TypeScript definition (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
BendingBender authored and sindresorhus committed Apr 4, 2019
1 parent a6e0030 commit ee2d762
Show file tree
Hide file tree
Showing 12 changed files with 103 additions and 76 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1 +1 @@
* text=auto
* text=auto eol=lf
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
yarn.lock
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
5 changes: 2 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
sudo: false
language: node_js
node_js:
- '6'
- '4'
- '10'
- '8'
28 changes: 28 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -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<boolean>;

/**
Synchronously check if a path exists.
@returns Whether the path exists.
*/
sync(path: string): boolean;
};

export = pathExists;
22 changes: 14 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
@@ -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;
}
};
5 changes: 5 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import {expectType} from 'tsd';
import pathExists = require('.');

expectType<Promise<boolean>>(pathExists('foo.ts'));
expectType<boolean>(pathExists.sync('foo.ts'));
20 changes: 4 additions & 16 deletions license
Original file line number Diff line number Diff line change
@@ -1,21 +1,9 @@
The MIT License (MIT)
MIT License

Copyright (c) Sindre Sorhus <[email protected]> (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.
75 changes: 37 additions & 38 deletions package.json
Original file line number Diff line number Diff line change
@@ -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": "[email protected]",
"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": "[email protected]",
"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"
}
}
8 changes: 4 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Never use this before handling a file though:
## Install

```
$ npm install --save path-exists
$ npm install path-exists
```


Expand All @@ -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
});
})();
```


Expand Down
10 changes: 5 additions & 5 deletions test.js
Original file line number Diff line number Diff line change
@@ -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'));
});

0 comments on commit ee2d762

Please sign in to comment.