@nemigo/helpers 0.0.3 → 0.0.5
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/dist/explorer.d.ts +14 -0
- package/dist/explorer.js +21 -0
- package/package.json +10 -3
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { PathLike, Stats } from "node:fs";
|
|
2
|
+
/**
|
|
3
|
+
* Проверяет существование пути и его тип (файл или папка) через {@link stat}
|
|
4
|
+
*
|
|
5
|
+
* @param path - Путь для проверки
|
|
6
|
+
* @param [expected] - Ожидаемый тип: `"file"` (файл) или `"folder"` (папка)
|
|
7
|
+
* @param [strict=false] - Если `true`, выбрасывает ошибку, если путь не существует
|
|
8
|
+
*
|
|
9
|
+
* @returns {@link Stats}, если путь существует и соответствует ожидаемому типу (если указан). Иначе возвращает `null`, если `strict = false`
|
|
10
|
+
* @throws {Error} Если `strict = true` и путь не существует или не соответствует ожидаемому типу
|
|
11
|
+
*/
|
|
12
|
+
declare function exists(path: PathLike, expected?: "file" | "folder", strict?: boolean): Promise<Stats | null>;
|
|
13
|
+
declare function exists(path: PathLike, expected: "file" | "folder" | undefined, strict: true): Promise<Stats>;
|
|
14
|
+
export { exists };
|
package/dist/explorer.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { stat } from "node:fs/promises";
|
|
2
|
+
async function exists(path, expected, strict = false) {
|
|
3
|
+
let existed;
|
|
4
|
+
try {
|
|
5
|
+
existed = await stat(path);
|
|
6
|
+
}
|
|
7
|
+
catch {
|
|
8
|
+
if (strict) {
|
|
9
|
+
throw new Error(`Путь "${path}" не существует или к нему нет доступа`);
|
|
10
|
+
}
|
|
11
|
+
return null;
|
|
12
|
+
}
|
|
13
|
+
if (expected === "file" && existed.isDirectory()) {
|
|
14
|
+
throw new Error(`Ожидалось, что "${path}" будет файлом`);
|
|
15
|
+
}
|
|
16
|
+
else if (expected === "folder" && existed.isFile()) {
|
|
17
|
+
throw new Error(`Ожидалось, что "${path}" будет папкой`);
|
|
18
|
+
}
|
|
19
|
+
return existed;
|
|
20
|
+
}
|
|
21
|
+
export { exists };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nemigo/helpers",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.5",
|
|
4
4
|
"private": false,
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "Vlad Logvin",
|
|
@@ -12,13 +12,20 @@
|
|
|
12
12
|
"check": "tsc --noemit",
|
|
13
13
|
"lint": "eslint ./",
|
|
14
14
|
"test": "vitest --run",
|
|
15
|
-
"format": "prettier --write ./"
|
|
16
|
-
"publish": "npm publish"
|
|
15
|
+
"format": "prettier --write ./"
|
|
17
16
|
},
|
|
18
17
|
"exports": {
|
|
18
|
+
"./explorer": {
|
|
19
|
+
"types": "./dist/explorer.d.ts",
|
|
20
|
+
"default": "./dist/explorer.js"
|
|
21
|
+
},
|
|
19
22
|
"./msgpack": {
|
|
20
23
|
"types": "./dist/msgpack.d.ts",
|
|
21
24
|
"default": "./dist/msgpack.js"
|
|
25
|
+
},
|
|
26
|
+
"./random": {
|
|
27
|
+
"types": "./dist/random.d.ts",
|
|
28
|
+
"default": "./dist/random.js"
|
|
22
29
|
}
|
|
23
30
|
},
|
|
24
31
|
"dependencies": {
|