@visulima/fs 4.0.4 → 4.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/CHANGELOG.md +18 -0
- package/LICENSE.md +3 -0
- package/dist/constants.d.ts +42 -0
- package/dist/ensure/ensure-dir-sync.d.ts +14 -0
- package/dist/ensure/ensure-dir.d.ts +14 -0
- package/dist/ensure/ensure-file-sync.d.ts +15 -0
- package/dist/ensure/ensure-file.d.ts +27 -0
- package/dist/ensure/ensure-link-sync.d.ts +16 -0
- package/dist/ensure/ensure-link.d.ts +16 -0
- package/dist/ensure/ensure-symlink-sync.d.ts +23 -0
- package/dist/ensure/ensure-symlink.d.ts +23 -0
- package/dist/ensure/utils/get-file-info-type.d.ts +7 -0
- package/dist/ensure/utils/is-stats-identical.d.ts +3 -0
- package/dist/ensure/utils/resolve-symlink-target.d.ts +2 -0
- package/dist/eol.d.ts +35 -7
- package/dist/error/already-exists-error.d.ts +39 -0
- package/dist/error/directory-error.d.ts +47 -0
- package/dist/error/json-error.d.ts +52 -0
- package/dist/error/not-empty-error.d.ts +51 -0
- package/dist/error/not-found-error.d.ts +44 -0
- package/dist/error/permission-error.d.ts +45 -0
- package/dist/error/walk-error.d.ts +51 -0
- package/dist/error.d.ts +7 -50
- package/dist/find/collect-sync.d.ts +31 -0
- package/dist/find/collect.d.ts +35 -0
- package/dist/find/find-up-sync.d.ts +42 -0
- package/dist/find/find-up.d.ts +46 -0
- package/dist/find/utils/glob-to-regex.d.ts +2 -0
- package/dist/find/utils/walk-include.d.ts +2 -0
- package/dist/find/walk-sync.d.ts +26 -0
- package/dist/find/walk.d.ts +29 -0
- package/dist/index.d.ts +34 -136
- package/dist/index.js +14 -14
- package/dist/is-accessible-sync.d.ts +3 -0
- package/dist/is-accessible.d.ts +29 -0
- package/dist/move/index.d.ts +70 -0
- package/dist/move/types.d.ts +38 -0
- package/dist/move/utils/internal-move-file-sync.d.ts +3 -0
- package/dist/move/utils/internal-move-file.d.ts +3 -0
- package/dist/move/utils/validate-same-directory.d.ts +2 -0
- package/dist/packem_shared/{F_OK-MldBaGxb.js → F_OK-BalxCn9n.js} +1 -1
- package/dist/packem_shared/{findUp-BSnyGqer.js → findUp-Lv9mZCIQ.js} +1 -1
- package/dist/packem_shared/{findUpSync-jRHbSCMV.js → findUpSync-BsK4Nrrq.js} +1 -1
- package/dist/packem_shared/{isAccessible-DuVrTNFV.js → isAccessible-iOp0Bou6.js} +1 -1
- package/dist/packem_shared/{isAccessibleSync-DI8mM0fA.js → isAccessibleSync-38BmiIcx.js} +1 -1
- package/dist/packem_shared/{move-DbpW5_vA.js → move-BD6JbYSu.js} +2 -2
- package/dist/packem_shared/{readFile-iHOVXeH-.js → readFile-XVOjAw8r.js} +11 -14
- package/dist/packem_shared/{readFileSync-BkEj9BQY.js → readFileSync-B9Zg5PSF.js} +2 -2
- package/dist/packem_shared/{readJson-pdfQBWDR.js → readJson-Dra2m6s-.js} +1 -1
- package/dist/packem_shared/{readJsonSync-BZqGcA5d.js → readJsonSync-B6Dl1I_6.js} +1 -1
- package/dist/packem_shared/{readYaml-BQXTlR2p.js → readYaml-Bu1nsexB.js} +1 -1
- package/dist/packem_shared/{readYamlSync-BBBQ5ujx.js → readYamlSync-Di88aMME.js} +1 -1
- package/dist/packem_shared/{stripJsonComments-vo4k0mpF.js → stripJsonComments-21XWVDwP.js} +1 -1
- package/dist/packem_shared/{writeFile-DSHERs0Z.js → writeFile-NT1fLEEe.js} +2 -2
- package/dist/packem_shared/{writeFileSync-CJp1kXQR.js → writeFileSync-Bj5UDSf0.js} +2 -2
- package/dist/packem_shared/{writeJson-C0OfLDbe.js → writeJson-DQLdAi0d.js} +3 -3
- package/dist/packem_shared/{writeJsonSync-Cs21FE7v.js → writeJsonSync-DvTCjVUi.js} +3 -3
- package/dist/packem_shared/{writeYaml-n4xzYN9a.js → writeYaml-BMY0kOPf.js} +1 -1
- package/dist/packem_shared/{writeYamlSync-DnOEnP10.js → writeYamlSync-DqPu8cCd.js} +1 -1
- package/dist/read/read-file-sync.d.ts +37 -0
- package/dist/read/read-file.d.ts +41 -0
- package/dist/read/read-json-sync.d.ts +5 -0
- package/dist/read/read-json.d.ts +5 -0
- package/dist/read/read-yaml-sync.d.ts +4 -0
- package/dist/read/read-yaml.d.ts +4 -0
- package/dist/remove/empty-dir-sync.d.ts +24 -0
- package/dist/remove/empty-dir.d.ts +28 -0
- package/dist/remove/remove-sync.d.ts +28 -0
- package/dist/remove/remove.d.ts +32 -0
- package/dist/size.d.ts +245 -12
- package/dist/types.d.ts +299 -0
- package/dist/utils/assert-valid-file-contents.d.ts +27 -0
- package/dist/utils/assert-valid-file-or-directory-path.d.ts +26 -0
- package/dist/utils/parse-json.d.ts +5 -0
- package/dist/utils/strip-json-comments.d.ts +44 -0
- package/dist/utils.d.ts +6 -19
- package/dist/utils.js +1 -1
- package/dist/write/utils/to-uint-8-array.d.ts +2 -0
- package/dist/write/write-file-sync.d.ts +30 -0
- package/dist/write/write-file.d.ts +30 -0
- package/dist/write/write-json-sync.d.ts +30 -0
- package/dist/write/write-json.d.ts +30 -0
- package/dist/write/write-yaml-sync.d.ts +4 -0
- package/dist/write/write-yaml.d.ts +4 -0
- package/dist/yaml.d.ts +5 -17
- package/dist/yaml.js +4 -4
- package/package.json +2 -3
- package/dist/packem_shared/json-error-ZPgZIXcg.d.ts +0 -11
- package/dist/packem_shared/types-T308NKwz.d.ts +0 -90
package/dist/size.d.ts
CHANGED
|
@@ -1,12 +1,245 @@
|
|
|
1
|
-
import { Readable } from
|
|
2
|
-
import { URL } from
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
1
|
+
import { Readable } from "node:stream";
|
|
2
|
+
import { URL } from "node:url";
|
|
3
|
+
import type { BrotliOptions, ZlibOptions } from "node:zlib";
|
|
4
|
+
/**
|
|
5
|
+
* Asynchronously calculates the gzipped size of the given input.
|
|
6
|
+
* The input can be a Buffer, a Readable stream, a URL object pointing to a file, or a string (file path or content).
|
|
7
|
+
* Uses memory-efficient streaming for files and streams to avoid loading entire contents into memory.
|
|
8
|
+
* @param input The input data to gzip and measure.
|
|
9
|
+
* @param [options] Optional Zlib options for gzip compression.
|
|
10
|
+
* @returns A promise that resolves with the gzipped size in bytes.
|
|
11
|
+
* @example
|
|
12
|
+
* ```javascript
|
|
13
|
+
* import { gzipSize } from "@visulima/fs";
|
|
14
|
+
* import { Readable } from "node:stream";
|
|
15
|
+
* import { writeFile, unlink } from "node:fs/promises";
|
|
16
|
+
* import { join } from "node:path";
|
|
17
|
+
*
|
|
18
|
+
* const text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
|
|
19
|
+
* const filePath = join("temp-file.txt");
|
|
20
|
+
*
|
|
21
|
+
* async function main() {
|
|
22
|
+
* // From Buffer
|
|
23
|
+
* const buffer = Buffer.from(text);
|
|
24
|
+
* console.log(`Gzip size of buffer: ${await gzipSize(buffer)} bytes`);
|
|
25
|
+
*
|
|
26
|
+
* // From string (content)
|
|
27
|
+
* console.log(`Gzip size of string content: ${await gzipSize(text)} bytes`);
|
|
28
|
+
*
|
|
29
|
+
* // From file path
|
|
30
|
+
* await writeFile(filePath, text);
|
|
31
|
+
* console.log(`Gzip size of file: ${await gzipSize(filePath)} bytes`);
|
|
32
|
+
*
|
|
33
|
+
* // From URL
|
|
34
|
+
* const fileUrl = new URL(`file://${filePath}`);
|
|
35
|
+
* console.log(`Gzip size of URL: ${await gzipSize(fileUrl)} bytes`);
|
|
36
|
+
*
|
|
37
|
+
* // From Readable stream
|
|
38
|
+
* const stream = Readable.from(text);
|
|
39
|
+
* console.log(`Gzip size of stream: ${await gzipSize(stream)} bytes`);
|
|
40
|
+
*
|
|
41
|
+
* await unlink(filePath); // Clean up temp file
|
|
42
|
+
* }
|
|
43
|
+
*
|
|
44
|
+
* main().catch(console.error);
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
47
|
+
export declare const gzipSize: (input: Buffer | Readable | URL | string, options?: ZlibOptions) => Promise<number>;
|
|
48
|
+
/**
|
|
49
|
+
* Asynchronously calculates the Brotli compressed size of the given input.
|
|
50
|
+
* The input can be a Buffer, a Readable stream, a URL object pointing to a file, or a string (file path or content).
|
|
51
|
+
* Uses memory-efficient streaming for files and streams to avoid loading entire contents into memory.
|
|
52
|
+
* @param input The input data to compress with Brotli and measure.
|
|
53
|
+
* @param [options] Optional Zlib options for Brotli compression.
|
|
54
|
+
* @returns A promise that resolves with the Brotli compressed size in bytes.
|
|
55
|
+
* @example
|
|
56
|
+
* ```javascript
|
|
57
|
+
* import { brotliSize } from "@visulima/fs";
|
|
58
|
+
* import { Readable } from "node:stream";
|
|
59
|
+
* import { writeFile, unlink } from "node:fs/promises";
|
|
60
|
+
* import { join } from "node:path";
|
|
61
|
+
*
|
|
62
|
+
* const text = "This is a test string for Brotli compression efficiency.";
|
|
63
|
+
* const filePath = join("temp-brotli-file.txt");
|
|
64
|
+
*
|
|
65
|
+
* async function main() {
|
|
66
|
+
* // From Buffer
|
|
67
|
+
* const buffer = Buffer.from(text);
|
|
68
|
+
* console.log(`Brotli size of buffer: ${await brotliSize(buffer)} bytes`);
|
|
69
|
+
*
|
|
70
|
+
* // From string (content)
|
|
71
|
+
* console.log(`Brotli size of string content: ${await brotliSize(text)} bytes`);
|
|
72
|
+
*
|
|
73
|
+
* // From file path
|
|
74
|
+
* await writeFile(filePath, text);
|
|
75
|
+
* console.log(`Brotli size of file: ${await brotliSize(filePath)} bytes`);
|
|
76
|
+
*
|
|
77
|
+
* // From URL
|
|
78
|
+
* const fileUrl = new URL(`file://${filePath}`);
|
|
79
|
+
* console.log(`Brotli size of URL: ${await brotliSize(fileUrl)} bytes`);
|
|
80
|
+
*
|
|
81
|
+
* // From Readable stream
|
|
82
|
+
* const stream = Readable.from(text);
|
|
83
|
+
* console.log(`Brotli size of stream: ${await brotliSize(stream)} bytes`);
|
|
84
|
+
*
|
|
85
|
+
* await unlink(filePath); // Clean up temp file
|
|
86
|
+
* }
|
|
87
|
+
*
|
|
88
|
+
* main().catch(console.error);
|
|
89
|
+
* ```
|
|
90
|
+
*/
|
|
91
|
+
export declare const brotliSize: (input: Buffer | Readable | URL | string, options?: BrotliOptions) => Promise<number>;
|
|
92
|
+
/**
|
|
93
|
+
* Asynchronously calculates the raw (uncompressed) size of the given input.
|
|
94
|
+
* The input can be a Buffer, a Readable stream, a URL object pointing to a file, or a string (file path or content).
|
|
95
|
+
* Uses memory-efficient streaming for files and streams to avoid loading entire contents into memory.
|
|
96
|
+
* @param input The input data to measure.
|
|
97
|
+
* @returns A promise that resolves with the raw size in bytes.
|
|
98
|
+
* @example
|
|
99
|
+
* ```javascript
|
|
100
|
+
* import { rawSize } from "@visulima/fs";
|
|
101
|
+
* import { Readable } from "node:stream";
|
|
102
|
+
* import { writeFile, unlink } from "node:fs/promises";
|
|
103
|
+
* import { join } from "node:path";
|
|
104
|
+
*
|
|
105
|
+
* const text = "Hello, World!";
|
|
106
|
+
* const filePath = join("temp-raw-file.txt");
|
|
107
|
+
*
|
|
108
|
+
* async function main() {
|
|
109
|
+
* // From Buffer
|
|
110
|
+
* const buffer = Buffer.from(text);
|
|
111
|
+
* console.log(`Raw size of buffer: ${await rawSize(buffer)} bytes`);
|
|
112
|
+
*
|
|
113
|
+
* // From string (content)
|
|
114
|
+
* console.log(`Raw size of string content: ${await rawSize(text)} bytes`);
|
|
115
|
+
*
|
|
116
|
+
* // From file path
|
|
117
|
+
* await writeFile(filePath, text);
|
|
118
|
+
* console.log(`Raw size of file: ${await rawSize(filePath)} bytes`);
|
|
119
|
+
*
|
|
120
|
+
* // From URL
|
|
121
|
+
* const fileUrl = new URL(`file://${filePath}`);
|
|
122
|
+
* console.log(`Raw size of URL: ${await rawSize(fileUrl)} bytes`);
|
|
123
|
+
*
|
|
124
|
+
* // From Readable stream
|
|
125
|
+
* const stream = Readable.from(text);
|
|
126
|
+
* console.log(`Raw size of stream: ${await rawSize(stream)} bytes`);
|
|
127
|
+
*
|
|
128
|
+
* await unlink(filePath); // Clean up temp file
|
|
129
|
+
* }
|
|
130
|
+
*
|
|
131
|
+
* main().catch(console.error);
|
|
132
|
+
* ```
|
|
133
|
+
*/
|
|
134
|
+
export declare const rawSize: (input: Buffer | Readable | URL | string) => Promise<number>;
|
|
135
|
+
/**
|
|
136
|
+
* Synchronously calculates the gzipped size of the given input.
|
|
137
|
+
* The input can be a Buffer, a URL object pointing to a file, or a string (file path or content).
|
|
138
|
+
* Note: For Readable streams or very large files, consider using the asynchronous `gzipSize` function for better performance and to avoid blocking.
|
|
139
|
+
* @param input The input data to gzip and measure.
|
|
140
|
+
* @param [options] Optional Zlib options for gzip compression.
|
|
141
|
+
* @returns The gzipped size in bytes.
|
|
142
|
+
* @example
|
|
143
|
+
* ```javascript
|
|
144
|
+
* import { gzipSizeSync } from "@visulima/fs";
|
|
145
|
+
* import { writeFileSync, unlinkSync } from "node:fs";
|
|
146
|
+
* import { join } from "node:path";
|
|
147
|
+
*
|
|
148
|
+
* const text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
|
|
149
|
+
* const filePath = join("temp-sync-file.txt");
|
|
150
|
+
*
|
|
151
|
+
* // From Buffer
|
|
152
|
+
* const buffer = Buffer.from(text);
|
|
153
|
+
* console.log(`Sync Gzip size of buffer: ${gzipSizeSync(buffer)} bytes`);
|
|
154
|
+
*
|
|
155
|
+
* // From string (content)
|
|
156
|
+
* console.log(`Sync Gzip size of string content: ${gzipSizeSync(text)} bytes`);
|
|
157
|
+
*
|
|
158
|
+
* // From file path
|
|
159
|
+
* try {
|
|
160
|
+
* writeFileSync(filePath, text);
|
|
161
|
+
* console.log(`Sync Gzip size of file: ${gzipSizeSync(filePath)} bytes`);
|
|
162
|
+
*
|
|
163
|
+
* // From URL
|
|
164
|
+
* const fileUrl = new URL(`file://${filePath}`);
|
|
165
|
+
* console.log(`Sync Gzip size of URL: ${gzipSizeSync(fileUrl)} bytes`);
|
|
166
|
+
* } finally {
|
|
167
|
+
* try { unlinkSync(filePath); } catch {} // Clean up temp file
|
|
168
|
+
* }
|
|
169
|
+
* ```
|
|
170
|
+
*/
|
|
171
|
+
export declare const gzipSizeSync: (input: Buffer | URL | string, options?: ZlibOptions) => number;
|
|
172
|
+
/**
|
|
173
|
+
* Synchronously calculates the Brotli compressed size of the given input.
|
|
174
|
+
* The input can be a Buffer, a URL object pointing to a file, or a string (file path or content).
|
|
175
|
+
* Note: For Readable streams or very large files, consider using the asynchronous `brotliSize` function for better performance and to avoid blocking.
|
|
176
|
+
* @param input The input data to compress with Brotli and measure.
|
|
177
|
+
* @param [options] Optional Zlib options for Brotli compression.
|
|
178
|
+
* @returns The Brotli compressed size in bytes.
|
|
179
|
+
* @example
|
|
180
|
+
* ```javascript
|
|
181
|
+
* import { brotliSizeSync } from "@visulima/fs";
|
|
182
|
+
* import { writeFileSync, unlinkSync } from "node:fs";
|
|
183
|
+
* import { join } from "node:path";
|
|
184
|
+
*
|
|
185
|
+
* const text = "This is a test string for Brotli compression efficiency, synchronously.";
|
|
186
|
+
* const filePath = join("temp-brotli-sync-file.txt");
|
|
187
|
+
*
|
|
188
|
+
* // From Buffer
|
|
189
|
+
* const buffer = Buffer.from(text);
|
|
190
|
+
* console.log(`Sync Brotli size of buffer: ${brotliSizeSync(buffer)} bytes`);
|
|
191
|
+
*
|
|
192
|
+
* // From string (content)
|
|
193
|
+
* console.log(`Sync Brotli size of string content: ${brotliSizeSync(text)} bytes`);
|
|
194
|
+
*
|
|
195
|
+
* // From file path
|
|
196
|
+
* try {
|
|
197
|
+
* writeFileSync(filePath, text);
|
|
198
|
+
* console.log(`Sync Brotli size of file: ${brotliSizeSync(filePath)} bytes`);
|
|
199
|
+
*
|
|
200
|
+
* // From URL
|
|
201
|
+
* const fileUrl = new URL(`file://${filePath}`);
|
|
202
|
+
* console.log(`Sync Brotli size of URL: ${brotliSizeSync(fileUrl)} bytes`);
|
|
203
|
+
* } finally {
|
|
204
|
+
* try { unlinkSync(filePath); } catch {} // Clean up temp file
|
|
205
|
+
* }
|
|
206
|
+
* ```
|
|
207
|
+
*/
|
|
208
|
+
export declare const brotliSizeSync: (input: Buffer | URL | string, options?: BrotliOptions) => number;
|
|
209
|
+
/**
|
|
210
|
+
* Synchronously calculates the raw (uncompressed) size of the given input.
|
|
211
|
+
* The input can be a Buffer, a URL object pointing to a file, or a string (file path or content).
|
|
212
|
+
* For file paths, it uses `statSync` to get the file size.
|
|
213
|
+
* Note: For Readable streams or very large files, consider using the asynchronous `rawSize` function for better performance and to avoid blocking.
|
|
214
|
+
* @param input The input data to measure.
|
|
215
|
+
* @returns The raw size in bytes.
|
|
216
|
+
* @example
|
|
217
|
+
* ```javascript
|
|
218
|
+
* import { rawSizeSync } from "@visulima/fs";
|
|
219
|
+
* import { writeFileSync, unlinkSync } from "node:fs";
|
|
220
|
+
* import { join } from "node:path";
|
|
221
|
+
*
|
|
222
|
+
* const text = "Hello, Synchronous World!";
|
|
223
|
+
* const filePath = join("temp-raw-sync-file.txt");
|
|
224
|
+
*
|
|
225
|
+
* // From Buffer
|
|
226
|
+
* const buffer = Buffer.from(text);
|
|
227
|
+
* console.log(`Sync Raw size of buffer: ${rawSizeSync(buffer)} bytes`);
|
|
228
|
+
*
|
|
229
|
+
* // From string (content)
|
|
230
|
+
* console.log(`Sync Raw size of string content: ${rawSizeSync(text)} bytes`);
|
|
231
|
+
*
|
|
232
|
+
* // From file path
|
|
233
|
+
* try {
|
|
234
|
+
* writeFileSync(filePath, text);
|
|
235
|
+
* console.log(`Sync Raw size of file: ${rawSizeSync(filePath)} bytes`);
|
|
236
|
+
*
|
|
237
|
+
* // From URL
|
|
238
|
+
* const fileUrl = new URL(`file://${filePath}`);
|
|
239
|
+
* console.log(`Sync Raw size of URL: ${rawSizeSync(fileUrl)} bytes`);
|
|
240
|
+
* } finally {
|
|
241
|
+
* try { unlinkSync(filePath); } catch {} // Clean up temp file
|
|
242
|
+
* }
|
|
243
|
+
* ```
|
|
244
|
+
*/
|
|
245
|
+
export declare const rawSizeSync: (input: Buffer | URL | string) => number;
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
import type { Dirent, PathLike } from "node:fs";
|
|
2
|
+
import type { CreateNodeOptions, DocumentOptions, ParseOptions, SchemaOptions, ToJSOptions, ToStringOptions } from "yaml";
|
|
3
|
+
import type { FIND_UP_STOP } from "./constants.d.ts";
|
|
4
|
+
type ColorizeMethod = (value: string) => string;
|
|
5
|
+
/**
|
|
6
|
+
* Options for the `walk` and `walkSync` functions.
|
|
7
|
+
*/
|
|
8
|
+
export interface WalkOptions {
|
|
9
|
+
/**
|
|
10
|
+
* List of file extensions used to filter entries.
|
|
11
|
+
* If specified, entries without the file extension specified by this option are excluded.
|
|
12
|
+
* @default {undefined}
|
|
13
|
+
*/
|
|
14
|
+
extensions?: string[];
|
|
15
|
+
/**
|
|
16
|
+
* Indicates whether symlinks should be resolved or not.
|
|
17
|
+
* @default {false}
|
|
18
|
+
*/
|
|
19
|
+
followSymlinks?: boolean;
|
|
20
|
+
/**
|
|
21
|
+
* Indicates whether directory entries should be included or not.
|
|
22
|
+
* @default {true}
|
|
23
|
+
*/
|
|
24
|
+
includeDirs?: boolean;
|
|
25
|
+
/**
|
|
26
|
+
* Indicates whether file entries should be included or not.
|
|
27
|
+
* @default {true}
|
|
28
|
+
*/
|
|
29
|
+
includeFiles?: boolean;
|
|
30
|
+
/**
|
|
31
|
+
* Indicates whether symlink entries should be included or not.
|
|
32
|
+
* This option is meaningful only if `followSymlinks` is set to `false`.
|
|
33
|
+
* @default {true}
|
|
34
|
+
*/
|
|
35
|
+
includeSymlinks?: boolean;
|
|
36
|
+
/**
|
|
37
|
+
* List of regular expression or glob patterns used to filter entries.
|
|
38
|
+
* If specified, entries that do not match the patterns specified by this option are excluded.
|
|
39
|
+
* @default {undefined}
|
|
40
|
+
*/
|
|
41
|
+
match?: (RegExp | string)[];
|
|
42
|
+
/**
|
|
43
|
+
* The maximum depth of the file tree to be walked recursively.
|
|
44
|
+
* @default {Infinity}
|
|
45
|
+
*/
|
|
46
|
+
maxDepth?: number;
|
|
47
|
+
/**
|
|
48
|
+
* List of regular expression or glob patterns used to filter entries.
|
|
49
|
+
* If specified, entries matching the patterns specified by this option are excluded.
|
|
50
|
+
* @default {undefined}
|
|
51
|
+
*/
|
|
52
|
+
skip?: (RegExp | string)[];
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Represents an entry found by `walk` or `walkSync`.
|
|
56
|
+
*/
|
|
57
|
+
export interface WalkEntry extends Pick<Dirent, "isDirectory" | "isFile" | "isSymbolicLink" | "name"> {
|
|
58
|
+
/** The full path to the entry. */
|
|
59
|
+
path: string;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Supported file encodings for reading files.
|
|
63
|
+
*/
|
|
64
|
+
export type ReadFileEncoding = "ascii" | "base64" | "base64url" | "hex" | "latin1" | "ucs-2" | "ucs2" | "utf-8" | "utf-16le" | "utf8" | "utf16le";
|
|
65
|
+
/**
|
|
66
|
+
* Options for reading files.
|
|
67
|
+
* @template C - The type of compression used.
|
|
68
|
+
*/
|
|
69
|
+
export type ReadFileOptions<C> = {
|
|
70
|
+
/**
|
|
71
|
+
* Return content as a Buffer. Default: `false`
|
|
72
|
+
*/
|
|
73
|
+
buffer?: boolean;
|
|
74
|
+
/**
|
|
75
|
+
* Compression method to decompress the file against. Default: `none`
|
|
76
|
+
*/
|
|
77
|
+
compression?: C;
|
|
78
|
+
/**
|
|
79
|
+
* The encoding to use. Default: `utf8`
|
|
80
|
+
* @see https://nodejs.org/api/buffer.html#buffer_buffers_and_character_encodings
|
|
81
|
+
*/
|
|
82
|
+
encoding?: ReadFileEncoding | undefined;
|
|
83
|
+
/**
|
|
84
|
+
* The flag used to open the file. Default: `r`
|
|
85
|
+
*/
|
|
86
|
+
flag?: number | string | undefined;
|
|
87
|
+
};
|
|
88
|
+
/**
|
|
89
|
+
* Represents the content type of a read file, which can be a Buffer or a string based on options.
|
|
90
|
+
* @template O - The ReadFileOptions type.
|
|
91
|
+
*/
|
|
92
|
+
export type ContentType<O = undefined> = O extends {
|
|
93
|
+
buffer: true;
|
|
94
|
+
} ? Buffer : string;
|
|
95
|
+
/**
|
|
96
|
+
* Type for the `reviver` parameter of `JSON.parse()`.
|
|
97
|
+
* A function that transforms the results. This function is called for each member of the object.
|
|
98
|
+
* If a member contains nested objects, the nested objects are transformed before the parent object is.
|
|
99
|
+
*/
|
|
100
|
+
export type JsonReviver = Parameters<(typeof JSON)["parse"]>["1"];
|
|
101
|
+
/**
|
|
102
|
+
* Specifies a location (line and column) in a file for code frame generation.
|
|
103
|
+
*/
|
|
104
|
+
export type CodeFrameLocation = {
|
|
105
|
+
/** The column number. */
|
|
106
|
+
column?: number;
|
|
107
|
+
/** The line number. */
|
|
108
|
+
line: number;
|
|
109
|
+
};
|
|
110
|
+
/**
|
|
111
|
+
* Options for customizing the appearance of code frames.
|
|
112
|
+
*/
|
|
113
|
+
export type CodeFrameOptions = {
|
|
114
|
+
/** Colorization methods for different parts of the code frame. */
|
|
115
|
+
color?: {
|
|
116
|
+
/** Color for the gutter (line numbers). */
|
|
117
|
+
gutter?: ColorizeMethod;
|
|
118
|
+
/** Color for the marker (pointing to the error). */
|
|
119
|
+
marker?: ColorizeMethod;
|
|
120
|
+
/** Color for the message. */
|
|
121
|
+
message?: ColorizeMethod;
|
|
122
|
+
};
|
|
123
|
+
};
|
|
124
|
+
/**
|
|
125
|
+
* Options for reading and parsing JSON files.
|
|
126
|
+
* Extends {@link CodeFrameOptions}.
|
|
127
|
+
*/
|
|
128
|
+
export type ReadJsonOptions = CodeFrameOptions & {
|
|
129
|
+
/**
|
|
130
|
+
* A function to transform the string content before parsing.
|
|
131
|
+
* @param source The raw string content of the file.
|
|
132
|
+
* @returns The transformed string content.
|
|
133
|
+
*/
|
|
134
|
+
beforeParse?: (source: string) => string;
|
|
135
|
+
};
|
|
136
|
+
/**
|
|
137
|
+
* Options for writing files.
|
|
138
|
+
*/
|
|
139
|
+
export type WriteFileOptions = {
|
|
140
|
+
/**
|
|
141
|
+
* The group and user ID used to set the file ownership. Default: `undefined`
|
|
142
|
+
*/
|
|
143
|
+
chown?: {
|
|
144
|
+
gid: number;
|
|
145
|
+
uid: number;
|
|
146
|
+
};
|
|
147
|
+
/**
|
|
148
|
+
* The encoding to use. Default: `utf8`
|
|
149
|
+
*/
|
|
150
|
+
encoding?: BufferEncoding | null | undefined;
|
|
151
|
+
/**
|
|
152
|
+
* The flag used to write the file. Default: `w`
|
|
153
|
+
*/
|
|
154
|
+
flag?: string | undefined;
|
|
155
|
+
/**
|
|
156
|
+
* The file mode (permission and sticky bits). Default: `0o666`
|
|
157
|
+
*/
|
|
158
|
+
mode?: number;
|
|
159
|
+
/**
|
|
160
|
+
* Indicates whether the file should be overwritten if it already exists. Default: `false`
|
|
161
|
+
*/
|
|
162
|
+
overwrite?: boolean;
|
|
163
|
+
/**
|
|
164
|
+
* Recursively create parent directories if needed. Default: `true`
|
|
165
|
+
*/
|
|
166
|
+
recursive?: boolean;
|
|
167
|
+
};
|
|
168
|
+
/**
|
|
169
|
+
* Type for the `replacer` parameter of `JSON.stringify()`.
|
|
170
|
+
* Can be a function that alters the behavior of the stringification process,
|
|
171
|
+
* or an array of strings and numbers that acts as a whitelist for selecting
|
|
172
|
+
* the properties of the value object to be included in the JSON string.
|
|
173
|
+
* If this value is null or not provided, all properties of the object are included in the resulting JSON string.
|
|
174
|
+
*/
|
|
175
|
+
export type JsonReplacer = (number | string)[] | ((this: unknown, key: string, value: unknown) => unknown) | null;
|
|
176
|
+
/**
|
|
177
|
+
* Type for the `replacer` parameter used in YAML serialization, similar to `JSON.stringify`'s replacer.
|
|
178
|
+
*/
|
|
179
|
+
export type YamlReplacer = JsonReplacer;
|
|
180
|
+
/**
|
|
181
|
+
* Options for writing JSON files.
|
|
182
|
+
* Extends {@link WriteFileOptions}.
|
|
183
|
+
*/
|
|
184
|
+
export type WriteJsonOptions = WriteFileOptions & {
|
|
185
|
+
/**
|
|
186
|
+
* Detect indentation automatically if the file exists. Default: `false`
|
|
187
|
+
*/
|
|
188
|
+
detectIndent?: boolean;
|
|
189
|
+
/**
|
|
190
|
+
* The space used for pretty-printing.
|
|
191
|
+
*
|
|
192
|
+
* Pass in `undefined` for no formatting.
|
|
193
|
+
*/
|
|
194
|
+
indent?: number | string | undefined;
|
|
195
|
+
/**
|
|
196
|
+
* Passed into `JSON.stringify`.
|
|
197
|
+
*/
|
|
198
|
+
replacer?: JsonReplacer;
|
|
199
|
+
/**
|
|
200
|
+
* Override the default `JSON.stringify` method.
|
|
201
|
+
*/
|
|
202
|
+
stringify?: (data: unknown, replacer: JsonReplacer, space: number | string | undefined) => string;
|
|
203
|
+
};
|
|
204
|
+
/**
|
|
205
|
+
* Options for the `findUp` and `findUpSync` functions.
|
|
206
|
+
*/
|
|
207
|
+
export type FindUpOptions = {
|
|
208
|
+
/**
|
|
209
|
+
* Whether to follow symbolic links.
|
|
210
|
+
* @default undefined (behaves like `true` for `findUp`, `false` for `findUpSync` due to `fs.stat` vs `fs.lstat`)
|
|
211
|
+
*/
|
|
212
|
+
allowSymlinks?: boolean;
|
|
213
|
+
/**
|
|
214
|
+
* The current working directory.
|
|
215
|
+
* @default process.cwd()
|
|
216
|
+
*/
|
|
217
|
+
cwd?: URL | string;
|
|
218
|
+
/**
|
|
219
|
+
* The directory to stop searching at.
|
|
220
|
+
* @default path.parse(cwd).root
|
|
221
|
+
*/
|
|
222
|
+
stopAt?: URL | string;
|
|
223
|
+
/**
|
|
224
|
+
* The type of path to find.
|
|
225
|
+
* @default "file"
|
|
226
|
+
*/
|
|
227
|
+
type?: "directory" | "file";
|
|
228
|
+
};
|
|
229
|
+
/**
|
|
230
|
+
* The result type for the name matcher function used in `findUp`.
|
|
231
|
+
* It can be a `PathLike` (string, Buffer, or URL), a Promise resolving to `PathLike` or `FIND_UP_STOP`,
|
|
232
|
+
* `FIND_UP_STOP` to stop the search, or `undefined` to continue.
|
|
233
|
+
*/
|
|
234
|
+
export type FindUpNameFnResult = PathLike | Promise<PathLike | typeof FIND_UP_STOP> | typeof FIND_UP_STOP | undefined;
|
|
235
|
+
/**
|
|
236
|
+
* Specifies the name(s) of the file or directory to search for in `findUp`.
|
|
237
|
+
* Can be a single name, an array of names, or a function that returns a name or `FIND_UP_STOP`.
|
|
238
|
+
*/
|
|
239
|
+
export type FindUpName = string[] | string | ((directory: string) => FindUpNameFnResult);
|
|
240
|
+
/**
|
|
241
|
+
* The result type for the name matcher function used in `findUpSync`.
|
|
242
|
+
* It can be a `PathLike` (string, Buffer, or URL), `FIND_UP_STOP` to stop the search,
|
|
243
|
+
* or `undefined` to continue.
|
|
244
|
+
*/
|
|
245
|
+
export type FindUpNameSyncFnResult = PathLike | typeof FIND_UP_STOP | undefined;
|
|
246
|
+
/**
|
|
247
|
+
* Specifies the name(s) of the file or directory to search for in `findUpSync`.
|
|
248
|
+
* Can be a single name, an array of names, or a function that returns a name or `FIND_UP_STOP`.
|
|
249
|
+
*/
|
|
250
|
+
export type FindUpNameSync = string[] | string | ((directory: string) => FindUpNameSyncFnResult);
|
|
251
|
+
/**
|
|
252
|
+
* Options for operations that might require retries, like `emptyDir` or `remove`.
|
|
253
|
+
*/
|
|
254
|
+
export type RetryOptions = {
|
|
255
|
+
/**
|
|
256
|
+
* If an `EBUSY`, `EMFILE`, `ENFILE`, `ENOTEMPTY`, or
|
|
257
|
+
* `EPERM` error is encountered, Node.js will retry the operation with a linear
|
|
258
|
+
* backoff wait of `retryDelay` ms longer on each try. This option represents the
|
|
259
|
+
* number of retries. This option is ignored if the `recursive` option is not
|
|
260
|
+
* `true` for operations that support it (like `rm`).
|
|
261
|
+
* @default 0
|
|
262
|
+
*/
|
|
263
|
+
maxRetries?: number | undefined;
|
|
264
|
+
/**
|
|
265
|
+
* The amount of time in milliseconds to wait between retries.
|
|
266
|
+
* This option is ignored if the `recursive` option is not `true` for operations that support it.
|
|
267
|
+
* @default 100
|
|
268
|
+
*/
|
|
269
|
+
retryDelay?: number | undefined;
|
|
270
|
+
};
|
|
271
|
+
/**
|
|
272
|
+
* Options for reading YAML files.
|
|
273
|
+
* Combines options from `yaml` library (DocumentOptions, ParseOptions, SchemaOptions, ToJSOptions)
|
|
274
|
+
* and custom {@link ReadFileOptions}.
|
|
275
|
+
* @template C - The type of compression used.
|
|
276
|
+
*/
|
|
277
|
+
export type ReadYamlOptions<C> = DocumentOptions & ParseOptions & ReadFileOptions<C> & SchemaOptions & ToJSOptions;
|
|
278
|
+
/**
|
|
279
|
+
* Type for the `reviver` parameter used in YAML deserialization, similar to `JSON.parse`'s reviver.
|
|
280
|
+
* A function that transforms the results. This function is called for each member of the object.
|
|
281
|
+
* If a member contains nested objects, the nested objects are transformed before the parent object is.
|
|
282
|
+
*/
|
|
283
|
+
export type YamlReviver = (key: unknown, value: unknown) => unknown;
|
|
284
|
+
/**
|
|
285
|
+
* Options for writing YAML files.
|
|
286
|
+
* Extends {@link WriteFileOptions} and includes options from the `yaml` library for stringification.
|
|
287
|
+
*/
|
|
288
|
+
export type WriteYamlOptions = CreateNodeOptions & DocumentOptions & ParseOptions & SchemaOptions & ToStringOptions & WriteFileOptions & {
|
|
289
|
+
/**
|
|
290
|
+
* Passed into `yaml.stringify` as the replacer argument.
|
|
291
|
+
*/
|
|
292
|
+
replacer?: YamlReplacer;
|
|
293
|
+
/**
|
|
294
|
+
* Passed into `yaml.stringify` as the space argument for indentation.
|
|
295
|
+
* Can be a number of spaces or a string (e.g., a tab character).
|
|
296
|
+
*/
|
|
297
|
+
space?: number | string;
|
|
298
|
+
};
|
|
299
|
+
export {};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Asserts that the provided contents are valid for writing to a file.
|
|
3
|
+
* Valid contents can be a string, an ArrayBuffer, or an ArrayBuffer view (e.g., Uint8Array).
|
|
4
|
+
* @param contents The file contents to validate.
|
|
5
|
+
* @throws {TypeError} If the contents are not a string, ArrayBuffer, or ArrayBuffer view.
|
|
6
|
+
* @example
|
|
7
|
+
* ```javascript
|
|
8
|
+
* import { assertValidFileContents } from "@visulima/fs"; // Assuming this util is exported
|
|
9
|
+
*
|
|
10
|
+
* try {
|
|
11
|
+
* assertValidFileContents("Hello, world!");
|
|
12
|
+
* assertValidFileContents(new Uint8Array([72, 101, 108, 108, 111])); // "Hello"
|
|
13
|
+
* assertValidFileContents(new ArrayBuffer(8));
|
|
14
|
+
* console.log("File contents are valid.");
|
|
15
|
+
* } catch (error) {
|
|
16
|
+
* console.error(error.message); // File contents must be a string, ArrayBuffer, or ArrayBuffer view.
|
|
17
|
+
* }
|
|
18
|
+
*
|
|
19
|
+
* try {
|
|
20
|
+
* assertValidFileContents(123); // Invalid content type
|
|
21
|
+
* } catch (error) {
|
|
22
|
+
* console.error(error.message); // File contents must be a string, ArrayBuffer, or ArrayBuffer view.
|
|
23
|
+
* }
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
declare const assertValidFileContents: (contents: any) => void;
|
|
27
|
+
export default assertValidFileContents;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Asserts that the provided path is a valid file or directory path.
|
|
3
|
+
* A valid path must be a non-empty string or a URL instance.
|
|
4
|
+
* @param fileOrDirectoryPath The path to validate.
|
|
5
|
+
* @throws {TypeError} If the path is not a non-empty string or a URL.
|
|
6
|
+
* @example
|
|
7
|
+
* ```javascript
|
|
8
|
+
* import { assertValidFileOrDirectoryPath } from "@visulima/fs"; // Assuming this util is exported
|
|
9
|
+
*
|
|
10
|
+
* try {
|
|
11
|
+
* assertValidFileOrDirectoryPath("/path/to/file.txt");
|
|
12
|
+
* assertValidFileOrDirectoryPath(new URL("file:///path/to/file.txt"));
|
|
13
|
+
* console.log("Path is valid.");
|
|
14
|
+
* } catch (error) {
|
|
15
|
+
* console.error(error.message); // Path must be a non-empty string or URL.
|
|
16
|
+
* }
|
|
17
|
+
*
|
|
18
|
+
* try {
|
|
19
|
+
* assertValidFileOrDirectoryPath(""); // Invalid path
|
|
20
|
+
* } catch (error) {
|
|
21
|
+
* console.error(error.message); // Path must be a non-empty string or URL.
|
|
22
|
+
* }
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
declare const assertValidFileOrDirectoryPath: (fileOrDirectoryPath: any) => void;
|
|
26
|
+
export default assertValidFileOrDirectoryPath;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { JsonValue } from "type-fest";
|
|
2
|
+
import type { CodeFrameOptions, JsonReviver } from "../types.d.ts";
|
|
3
|
+
declare function parseJson<T = JsonValue>(string: string, filename?: string, options?: CodeFrameOptions): T;
|
|
4
|
+
declare function parseJson<T = JsonValue>(string: string, reviver: JsonReviver, fileName?: string, options?: CodeFrameOptions): T;
|
|
5
|
+
export default parseJson;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Strips comments from a JSON string.
|
|
3
|
+
* Handles both single-line (//) and multi-line (/* ... *&#47;) comments.
|
|
4
|
+
* @param jsonString The JSON string possibly containing comments.
|
|
5
|
+
* @param [options] Optional configuration for stripping comments.
|
|
6
|
+
* @param [options.whitespace] If `true` (default), comments are replaced with whitespace to preserve line numbers and character positions. If `false`, comments are removed entirely.
|
|
7
|
+
* @returns The JSON string with comments stripped.
|
|
8
|
+
* @example
|
|
9
|
+
* ```javascript
|
|
10
|
+
* import { stripJsonComments } from "@visulima/fs"; // Assuming this util is exported
|
|
11
|
+
*
|
|
12
|
+
* const jsonWithComments = `{
|
|
13
|
+
* // This is a single-line comment
|
|
14
|
+
* "name": "John Doe",
|
|
15
|
+
* "age": 30, /* This is a
|
|
16
|
+
* multi-line comment */
|
|
17
|
+
* "city": "New York"
|
|
18
|
+
* }`;
|
|
19
|
+
*
|
|
20
|
+
* const stripped = stripJsonComments(jsonWithComments);
|
|
21
|
+
* console.log(stripped);
|
|
22
|
+
* // Output (with whitespace=true):
|
|
23
|
+
* // {
|
|
24
|
+
* //
|
|
25
|
+
* // "name": "John Doe",
|
|
26
|
+
* // "age": 30, /*
|
|
27
|
+
* //
|
|
28
|
+
* // "city": "New York"
|
|
29
|
+
* // }
|
|
30
|
+
*
|
|
31
|
+
* const strippedWithoutWhitespace = stripJsonComments(jsonWithComments, { whitespace: false });
|
|
32
|
+
* console.log(strippedWithoutWhitespace);
|
|
33
|
+
* // Output (with whitespace=false):
|
|
34
|
+
* // {
|
|
35
|
+
* // "name": "John Doe",
|
|
36
|
+
* // "age": 30,
|
|
37
|
+
* // "city": "New York"
|
|
38
|
+
* // }
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
declare const stripJsonComments: (jsonString: string, options?: {
|
|
42
|
+
whitespace?: boolean;
|
|
43
|
+
}) => string;
|
|
44
|
+
export default stripJsonComments;
|