async-file-tried 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.ts ADDED
@@ -0,0 +1,472 @@
1
+ import * as path from 'node:path';
2
+ import * as fsp from 'node:fs/promises';
3
+ import * as fsNormal from 'node:fs';
4
+ import { fileURLToPath } from 'node:url';
5
+ import {CopyOptions} from "node:fs";
6
+
7
+ /****************************************
8
+ * Helpers
9
+ ****************************************/
10
+
11
+ /**
12
+ *
13
+ * @param filePath
14
+ * @returns {*}
15
+ */
16
+ const __resolvePath = (filePath: string|Array<string>) => {
17
+ return Array.isArray(filePath)
18
+ ? path.join(...filePath)
19
+ : filePath
20
+ };
21
+
22
+ /** asyncHandler
23
+ *
24
+ * @param func
25
+ * @returns {Promise<*[]>}
26
+ */
27
+ const asyncHandler = async (func: Function) => {
28
+ try {
29
+ return [await func(), null]
30
+ }
31
+ catch (error) {
32
+ return [null, error]
33
+ }
34
+ };
35
+
36
+
37
+ /****************************************
38
+ * fs interface
39
+ ****************************************/
40
+
41
+ /** access
42
+ *
43
+ * @param path
44
+ * @param mode
45
+ * @returns {Promise<*[]>}
46
+ */
47
+ const access = async (path: string|Array<string>, mode?: number|string) => {
48
+ const pathResolved = __resolvePath(path);
49
+ return await asyncHandler(() => fsp.access(pathResolved, <any>mode));
50
+ };
51
+
52
+ /** appendFile
53
+ *
54
+ * @param path
55
+ * @param data
56
+ * @param options
57
+ * @returns {Promise<*[]>}
58
+ */
59
+ const appendFile = async (path: string|Array<string>, data: any, options?: { encoding?: Encoding; mode?: number|string; flag?: Flags; }) => {
60
+ const pathResolved = __resolvePath(path);
61
+ return await asyncHandler(() => fsp.appendFile(pathResolved, data, options));
62
+ };
63
+
64
+ /** chmod
65
+ *
66
+ * @param path
67
+ * @param mode
68
+ * @returns {Promise<*[]>}
69
+ */
70
+ const chmod = async (path: string|Array<string>, mode?: number|string) => {
71
+ const pathResolved = __resolvePath(path);
72
+ return await asyncHandler(() => fsp.chmod(pathResolved, <any>mode));
73
+ };
74
+
75
+ /** chown
76
+ *
77
+ * @param path
78
+ * @param uid
79
+ * @param gid
80
+ * @returns {Promise<*[]>}
81
+ */
82
+ const chown = async (path: string|Array<string>, uid: number, gid: number) => {
83
+ const pathResolved = __resolvePath(path);
84
+ return await asyncHandler(() => fsp.chown(pathResolved, uid, gid));
85
+ };
86
+
87
+ /** copyFile
88
+ *
89
+ * @param srcPath
90
+ * @param destPath
91
+ * @param flags
92
+ * @returns {Promise<*[]>}
93
+ */
94
+ const copyFile = async (srcPath: string|Array<string>, destPath: string|Array<string>, flags?: number) => {
95
+ const srcPathResolved = __resolvePath(srcPath);
96
+ const destPathResolved = __resolvePath(destPath);
97
+ return await asyncHandler(() => fsp.copyFile(srcPathResolved, destPathResolved, flags));
98
+ };
99
+
100
+ /** cp
101
+ *
102
+ * @param srcPath
103
+ * @param destPath
104
+ * @returns {Promise<*[]>}
105
+ */
106
+ const cp = async (srcPath: string|Array<string>, destPath: string|Array<string>, options?: CopyOptions) => {
107
+ const srcPathResolved = __resolvePath(srcPath);
108
+ const destPathResolved = __resolvePath(destPath);
109
+ return await asyncHandler(() => fsp.cp(srcPathResolved, destPathResolved, options));
110
+ };
111
+
112
+ /** lchmod
113
+ *
114
+ * @param path
115
+ * @param mode
116
+ * @returns {Promise<*[]>}
117
+ */
118
+ const lchmod = async (path: string|Array<string>, mode?: number|string) => {
119
+ const pathResolved = __resolvePath(path);
120
+ return await asyncHandler(() => fsp.lchmod(pathResolved, <any>mode));
121
+ };
122
+
123
+ /** lchown
124
+ *
125
+ * @param path
126
+ * @param mode
127
+ * @returns {Promise<*[]>}
128
+ */
129
+ const lchown = async (path: string|Array<string>, uid: number, gid: number) => {
130
+ const pathResolved = __resolvePath(path);
131
+ return await asyncHandler(() => fsp.lchown(pathResolved, uid, gid));
132
+ };
133
+
134
+ /** link
135
+ *
136
+ * @param existingPath
137
+ * @param newPath
138
+ * @returns {Promise<*[]>}
139
+ */
140
+ const link = async (existingPath: string|Array<string>, newPath: string|Array<string>) => {
141
+ const existingPathResolved = __resolvePath(existingPath);
142
+ const newPathResolved = __resolvePath(newPath);
143
+ return await asyncHandler(() => fsp.link(existingPathResolved, newPathResolved));
144
+ };
145
+
146
+ /** lstat
147
+ *
148
+ * @param path
149
+ * @param options
150
+ * @returns {Promise<*[]>}
151
+ */
152
+ const lstat = async (path: string|Array<string>, options?: object) => {
153
+ const pathResolved = __resolvePath(path);
154
+ return await asyncHandler(() => fsp.lstat(pathResolved, options));
155
+ };
156
+
157
+ /** lutimes
158
+ *
159
+ * @param path
160
+ * @param atime
161
+ * @param mtime
162
+ * @returns {Promise<*[]>}
163
+ */
164
+ const lutimes = async (path: string|Array<string>, atime: Date|number, mtime: Date|number) => {
165
+ const pathResolved = __resolvePath(path);
166
+ return await asyncHandler(() => fsp.lutimes(pathResolved, atime, mtime));
167
+ };
168
+
169
+ /** mkdir
170
+ *
171
+ * @param path
172
+ * @param options
173
+ * @returns {Promise<*[]>}
174
+ */
175
+ const mkdir = async (path: string|Array<string>, options?: number|string) => {
176
+ const pathResolved = __resolvePath(path);
177
+ return await asyncHandler(() => fsp.mkdir(pathResolved, options));
178
+ };
179
+
180
+ /**
181
+ *
182
+ * @param prefix
183
+ * @param options
184
+ * @returns {Promise<*[]>}
185
+ */
186
+ const mkdtemp = async (prefix: string, encoding?: Encoding) => {
187
+ return await asyncHandler(() => fsp.mkdtemp(prefix, encoding));
188
+ };
189
+
190
+ /** open
191
+ *
192
+ * @param path
193
+ * @param flags
194
+ * @param mode
195
+ * @returns {Promise<*[]>}
196
+ */
197
+ const open = async (path: string|Array<string>, flags?: Flags, mode?: number|string) => {
198
+ const pathResolved = __resolvePath(path);
199
+ return await asyncHandler(() => fsp.open(pathResolved, flags, <any>mode));
200
+ };
201
+
202
+ /** opendir
203
+ *
204
+ * @param path
205
+ * @param options
206
+ * @returns {Promise<*[]>}
207
+ */
208
+ const opendir = async (path: string|Array<string>, options?: object) => {
209
+ const pathResolved = __resolvePath(path);
210
+ return await asyncHandler(() => fsp.opendir(pathResolved, options));
211
+ };
212
+
213
+ /** readFile
214
+ *
215
+ * @param path
216
+ * @param options
217
+ * @returns {Promise<*[]>}
218
+ */
219
+ const readFile = async (path: string|Array<string>, options?: Encoding) => {
220
+ const pathResolved = __resolvePath(path);
221
+ return await asyncHandler(() => fsp.readFile(pathResolved, options));
222
+ };
223
+
224
+ /** readdir
225
+ *
226
+ * @param path
227
+ * @param options
228
+ * @returns {Promise<*[]>}
229
+ */
230
+ const readdir = async (path: string|Array<string>, options?: object) => {
231
+ const pathResolved = __resolvePath(path);
232
+ return await asyncHandler(() => fsp.readdir(pathResolved, options));
233
+ };
234
+
235
+ /** readlink
236
+ *
237
+ * @param path
238
+ * @param options
239
+ * @returns {Promise<*[]>}
240
+ */
241
+ const readlink = async (path: string|Array<string>, options?: object|string) => {
242
+ const pathResolved = __resolvePath(path);
243
+ return await asyncHandler(() => fsp.readlink(pathResolved, options));
244
+ };
245
+
246
+ /** realpath
247
+ *
248
+ * @param path
249
+ * @param options
250
+ * @returns {Promise<*[]>}
251
+ */
252
+ const realpath = async (path: string|Array<string>, options?: object) => {
253
+ const pathResolved = __resolvePath(path);
254
+ return await asyncHandler(() => fsp.realpath(pathResolved, options));
255
+ };
256
+
257
+ /** rename
258
+ *
259
+ * @param oldPath
260
+ * @param newPath
261
+ * @returns {Promise<*[]>}
262
+ */
263
+ const rename = async (oldPath: string|Array<string>, newPath: string|Array<string>) => {
264
+ const oldPathResolved = __resolvePath(oldPath);
265
+ const newPathResolved = __resolvePath(newPath);
266
+ return await asyncHandler(() => fsp.rename(oldPathResolved, newPathResolved));
267
+ };
268
+
269
+ /** rm
270
+ *
271
+ * @param path
272
+ * @param options
273
+ * @returns {Promise<*[]>}
274
+ */
275
+ const rm = async (path: string|Array<string>, options?: object) => {
276
+ const pathResolved = __resolvePath(path);
277
+ return await asyncHandler(() => fsp.rm(pathResolved, options));
278
+ };
279
+
280
+ /** rmdir
281
+ *
282
+ * @param path
283
+ * @param options
284
+ * @returns {Promise<*[]>}
285
+ */
286
+ const rmdir = async (path: string|Array<string>, options?: object) => {
287
+ const pathResolved = __resolvePath(path);
288
+ return await asyncHandler(() => fsp.rmdir(pathResolved, options));
289
+ };
290
+
291
+ /** stat
292
+ *
293
+ * @param path
294
+ * @param options
295
+ * @returns {Promise<*[]>}
296
+ */
297
+ const stat = async (path: string|Array<string>, options?: object) => {
298
+ const pathResolved = __resolvePath(path);
299
+ return await asyncHandler(() => fsp.stat(pathResolved, options));
300
+ };
301
+
302
+
303
+ /** symlink
304
+ *
305
+ * @param target
306
+ * @param path
307
+ * @param type
308
+ * @returns {Promise<*[]>}
309
+ */
310
+ const symlink = async (target: string|Array<string>, path: string|Array<string>, type?: string) => {
311
+ const targetResolved = __resolvePath(target);
312
+ const pathResolved = __resolvePath(path);
313
+ return await asyncHandler(() => fsp.symlink(targetResolved, pathResolved, type));
314
+ };
315
+
316
+ /** truncate
317
+ *
318
+ * @param path
319
+ * @param len
320
+ * @returns {Promise<*[]>}
321
+ */
322
+ const truncate = async (path: string|Array<string>, len:number) => {
323
+ const pathResolved = __resolvePath(path);
324
+ return await asyncHandler(() => fsp.truncate(pathResolved, len));
325
+ };
326
+
327
+ /** unlink
328
+ *
329
+ * @param path
330
+ * @returns {Promise<*[]>}
331
+ */
332
+ const unlink = async (path: string|Array<string>) => {
333
+ const pathResolved = __resolvePath(path);
334
+ return await asyncHandler(() => fsp.unlink(pathResolved));
335
+ };
336
+
337
+ /** utimes
338
+ *
339
+ * @param path
340
+ * @param atime
341
+ * @param mtime
342
+ * @returns {Promise<*[]>}
343
+ */
344
+ const utimes = async (path: string|Array<string>, atime: number|string|Date, mtime: number|string|Date) => {
345
+ const pathResolved = __resolvePath(path);
346
+ return await asyncHandler(() => fsp.utimes(pathResolved, atime, mtime));
347
+ };
348
+
349
+ /** watch
350
+ *
351
+ * @param filename
352
+ * @param options
353
+ * @returns {Promise<*[]>}
354
+ */
355
+ const watch = async (filename: string|Array<string>, options?: object|string) => {
356
+ const filenameResolved = __resolvePath(filename);
357
+ return await asyncHandler(() => fsp.watch(filenameResolved, options));
358
+ };
359
+
360
+ /** writeFile
361
+ *
362
+ * @param path
363
+ * @param data
364
+ * @param options
365
+ * @returns {Promise<*[]>}
366
+ */
367
+ const writeFile = async (path: string|Array<string>, data: any, options?: Encoding) => {
368
+ const pathResolved = __resolvePath(path);
369
+ return await asyncHandler(() => fsp.writeFile(pathResolved, data, options));
370
+ };
371
+
372
+
373
+ /****************************************
374
+ * Constants
375
+ ****************************************/
376
+
377
+ /**
378
+ * fs.constants
379
+ */
380
+ const constants = fsNormal.constants;
381
+
382
+ /**
383
+ * __dirname in Node.js when using ES6 modules
384
+ */
385
+ const __dirname = path.dirname(fileURLToPath(import.meta.url));
386
+
387
+ /**
388
+ * __filename in Node.js when using ES6 modules
389
+ */
390
+ const __filename = fileURLToPath(import.meta.url);
391
+
392
+
393
+ /****************************************
394
+ * Extra
395
+ ****************************************/
396
+
397
+ /**
398
+ *
399
+ * @param path
400
+ * @param options
401
+ * @returns {Promise<any>}
402
+ */
403
+ const readJson = async (path: string|Array<string>, options?: Encoding) => {
404
+ const pathResolved = __resolvePath(path);
405
+ let [res, err] = await asyncHandler(() => fsp.readFile(pathResolved, options ?? 'utf8'));
406
+ return res
407
+ ? [JSON.parse(res), null]
408
+ : [null, err];
409
+ };
410
+
411
+ /**
412
+ *
413
+ * @param path
414
+ * @param data
415
+ * @param options
416
+ * @returns {Promise<*[]>}
417
+ */
418
+ const writeJson = async (path: string|Array<string>, data: Object, options?: Encoding)=> {
419
+ const pathResolved = __resolvePath(path);
420
+ return await asyncHandler(() => fsp.writeFile(pathResolved, JSON.stringify(data, null, 2), options ?? 'utf8'));
421
+ };
422
+
423
+
424
+ /****************************************
425
+ * Export
426
+ ****************************************/
427
+
428
+ export type Encoding = 'ascii'|'base64'|'binary'|'hex'|'ucs2'|'utf16le'|'utf8';
429
+ export type Flags = 'r'|'r+'|'rs'|'rs+'|'w'|'wx'|'w+'|'wx+'|'a'|'ax'|'a+'|'ax+';
430
+
431
+ const fs = {
432
+ access,
433
+ appendFile,
434
+ chmod,
435
+ chown,
436
+ copyFile,
437
+ cp,
438
+ lchmod,
439
+ lchown,
440
+ link,
441
+ lstat,
442
+ lutimes,
443
+ mkdir,
444
+ mkdtemp,
445
+ open,
446
+ opendir,
447
+ readFile,
448
+ readdir,
449
+ readlink,
450
+ realpath,
451
+ rename,
452
+ rm,
453
+ rmdir,
454
+ stat,
455
+ symlink,
456
+ truncate,
457
+ unlink,
458
+ utimes,
459
+ watch,
460
+ writeFile,
461
+ constants,
462
+ __dirname,
463
+ __filename,
464
+ asyncHandler,
465
+ readJson,
466
+ writeJson,
467
+ };
468
+
469
+ if (process.env.NODE_ENV === 'TEST')
470
+ fs['__resolvePath'] = __resolvePath;
471
+
472
+ export default fs;
package/package.json ADDED
@@ -0,0 +1,54 @@
1
+ {
2
+ "name": "async-file-tried",
3
+ "version": "1.0.0",
4
+ "description": "A try-catch wrapper around node’s fs/promises.",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "type": "module",
8
+ "scripts": {
9
+ "test": "NODE_ENV=TEST c8 mocha",
10
+ "coverage": "NODE_ENV=TEST c8 report --reporter=text-lcov > coverage/lcov-report/lcov.info"
11
+ },
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "git+https://fwalzel@b:coverageitbucket.org/fwalzel/async-file-tried.git"
15
+ },
16
+ "author": "Florian Walzel",
17
+ "license": "MIT",
18
+ "bugs": {
19
+ "url": "https://bitbucket.org/fwalzel/async-file-tried/issues"
20
+ },
21
+ "homepage": "https://bitbucket.org/fwalzel/async-file-tried#readme",
22
+ "dependencies": {
23
+ "async-file": "^2.0.2"
24
+ },
25
+ "devDependencies": {
26
+ "@types/node": "^20.9.5",
27
+ "chai": "^4.3.6",
28
+ "mocha": "^10.1.0",
29
+ "test-console": "^2.0.0"
30
+ },
31
+ "nyc": {
32
+ "reporter": [
33
+ "text",
34
+ "lcov"
35
+ ],
36
+ "all": true,
37
+ "include": [
38
+ "dist/index.js"
39
+ ],
40
+ "exclude": [
41
+ "node_modules/**",
42
+ "test/*.test.js",
43
+ "index.ts"
44
+ ]
45
+ },
46
+ "keywords": [
47
+ "fs promises",
48
+ "fs async",
49
+ "node file system",
50
+ "better error handling for fs",
51
+ "try catch wrapper",
52
+ "Typescript for fs"
53
+ ]
54
+ }