UNPKG

14.1 kBTypeScriptView Raw
1/// <reference types="node" />
2
3// More docs also available at https://github.com/szwacz/fs-jetpack
4
5import * as fs from "fs";
6
7type AppendData = string | Buffer;
8
9type AppendOptions = {
10 mode?: string | number;
11};
12
13type OverwriteFunction = (
14 srcInspectData: InspectResult,
15 destInspectData: InspectResult
16) => boolean | Promise<boolean>;
17
18type CopyOptions = {
19 overwrite?: boolean | OverwriteFunction;
20 matching?: string | string[];
21 ignoreCase?: boolean;
22};
23
24type DirCriteria = {
25 empty?: boolean;
26 mode?: string | number;
27};
28
29export type ExistsResult = false | "dir" | "file" | "other";
30
31type FileOptions = {
32 content?: WritableData;
33 jsonIndent?: number;
34 mode?: string | number;
35};
36
37type FindOptions = {
38 matching?: string | string[];
39 filter?: (fileInspect: InspectResult) => boolean | Promise<boolean>;
40 files?: boolean;
41 directories?: boolean;
42 recursive?: boolean;
43 ignoreCase?: boolean;
44};
45
46export type Checksum = "md5" | "sha1" | "sha256" | "sha512";
47
48type InspectOptions = {
49 checksum?: Checksum;
50 mode?: boolean;
51 times?: boolean;
52 absolutePath?: boolean;
53 symlinks?: "report" | "follow";
54};
55
56export interface InspectResult {
57 name: string;
58 type: "file" | "dir" | "symlink";
59 size: number;
60 absolutePath?: string;
61 md5?: string;
62 sha1?: string;
63 sha256?: string;
64 sha512?: string;
65 mode?: number;
66 accessTime?: Date;
67 modifyTime?: Date;
68 changeTime?: Date;
69 birthTime?: Date;
70}
71
72type InspectTreeOptions = {
73 checksum?: Checksum;
74 relativePath?: boolean;
75 times?: boolean;
76 symlinks?: "report" | "follow";
77};
78
79export interface InspectTreeResult extends InspectResult {
80 relativePath: string;
81 children: InspectTreeResult[];
82}
83
84type WritableData = string | object | Array<any> | Buffer;
85
86type WriteOptions = {
87 mode?: string | number;
88 atomic?: boolean;
89 jsonIndent?: number;
90};
91
92type MoveOptions = {
93 overwrite?: boolean;
94};
95
96type RenameOptions = {
97 overwrite?: boolean;
98};
99
100type TmpDirOptions = {
101 prefix?: string;
102 basePath?: string;
103};
104
105// API has the same set of synchronous and asynchronous methods.
106// All async methods are promise based (no callbacks).
107
108// Commonly used naming convention in Node world is reversed in this library (no 'method' and 'methodSync' naming).
109// Asynchronous methods are those with 'Async' suffix, all methods without 'Async' in the name are synchronous.
110// Reason behind this is that it gives very nice look to blocking API. And promise-based, non-blocking code is verbose
111// anyway, so one more word is not much of a difference.
112
113export interface FSJetpack {
114 /**
115 * Returns Current Working Directory (CWD) for this instance of jetpack, or creates new jetpack object with given path as its internal CWD.
116 *
117 * **Note:** fs-jetpack never changes value of `process.cwd()`, the CWD we are talking about here is internal value inside every jetpack instance.
118 *
119 * @param path (optional) path (or many path parts) to become new CWD. Could be absolute, or relative. If relative path given new CWD will be resolved basing on current CWD of this jetpack instance.
120 */
121 cwd: {
122 (): string;
123 (...pathParts: string[]): FSJetpack;
124 };
125
126 path(...pathParts: string[]): string;
127
128 /**
129 * Appends given data to the end of file. If file or any parent directory doesn't exist it will be created.
130 *
131 * @param path the path to file.
132 * @param data data to append (can be `String` or `Buffer`).
133 * @param options
134 */
135 append(path: string, data: AppendData, options?: AppendOptions): void;
136
137 /**
138 * Appends given data to the end of file. If file or any parent directory doesn't exist it will be created.
139 *
140 * @param path the path to file.
141 * @param data data to append (can be `String` or `Buffer`).
142 * @param options
143 */
144 appendAsync(
145 path: string,
146 data: AppendData,
147 options?: AppendOptions
148 ): Promise<void>;
149
150 /**
151 * Copies given file or directory (with everything inside).
152 *
153 * @param from path to location you want to copy.
154 * @param to path to destination location, where the copy should be placed.
155 * @param options
156 */
157 copy(from: string, to: string, options?: CopyOptions): void;
158
159 /**
160 * Copies given file or directory (with everything inside).
161 *
162 * @param from path to location you want to copy.
163 * @param to path to destination location, where the copy should be placed.
164 * @param options
165 */
166 copyAsync(from: string, to: string, options?: CopyOptions): Promise<void>;
167
168 /**
169 * Just an alias to vanilla [fs.createReadStream](http://nodejs.org/api/fs.html#fs_fs_createreadstream_path_options).
170 */
171 createReadStream: typeof fs.createReadStream;
172 /**
173 * Just an alias to vanilla [fs.createWriteStream](http://nodejs.org/api/fs.html#fs_fs_createwritestream_path_options).
174 */
175 createWriteStream: typeof fs.createWriteStream;
176
177 /**
178 * Ensures that directory on given path exists and meets given criteria. If any criterium is not met it will be
179 * after this call. If any parent directory in `path` doesn't exist it will be created (like `mkdir -p`).
180 *
181 * @param path path to directory to examine
182 * @param criteria criteria to be met by the directory
183 */
184 dir(path: string, criteria?: DirCriteria): FSJetpack;
185
186 /**
187 * Ensures that directory on given path exists and meets given criteria. If any criterium is not met it will be
188 * after this call. If any parent directory in `path` doesn't exist it will be created (like `mkdir -p`).
189 *
190 * @param path path to directory to examine
191 * @param criteria criteria to be met by the directory
192 */
193 dirAsync(path: string, criteria?: DirCriteria): Promise<FSJetpack>;
194
195 /**
196 * Checks whether something exists on given `path`. This method returns values more specific than `true/false` to
197 * protect from errors like "I was expecting directory, but it was a file".
198 *
199 * @param path path to look for
200 *
201 * Returns:
202 * - `false` if path doesn't exist.
203 * - `"dir"` if path is a directory.
204 * - `"file"` if path is a file.
205 * - `"other"` if none of the above.
206 */
207 exists(path: string): ExistsResult;
208 /**
209 * Checks whether something exists on given `path`. This method returns values more specific than `true/false` to
210 * protect from errors like "I was expecting directory, but it was a file".
211 *
212 * @param path path to look for
213 *
214 * Returns:
215 * - `false` if path doesn't exist.
216 * - `"dir"` if path is a directory.
217 * - `"file"` if path is a file.
218 * - `"other"` if none of the above.
219 */
220 existsAsync(path: string): Promise<ExistsResult>;
221
222 /**
223 * Ensures that file exists and meets given criteria. If any criterium is not met it will be after this call. If
224 * any parent directory in `path` doesn't exist it will be created (like `mkdir -p`).
225 *
226 * @param path the path to create
227 * @param criteria criteria to be met by the file
228 */
229 file(path: string, criteria?: FileOptions): FSJetpack;
230
231 /**
232 * Ensures that file exists and meets given criteria. If any criterium is not met it will be after this call. If
233 * any parent directory in `path` doesn't exist it will be created (like `mkdir -p`).
234 *
235 * @param path the path to create
236 * @param criteria criteria to be met by the file
237 */
238 fileAsync(path: string, criteria?: FileOptions): Promise<FSJetpack>;
239
240 /**
241 * Finds in directory specified by `path` all files fulfilling `searchOptions`. Returned paths are relative to current CWD of jetpack instance.
242 * @param options search options
243 */
244 find(options?: FindOptions): string[];
245 /**
246 * Finds in directory specified by `path` all files fulfilling `searchOptions`. Returned paths are relative to current CWD of jetpack instance.
247 * @param startPath Path to start search in (all subdirectories will be searched).
248 * @param options search options
249 */
250 find(startPath: string, options?: FindOptions): string[];
251 /**
252 * Finds in directory specified by `path` all files fulfilling `searchOptions`. Returned paths are relative to current CWD of jetpack instance.
253 * @param options search options
254 */
255 findAsync(options?: FindOptions): Promise<string[]>;
256 /**
257 * Finds in directory specified by `path` all files fulfilling `searchOptions`. Returned paths are relative to current CWD of jetpack instance.
258 * @param startPath Path to start search in (all subdirectories will be searched).
259 * @param options search options
260 */
261 findAsync(startPath: string, options?: FindOptions): Promise<string[]>;
262
263 /**
264 * Inspects given path (replacement for `fs.stat`). Returned object by default contains only very basic, not
265 * platform-dependent properties (so you have something e.g. your unit tests can rely on), you can enable more
266 * properties through options object.
267 *
268 * @param path path to inspect
269 * @param options
270 */
271 inspect(path: string, options?: InspectOptions): InspectResult | undefined;
272
273 /**
274 * Inspects given path (replacement for `fs.stat`). Returned object by default contains only very basic, not
275 * platform-dependent properties (so you have something e.g. your unit tests can rely on), you can enable more
276 * properties through options object.
277 *
278 * @param path path to inspect
279 * @param options
280 */
281 inspectAsync(
282 path: string,
283 options?: InspectOptions
284 ): Promise<InspectResult | undefined>;
285
286 /**
287 * Calls inspect recursively on given path so it creates a tree of all directories and sub-directories inside it.
288 *
289 * @param path starting path to inspect
290 * @param options
291 */
292 inspectTree(
293 path: string,
294 options?: InspectTreeOptions
295 ): InspectTreeResult | undefined;
296
297 /**
298 * Calls inspect recursively on given path so it creates a tree of all directories and sub-directories inside it.
299 *
300 * @param path starting path to inspect
301 * @param options
302 */
303 inspectTreeAsync(
304 path: string,
305 options?: InspectTreeOptions
306 ): Promise<InspectTreeResult | undefined>;
307
308 /**
309 * Lists the contents of directory. Equivalent of `fs.readdir`.
310 * @param path directory to list
311 */
312 list(path?: string): string[] | undefined;
313 /**
314 * Lists the contents of directory. Equivalent of `fs.readdir`.
315 * @param path directory to list
316 */
317 listAsync(path?: string): Promise<string[] | undefined>;
318
319 /**
320 * Moves given path to new location.
321 *
322 * @param from path
323 * @param to path
324 * @param options
325 */
326 move(from: string, to: string, options?: MoveOptions): void;
327
328 /**
329 * Moves given path to new location.
330 *
331 * @param from path
332 * @param to path
333 * @param options
334 */
335 moveAsync(from: string, to: string, options?: MoveOptions): Promise<void>;
336
337 /**
338 * Reads content of file.
339 *
340 * @param path path to file
341 * @param returnAs a custom return types
342 */
343 read(path: string): string | undefined;
344 read(path: string, returnAs: "utf8"): string | undefined;
345 read(path: string, returnAs: "buffer"): Buffer | undefined;
346 read(path: string, returnAs: "json" | "jsonWithDates"): any | undefined;
347 /**
348 * Reads content of file.
349 *
350 * @param path path to file
351 * @param returnAs a custom return types
352 */
353 readAsync(path: string): Promise<string | undefined>;
354 readAsync(path: string, returnAs: "utf8"): Promise<string | undefined>;
355 readAsync(path: string, returnAs: "buffer"): Promise<Buffer | undefined>;
356 readAsync(
357 path: string,
358 returnAs: "json" | "jsonWithDates"
359 ): Promise<any | undefined>;
360
361 /**
362 * Deletes given path, no matter what it is (file, directory or non-empty directory). If path already doesn't exist
363 * terminates gracefully without throwing, so you can use it as 'ensure path doesn't exist'.
364 *
365 * @param path path to delete
366 */
367 remove(path?: string): void;
368 /**
369 * Deletes given path, no matter what it is (file, directory or non-empty directory). If path already doesn't exist
370 * terminates gracefully without throwing, so you can use it as 'ensure path doesn't exist'.
371 *
372 * @param path path to delete
373 */
374 removeAsync(path?: string): Promise<void>;
375
376 /**
377 * Renames given file or directory.
378 *
379 * @param path path to file being renamed
380 * @param newName just the name of the thing being renamed
381 * @param options
382 */
383 rename(path: string, newName: string, options?: RenameOptions): void;
384 /**
385 * Renames given file or directory.
386 *
387 * @param path path to file being renamed
388 * @param newName just the name of the thing being renamed
389 * @param options
390 */
391 renameAsync(
392 path: string,
393 newName: string,
394 options?: RenameOptions
395 ): Promise<void>;
396
397 /**
398 * Creates symbolic link.
399 *
400 * @param symlinkValue path where symbolic link should point.
401 * @param path where symbolic link should be put.
402 */
403 symlink(symlinkValue: string, path: string): void;
404
405 /**
406 * Creates symbolic link.
407 *
408 * @param symlinkValue path where symbolic link should point.
409 * @param path where symbolic link should be put.
410 */
411 symlinkAsync(symlinkValue: string, path: string): Promise<void>;
412
413 /**
414 * Creates temporary directory.
415 *
416 * @param options
417 */
418 tmpDir(options?: TmpDirOptions): FSJetpack;
419
420 /**
421 * Creates temporary directory.
422 *
423 * @param options
424 */
425 tmpDirAsync(options?: TmpDirOptions): Promise<FSJetpack>;
426
427 /**
428 * Writes data to file. If any parent directory in `path` doesn't exist it will be created (like `mkdir -p`).
429 *
430 * @param path path to file
431 * @param data data to be written. This could be `String`, `Buffer`, `Object` or `Array` (if last two used, the data will be outputted into file as JSON).
432 * @param options
433 */
434 write(path: string, data: WritableData, options?: WriteOptions): void;
435
436 /**
437 * Writes data to file. If any parent directory in `path` doesn't exist it will be created (like `mkdir -p`).
438 *
439 * @param path path to file
440 * @param data data to be written. This could be `String`, `Buffer`, `Object` or `Array` (if last two used, the data will be outputted into file as JSON).
441 * @param options
442 */
443 writeAsync(
444 path: string,
445 data: WritableData,
446 options?: WriteOptions
447 ): Promise<void>;
448}