@visulima/fs 5.0.0-alpha.12 → 5.0.0-alpha.14
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/CHANGELOG.md +29 -0
- package/LICENSE.md +52 -4
- package/dist/eol.d.ts +30 -0
- package/dist/eol.js +3 -26
- package/dist/error.d.ts +243 -13
- package/dist/error.js +1 -7
- package/dist/glob-parent.d.ts +30 -0
- package/dist/glob-parent.js +1 -90
- package/dist/glob.d.ts +2 -3
- package/dist/glob.js +1 -2
- package/dist/index.d.ts +784 -7
- package/dist/index.js +1 -38
- package/dist/ini.d.ts +63 -3
- package/dist/ini.js +1 -4
- package/dist/is-glob.d.ts +29 -0
- package/dist/is-glob.js +1 -9
- package/dist/json5.d.ts +27 -3
- package/dist/json5.js +1 -4
- package/dist/jsonc.d.ts +58 -3
- package/dist/jsonc.js +1 -4
- package/dist/match.d.ts +44 -3
- package/dist/match.js +1 -15
- package/dist/packem_shared/AlreadyExistsError-CEu6_Tjb.js +1 -0
- package/dist/packem_shared/DirectoryError-ddlbUV8C.js +1 -0
- package/dist/packem_shared/F_OK-CAwY1qU7.js +1 -0
- package/dist/packem_shared/JSONError-D7h6PNWc.js +4 -0
- package/dist/packem_shared/NotEmptyError-DMh1o7UL.js +1 -0
- package/dist/packem_shared/NotFoundError-BPiW0icm.js +1 -0
- package/dist/packem_shared/PermissionError-B1Emi5a9.js +1 -0
- package/dist/packem_shared/WalkError-Olmh4908.js +1 -0
- package/dist/packem_shared/_commonjsHelpers-D6W6KoPK.js +1 -0
- package/dist/packem_shared/assertValidFileContents-BZFnXa7K.js +1 -0
- package/dist/packem_shared/assertValidFileOrDirectoryPath-DgPIPmZT.js +1 -0
- package/dist/packem_shared/build-rm-options-C1R4reBu.js +1 -0
- package/dist/packem_shared/collect-2KPTpCdX.js +1 -0
- package/dist/packem_shared/collectSync-BX55r_BH.js +1 -0
- package/dist/packem_shared/emptyDir-8utO028L.js +1 -0
- package/dist/packem_shared/emptyDirSync-Bk5fUX73.js +1 -0
- package/dist/packem_shared/ensureDir-CZ4oUFuo.js +1 -0
- package/dist/packem_shared/ensureDirSync-CoPWhbjM.js +1 -0
- package/dist/packem_shared/ensureFile-CF1cAAyh.js +1 -0
- package/dist/packem_shared/ensureFileSync-C3qR6naU.js +1 -0
- package/dist/packem_shared/ensureLink-LLLxlGHn.js +1 -0
- package/dist/packem_shared/ensureLinkSync-Dh_GtT8F.js +1 -0
- package/dist/packem_shared/ensureSymlink-D50kj2zl.js +1 -0
- package/dist/packem_shared/ensureSymlinkSync-BbVDPvI4.js +1 -0
- package/dist/packem_shared/findUp-BT07y_Me.js +1 -0
- package/dist/packem_shared/findUpSync-BvZiYsKJ.js +1 -0
- package/dist/packem_shared/get-file-info-type-CaWGXRjY.js +1 -0
- package/dist/packem_shared/glob-MHJQjR39.js +1 -0
- package/dist/packem_shared/glob-sync.d-p6lOZXnE.d.ts +54 -0
- package/dist/packem_shared/globSync-CwzlELgS.js +1 -0
- package/dist/packem_shared/index-CYIVJA0W.js +1 -0
- package/dist/packem_shared/index-DrapuBsB.js +1 -0
- package/dist/packem_shared/index-K-NNTkpb.js +11 -0
- package/dist/packem_shared/index-yGx8uzZs.js +1 -0
- package/dist/packem_shared/indexToLineColumn-BRSZFC6I-BKj3uzZt.js +6 -0
- package/dist/packem_shared/ini-preserve-CXc_xkpG.js +4 -0
- package/dist/packem_shared/is-stats-identical-CohsLtWy.js +1 -0
- package/dist/packem_shared/isAccessible-CDFxsAAo.js +1 -0
- package/dist/packem_shared/isAccessibleSync-5nzZWgin.js +1 -0
- package/dist/packem_shared/isFsCaseSensitive-CMZVY1A2.js +1 -0
- package/dist/packem_shared/json-error.d-DVILOyHc.d.ts +52 -0
- package/dist/packem_shared/jsonc-merge-CQMjx3Vw.js +2 -0
- package/dist/packem_shared/move-LmP6P9sn.js +1 -0
- package/dist/packem_shared/parseJson-Xx6vHYbv.js +1 -0
- package/dist/packem_shared/readFile-BAVMZg5e.js +1 -0
- package/dist/packem_shared/readFileSync-CGmzMUF2.js +1 -0
- package/dist/packem_shared/readIni-Cfl6HmTS.js +1 -0
- package/dist/packem_shared/readIniSync-DPg4UD0P.js +1 -0
- package/dist/packem_shared/readJson-kPx68Wv1.js +1 -0
- package/dist/packem_shared/readJson5-pXTL9nUE.js +1 -0
- package/dist/packem_shared/readJson5Sync-BedQc292.js +1 -0
- package/dist/packem_shared/readJsonSync-_HXNI3Se.js +1 -0
- package/dist/packem_shared/readJsonc-BzvuAXbT.js +1 -0
- package/dist/packem_shared/readJsoncSync-CVG1_QnX.js +1 -0
- package/dist/packem_shared/readToml-BO5CZK2P.js +1 -0
- package/dist/packem_shared/readTomlSync-1fKo0R52.js +1 -0
- package/dist/packem_shared/readYaml-DjK_Vqj1.js +1 -0
- package/dist/packem_shared/readYamlSync-C3h6KA2c.js +1 -0
- package/dist/packem_shared/remove-DH41Ov_c.js +1 -0
- package/dist/packem_shared/removeSync-50OEdnHd.js +1 -0
- package/dist/packem_shared/resolve-symlink-target-BofZSDIc.js +1 -0
- package/dist/packem_shared/sanitize-CU1xmyrg.js +1 -0
- package/dist/packem_shared/stripJsonComments-CzNR77MF.js +1 -0
- package/dist/packem_shared/to-uint-8-array-DwgaTasY.js +1 -0
- package/dist/packem_shared/types.d-BsmmADqx.d.ts +1660 -0
- package/dist/packem_shared/walk-D01Qmmvz.js +1 -0
- package/dist/packem_shared/walk-include-DCiVAwMP.js +1 -0
- package/dist/packem_shared/walkSync-BknSdmKD.js +1 -0
- package/dist/packem_shared/writeFile-DNGaDOMX.js +1 -0
- package/dist/packem_shared/writeFileSync-CE1i-eMA.js +1 -0
- package/dist/packem_shared/writeIni-DG7U4ji0.js +4 -0
- package/dist/packem_shared/writeIniSync-Ldt6HFLb.js +4 -0
- package/dist/packem_shared/writeJson-BkGlq1Kv.js +4 -0
- package/dist/packem_shared/writeJson5-Btud4sQh.js +4 -0
- package/dist/packem_shared/writeJson5Sync-Iln3i1IV.js +4 -0
- package/dist/packem_shared/writeJsonSync-HndmyvEK.js +4 -0
- package/dist/packem_shared/writeJsonc-9oiamG16.js +4 -0
- package/dist/packem_shared/writeJsoncSync-BHBziPiK.js +4 -0
- package/dist/packem_shared/writeToml-Cj57wZ8B.js +1 -0
- package/dist/packem_shared/writeTomlSync-B34ZZp0e.js +1 -0
- package/dist/packem_shared/writeYaml-BhQX2yN9.js +1 -0
- package/dist/packem_shared/writeYamlSync-DG01FBeC.js +1 -0
- package/dist/size.d.ts +244 -0
- package/dist/size.js +1 -146
- package/dist/toml.d.ts +56 -2
- package/dist/toml.js +1 -4
- package/dist/utils.d.ts +91 -3
- package/dist/utils.js +1 -6
- package/dist/yaml.d.ts +2 -3
- package/dist/yaml.js +1 -4
- package/package.json +39 -40
- package/dist/packem_shared/AlreadyExistsError-6Y9hjgMn.js +0 -27
- package/dist/packem_shared/DirectoryError-BfYPkIYP.js +0 -27
- package/dist/packem_shared/F_OK-MldBaGxb.js +0 -8
- package/dist/packem_shared/JSONError-BkHRnInH.js +0 -26
- package/dist/packem_shared/NotEmptyError-B21RUlVr.js +0 -27
- package/dist/packem_shared/NotFoundError-BIp61X6k.js +0 -27
- package/dist/packem_shared/PermissionError-CjoiJgip.js +0 -27
- package/dist/packem_shared/WalkError-DER5A43G.js +0 -24
- package/dist/packem_shared/_commonjsHelpers-BqLXS_qQ.js +0 -5
- package/dist/packem_shared/assertValidFileContents-BmcLtsGd.js +0 -7
- package/dist/packem_shared/assertValidFileOrDirectoryPath-8HANmVjk.js +0 -7
- package/dist/packem_shared/build-rm-options-Cl3VDY4O.js +0 -12
- package/dist/packem_shared/collect-CqQ3eVab.js +0 -14
- package/dist/packem_shared/collectSync-noE08NNv.js +0 -14
- package/dist/packem_shared/emptyDir-DnwENOaV.js +0 -46
- package/dist/packem_shared/emptyDirSync-6WiHBo8a.js +0 -43
- package/dist/packem_shared/ensureDir-DNlPftys.js +0 -53
- package/dist/packem_shared/ensureDirSync-DcPZFMy5.js +0 -53
- package/dist/packem_shared/ensureFile-BRQpUOUM.js +0 -47
- package/dist/packem_shared/ensureFileSync-SBY7A9-C.js +0 -47
- package/dist/packem_shared/ensureLink-DJQeauEn.js +0 -55
- package/dist/packem_shared/ensureLinkSync-D9RVAglV.js +0 -55
- package/dist/packem_shared/ensureSymlink-CoRYDRwr.js +0 -80
- package/dist/packem_shared/ensureSymlinkSync-CFimFlgh.js +0 -81
- package/dist/packem_shared/findUp-YcNLKdfV.js +0 -84
- package/dist/packem_shared/findUpSync-BfA5fheN.js +0 -83
- package/dist/packem_shared/get-file-info-type-FD4-jsyg.js +0 -14
- package/dist/packem_shared/glob-DY8l7izD.js +0 -5
- package/dist/packem_shared/glob-sync.d-BlGjS68e.d.ts +0 -4
- package/dist/packem_shared/globSync-5P2KJf_g.js +0 -5
- package/dist/packem_shared/index-C8W8sfzP.js +0 -830
- package/dist/packem_shared/index-CHM-in-V.js +0 -100
- package/dist/packem_shared/index-bhgnhm4u.js +0 -167
- package/dist/packem_shared/index-xe4o3cYi.js +0 -1784
- package/dist/packem_shared/indexToLineColumn-BUb0GPKl-BF770uX7.js +0 -171
- package/dist/packem_shared/ini-preserve-Dq_Q_Jgb.js +0 -251
- package/dist/packem_shared/is-stats-identical-D8FxpvQU.js +0 -3
- package/dist/packem_shared/isAccessible-DuVrTNFV.js +0 -38
- package/dist/packem_shared/isAccessibleSync-DI8mM0fA.js +0 -38
- package/dist/packem_shared/isFsCaseSensitive-DunL7Iry.js +0 -62
- package/dist/packem_shared/json-error.d-DgKaeuIf.d.ts +0 -10
- package/dist/packem_shared/jsonc-merge-C6jWcfWh.js +0 -72
- package/dist/packem_shared/move-DbpW5_vA.js +0 -134
- package/dist/packem_shared/parseJson-BIp89Xjo.js +0 -63
- package/dist/packem_shared/readFile-Iz7kvCCk.js +0 -65
- package/dist/packem_shared/readFileSync-gm9yfIhh.js +0 -54
- package/dist/packem_shared/readIni-CJ2xfpjB.js +0 -10
- package/dist/packem_shared/readIniSync-9LUjaSSp.js +0 -10
- package/dist/packem_shared/readJson-C_rwm-wq.js +0 -22
- package/dist/packem_shared/readJson5-mBoTZppy.js +0 -15
- package/dist/packem_shared/readJson5Sync-8WS2yUok.js +0 -15
- package/dist/packem_shared/readJsonSync-BMEmCcJ6.js +0 -22
- package/dist/packem_shared/readJsonc-j-OS-APg.js +0 -28
- package/dist/packem_shared/readJsoncSync-Aw6EMlWO.js +0 -16
- package/dist/packem_shared/readToml-B2MHaSes.js +0 -9
- package/dist/packem_shared/readTomlSync-DNbK8NEb.js +0 -9
- package/dist/packem_shared/readYaml-D5q2a0e1.js +0 -10
- package/dist/packem_shared/readYamlSync-BKX-PAfq.js +0 -10
- package/dist/packem_shared/remove-C8_gl3jF.js +0 -39
- package/dist/packem_shared/removeSync-BJR_wTwN.js +0 -39
- package/dist/packem_shared/resolve-symlink-target-Kh4GovFf.js +0 -16
- package/dist/packem_shared/sanitize-BZoLN9Fg.js +0 -205
- package/dist/packem_shared/stripJsonComments-vo4k0mpF.js +0 -19
- package/dist/packem_shared/to-uint-8-array-Dz2nF1y1.js +0 -19
- package/dist/packem_shared/types.d-CYpAhWov.d.ts +0 -145
- package/dist/packem_shared/walk-CSZgCuDx.js +0 -124
- package/dist/packem_shared/walk-include-CZco7BvN.js +0 -16
- package/dist/packem_shared/walkSync-C4Cy28xb.js +0 -123
- package/dist/packem_shared/writeFile-B8HX0JIQ.js +0 -81
- package/dist/packem_shared/writeFileSync-DB8vv8xR.js +0 -81
- package/dist/packem_shared/writeIni-i55QrL8y.js +0 -65
- package/dist/packem_shared/writeIniSync-CLqJE5t3.js +0 -64
- package/dist/packem_shared/writeJson-Bo7C2UHw.js +0 -50
- package/dist/packem_shared/writeJson5-vVmHUQOd.js +0 -53
- package/dist/packem_shared/writeJson5Sync-oJ546h5k.js +0 -53
- package/dist/packem_shared/writeJsonSync-BKZ_9Acr.js +0 -50
- package/dist/packem_shared/writeJsonc-BufH_oC5.js +0 -60
- package/dist/packem_shared/writeJsoncSync-DB9be1YL.js +0 -59
- package/dist/packem_shared/writeToml-DgTW_-7F.js +0 -8
- package/dist/packem_shared/writeTomlSync-DiBiE9ja.js +0 -8
- package/dist/packem_shared/writeYaml-BWCOome-.js +0 -24
- package/dist/packem_shared/writeYamlSync-DBVnXmuY.js +0 -24
package/dist/index.d.ts
CHANGED
|
@@ -1,28 +1,334 @@
|
|
|
1
|
-
import { q as WalkOptions, F as FindUpName, r as FindUpOptions, s as FindUpNameSync, t as WalkEntry, u as ReadFileOptions, v as ContentType, w as ReadJsonOptions,
|
|
2
|
-
export { type A as CodeFrameLocation, type
|
|
1
|
+
import { q as WalkOptions, F as FindUpName, r as FindUpOptions, s as FindUpNameSync, t as WalkEntry, u as ReadFileOptions, v as ContentType, w as ReadJsonOptions, c as JsonReviver, x as RetryOptions, y as WriteFileOptions, z as WriteJsonOptions } from "./packem_shared/types.d-BsmmADqx.js";
|
|
2
|
+
export { type A as CodeFrameLocation, type b as CodeFrameOptions, B as FIND_UP_STOP, D as F_OK, type E as FindUpNameFnResult, type H as FindUpNameSyncFnResult, type G as GlobOptions, type o as JsonReplacer, K as R_OK, type L as ReadFileEncoding, M as W_OK, X as X_OK } from "./packem_shared/types.d-BsmmADqx.js";
|
|
3
3
|
import { symlink } from 'node:fs';
|
|
4
4
|
export { CRLF, EOL, LF, detect, format } from "./eol.js";
|
|
5
|
-
export { g as glob, a as globSync } from "./packem_shared/glob-sync.d-
|
|
5
|
+
export { g as glob, a as globSync } from "./packem_shared/glob-sync.d-p6lOZXnE.js";
|
|
6
6
|
export { type GlobParentOptions, default as globParent } from "./glob-parent.js";
|
|
7
7
|
export { type IsGlobOptions, default as isGlob } from "./is-glob.js";
|
|
8
8
|
export { type MatchOptions, match, matcher } from "./match.js";
|
|
9
9
|
import { JsonValue } from 'type-fest';
|
|
10
|
-
|
|
10
|
+
import fs from 'fs';
|
|
11
11
|
import 'tinyglobby';
|
|
12
|
-
import './
|
|
13
|
-
|
|
12
|
+
import './lib/picomatch';
|
|
13
|
+
/**
|
|
14
|
+
* Ensures that the directory exists.
|
|
15
|
+
* If the directory structure does not exist, it is created. Like mkdir -p.
|
|
16
|
+
* @param directory The path to the directory to ensure exists.
|
|
17
|
+
* @example
|
|
18
|
+
* ```javascript
|
|
19
|
+
* import ensureDir from "@visulima/fs/ensure/ensure-dir";
|
|
20
|
+
*
|
|
21
|
+
* await ensureDir("/tmp/foo/bar/baz");
|
|
22
|
+
* // Creates the directory structure /tmp/foo/bar/baz if it doesn't exist
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
14
25
|
declare const ensureDir: (directory: URL | string) => Promise<void>;
|
|
26
|
+
/**
|
|
27
|
+
* Ensures that the directory exists.
|
|
28
|
+
* If the directory structure does not exist, it is created. Like mkdir -p.
|
|
29
|
+
* @param directory The path to the directory to ensure exists.
|
|
30
|
+
* @example
|
|
31
|
+
* ```javascript
|
|
32
|
+
* import ensureDirSync from "@visulima/fs/ensure/ensure-dir-sync";
|
|
33
|
+
*
|
|
34
|
+
* ensureDirSync("/tmp/foo/bar/baz");
|
|
35
|
+
* // Creates the directory structure /tmp/foo/bar/baz if it doesn't exist
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
15
38
|
declare const ensureDirSync: (directory: URL | string) => void;
|
|
39
|
+
/**
|
|
40
|
+
* Asynchronously ensures that a file exists.
|
|
41
|
+
* If the directory structure for the file does not exist, it is created.
|
|
42
|
+
* If the file already exists, it is not modified.
|
|
43
|
+
* @param filePath The path to the file. Can be a string or a URL object.
|
|
44
|
+
* @returns A Promise that resolves when the file has been created or confirmed to exist.
|
|
45
|
+
* @throws Will throw an error if the path exists and is not a file.
|
|
46
|
+
* @throws Will throw an error if directory or file creation fails for reasons other than the path not existing initially.
|
|
47
|
+
* @example
|
|
48
|
+
* ```typescript
|
|
49
|
+
* import { ensureFile } from "@visulima/fs";
|
|
50
|
+
*
|
|
51
|
+
* (async () => {
|
|
52
|
+
* try {
|
|
53
|
+
* await ensureFile("path/to/my/file.txt");
|
|
54
|
+
* console.log("File ensured!");
|
|
55
|
+
*
|
|
56
|
+
* await ensureFile(new URL("file:///path/to/another/file.log"));
|
|
57
|
+
* console.log("Another file ensured!");
|
|
58
|
+
* } catch (error) {
|
|
59
|
+
* console.error("Failed to ensure file:", error);
|
|
60
|
+
* }
|
|
61
|
+
* })();
|
|
62
|
+
* ```
|
|
63
|
+
*/
|
|
16
64
|
declare const ensureFile: (filePath: URL | string) => Promise<void>;
|
|
65
|
+
/**
|
|
66
|
+
* Ensures that the file exists.
|
|
67
|
+
* If the file that is requested to be created is in directories that do not exist,
|
|
68
|
+
* these directories are created. If the file already exists, it is NOT MODIFIED.
|
|
69
|
+
* @param filePath The path to the file to ensure exists.
|
|
70
|
+
* @example
|
|
71
|
+
* ```javascript
|
|
72
|
+
* import { ensureFileSync } from "@visulima/fs";
|
|
73
|
+
*
|
|
74
|
+
* ensureFileSync("/tmp/foo/bar/baz.txt");
|
|
75
|
+
* // Creates the file /tmp/foo/bar/baz.txt and any missing parent directories if they don't exist
|
|
76
|
+
* ```
|
|
77
|
+
*/
|
|
17
78
|
declare const ensureFileSync: (filePath: URL | string) => void;
|
|
79
|
+
/**
|
|
80
|
+
* Ensures that the hard link exists.
|
|
81
|
+
* If the directory structure does not exist, it is created.
|
|
82
|
+
* @param source The path to the source file or directory.
|
|
83
|
+
* @param destination The path to the destination link.
|
|
84
|
+
* @example
|
|
85
|
+
* ```javascript
|
|
86
|
+
* import { ensureLink } from "@visulima/fs";
|
|
87
|
+
* import { join } from "node:path";
|
|
88
|
+
*
|
|
89
|
+
* // ensure the link /tmp/foo/bar-link.txt points to /tmp/foo/bar.txt
|
|
90
|
+
* await ensureLink(join("/tmp", "foo", "bar.txt"), join("/tmp", "foo", "bar-link.txt"));
|
|
91
|
+
* ```
|
|
92
|
+
*/
|
|
18
93
|
declare const ensureLink: (source: URL | string, destination: URL | string) => Promise<void>;
|
|
94
|
+
/**
|
|
95
|
+
* Ensures that the hard link exists.
|
|
96
|
+
* If the directory structure does not exist, it is created.
|
|
97
|
+
* @param source The path to the source file or directory.
|
|
98
|
+
* @param destination The path to the destination link.
|
|
99
|
+
* @example
|
|
100
|
+
* ```javascript
|
|
101
|
+
* import { ensureLinkSync } from "@visulima/fs";
|
|
102
|
+
* import { join } from "node:path";
|
|
103
|
+
*
|
|
104
|
+
* // ensure the link /tmp/foo/bar-link.txt points to /tmp/foo/bar.txt
|
|
105
|
+
* ensureLinkSync(join("/tmp", "foo", "bar.txt"), join("/tmp", "foo", "bar-link.txt"));
|
|
106
|
+
* ```
|
|
107
|
+
*/
|
|
19
108
|
declare const ensureLinkSync: (source: URL | string, destination: URL | string) => void;
|
|
109
|
+
/**
|
|
110
|
+
* Ensures that the link exists, and points to a valid file.
|
|
111
|
+
* If the directory structure does not exist, it is created.
|
|
112
|
+
* If the link already exists, it is not modified but error is thrown if it is not point to the given target.
|
|
113
|
+
* @param target the source file path
|
|
114
|
+
* @param linkName the destination link path
|
|
115
|
+
* @param type the type of the symlink, or null to use automatic detection
|
|
116
|
+
* @returns A void promise that resolves once the link exists.
|
|
117
|
+
* @example
|
|
118
|
+
* ```javascript
|
|
119
|
+
* import { ensureSymlink } from "@visulima/fs";
|
|
120
|
+
* import { join } from "node:path";
|
|
121
|
+
*
|
|
122
|
+
* // Ensure a symlink /tmp/foo/link-to-bar.txt points to /tmp/foo/bar.txt
|
|
123
|
+
* await ensureSymlink(join("/tmp", "foo", "bar.txt"), join("/tmp", "foo", "link-to-bar.txt"));
|
|
124
|
+
*
|
|
125
|
+
* // Ensure a directory symlink /tmp/foo/link-to-baz-dir points to /tmp/foo/baz-dir
|
|
126
|
+
* await ensureSymlink(join("/tmp", "foo", "baz-dir"), join("/tmp", "foo", "link-to-baz-dir"), "dir");
|
|
127
|
+
* ```
|
|
128
|
+
*/
|
|
20
129
|
declare const ensureSymlink: (target: URL | string, linkName: URL | string, type?: symlink.Type) => Promise<void>;
|
|
130
|
+
/**
|
|
131
|
+
* Ensures that the link exists, and points to a valid file.
|
|
132
|
+
* If the directory structure does not exist, it is created.
|
|
133
|
+
* If the link already exists, it is not modified but error is thrown if it is not point to the given target.
|
|
134
|
+
* @param target the source file path
|
|
135
|
+
* @param linkName the destination link path
|
|
136
|
+
* @param type the type of the symlink, or null to use automatic detection
|
|
137
|
+
* @returns A void.
|
|
138
|
+
* @example
|
|
139
|
+
* ```javascript
|
|
140
|
+
* import { ensureSymlinkSync } from "@visulima/fs";
|
|
141
|
+
* import { join } from "node:path";
|
|
142
|
+
*
|
|
143
|
+
* // Ensure a symlink /tmp/foo/link-to-bar.txt points to /tmp/foo/bar.txt
|
|
144
|
+
* ensureSymlinkSync(join("/tmp", "foo", "bar.txt"), join("/tmp", "foo", "link-to-bar.txt"));
|
|
145
|
+
*
|
|
146
|
+
* // Ensure a directory symlink /tmp/foo/link-to-baz-dir points to /tmp/foo/baz-dir
|
|
147
|
+
* ensureSymlinkSync(join("/tmp", "foo", "baz-dir"), join("/tmp", "foo", "link-to-baz-dir"), "dir");
|
|
148
|
+
* ```
|
|
149
|
+
*/
|
|
21
150
|
declare const ensureSymlinkSync: (target: URL | string, linkName: URL | string, type?: symlink.Type) => void;
|
|
151
|
+
/**
|
|
152
|
+
* Asynchronously collects all file paths within a directory that match the specified criteria.
|
|
153
|
+
* By default, it searches for JavaScript and TypeScript file extensions.
|
|
154
|
+
* @param directory The root directory to start collecting files from.
|
|
155
|
+
* @param options Optional configuration to control the collection process. See {@link WalkOptions}.
|
|
156
|
+
* @returns A promise that resolves to an array of absolute file paths.
|
|
157
|
+
* @example
|
|
158
|
+
* ```javascript
|
|
159
|
+
* import { collect } from "@visulima/fs";
|
|
160
|
+
* import { join } from "node:path";
|
|
161
|
+
*
|
|
162
|
+
* const collectFiles = async () => {
|
|
163
|
+
* // Collect all .txt and .md files in /tmp/docs, up to 2 levels deep
|
|
164
|
+
* const files = await collect(join("/tmp", "docs"), {
|
|
165
|
+
* extensions: ["txt", "md"],
|
|
166
|
+
* maxDepth: 2,
|
|
167
|
+
* includeDirs: false, // Only collect files
|
|
168
|
+
* });
|
|
169
|
+
* console.log(files);
|
|
170
|
+
* // Example output: ['/tmp/docs/file1.txt', '/tmp/docs/subdir/report.md']
|
|
171
|
+
*
|
|
172
|
+
* // Collect all .js files, excluding anything in node_modules
|
|
173
|
+
* const jsFiles = await collect(join("/tmp", "project"), {
|
|
174
|
+
* extensions: ["js"],
|
|
175
|
+
* skip: [/node_modules/],
|
|
176
|
+
* });
|
|
177
|
+
* console.log(jsFiles);
|
|
178
|
+
* };
|
|
179
|
+
*
|
|
180
|
+
* collectFiles();
|
|
181
|
+
* ```
|
|
182
|
+
*/
|
|
22
183
|
declare const collect: (directory: string, options?: WalkOptions) => Promise<string[]>;
|
|
184
|
+
/**
|
|
185
|
+
* Synchronously collects all file paths within a directory that match the specified criteria.
|
|
186
|
+
* By default, it searches for JavaScript and TypeScript file extensions.
|
|
187
|
+
* @param directory The root directory to start collecting files from.
|
|
188
|
+
* @param options Optional configuration to control the collection process. See {@link WalkOptions}.
|
|
189
|
+
* @returns An array of absolute file paths.
|
|
190
|
+
* @example
|
|
191
|
+
* ```javascript
|
|
192
|
+
* import { collectSync } from "@visulima/fs";
|
|
193
|
+
* import { join } from "node:path";
|
|
194
|
+
*
|
|
195
|
+
* // Collect all .txt and .md files in /tmp/docs, up to 2 levels deep
|
|
196
|
+
* const files = collectSync(join("/tmp", "docs"), {
|
|
197
|
+
* extensions: ["txt", "md"],
|
|
198
|
+
* maxDepth: 2,
|
|
199
|
+
* includeDirs: false, // Only collect files
|
|
200
|
+
* });
|
|
201
|
+
* console.log(files);
|
|
202
|
+
* // Example output: ['/tmp/docs/file1.txt', '/tmp/docs/subdir/report.md']
|
|
203
|
+
*
|
|
204
|
+
* // Collect all .js files, excluding anything in node_modules
|
|
205
|
+
* const jsFiles = collectSync(join("/tmp", "project"), {
|
|
206
|
+
* extensions: ["js"],
|
|
207
|
+
* skip: [/node_modules/],
|
|
208
|
+
* });
|
|
209
|
+
* console.log(jsFiles);
|
|
210
|
+
* ```
|
|
211
|
+
*/
|
|
23
212
|
declare const collectSync: (directory: string, options?: WalkOptions) => string[];
|
|
213
|
+
/**
|
|
214
|
+
* Asynchronously finds a file or directory by walking up parent directories.
|
|
215
|
+
* @param name The name(s) of the file or directory to find. Can be a string, an array of strings, or a function that returns a name or `FIND_UP_STOP`.
|
|
216
|
+
* @param options Optional configuration for the search. See {@link FindUpOptions}.
|
|
217
|
+
* @returns A promise that resolves to the absolute path of the first found file/directory, or `undefined` if not found.
|
|
218
|
+
* @example
|
|
219
|
+
* ```javascript
|
|
220
|
+
* import { findUp } from "@visulima/fs";
|
|
221
|
+
* import { join } from "node:path";
|
|
222
|
+
*
|
|
223
|
+
* const findProjectRoot = async () => {
|
|
224
|
+
* // Find the closest package.json, starting from /tmp/foo/bar/baz
|
|
225
|
+
* const projectRoot = await findUp("package.json", {
|
|
226
|
+
* cwd: join("/tmp", "foo", "bar", "baz"),
|
|
227
|
+
* type: "file",
|
|
228
|
+
* });
|
|
229
|
+
* console.log(projectRoot); // e.g., /tmp/foo/package.json or undefined
|
|
230
|
+
*
|
|
231
|
+
* // Find the closest .git directory or a README.md file
|
|
232
|
+
* const gitDirOrReadme = await findUp([".git", "README.md"], {
|
|
233
|
+
* cwd: join("/tmp", "foo", "bar"),
|
|
234
|
+
* });
|
|
235
|
+
* console.log(gitDirOrReadme);
|
|
236
|
+
*
|
|
237
|
+
* // Find using a custom function, stopping at /tmp
|
|
238
|
+
* const customFound = await findUp(
|
|
239
|
+
* (directory) => {
|
|
240
|
+
* if (directory === join("/tmp", "foo")) {
|
|
241
|
+
* return "found-it-here.txt"; // Pretend this file exists in /tmp/foo
|
|
242
|
+
* }
|
|
243
|
+
* return undefined;
|
|
244
|
+
* },
|
|
245
|
+
* {
|
|
246
|
+
* cwd: join("/tmp", "foo", "bar", "baz"),
|
|
247
|
+
* stopAt: join("/tmp"),
|
|
248
|
+
* }
|
|
249
|
+
* );
|
|
250
|
+
* console.log(customFound);
|
|
251
|
+
* };
|
|
252
|
+
*
|
|
253
|
+
* findProjectRoot();
|
|
254
|
+
* ```
|
|
255
|
+
*/
|
|
24
256
|
declare const findUp: (name: FindUpName, options?: FindUpOptions) => Promise<string | undefined>;
|
|
257
|
+
/**
|
|
258
|
+
* Synchronously finds a file or directory by walking up parent directories.
|
|
259
|
+
* @param name The name(s) of the file or directory to find. Can be a string, an array of strings, or a function that returns a name or `FIND_UP_STOP`.
|
|
260
|
+
* @param options Optional configuration for the search. See {@link FindUpOptions}.
|
|
261
|
+
* @returns The absolute path of the first found file/directory, or `undefined` if not found.
|
|
262
|
+
* @example
|
|
263
|
+
* ```javascript
|
|
264
|
+
* import { findUpSync } from "@visulima/fs";
|
|
265
|
+
* import { join } from "node:path";
|
|
266
|
+
*
|
|
267
|
+
* // Find the closest package.json, starting from /tmp/foo/bar/baz
|
|
268
|
+
* const projectRoot = findUpSync("package.json", {
|
|
269
|
+
* cwd: join("/tmp", "foo", "bar", "baz"),
|
|
270
|
+
* type: "file",
|
|
271
|
+
* });
|
|
272
|
+
* console.log(projectRoot); // e.g., /tmp/foo/package.json or undefined
|
|
273
|
+
*
|
|
274
|
+
* // Find the closest .git directory or a README.md file
|
|
275
|
+
* const gitDirOrReadme = findUpSync([".git", "README.md"], {
|
|
276
|
+
* cwd: join("/tmp", "foo", "bar"),
|
|
277
|
+
* });
|
|
278
|
+
* console.log(gitDirOrReadme);
|
|
279
|
+
*
|
|
280
|
+
* // Find using a custom function, stopping at /tmp
|
|
281
|
+
* const customFound = findUpSync(
|
|
282
|
+
* (directory) => {
|
|
283
|
+
* if (directory === join("/tmp", "foo")) {
|
|
284
|
+
* return "found-it-here.txt"; // Pretend this file exists in /tmp/foo
|
|
285
|
+
* }
|
|
286
|
+
* return undefined;
|
|
287
|
+
* },
|
|
288
|
+
* {
|
|
289
|
+
* cwd: join("/tmp", "foo", "bar", "baz"),
|
|
290
|
+
* stopAt: join("/tmp"),
|
|
291
|
+
* }
|
|
292
|
+
* );
|
|
293
|
+
* console.log(customFound);
|
|
294
|
+
* ```
|
|
295
|
+
*/
|
|
25
296
|
declare const findUpSync: (name: FindUpNameSync, options?: FindUpOptions) => string | undefined;
|
|
297
|
+
/**
|
|
298
|
+
* Asynchronously walks the file tree rooted at `directory`, yielding each file or directory that matches the criteria specified in `options`.
|
|
299
|
+
* @param directory The root directory to start walking from.
|
|
300
|
+
* @param options Optional configuration to control the walking process. See {@link WalkOptions}.
|
|
301
|
+
* @param options.extensions List of file extensions used to filter entries.
|
|
302
|
+
* @param options.followSymlinks Indicates whether symlinks should be resolved or not.
|
|
303
|
+
* @param options.includeDirs Indicates whether directory entries should be included or not.
|
|
304
|
+
* @param options.includeFiles Indicates whether file entries should be included or not.
|
|
305
|
+
* @param options.includeSymlinks Indicates whether symlink entries should be included or not.
|
|
306
|
+
* @param options.match List of regular expression or glob patterns used to filter entries.
|
|
307
|
+
* @param options.maxDepth Maximum depth to walk. Defaults to infinity.
|
|
308
|
+
* @param options.skip List of regular expression or glob patterns used to skip entries.
|
|
309
|
+
* @returns An async iterable iterator yielding {@link WalkEntry} objects for each matching file or directory.
|
|
310
|
+
* @example
|
|
311
|
+
* ```javascript
|
|
312
|
+
* import { walk } from "@visulima/fs";
|
|
313
|
+
* import { join } from "node:path";
|
|
314
|
+
*
|
|
315
|
+
* const printEntries = async () => {
|
|
316
|
+
* // Walk through /tmp/my-project, looking for .ts files, max depth 2
|
|
317
|
+
* for await (const entry of walk(join("/tmp", "my-project"), { extensions: ["ts"], maxDepth: 2 })) {
|
|
318
|
+
* console.log(`Found: ${entry.path} (Type: ${entry.isFile() ? 'file' : 'directory'})`);
|
|
319
|
+
* }
|
|
320
|
+
*
|
|
321
|
+
* // Walk, including only directories, and skip any node_modules folders
|
|
322
|
+
* for await (const entry of walk(join("/tmp", "another-project"), { includeFiles: false, skip: [/node_modules/] })) {
|
|
323
|
+
* if (entry.isDirectory()) {
|
|
324
|
+
* console.log(`Directory: ${entry.path}`);
|
|
325
|
+
* }
|
|
326
|
+
* }
|
|
327
|
+
* };
|
|
328
|
+
*
|
|
329
|
+
* printEntries();
|
|
330
|
+
* ```
|
|
331
|
+
*/
|
|
26
332
|
declare function walk(directory: URL | string, {
|
|
27
333
|
extensions,
|
|
28
334
|
followSymlinks,
|
|
@@ -33,6 +339,38 @@ declare function walk(directory: URL | string, {
|
|
|
33
339
|
maxDepth,
|
|
34
340
|
skip
|
|
35
341
|
}?: WalkOptions): AsyncIterableIterator<WalkEntry>;
|
|
342
|
+
/**
|
|
343
|
+
* Synchronously walks the file tree rooted at `directory`, yielding each file or directory that matches the criteria specified in `options`.
|
|
344
|
+
* This is the synchronous version of the async walk function.
|
|
345
|
+
* @param directory The root directory to start walking from.
|
|
346
|
+
* @param options Optional configuration to control the walking process. See {@link WalkOptions}.
|
|
347
|
+
* @param options.extensions List of file extensions used to filter entries.
|
|
348
|
+
* @param options.followSymlinks Indicates whether symlinks should be resolved or not.
|
|
349
|
+
* @param options.includeDirs Indicates whether directory entries should be included or not.
|
|
350
|
+
* @param options.includeFiles Indicates whether file entries should be included or not.
|
|
351
|
+
* @param options.includeSymlinks Indicates whether symlink entries should be included or not.
|
|
352
|
+
* @param options.match List of regular expression or glob patterns used to filter entries.
|
|
353
|
+
* @param options.maxDepth Maximum depth to walk. Defaults to infinity.
|
|
354
|
+
* @param options.skip List of regular expression or glob patterns used to skip entries.
|
|
355
|
+
* @returns An iterable iterator yielding {@link WalkEntry} objects for each matching file or directory.
|
|
356
|
+
* @example
|
|
357
|
+
* ```javascript
|
|
358
|
+
* import { walkSync } from "@visulima/fs";
|
|
359
|
+
* import { join } from "node:path";
|
|
360
|
+
*
|
|
361
|
+
* // Walk through /tmp/my-project, looking for .ts files, max depth 2
|
|
362
|
+
* for (const entry of walkSync(join("/tmp", "my-project"), { extensions: ["ts"], maxDepth: 2 })) {
|
|
363
|
+
* console.log(`Found: ${entry.path} (Type: ${entry.isFile() ? 'file' : 'directory'})`);
|
|
364
|
+
* }
|
|
365
|
+
*
|
|
366
|
+
* // Walk, including only directories, and skip any node_modules folders
|
|
367
|
+
* for (const entry of walkSync(join("/tmp", "another-project"), { includeFiles: false, skip: [/node_modules/] })) {
|
|
368
|
+
* if (entry.isDirectory()) {
|
|
369
|
+
* console.log(`Directory: ${entry.path}`);
|
|
370
|
+
* }
|
|
371
|
+
* }
|
|
372
|
+
* ```
|
|
373
|
+
*/
|
|
36
374
|
declare function walkSync(directory: URL | string, {
|
|
37
375
|
extensions,
|
|
38
376
|
followSymlinks,
|
|
@@ -43,39 +381,478 @@ declare function walkSync(directory: URL | string, {
|
|
|
43
381
|
maxDepth,
|
|
44
382
|
skip
|
|
45
383
|
}?: WalkOptions): IterableIterator<WalkEntry>;
|
|
384
|
+
/**
|
|
385
|
+
* Asynchronously tests a user's permissions for the file or directory specified by path.
|
|
386
|
+
* Returns a Promise that resolves to `true` if the accessibility check is successful, `false` otherwise.
|
|
387
|
+
* @param path The path to the file or directory. Can be a string or a URL object.
|
|
388
|
+
* @param mode The accessibility checks to perform. Defaults to `F_OK` (check for existence).
|
|
389
|
+
* Other possible values include `R_OK` (check for read access), `W_OK` (check for write access),
|
|
390
|
+
* and `X_OK` (check for execute/search access). Multiple modes can be combined using bitwise OR.
|
|
391
|
+
* @returns A Promise that resolves to a boolean indicating if the path is accessible with the specified mode.
|
|
392
|
+
* @example
|
|
393
|
+
* ```typescript
|
|
394
|
+
* import { isAccessible, F_OK, R_OK } from "@visulima/fs";
|
|
395
|
+
*
|
|
396
|
+
* (async () => {
|
|
397
|
+
* if (await isAccessible("myFile.txt")) {
|
|
398
|
+
* console.log("myFile.txt exists");
|
|
399
|
+
* }
|
|
400
|
+
*
|
|
401
|
+
* if (await isAccessible("myFile.txt", R_OK)) {
|
|
402
|
+
* console.log("myFile.txt is readable");
|
|
403
|
+
* }
|
|
404
|
+
*
|
|
405
|
+
* if (await isAccessible("myDirectory", F_OK | R_OK | W_OK)) {
|
|
406
|
+
* console.log("myDirectory exists, is readable and writable");
|
|
407
|
+
* }
|
|
408
|
+
* })();
|
|
409
|
+
* ```
|
|
410
|
+
*/
|
|
46
411
|
declare function isAccessible(path: URL | string, mode?: number): Promise<boolean>;
|
|
412
|
+
/** Returns a boolean indicating if the path is accessible or not. */
|
|
47
413
|
declare function isAccessibleSync(path: URL | string, mode?: number): boolean;
|
|
48
414
|
type Options = {
|
|
415
|
+
/**
|
|
416
|
+
* The working directory to find source files.
|
|
417
|
+
* The source and destination path are relative to this.
|
|
418
|
+
* @default process.cwd()
|
|
419
|
+
*/
|
|
49
420
|
cwd?: URL | string;
|
|
421
|
+
/**
|
|
422
|
+
* [Permissions](https://en.wikipedia.org/wiki/File-system_permissions#Numeric_notation) for created directories.
|
|
423
|
+
*
|
|
424
|
+
* It has no effect on Windows.
|
|
425
|
+
* @default 0o777
|
|
426
|
+
*/
|
|
50
427
|
readonly directoryMode?: number;
|
|
428
|
+
/**
|
|
429
|
+
* Overwrite existing destination file.
|
|
430
|
+
* @default true
|
|
431
|
+
*/
|
|
51
432
|
readonly overwrite?: boolean;
|
|
52
433
|
};
|
|
434
|
+
/**
|
|
435
|
+
* Move a file asynchronously.
|
|
436
|
+
* @param sourcePath The file you want to move.
|
|
437
|
+
* @param destinationPath Where you want the file moved.
|
|
438
|
+
* @param options Configuration options.
|
|
439
|
+
* @returns A `Promise` that resolves when the file has been moved.
|
|
440
|
+
* @example
|
|
441
|
+
* ```
|
|
442
|
+
* import { move } from '@visulima/fs';
|
|
443
|
+
*
|
|
444
|
+
* await move('source/test.png', 'destination/test.png');
|
|
445
|
+
* console.log('The file has been moved');
|
|
446
|
+
* ```
|
|
447
|
+
*/
|
|
53
448
|
declare const move: (sourcePath: string, destinationPath: string, options?: Options) => Promise<void>;
|
|
449
|
+
/**
|
|
450
|
+
* Move a file synchronously.
|
|
451
|
+
* @param sourcePath The file you want to move.
|
|
452
|
+
* @param destinationPath Where you want the file moved.
|
|
453
|
+
* @param options Configuration options.
|
|
454
|
+
* @example
|
|
455
|
+
* ```
|
|
456
|
+
* import { moveSync } from '@visulima/fs';
|
|
457
|
+
*
|
|
458
|
+
* moveSync('source/test.png', 'destination/test.png');
|
|
459
|
+
* console.log('The file has been moved');
|
|
460
|
+
* ```
|
|
461
|
+
*/
|
|
54
462
|
declare const moveSync: (sourcePath: string, destinationPath: string, options?: Options) => void;
|
|
463
|
+
/**
|
|
464
|
+
* Rename a file asynchronously.
|
|
465
|
+
* @param source The file you want to rename.
|
|
466
|
+
* @param destination The name of the renamed file.
|
|
467
|
+
* @param options Configuration options.
|
|
468
|
+
* @returns A `Promise` that resolves when the file has been renamed.
|
|
469
|
+
* @example
|
|
470
|
+
* ```
|
|
471
|
+
* import { rename } from '@visulima/fs';
|
|
472
|
+
*
|
|
473
|
+
* await rename('test.png', 'tests.png', {cwd: 'source'});
|
|
474
|
+
* console.log('The file has been renamed');
|
|
475
|
+
* ```
|
|
476
|
+
*/
|
|
55
477
|
declare const rename: (source: string, destination: string, options?: Options) => Promise<void>;
|
|
478
|
+
/**
|
|
479
|
+
* Rename a file synchronously.
|
|
480
|
+
* @param source The file you want to rename.
|
|
481
|
+
* @param destination The name of the renamed file.
|
|
482
|
+
* @param options Configuration options.
|
|
483
|
+
* @example
|
|
484
|
+
* ```
|
|
485
|
+
* import { renameSync } from '@visulima/fs';
|
|
486
|
+
*
|
|
487
|
+
* renameSync('test.png', 'tests.png', {cwd: 'source'});
|
|
488
|
+
* console.log('The file has been renamed');
|
|
489
|
+
* ```
|
|
490
|
+
*/
|
|
56
491
|
declare const renameSync: (source: string, destination: string, options?: Options) => void;
|
|
57
492
|
type DecompressionMethod$1 = (buffer: Buffer, callback: (error: Error | null, result: Buffer) => void) => void;
|
|
58
493
|
declare const decompressionMethods$1: Record<string, DecompressionMethod$1>;
|
|
494
|
+
/**
|
|
495
|
+
* Asynchronously reads the entire contents of a file.
|
|
496
|
+
* It can also decompress the file content if a `compression` option is provided.
|
|
497
|
+
* @template O - The type of the options object, extending {@link ReadFileOptions}.
|
|
498
|
+
* @param path The path to the file to read. Can be a file URL or a string path.
|
|
499
|
+
* @param options Optional configuration for reading the file. See {@link ReadFileOptions}.
|
|
500
|
+
* Available `compression` methods: "brotli", "gzip", "none" (default).
|
|
501
|
+
* @returns A promise that resolves with the file content. The type of the content (string or Buffer)
|
|
502
|
+
* depends on the `buffer` option (defaults to string if `buffer` is false or not set).
|
|
503
|
+
* @example
|
|
504
|
+
* ```javascript
|
|
505
|
+
* import { readFile } from "@visulima/fs";
|
|
506
|
+
* import { join } from "node:path";
|
|
507
|
+
*
|
|
508
|
+
* const readMyFile = async () => {
|
|
509
|
+
* try {
|
|
510
|
+
* // Read a regular text file
|
|
511
|
+
* const content = await readFile(join("path", "to", "my-file.txt"));
|
|
512
|
+
* console.log("File content:", content);
|
|
513
|
+
*
|
|
514
|
+
* // Read a file as a Buffer
|
|
515
|
+
* const bufferContent = await readFile(join("path", "to", "another-file.bin"), { buffer: true });
|
|
516
|
+
* console.log("Buffer length:", bufferContent.length);
|
|
517
|
+
*
|
|
518
|
+
* // Read and decompress a gzipped file
|
|
519
|
+
* // Assume my-archive.txt.gz exists
|
|
520
|
+
* // const decompressedContent = await readFile(join("path", "to", "my-archive.txt.gz"), { compression: "gzip", encoding: "utf8" });
|
|
521
|
+
* // console.log("Decompressed content:", decompressedContent);
|
|
522
|
+
* } catch (error) {
|
|
523
|
+
* console.error("Failed to read file:", error);
|
|
524
|
+
* }
|
|
525
|
+
* };
|
|
526
|
+
*
|
|
527
|
+
* readMyFile();
|
|
528
|
+
* ```
|
|
529
|
+
*/
|
|
59
530
|
declare const readFile: <O extends ReadFileOptions<keyof typeof decompressionMethods$1> | undefined = undefined>(path: URL | string, options?: O) => Promise<ContentType<O>>;
|
|
60
531
|
type DecompressionMethod = (buffer: Buffer) => Buffer;
|
|
61
532
|
declare const decompressionMethods: Record<string, DecompressionMethod>;
|
|
533
|
+
/**
|
|
534
|
+
* Synchronously reads the entire contents of a file.
|
|
535
|
+
* It can also decompress the file content if a `compression` option is provided.
|
|
536
|
+
* @template O - The type of the options object, extending {@link ReadFileOptions}.
|
|
537
|
+
* @param path The path to the file to read. Can be a file URL or a string path.
|
|
538
|
+
* @param options Optional configuration for reading the file. See {@link ReadFileOptions}.
|
|
539
|
+
* Available `compression` methods: "brotli", "gzip", "none" (default).
|
|
540
|
+
* @returns The file content. The type of the content (string or Buffer)
|
|
541
|
+
* depends on the `buffer` option (defaults to string if `buffer` is false or not set).
|
|
542
|
+
* @example
|
|
543
|
+
* ```javascript
|
|
544
|
+
* import { readFileSync } from "@visulima/fs";
|
|
545
|
+
* import { join } from "node:path";
|
|
546
|
+
*
|
|
547
|
+
* try {
|
|
548
|
+
* // Read a regular text file
|
|
549
|
+
* const content = readFileSync(join("path", "to", "my-file.txt"));
|
|
550
|
+
* console.log("File content:", content);
|
|
551
|
+
*
|
|
552
|
+
* // Read a file as a Buffer
|
|
553
|
+
* const bufferContent = readFileSync(join("path", "to", "another-file.bin"), { buffer: true });
|
|
554
|
+
* console.log("Buffer length:", bufferContent.length);
|
|
555
|
+
*
|
|
556
|
+
* // Read and decompress a gzipped file
|
|
557
|
+
* // Assume my-archive.txt.gz exists
|
|
558
|
+
* // const decompressedContent = readFileSync(join("path", "to", "my-archive.txt.gz"), { compression: "gzip", encoding: "utf8" });
|
|
559
|
+
* // console.log("Decompressed content:", decompressedContent);
|
|
560
|
+
* } catch (error) {
|
|
561
|
+
* console.error("Failed to read file:", error);
|
|
562
|
+
* }
|
|
563
|
+
* ```
|
|
564
|
+
*/
|
|
62
565
|
declare const readFileSync: <O extends ReadFileOptions<keyof typeof decompressionMethods> | undefined = undefined>(path: URL | string, options?: O) => ContentType<O>;
|
|
63
566
|
declare function readJson<T extends JsonValue>(path: URL | string, options?: ReadJsonOptions): Promise<T>;
|
|
64
567
|
declare function readJson<T extends JsonValue>(path: URL | string, reviver: JsonReviver, options?: ReadJsonOptions): Promise<T>;
|
|
65
568
|
declare function readJsonSync(path: URL | string, options?: ReadJsonOptions): JsonValue;
|
|
66
569
|
declare function readJsonSync(path: URL | string, reviver: JsonReviver, options?: ReadJsonOptions): JsonValue;
|
|
570
|
+
/**
|
|
571
|
+
* Ensures that a directory is empty.
|
|
572
|
+
* Deletes directory contents if the directory is not empty.
|
|
573
|
+
* If the directory does not exist, it is created.
|
|
574
|
+
* The directory itself is not deleted.
|
|
575
|
+
* @param dir The path to the directory to empty.
|
|
576
|
+
* @param options Optional configuration for the operation. See {@link RetryOptions}.
|
|
577
|
+
* @returns A promise that resolves when the directory is empty.
|
|
578
|
+
* @example
|
|
579
|
+
* ```javascript
|
|
580
|
+
* import { emptyDir } from "@visulima/fs";
|
|
581
|
+
* import { join } from "node:path";
|
|
582
|
+
*
|
|
583
|
+
* const clearTempDir = async () => {
|
|
584
|
+
* try {
|
|
585
|
+
* await emptyDir(join("/tmp", "my-app-temp"));
|
|
586
|
+
* console.log("Temporary directory emptied or created.");
|
|
587
|
+
* } catch (error) {
|
|
588
|
+
* console.error("Failed to empty directory:", error);
|
|
589
|
+
* }
|
|
590
|
+
* };
|
|
591
|
+
*
|
|
592
|
+
* clearTempDir();
|
|
593
|
+
* ```
|
|
594
|
+
*/
|
|
67
595
|
declare const emptyDir: (dir: URL | string, options?: RetryOptions) => Promise<void>;
|
|
596
|
+
/**
|
|
597
|
+
* Ensures that a directory is empty.
|
|
598
|
+
* Deletes directory contents if the directory is not empty.
|
|
599
|
+
* If the directory does not exist, it is created.
|
|
600
|
+
* The directory itself is not deleted.
|
|
601
|
+
* @param dir The path to the directory to empty.
|
|
602
|
+
* @param options Optional configuration for the operation. See {@link RetryOptions}.
|
|
603
|
+
* @example
|
|
604
|
+
* ```javascript
|
|
605
|
+
* import { emptyDirSync } from "@visulima/fs";
|
|
606
|
+
* import { join } from "node:path";
|
|
607
|
+
*
|
|
608
|
+
* try {
|
|
609
|
+
* emptyDirSync(join("/tmp", "my-app-temp"));
|
|
610
|
+
* console.log("Temporary directory emptied or created.");
|
|
611
|
+
* } catch (error) {
|
|
612
|
+
* console.error("Failed to empty directory:", error);
|
|
613
|
+
* }
|
|
614
|
+
* ```
|
|
615
|
+
*/
|
|
68
616
|
declare const emptyDirSync: (dir: URL | string, options?: RetryOptions) => void;
|
|
617
|
+
/**
|
|
618
|
+
* Asynchronously removes a file or directory (recursively).
|
|
619
|
+
* If the path does not exist, it does nothing.
|
|
620
|
+
* @param path The path to the file or directory to remove.
|
|
621
|
+
* @param options Optional configuration for the operation. See {@link RetryOptions}.
|
|
622
|
+
* @returns A promise that resolves when the path has been removed.
|
|
623
|
+
* @example
|
|
624
|
+
* ```javascript
|
|
625
|
+
* import { remove } from "@visulima/fs";
|
|
626
|
+
* import { join } from "node:path";
|
|
627
|
+
*
|
|
628
|
+
* const deleteFileOrDir = async () => {
|
|
629
|
+
* try {
|
|
630
|
+
* await remove(join("/tmp", "my-file.txt"));
|
|
631
|
+
* console.log("File /tmp/my-file.txt removed.");
|
|
632
|
+
*
|
|
633
|
+
* await remove(join("/tmp", "my-empty-dir"));
|
|
634
|
+
* console.log("Directory /tmp/my-empty-dir removed.");
|
|
635
|
+
*
|
|
636
|
+
* await remove(join("/tmp", "my-dir-with-contents"));
|
|
637
|
+
* console.log("Directory /tmp/my-dir-with-contents and its contents removed.");
|
|
638
|
+
* } catch (error) {
|
|
639
|
+
* console.error("Failed to remove path:", error);
|
|
640
|
+
* }
|
|
641
|
+
* };
|
|
642
|
+
*
|
|
643
|
+
* deleteFileOrDir();
|
|
644
|
+
* ```
|
|
645
|
+
*/
|
|
69
646
|
declare const remove: (path: URL | string, options?: RetryOptions) => Promise<void>;
|
|
647
|
+
/**
|
|
648
|
+
* Synchronously removes a file or directory (recursively).
|
|
649
|
+
* If the path does not exist, it does nothing.
|
|
650
|
+
* @param path The path to the file or directory to remove.
|
|
651
|
+
* @param options Optional configuration for the operation. See {@link RetryOptions}.
|
|
652
|
+
* @example
|
|
653
|
+
* ```javascript
|
|
654
|
+
* import { removeSync } from "@visulima/fs";
|
|
655
|
+
* import { join } from "node:path";
|
|
656
|
+
*
|
|
657
|
+
* try {
|
|
658
|
+
* removeSync(join("/tmp", "my-file.txt"));
|
|
659
|
+
* console.log("File /tmp/my-file.txt removed.");
|
|
660
|
+
*
|
|
661
|
+
* removeSync(join("/tmp", "my-empty-dir"));
|
|
662
|
+
* console.log("Directory /tmp/my-empty-dir removed.");
|
|
663
|
+
*
|
|
664
|
+
* removeSync(join("/tmp", "my-dir-with-contents"));
|
|
665
|
+
* console.log("Directory /tmp/my-dir-with-contents and its contents removed.");
|
|
666
|
+
* } catch (error) {
|
|
667
|
+
* console.error("Failed to remove path:", error);
|
|
668
|
+
* }
|
|
669
|
+
* ```
|
|
670
|
+
*/
|
|
70
671
|
declare const removeSync: (path: URL | string, options?: RetryOptions) => void;
|
|
672
|
+
/**
|
|
673
|
+
* Supported filesystem types
|
|
674
|
+
*/
|
|
71
675
|
type FileSystemType = "win32" | "unix" | "darwin" | "fat32" | "auto";
|
|
676
|
+
/**
|
|
677
|
+
* Options for the sanitize function
|
|
678
|
+
*/
|
|
72
679
|
interface SanitizeOptions {
|
|
680
|
+
/**
|
|
681
|
+
* Target filesystem type for sanitization rules
|
|
682
|
+
* - "win32": Windows filesystem rules (reserved names, Windows forbidden chars, no trailing periods/spaces)
|
|
683
|
+
* - "unix": Unix/Linux filesystem rules (only / and null forbidden)
|
|
684
|
+
* - "darwin": macOS filesystem rules (same as unix)
|
|
685
|
+
* - "fat32": FAT32 filesystem rules (Windows rules + no leading/trailing spaces/periods in name part)
|
|
686
|
+
* - "auto": Automatically detect from process.platform
|
|
687
|
+
* @default "auto"
|
|
688
|
+
*/
|
|
73
689
|
filesystem?: FileSystemType;
|
|
690
|
+
/**
|
|
691
|
+
* Maximum length of the sanitized name
|
|
692
|
+
* @default 128
|
|
693
|
+
*/
|
|
74
694
|
maxLength?: number;
|
|
75
695
|
}
|
|
696
|
+
/**
|
|
697
|
+
* Sanitizes a filename by removing or replacing forbidden characters.
|
|
698
|
+
* @param name The filename to sanitize.
|
|
699
|
+
* @param options Optional configuration.
|
|
700
|
+
* @returns The sanitized filename, or "unnamed" if the result would be empty.
|
|
701
|
+
*/
|
|
76
702
|
declare const sanitize: (name: string, options?: Partial<SanitizeOptions>) => string;
|
|
703
|
+
/**
|
|
704
|
+
* Asynchronously writes data to a file, replacing the file if it already exists.
|
|
705
|
+
* This function includes safeguards like writing to a temporary file first and then renaming, and handling permissions.
|
|
706
|
+
* @param path The path to the file to write. Can be a file URL or a string path.
|
|
707
|
+
* @param content The data to write. Can be a string, Buffer, ArrayBuffer, or ArrayBufferView.
|
|
708
|
+
* @param options Optional configuration for writing the file. See {@link WriteFileOptions}.
|
|
709
|
+
* @returns A promise that resolves when the file has been written.
|
|
710
|
+
* @example
|
|
711
|
+
* ```javascript
|
|
712
|
+
* import { writeFile } from "@visulima/fs";
|
|
713
|
+
* import { join } from "node:path";
|
|
714
|
+
*
|
|
715
|
+
* const writeMyFile = async () => {
|
|
716
|
+
* try {
|
|
717
|
+
* await writeFile(join("/tmp", "my-new-file.txt"), "Hello World!");
|
|
718
|
+
* console.log("File written successfully.");
|
|
719
|
+
*
|
|
720
|
+
* await writeFile(join("/tmp", "another-file.txt"), "Some other content", { encoding: 'utf16le', mode: 0o600 });
|
|
721
|
+
* console.log("Another file written with specific options.");
|
|
722
|
+
* } catch (error) {
|
|
723
|
+
* console.error("Failed to write file:", error);
|
|
724
|
+
* }
|
|
725
|
+
* };
|
|
726
|
+
*
|
|
727
|
+
* writeMyFile();
|
|
728
|
+
* ```
|
|
729
|
+
*/
|
|
77
730
|
declare const writeFile: (path: URL | string, content: ArrayBuffer | ArrayBufferView | string, options?: WriteFileOptions) => Promise<void>;
|
|
731
|
+
/**
|
|
732
|
+
* Synchronously writes data to a file, replacing the file if it already exists.
|
|
733
|
+
* This function includes safeguards like writing to a temporary file first and then renaming, and handling permissions.
|
|
734
|
+
* @param path The path to the file to write. Can be a file URL or a string path.
|
|
735
|
+
* @param content The data to write. Can be a string, Buffer, ArrayBuffer, or ArrayBufferView.
|
|
736
|
+
* @param options Optional configuration for writing the file. See {@link WriteFileOptions}.
|
|
737
|
+
* @returns void
|
|
738
|
+
* @example
|
|
739
|
+
* ```javascript
|
|
740
|
+
* import { writeFileSync } from "@visulima/fs";
|
|
741
|
+
* import { join } from "node:path";
|
|
742
|
+
*
|
|
743
|
+
* const writeMyFileSync = () => {
|
|
744
|
+
* try {
|
|
745
|
+
* writeFileSync(join("/tmp", "my-new-file-sync.txt"), "Hello World Synchronously!");
|
|
746
|
+
* console.log("File written successfully (sync).");
|
|
747
|
+
*
|
|
748
|
+
* writeFileSync(join("/tmp", "another-file-sync.txt"), "Some other sync content", { encoding: 'utf16le', mode: 0o600 });
|
|
749
|
+
* console.log("Another file written with specific options (sync).");
|
|
750
|
+
* } catch (error) {
|
|
751
|
+
* console.error("Failed to write file (sync):", error);
|
|
752
|
+
* }
|
|
753
|
+
* };
|
|
754
|
+
*
|
|
755
|
+
* writeMyFileSync();
|
|
756
|
+
* ```
|
|
757
|
+
*/
|
|
78
758
|
declare const writeFileSync: (path: URL | string, content: ArrayBuffer | ArrayBufferView | string, options?: WriteFileOptions) => void;
|
|
759
|
+
/**
|
|
760
|
+
* Asynchronously writes an object to a JSON file.
|
|
761
|
+
* Handles indentation detection, custom stringifiers, and gracefully manages existing files.
|
|
762
|
+
* @param path The path to the JSON file to write. Can be a file URL or a string path.
|
|
763
|
+
* @param data The data to serialize and write. Can be any JavaScript value that can be stringified by `JSON.stringify` or a custom stringifier.
|
|
764
|
+
* @param options Optional configuration for writing the JSON file. See {@link WriteJsonOptions}.
|
|
765
|
+
* @returns A promise that resolves when the JSON file has been written.
|
|
766
|
+
* @example
|
|
767
|
+
* ```javascript
|
|
768
|
+
* import { writeJson } from "@visulima/fs";
|
|
769
|
+
* import { join } from "node:path";
|
|
770
|
+
*
|
|
771
|
+
* const writeMyJson = async () => {
|
|
772
|
+
* try {
|
|
773
|
+
* await writeJson(join("/tmp", "my-config.json"), { setting: "enabled", value: 123 });
|
|
774
|
+
* console.log("JSON file written successfully.");
|
|
775
|
+
*
|
|
776
|
+
* await writeJson(join("/tmp", "another-config.json"), { user: "test", id: "abc" }, { indent: 2, replacer: ["user"] });
|
|
777
|
+
* console.log("Another JSON file written with specific options (indent 2, only 'user' key).");
|
|
778
|
+
* } catch (error) {
|
|
779
|
+
* console.error("Failed to write JSON file:", error);
|
|
780
|
+
* }
|
|
781
|
+
* };
|
|
782
|
+
*
|
|
783
|
+
* writeMyJson();
|
|
784
|
+
* ```
|
|
785
|
+
*/
|
|
79
786
|
declare const writeJson: (path: URL | string, data: unknown, options?: WriteJsonOptions) => Promise<void>;
|
|
787
|
+
/**
|
|
788
|
+
* Synchronously writes an object to a JSON file.
|
|
789
|
+
* Handles indentation detection, custom stringifiers, and gracefully manages existing files.
|
|
790
|
+
* @param path The path to the JSON file to write. Can be a file URL or a string path.
|
|
791
|
+
* @param data The data to serialize and write. Can be any JavaScript value that can be stringified by `JSON.stringify` or a custom stringifier.
|
|
792
|
+
* @param options Optional configuration for writing the JSON file. See {@link WriteJsonOptions}.
|
|
793
|
+
* @example
|
|
794
|
+
* ```javascript
|
|
795
|
+
* import { writeJsonSync } from "@visulima/fs";
|
|
796
|
+
* import { join } from "node:path";
|
|
797
|
+
*
|
|
798
|
+
* const writeMyJsonSync = () => {
|
|
799
|
+
* try {
|
|
800
|
+
* writeJsonSync(join("/tmp", "my-config-sync.json"), { setting: "enabled", value: 456 });
|
|
801
|
+
* console.log("JSON file written successfully (sync).");
|
|
802
|
+
*
|
|
803
|
+
* writeJsonSync(join("/tmp", "another-config-sync.json"), { user: "testSync", id: "def" }, { indent: 4, replacer: ["id"] });
|
|
804
|
+
* console.log("Another JSON file written with specific options (sync, indent 4, only 'id' key).");
|
|
805
|
+
* } catch (error) {
|
|
806
|
+
* console.error("Failed to write JSON file (sync):", error);
|
|
807
|
+
* }
|
|
808
|
+
* };
|
|
809
|
+
*
|
|
810
|
+
* writeMyJsonSync();
|
|
811
|
+
* ```
|
|
812
|
+
*/
|
|
80
813
|
declare const writeJsonSync: (path: URL | string, data: unknown, options?: WriteJsonOptions) => void;
|
|
81
|
-
|
|
814
|
+
/**
|
|
815
|
+
* Subset of Node.js fs module methods required for case-sensitivity detection.
|
|
816
|
+
* This allows custom fs implementations to be passed for testing purposes.
|
|
817
|
+
*/
|
|
818
|
+
type FsSubset = {
|
|
819
|
+
existsSync: typeof fs.existsSync;
|
|
820
|
+
writeFileSync: typeof fs.writeFileSync;
|
|
821
|
+
unlinkSync: typeof fs.unlinkSync;
|
|
822
|
+
};
|
|
823
|
+
/**
|
|
824
|
+
* Detects whether the filesystem is case-sensitive.
|
|
825
|
+
*
|
|
826
|
+
* Uses a fast, I/O-free primary method that checks if the specified directory
|
|
827
|
+
* path can be accessed with inverted case. Falls back to writing a temporary
|
|
828
|
+
* file if the primary method is inconclusive.
|
|
829
|
+
*
|
|
830
|
+
* Different mount points can have different case-sensitivity settings, so this
|
|
831
|
+
* function checks the filesystem where the specified directory resides.
|
|
832
|
+
*
|
|
833
|
+
* @param directoryPath - The directory path to check. Defaults to
|
|
834
|
+
* `process.cwd()`. Different mount points can have different case-sensitivity.
|
|
835
|
+
* @param fsInstance - Custom filesystem implementation (primarily for
|
|
836
|
+
* testing). Defaults to Node.js `fs` module.
|
|
837
|
+
* @param useCache - Whether to cache the result per directory. Defaults to
|
|
838
|
+
* `true`. When enabled, subsequent calls for the same directory return
|
|
839
|
+
* instantly without re-checking.
|
|
840
|
+
* @returns `true` if the filesystem is case-sensitive, `false` otherwise
|
|
841
|
+
*
|
|
842
|
+
* @example
|
|
843
|
+
* ```ts
|
|
844
|
+
* import { isFsCaseSensitive } from 'is-fs-case-sensitive'
|
|
845
|
+
*
|
|
846
|
+
* // Check current working directory's filesystem
|
|
847
|
+
* if (isFsCaseSensitive()) {
|
|
848
|
+
* console.log('Case-sensitive filesystem (likely Linux)')
|
|
849
|
+
* } else {
|
|
850
|
+
* console.log('Case-insensitive filesystem (likely macOS/Windows)')
|
|
851
|
+
* }
|
|
852
|
+
*
|
|
853
|
+
* // Check specific directory
|
|
854
|
+
* const isHomeCaseSensitive = isFsCaseSensitive('/home/user')
|
|
855
|
+
* ```
|
|
856
|
+
*/
|
|
857
|
+
declare const isFsCaseSensitive: (directoryPath?: string, fsInstance?: FsSubset, useCache?: boolean) => boolean;
|
|
858
|
+
export { type ContentType, type FindUpName, type FindUpNameSync, type FindUpOptions, type JsonReviver, type Options as MoveOptions, type ReadFileOptions, type ReadJsonOptions, type SanitizeOptions, type WalkEntry, type WalkOptions, type WriteFileOptions, type WriteJsonOptions, collect, collectSync, emptyDir, emptyDirSync, ensureDir, ensureDirSync, ensureFile, ensureFileSync, ensureLink, ensureLinkSync, ensureSymlink, ensureSymlinkSync, findUp, findUpSync, isAccessible, isAccessibleSync, isFsCaseSensitive, move, moveSync, readFile, readFileSync, readJson, readJsonSync, remove, removeSync, rename, renameSync, sanitize, walk, walkSync, writeFile, writeFileSync, writeJson, writeJsonSync };
|