path-ok 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +37 -0
- package/package.json +44 -0
- package/src/index.js +40 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2016 Alex Stevovich
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# path-ok
|
|
2
|
+
|
|
3
|
+
**URL:**
|
|
4
|
+
[https://alexstevovich.com/repos/path-ok-nodejs](https://alexstevovich.com/repos/path-ok-nodejs)
|
|
5
|
+
|
|
6
|
+
Checks whether a filesystem path exists and is accessible. Returns a boolean instead of throwing. Designed to remove repetitive try/catch boilerplate when working with the Node.js filesystem.
|
|
7
|
+
|
|
8
|
+
---
|
|
9
|
+
|
|
10
|
+
## Installation
|
|
11
|
+
|
|
12
|
+
```sh
|
|
13
|
+
npm install path-ok
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## Example
|
|
17
|
+
|
|
18
|
+
```js
|
|
19
|
+
import pathOk from 'path-ok';
|
|
20
|
+
|
|
21
|
+
if (await pathOk('./config.json')) {
|
|
22
|
+
console.log('Config file exists');
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// Works for directories as well
|
|
26
|
+
const hasBuildDir = await pathOk('./build');
|
|
27
|
+
console.log(hasBuildDir);
|
|
28
|
+
// → true / false
|
|
29
|
+
|
|
30
|
+
// Non-existent paths
|
|
31
|
+
await pathOk('./does-not-exist.txt');
|
|
32
|
+
// → false
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## License
|
|
36
|
+
|
|
37
|
+
Licensed under the [MIT License](https://opensource.org/licenses/MIT).
|
package/package.json
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "path-ok",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"homepage": "https://alexstevovich.com/repos/path-ok-nodejs",
|
|
5
|
+
"author": {
|
|
6
|
+
"name": "Alex Stevovich",
|
|
7
|
+
"email": "dev@alexstevovich.com",
|
|
8
|
+
"url": "https://alexstevovich.com"
|
|
9
|
+
},
|
|
10
|
+
"description": "Checks whether a filesystem path exists and is accessible.",
|
|
11
|
+
"type": "module",
|
|
12
|
+
"exports": {
|
|
13
|
+
".": "./src/index.js"
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"./src"
|
|
17
|
+
],
|
|
18
|
+
"license": "MIT",
|
|
19
|
+
"scripts": {
|
|
20
|
+
"poshify": "npx poshify",
|
|
21
|
+
"pretty": "npx prettier --write .",
|
|
22
|
+
"lint": "eslint ./src/",
|
|
23
|
+
"lint:fix": "eslint --fix ./src/",
|
|
24
|
+
"test": "npx vitest --run",
|
|
25
|
+
"build": "npm run poshify && npm run pretty && npm run lint:fix && npm run lint"
|
|
26
|
+
},
|
|
27
|
+
"keywords": [
|
|
28
|
+
"path",
|
|
29
|
+
"filesystem",
|
|
30
|
+
"fs",
|
|
31
|
+
"exists",
|
|
32
|
+
"file",
|
|
33
|
+
"directory",
|
|
34
|
+
"utility"
|
|
35
|
+
],
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"eslint": "^9.36.0",
|
|
38
|
+
"eslint-plugin-import": "^2.32.0",
|
|
39
|
+
"eslint-plugin-prettier": "^5.5.4",
|
|
40
|
+
"poshify": "^1.0.0",
|
|
41
|
+
"prettier": "^3.6.2",
|
|
42
|
+
"vitest": "^3.2.4"
|
|
43
|
+
}
|
|
44
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* path-ok
|
|
3
|
+
* https://alexstevovich.com/repos/path-ok-nodejs
|
|
4
|
+
*
|
|
5
|
+
* Copyright (c) 2016 Alex Stevovich
|
|
6
|
+
*
|
|
7
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
8
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
9
|
+
* in the Software without restriction, including without limitation the rights
|
|
10
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
11
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
12
|
+
* furnished to do so, subject to the following conditions:
|
|
13
|
+
*
|
|
14
|
+
* The above copyright notice and this permission notice shall be included in
|
|
15
|
+
* all copies or substantial portions of the Software.
|
|
16
|
+
*
|
|
17
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
18
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
19
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
20
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
21
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
22
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
23
|
+
* THE SOFTWARE.
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
import fs from 'fs/promises';
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Checks if a file exists at the specified path.
|
|
30
|
+
* @param {string} filePath - The path of the file to check.
|
|
31
|
+
* @returns {Promise<boolean>} - Resolves to `true` if the file exists, `false` otherwise.
|
|
32
|
+
*/
|
|
33
|
+
export default async function pathOk(filePath) {
|
|
34
|
+
try {
|
|
35
|
+
await fs.access(filePath);
|
|
36
|
+
return true; // File exists
|
|
37
|
+
} catch (_) {
|
|
38
|
+
return false; // File does not exist
|
|
39
|
+
}
|
|
40
|
+
}
|