@catbee/utils 0.0.1 → 0.0.3

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.
Files changed (197) hide show
  1. package/README.md +303 -107
  2. package/build/esm/config.d.ts +13 -1
  3. package/build/esm/config.js +16 -4
  4. package/build/esm/config.js.map +1 -1
  5. package/build/esm/index.d.ts +20 -18
  6. package/build/esm/index.js +20 -18
  7. package/build/esm/index.js.map +1 -1
  8. package/build/esm/types/api-response.d.ts +83 -1
  9. package/build/esm/types/api-response.js.map +1 -1
  10. package/build/esm/utils/array.utils.d.ts +65 -2
  11. package/build/esm/utils/array.utils.js +147 -20
  12. package/build/esm/utils/array.utils.js.map +1 -1
  13. package/build/esm/utils/async.utils.d.ts +56 -1
  14. package/build/esm/utils/async.utils.js +166 -11
  15. package/build/esm/utils/async.utils.js.map +1 -1
  16. package/build/esm/utils/cache.utils.d.ts +67 -4
  17. package/build/esm/utils/cache.utils.js +210 -28
  18. package/build/esm/utils/cache.utils.js.map +1 -1
  19. package/build/esm/utils/context-store.utils.d.ts +102 -1
  20. package/build/esm/utils/context-store.utils.js +185 -3
  21. package/build/esm/utils/context-store.utils.js.map +1 -1
  22. package/build/esm/utils/crypto.utils.d.ts +110 -8
  23. package/build/esm/utils/crypto.utils.js +298 -23
  24. package/build/esm/utils/crypto.utils.js.map +1 -1
  25. package/build/esm/utils/decorators.utils.d.ts +40 -0
  26. package/build/esm/utils/decorators.utils.js +312 -0
  27. package/build/esm/utils/decorators.utils.js.map +1 -0
  28. package/build/esm/utils/dir.utils.d.ts +122 -1
  29. package/build/esm/utils/dir.utils.js +578 -3
  30. package/build/esm/utils/dir.utils.js.map +1 -1
  31. package/build/esm/utils/env.utils.d.ts +83 -0
  32. package/build/esm/utils/env.utils.js +235 -6
  33. package/build/esm/utils/env.utils.js.map +1 -1
  34. package/build/esm/utils/exception.utils.d.ts +117 -1
  35. package/build/esm/utils/exception.utils.js +331 -12
  36. package/build/esm/utils/exception.utils.js.map +1 -1
  37. package/build/esm/utils/fs.utils.d.ts +132 -0
  38. package/build/esm/utils/fs.utils.js +421 -4
  39. package/build/esm/utils/fs.utils.js.map +1 -1
  40. package/build/esm/utils/http-status-codes.d.ts +87 -178
  41. package/build/esm/utils/http-status-codes.js +92 -178
  42. package/build/esm/utils/http-status-codes.js.map +1 -1
  43. package/build/esm/utils/id.utils.d.ts +11 -10
  44. package/build/esm/utils/id.utils.js +30 -21
  45. package/build/esm/utils/id.utils.js.map +1 -1
  46. package/build/esm/utils/logger.utils.d.ts +35 -2
  47. package/build/esm/utils/logger.utils.js +68 -14
  48. package/build/esm/utils/logger.utils.js.map +1 -1
  49. package/build/esm/utils/middleware.utils.d.ts +73 -0
  50. package/build/esm/utils/middleware.utils.js +158 -0
  51. package/build/esm/utils/middleware.utils.js.map +1 -0
  52. package/build/esm/utils/obj.utils.d.ts +65 -3
  53. package/build/esm/utils/obj.utils.js +206 -18
  54. package/build/esm/utils/obj.utils.js.map +1 -1
  55. package/build/esm/utils/response.utils.d.ts +103 -2
  56. package/build/esm/utils/response.utils.js +156 -3
  57. package/build/esm/utils/response.utils.js.map +1 -1
  58. package/build/esm/utils/string.utils.d.ts +61 -5
  59. package/build/esm/utils/string.utils.js +109 -19
  60. package/build/esm/utils/string.utils.js.map +1 -1
  61. package/build/esm/utils/url.utils.d.ts +117 -0
  62. package/build/esm/utils/url.utils.js +287 -2
  63. package/build/esm/utils/url.utils.js.map +1 -1
  64. package/build/esm/utils/validate.utils.d.ts +110 -0
  65. package/build/esm/utils/validate.utils.js +190 -8
  66. package/build/esm/utils/validate.utils.js.map +1 -1
  67. package/build/esnext/config.d.ts +13 -1
  68. package/build/esnext/config.js +16 -4
  69. package/build/esnext/config.js.map +1 -1
  70. package/build/esnext/index.d.ts +20 -18
  71. package/build/esnext/index.js +20 -18
  72. package/build/esnext/index.js.map +1 -1
  73. package/build/esnext/types/api-response.d.ts +83 -1
  74. package/build/esnext/types/api-response.js.map +1 -1
  75. package/build/esnext/utils/array.utils.d.ts +65 -2
  76. package/build/esnext/utils/array.utils.js +140 -21
  77. package/build/esnext/utils/array.utils.js.map +1 -1
  78. package/build/esnext/utils/async.utils.d.ts +56 -1
  79. package/build/esnext/utils/async.utils.js +130 -14
  80. package/build/esnext/utils/async.utils.js.map +1 -1
  81. package/build/esnext/utils/cache.utils.d.ts +67 -4
  82. package/build/esnext/utils/cache.utils.js +137 -9
  83. package/build/esnext/utils/cache.utils.js.map +1 -1
  84. package/build/esnext/utils/context-store.utils.d.ts +102 -1
  85. package/build/esnext/utils/context-store.utils.js +171 -3
  86. package/build/esnext/utils/context-store.utils.js.map +1 -1
  87. package/build/esnext/utils/crypto.utils.d.ts +110 -8
  88. package/build/esnext/utils/crypto.utils.js +199 -19
  89. package/build/esnext/utils/crypto.utils.js.map +1 -1
  90. package/build/esnext/utils/decorators.utils.d.ts +40 -0
  91. package/build/esnext/utils/decorators.utils.js +155 -0
  92. package/build/esnext/utils/decorators.utils.js.map +1 -0
  93. package/build/esnext/utils/dir.utils.d.ts +122 -1
  94. package/build/esnext/utils/dir.utils.js +307 -3
  95. package/build/esnext/utils/dir.utils.js.map +1 -1
  96. package/build/esnext/utils/env.utils.d.ts +83 -0
  97. package/build/esnext/utils/env.utils.js +191 -8
  98. package/build/esnext/utils/env.utils.js.map +1 -1
  99. package/build/esnext/utils/exception.utils.d.ts +117 -1
  100. package/build/esnext/utils/exception.utils.js +222 -12
  101. package/build/esnext/utils/exception.utils.js.map +1 -1
  102. package/build/esnext/utils/fs.utils.d.ts +132 -0
  103. package/build/esnext/utils/fs.utils.js +268 -4
  104. package/build/esnext/utils/fs.utils.js.map +1 -1
  105. package/build/esnext/utils/http-status-codes.d.ts +87 -178
  106. package/build/esnext/utils/http-status-codes.js +92 -178
  107. package/build/esnext/utils/http-status-codes.js.map +1 -1
  108. package/build/esnext/utils/id.utils.d.ts +11 -10
  109. package/build/esnext/utils/id.utils.js +28 -20
  110. package/build/esnext/utils/id.utils.js.map +1 -1
  111. package/build/esnext/utils/logger.utils.d.ts +35 -2
  112. package/build/esnext/utils/logger.utils.js +56 -14
  113. package/build/esnext/utils/logger.utils.js.map +1 -1
  114. package/build/esnext/utils/middleware.utils.d.ts +73 -0
  115. package/build/esnext/utils/middleware.utils.js +157 -0
  116. package/build/esnext/utils/middleware.utils.js.map +1 -0
  117. package/build/esnext/utils/obj.utils.d.ts +65 -3
  118. package/build/esnext/utils/obj.utils.js +169 -14
  119. package/build/esnext/utils/obj.utils.js.map +1 -1
  120. package/build/esnext/utils/response.utils.d.ts +103 -2
  121. package/build/esnext/utils/response.utils.js +141 -3
  122. package/build/esnext/utils/response.utils.js.map +1 -1
  123. package/build/esnext/utils/string.utils.d.ts +61 -5
  124. package/build/esnext/utils/string.utils.js +110 -14
  125. package/build/esnext/utils/string.utils.js.map +1 -1
  126. package/build/esnext/utils/url.utils.d.ts +117 -0
  127. package/build/esnext/utils/url.utils.js +248 -2
  128. package/build/esnext/utils/url.utils.js.map +1 -1
  129. package/build/esnext/utils/validate.utils.d.ts +110 -0
  130. package/build/esnext/utils/validate.utils.js +190 -8
  131. package/build/esnext/utils/validate.utils.js.map +1 -1
  132. package/build/src/config.d.ts +13 -1
  133. package/build/src/config.js +15 -3
  134. package/build/src/config.js.map +1 -1
  135. package/build/src/index.d.ts +20 -18
  136. package/build/src/index.js +2 -0
  137. package/build/src/index.js.map +1 -1
  138. package/build/src/types/api-response.d.ts +83 -1
  139. package/build/src/types/api-response.js.map +1 -1
  140. package/build/src/utils/array.utils.d.ts +65 -2
  141. package/build/src/utils/array.utils.js +147 -22
  142. package/build/src/utils/array.utils.js.map +1 -1
  143. package/build/src/utils/async.utils.d.ts +56 -1
  144. package/build/src/utils/async.utils.js +137 -17
  145. package/build/src/utils/async.utils.js.map +1 -1
  146. package/build/src/utils/cache.utils.d.ts +67 -4
  147. package/build/src/utils/cache.utils.js +137 -9
  148. package/build/src/utils/cache.utils.js.map +1 -1
  149. package/build/src/utils/context-store.utils.d.ts +102 -1
  150. package/build/src/utils/context-store.utils.js +173 -3
  151. package/build/src/utils/context-store.utils.js.map +1 -1
  152. package/build/src/utils/crypto.utils.d.ts +110 -8
  153. package/build/src/utils/crypto.utils.js +214 -27
  154. package/build/src/utils/crypto.utils.js.map +1 -1
  155. package/build/src/utils/decorators.utils.d.ts +40 -0
  156. package/build/src/utils/decorators.utils.js +165 -0
  157. package/build/src/utils/decorators.utils.js.map +1 -0
  158. package/build/src/utils/dir.utils.d.ts +122 -1
  159. package/build/src/utils/dir.utils.js +314 -0
  160. package/build/src/utils/dir.utils.js.map +1 -1
  161. package/build/src/utils/env.utils.d.ts +83 -0
  162. package/build/src/utils/env.utils.js +192 -9
  163. package/build/src/utils/env.utils.js.map +1 -1
  164. package/build/src/utils/exception.utils.d.ts +117 -1
  165. package/build/src/utils/exception.utils.js +233 -11
  166. package/build/src/utils/exception.utils.js.map +1 -1
  167. package/build/src/utils/fs.utils.d.ts +132 -0
  168. package/build/src/utils/fs.utils.js +281 -3
  169. package/build/src/utils/fs.utils.js.map +1 -1
  170. package/build/src/utils/http-status-codes.d.ts +87 -178
  171. package/build/src/utils/http-status-codes.js +92 -178
  172. package/build/src/utils/http-status-codes.js.map +1 -1
  173. package/build/src/utils/id.utils.d.ts +11 -10
  174. package/build/src/utils/id.utils.js +28 -20
  175. package/build/src/utils/id.utils.js.map +1 -1
  176. package/build/src/utils/logger.utils.d.ts +35 -2
  177. package/build/src/utils/logger.utils.js +56 -11
  178. package/build/src/utils/logger.utils.js.map +1 -1
  179. package/build/src/utils/middleware.utils.d.ts +73 -0
  180. package/build/src/utils/middleware.utils.js +163 -0
  181. package/build/src/utils/middleware.utils.js.map +1 -0
  182. package/build/src/utils/obj.utils.d.ts +65 -3
  183. package/build/src/utils/obj.utils.js +179 -18
  184. package/build/src/utils/obj.utils.js.map +1 -1
  185. package/build/src/utils/response.utils.d.ts +103 -2
  186. package/build/src/utils/response.utils.js +147 -2
  187. package/build/src/utils/response.utils.js.map +1 -1
  188. package/build/src/utils/string.utils.d.ts +61 -5
  189. package/build/src/utils/string.utils.js +122 -20
  190. package/build/src/utils/string.utils.js.map +1 -1
  191. package/build/src/utils/url.utils.d.ts +117 -0
  192. package/build/src/utils/url.utils.js +256 -1
  193. package/build/src/utils/url.utils.js.map +1 -1
  194. package/build/src/utils/validate.utils.d.ts +110 -0
  195. package/build/src/utils/validate.utils.js +204 -8
  196. package/build/src/utils/validate.utils.js.map +1 -1
  197. package/package.json +20 -12
@@ -1,3 +1,5 @@
1
+ import { BufferEncoding } from './crypto.utils';
2
+ import { Stats } from 'fs';
1
3
  /**
2
4
  * Checks whether a file or directory exists at the given path.
3
5
  *
@@ -29,4 +31,134 @@ export declare function writeJsonFile(path: string, data: any, space?: number):
29
31
  * @returns {Promise<boolean>} Resolves to `true` if the file was deleted or didn't exist, `false` if deletion failed.
30
32
  */
31
33
  export declare function deleteFileIfExists(path: string): Promise<boolean>;
34
+ /**
35
+ * Reads a text file from the specified path.
36
+ *
37
+ * @param {string} path - The path to the text file.
38
+ * @param {BufferEncoding} [encoding="utf-8"] - The encoding to use.
39
+ * @returns {Promise<string | null>} The file contents, or `null` if reading fails.
40
+ */
41
+ export declare function readTextFile(path: string, encoding?: BufferEncoding): Promise<string | null>;
42
+ /**
43
+ * Writes text content to a file.
44
+ *
45
+ * @param {string} path - The destination file path.
46
+ * @param {string} content - The text content to write.
47
+ * @param {BufferEncoding} [encoding="utf-8"] - The encoding to use.
48
+ * @returns {Promise<boolean>} Resolves to `true` if successful, `false` otherwise.
49
+ */
50
+ export declare function writeTextFile(path: string, content: string, encoding?: BufferEncoding): Promise<boolean>;
51
+ /**
52
+ * Appends text content to a file.
53
+ *
54
+ * @param {string} path - The file path to append to.
55
+ * @param {string} content - The text content to append.
56
+ * @param {BufferEncoding} [encoding="utf-8"] - The encoding to use.
57
+ * @returns {Promise<boolean>} Resolves to `true` if successful, `false` otherwise.
58
+ */
59
+ export declare function appendTextFile(path: string, content: string, encoding?: BufferEncoding): Promise<boolean>;
60
+ /**
61
+ * Copies a file from source to destination.
62
+ *
63
+ * @param {string} source - Source file path.
64
+ * @param {string} destination - Destination file path.
65
+ * @param {boolean} [overwrite=false] - Whether to overwrite if destination exists.
66
+ * @returns {Promise<boolean>} Resolves to `true` if successful, `false` otherwise.
67
+ */
68
+ export declare function copyFile(source: string, destination: string, overwrite?: boolean): Promise<boolean>;
69
+ /**
70
+ * Renames/moves a file.
71
+ *
72
+ * @param {string} oldPath - Current file path.
73
+ * @param {string} newPath - New file path.
74
+ * @returns {Promise<boolean>} Resolves to `true` if successful, `false` otherwise.
75
+ */
76
+ export declare function moveFile(oldPath: string, newPath: string): Promise<boolean>;
77
+ /**
78
+ * Gets file stats if the file exists.
79
+ *
80
+ * @param {string} path - Path to the file.
81
+ * @returns {Promise<fs.Stats | null>} File stats object or null if file doesn't exist.
82
+ */
83
+ export declare function getFileStats(path: string): Promise<Stats | null>;
84
+ /**
85
+ * Creates a temporary file with optional content.
86
+ *
87
+ * @param {object} [options] - Options for creating the temp file.
88
+ * @param {string} [options.prefix="tmp-"] - Filename prefix.
89
+ * @param {string} [options.extension=""] - File extension.
90
+ * @param {string} [options.dir] - Directory to create the file in (defaults to OS temp dir).
91
+ * @param {string | Buffer} [options.content] - Optional content to write to the file.
92
+ * @returns {Promise<string>} Path to the created temporary file.
93
+ */
94
+ export declare function createTempFile({ prefix, extension, dir, content }?: {
95
+ prefix?: string;
96
+ extension?: string;
97
+ dir?: string;
98
+ content?: string | Buffer;
99
+ }): Promise<string>;
100
+ /**
101
+ * Streams a file from source to destination.
102
+ * Useful for large files to avoid loading the entire file into memory.
103
+ *
104
+ * @param {string} source - Source file path.
105
+ * @param {string} destination - Destination file path.
106
+ * @returns {Promise<void>} Resolves when streaming completes.
107
+ * @throws {Error} If streaming fails.
108
+ */
109
+ export declare function streamFile(source: string, destination: string): Promise<void>;
110
+ /**
111
+ * Reads a directory and returns file names.
112
+ *
113
+ * @param {string} dirPath - Path to the directory.
114
+ * @param {object} [options] - Options for reading the directory.
115
+ * @param {boolean} [options.fullPaths=false] - Whether to return full paths.
116
+ * @param {RegExp} [options.filter] - Optional regex to filter files.
117
+ * @returns {Promise<string[]>} Array of file names or paths.
118
+ * @throws {Error} If directory cannot be read.
119
+ */
120
+ export declare function readDirectory(dirPath: string, options?: {
121
+ fullPaths?: boolean;
122
+ filter?: RegExp;
123
+ }): Promise<string[]>;
124
+ /**
125
+ * Creates a directory if it doesn't exist.
126
+ *
127
+ * @param {string} dirPath - Path to the directory.
128
+ * @param {boolean} [recursive=true] - Whether to create parent directories.
129
+ * @returns {Promise<boolean>} Resolves to `true` if successful, `false` otherwise.
130
+ */
131
+ export declare function createDirectory(dirPath: string, recursive?: boolean): Promise<boolean>;
132
+ /**
133
+ * Safely reads and parses a JSON file with error details.
134
+ *
135
+ * @typeParam T - The expected type of the parsed JSON object.
136
+ * @param {string} path - The path to the JSON file.
137
+ * @returns {Promise<{ data: T | null; error: Error | null }>} Object with data and error properties.
138
+ */
139
+ export declare function safeReadJsonFile<T = any>(path: string): Promise<{
140
+ data: T | null;
141
+ error: Error | null;
142
+ }>;
143
+ /**
144
+ * Checks if a path points to a file (not a directory).
145
+ *
146
+ * @param {string} path - Path to check.
147
+ * @returns {Promise<boolean>} True if the path is a file, false otherwise.
148
+ */
149
+ export declare function isFile(path: string): Promise<boolean>;
150
+ /**
151
+ * Gets the size of a file in bytes.
152
+ *
153
+ * @param {string} path - Path to the file.
154
+ * @returns {Promise<number>} Size in bytes or -1 if file doesn't exist.
155
+ */
156
+ export declare function getFileSize(path: string): Promise<number>;
157
+ /**
158
+ * Reads a file as a Buffer.
159
+ *
160
+ * @param {string} path - Path to the file.
161
+ * @returns {Promise<Buffer | null>} File contents as Buffer or null if reading fails.
162
+ */
163
+ export declare function readFileBuffer(path: string): Promise<Buffer | null>;
32
164
  //# sourceMappingURL=fs.utils.d.ts.map
@@ -34,7 +34,12 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
34
34
  if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
35
35
  }
36
36
  };
37
- import fs from "fs/promises";
37
+ import fs from 'fs/promises';
38
+ import { createReadStream, createWriteStream } from 'fs';
39
+ import { tmpdir } from 'os';
40
+ import path from 'path';
41
+ import { pipeline } from 'stream/promises';
42
+ import { randomUUID } from 'crypto';
38
43
  /**
39
44
  * Checks whether a file or directory exists at the given path.
40
45
  *
@@ -74,7 +79,7 @@ export function readJsonFile(path) {
74
79
  switch (_b.label) {
75
80
  case 0:
76
81
  _b.trys.push([0, 2, , 3]);
77
- return [4 /*yield*/, fs.readFile(path, "utf-8")];
82
+ return [4 /*yield*/, fs.readFile(path, 'utf-8')];
78
83
  case 1:
79
84
  data = _b.sent();
80
85
  return [2 /*return*/, JSON.parse(data)];
@@ -102,7 +107,7 @@ export function writeJsonFile(path_1, data_1) {
102
107
  switch (_a.label) {
103
108
  case 0:
104
109
  json = JSON.stringify(data, null, space);
105
- return [4 /*yield*/, fs.writeFile(path, json, "utf-8")];
110
+ return [4 /*yield*/, fs.writeFile(path, json, 'utf-8')];
106
111
  case 1:
107
112
  _a.sent();
108
113
  return [2 /*return*/];
@@ -129,7 +134,7 @@ export function deleteFileIfExists(path) {
129
134
  return [2 /*return*/, true];
130
135
  case 2:
131
136
  err_1 = _a.sent();
132
- if (err_1.code === "ENOENT")
137
+ if (err_1.code === 'ENOENT')
133
138
  return [2 /*return*/, true];
134
139
  return [2 /*return*/, false];
135
140
  case 3: return [2 /*return*/];
@@ -137,4 +142,416 @@ export function deleteFileIfExists(path) {
137
142
  });
138
143
  });
139
144
  }
145
+ /**
146
+ * Reads a text file from the specified path.
147
+ *
148
+ * @param {string} path - The path to the text file.
149
+ * @param {BufferEncoding} [encoding="utf-8"] - The encoding to use.
150
+ * @returns {Promise<string | null>} The file contents, or `null` if reading fails.
151
+ */
152
+ export function readTextFile(path_1) {
153
+ return __awaiter(this, arguments, void 0, function (path, encoding) {
154
+ var _a;
155
+ if (encoding === void 0) { encoding = 'utf-8'; }
156
+ return __generator(this, function (_b) {
157
+ switch (_b.label) {
158
+ case 0:
159
+ _b.trys.push([0, 2, , 3]);
160
+ return [4 /*yield*/, fs.readFile(path, encoding)];
161
+ case 1: return [2 /*return*/, _b.sent()];
162
+ case 2:
163
+ _a = _b.sent();
164
+ return [2 /*return*/, null];
165
+ case 3: return [2 /*return*/];
166
+ }
167
+ });
168
+ });
169
+ }
170
+ /**
171
+ * Writes text content to a file.
172
+ *
173
+ * @param {string} path - The destination file path.
174
+ * @param {string} content - The text content to write.
175
+ * @param {BufferEncoding} [encoding="utf-8"] - The encoding to use.
176
+ * @returns {Promise<boolean>} Resolves to `true` if successful, `false` otherwise.
177
+ */
178
+ export function writeTextFile(path_1, content_1) {
179
+ return __awaiter(this, arguments, void 0, function (path, content, encoding) {
180
+ var _a;
181
+ if (encoding === void 0) { encoding = 'utf-8'; }
182
+ return __generator(this, function (_b) {
183
+ switch (_b.label) {
184
+ case 0:
185
+ _b.trys.push([0, 2, , 3]);
186
+ return [4 /*yield*/, fs.writeFile(path, content, encoding)];
187
+ case 1:
188
+ _b.sent();
189
+ return [2 /*return*/, true];
190
+ case 2:
191
+ _a = _b.sent();
192
+ return [2 /*return*/, false];
193
+ case 3: return [2 /*return*/];
194
+ }
195
+ });
196
+ });
197
+ }
198
+ /**
199
+ * Appends text content to a file.
200
+ *
201
+ * @param {string} path - The file path to append to.
202
+ * @param {string} content - The text content to append.
203
+ * @param {BufferEncoding} [encoding="utf-8"] - The encoding to use.
204
+ * @returns {Promise<boolean>} Resolves to `true` if successful, `false` otherwise.
205
+ */
206
+ export function appendTextFile(path_1, content_1) {
207
+ return __awaiter(this, arguments, void 0, function (path, content, encoding) {
208
+ var _a;
209
+ if (encoding === void 0) { encoding = 'utf-8'; }
210
+ return __generator(this, function (_b) {
211
+ switch (_b.label) {
212
+ case 0:
213
+ _b.trys.push([0, 2, , 3]);
214
+ return [4 /*yield*/, fs.appendFile(path, content, encoding)];
215
+ case 1:
216
+ _b.sent();
217
+ return [2 /*return*/, true];
218
+ case 2:
219
+ _a = _b.sent();
220
+ return [2 /*return*/, false];
221
+ case 3: return [2 /*return*/];
222
+ }
223
+ });
224
+ });
225
+ }
226
+ /**
227
+ * Copies a file from source to destination.
228
+ *
229
+ * @param {string} source - Source file path.
230
+ * @param {string} destination - Destination file path.
231
+ * @param {boolean} [overwrite=false] - Whether to overwrite if destination exists.
232
+ * @returns {Promise<boolean>} Resolves to `true` if successful, `false` otherwise.
233
+ */
234
+ export function copyFile(source_1, destination_1) {
235
+ return __awaiter(this, arguments, void 0, function (source, destination, overwrite) {
236
+ var flags, _a;
237
+ if (overwrite === void 0) { overwrite = false; }
238
+ return __generator(this, function (_b) {
239
+ switch (_b.label) {
240
+ case 0:
241
+ _b.trys.push([0, 2, , 3]);
242
+ flags = overwrite ? 0 : fs.constants.COPYFILE_EXCL;
243
+ return [4 /*yield*/, fs.copyFile(source, destination, flags)];
244
+ case 1:
245
+ _b.sent();
246
+ return [2 /*return*/, true];
247
+ case 2:
248
+ _a = _b.sent();
249
+ return [2 /*return*/, false];
250
+ case 3: return [2 /*return*/];
251
+ }
252
+ });
253
+ });
254
+ }
255
+ /**
256
+ * Renames/moves a file.
257
+ *
258
+ * @param {string} oldPath - Current file path.
259
+ * @param {string} newPath - New file path.
260
+ * @returns {Promise<boolean>} Resolves to `true` if successful, `false` otherwise.
261
+ */
262
+ export function moveFile(oldPath, newPath) {
263
+ return __awaiter(this, void 0, void 0, function () {
264
+ var _a, _b;
265
+ return __generator(this, function (_c) {
266
+ switch (_c.label) {
267
+ case 0:
268
+ _c.trys.push([0, 2, , 8]);
269
+ return [4 /*yield*/, fs.rename(oldPath, newPath)];
270
+ case 1:
271
+ _c.sent();
272
+ return [2 /*return*/, true];
273
+ case 2:
274
+ _a = _c.sent();
275
+ _c.label = 3;
276
+ case 3:
277
+ _c.trys.push([3, 6, , 7]);
278
+ return [4 /*yield*/, fs.copyFile(oldPath, newPath)];
279
+ case 4:
280
+ _c.sent();
281
+ return [4 /*yield*/, fs.unlink(oldPath)];
282
+ case 5:
283
+ _c.sent();
284
+ return [2 /*return*/, true];
285
+ case 6:
286
+ _b = _c.sent();
287
+ return [2 /*return*/, false];
288
+ case 7: return [3 /*break*/, 8];
289
+ case 8: return [2 /*return*/];
290
+ }
291
+ });
292
+ });
293
+ }
294
+ /**
295
+ * Gets file stats if the file exists.
296
+ *
297
+ * @param {string} path - Path to the file.
298
+ * @returns {Promise<fs.Stats | null>} File stats object or null if file doesn't exist.
299
+ */
300
+ export function getFileStats(path) {
301
+ return __awaiter(this, void 0, void 0, function () {
302
+ var _a;
303
+ return __generator(this, function (_b) {
304
+ switch (_b.label) {
305
+ case 0:
306
+ _b.trys.push([0, 2, , 3]);
307
+ return [4 /*yield*/, fs.stat(path)];
308
+ case 1: return [2 /*return*/, _b.sent()];
309
+ case 2:
310
+ _a = _b.sent();
311
+ return [2 /*return*/, null];
312
+ case 3: return [2 /*return*/];
313
+ }
314
+ });
315
+ });
316
+ }
317
+ /**
318
+ * Creates a temporary file with optional content.
319
+ *
320
+ * @param {object} [options] - Options for creating the temp file.
321
+ * @param {string} [options.prefix="tmp-"] - Filename prefix.
322
+ * @param {string} [options.extension=""] - File extension.
323
+ * @param {string} [options.dir] - Directory to create the file in (defaults to OS temp dir).
324
+ * @param {string | Buffer} [options.content] - Optional content to write to the file.
325
+ * @returns {Promise<string>} Path to the created temporary file.
326
+ */
327
+ export function createTempFile() {
328
+ return __awaiter(this, arguments, void 0, function (_a) {
329
+ var ext, filename, filepath;
330
+ var _b = _a === void 0 ? {} : _a, _c = _b.prefix, prefix = _c === void 0 ? 'tmp-' : _c, _d = _b.extension, extension = _d === void 0 ? '' : _d, _e = _b.dir, dir = _e === void 0 ? tmpdir() : _e, content = _b.content;
331
+ return __generator(this, function (_f) {
332
+ switch (_f.label) {
333
+ case 0:
334
+ ext = extension ? (extension.startsWith('.') ? extension : ".".concat(extension)) : '';
335
+ filename = "".concat(prefix).concat(randomUUID()).concat(ext);
336
+ filepath = path.join(dir, filename);
337
+ if (!content) return [3 /*break*/, 2];
338
+ return [4 /*yield*/, fs.writeFile(filepath, content)];
339
+ case 1:
340
+ _f.sent();
341
+ return [3 /*break*/, 4];
342
+ case 2: return [4 /*yield*/, fs.writeFile(filepath, '')];
343
+ case 3:
344
+ _f.sent();
345
+ _f.label = 4;
346
+ case 4: return [2 /*return*/, filepath];
347
+ }
348
+ });
349
+ });
350
+ }
351
+ /**
352
+ * Streams a file from source to destination.
353
+ * Useful for large files to avoid loading the entire file into memory.
354
+ *
355
+ * @param {string} source - Source file path.
356
+ * @param {string} destination - Destination file path.
357
+ * @returns {Promise<void>} Resolves when streaming completes.
358
+ * @throws {Error} If streaming fails.
359
+ */
360
+ export function streamFile(source, destination) {
361
+ return __awaiter(this, void 0, void 0, function () {
362
+ var readStream, writeStream, error_1;
363
+ return __generator(this, function (_a) {
364
+ switch (_a.label) {
365
+ case 0:
366
+ readStream = createReadStream(source);
367
+ writeStream = createWriteStream(destination);
368
+ _a.label = 1;
369
+ case 1:
370
+ _a.trys.push([1, 3, , 4]);
371
+ return [4 /*yield*/, pipeline(readStream, writeStream)];
372
+ case 2:
373
+ _a.sent();
374
+ return [3 /*break*/, 4];
375
+ case 3:
376
+ error_1 = _a.sent();
377
+ // Ensure both streams are closed in case of error
378
+ readStream.destroy();
379
+ writeStream.destroy();
380
+ throw error_1;
381
+ case 4: return [2 /*return*/];
382
+ }
383
+ });
384
+ });
385
+ }
386
+ /**
387
+ * Reads a directory and returns file names.
388
+ *
389
+ * @param {string} dirPath - Path to the directory.
390
+ * @param {object} [options] - Options for reading the directory.
391
+ * @param {boolean} [options.fullPaths=false] - Whether to return full paths.
392
+ * @param {RegExp} [options.filter] - Optional regex to filter files.
393
+ * @returns {Promise<string[]>} Array of file names or paths.
394
+ * @throws {Error} If directory cannot be read.
395
+ */
396
+ export function readDirectory(dirPath_1) {
397
+ return __awaiter(this, arguments, void 0, function (dirPath, options) {
398
+ var files, result;
399
+ if (options === void 0) { options = {}; }
400
+ return __generator(this, function (_a) {
401
+ switch (_a.label) {
402
+ case 0: return [4 /*yield*/, fs.readdir(dirPath)];
403
+ case 1:
404
+ files = _a.sent();
405
+ result = files;
406
+ if (options.filter) {
407
+ result = result.filter(function (file) { return options.filter.test(file); });
408
+ }
409
+ if (options.fullPaths) {
410
+ result = result.map(function (file) { return path.join(dirPath, file); });
411
+ }
412
+ return [2 /*return*/, result];
413
+ }
414
+ });
415
+ });
416
+ }
417
+ /**
418
+ * Creates a directory if it doesn't exist.
419
+ *
420
+ * @param {string} dirPath - Path to the directory.
421
+ * @param {boolean} [recursive=true] - Whether to create parent directories.
422
+ * @returns {Promise<boolean>} Resolves to `true` if successful, `false` otherwise.
423
+ */
424
+ export function createDirectory(dirPath_1) {
425
+ return __awaiter(this, arguments, void 0, function (dirPath, recursive) {
426
+ var err_2;
427
+ if (recursive === void 0) { recursive = true; }
428
+ return __generator(this, function (_a) {
429
+ switch (_a.label) {
430
+ case 0:
431
+ _a.trys.push([0, 2, , 3]);
432
+ return [4 /*yield*/, fs.mkdir(dirPath, { recursive: recursive })];
433
+ case 1:
434
+ _a.sent();
435
+ return [2 /*return*/, true];
436
+ case 2:
437
+ err_2 = _a.sent();
438
+ // Consider it success if directory already exists
439
+ return [2 /*return*/, err_2.code === 'EEXIST'];
440
+ case 3: return [2 /*return*/];
441
+ }
442
+ });
443
+ });
444
+ }
445
+ /**
446
+ * Safely reads and parses a JSON file with error details.
447
+ *
448
+ * @typeParam T - The expected type of the parsed JSON object.
449
+ * @param {string} path - The path to the JSON file.
450
+ * @returns {Promise<{ data: T | null; error: Error | null }>} Object with data and error properties.
451
+ */
452
+ export function safeReadJsonFile(path) {
453
+ return __awaiter(this, void 0, void 0, function () {
454
+ var content, data, error_2;
455
+ return __generator(this, function (_a) {
456
+ switch (_a.label) {
457
+ case 0:
458
+ _a.trys.push([0, 2, , 3]);
459
+ return [4 /*yield*/, fs.readFile(path, 'utf-8')];
460
+ case 1:
461
+ content = _a.sent();
462
+ try {
463
+ data = JSON.parse(content);
464
+ return [2 /*return*/, { data: data, error: null }];
465
+ }
466
+ catch (error) {
467
+ return [2 /*return*/, {
468
+ data: null,
469
+ error: new Error("Invalid JSON in file ".concat(path, ": ").concat(error.message))
470
+ }];
471
+ }
472
+ return [3 /*break*/, 3];
473
+ case 2:
474
+ error_2 = _a.sent();
475
+ return [2 /*return*/, {
476
+ data: null,
477
+ error: new Error("Failed to read file ".concat(path, ": ").concat(error_2.message))
478
+ }];
479
+ case 3: return [2 /*return*/];
480
+ }
481
+ });
482
+ });
483
+ }
484
+ /**
485
+ * Checks if a path points to a file (not a directory).
486
+ *
487
+ * @param {string} path - Path to check.
488
+ * @returns {Promise<boolean>} True if the path is a file, false otherwise.
489
+ */
490
+ export function isFile(path) {
491
+ return __awaiter(this, void 0, void 0, function () {
492
+ var stat, _a;
493
+ return __generator(this, function (_b) {
494
+ switch (_b.label) {
495
+ case 0:
496
+ _b.trys.push([0, 2, , 3]);
497
+ return [4 /*yield*/, fs.stat(path)];
498
+ case 1:
499
+ stat = _b.sent();
500
+ return [2 /*return*/, stat.isFile()];
501
+ case 2:
502
+ _a = _b.sent();
503
+ return [2 /*return*/, false];
504
+ case 3: return [2 /*return*/];
505
+ }
506
+ });
507
+ });
508
+ }
509
+ /**
510
+ * Gets the size of a file in bytes.
511
+ *
512
+ * @param {string} path - Path to the file.
513
+ * @returns {Promise<number>} Size in bytes or -1 if file doesn't exist.
514
+ */
515
+ export function getFileSize(path) {
516
+ return __awaiter(this, void 0, void 0, function () {
517
+ var stat, _a;
518
+ return __generator(this, function (_b) {
519
+ switch (_b.label) {
520
+ case 0:
521
+ _b.trys.push([0, 2, , 3]);
522
+ return [4 /*yield*/, fs.stat(path)];
523
+ case 1:
524
+ stat = _b.sent();
525
+ return [2 /*return*/, stat.size];
526
+ case 2:
527
+ _a = _b.sent();
528
+ return [2 /*return*/, -1];
529
+ case 3: return [2 /*return*/];
530
+ }
531
+ });
532
+ });
533
+ }
534
+ /**
535
+ * Reads a file as a Buffer.
536
+ *
537
+ * @param {string} path - Path to the file.
538
+ * @returns {Promise<Buffer | null>} File contents as Buffer or null if reading fails.
539
+ */
540
+ export function readFileBuffer(path) {
541
+ return __awaiter(this, void 0, void 0, function () {
542
+ var _a;
543
+ return __generator(this, function (_b) {
544
+ switch (_b.label) {
545
+ case 0:
546
+ _b.trys.push([0, 2, , 3]);
547
+ return [4 /*yield*/, fs.readFile(path)];
548
+ case 1: return [2 /*return*/, _b.sent()];
549
+ case 2:
550
+ _a = _b.sent();
551
+ return [2 /*return*/, null];
552
+ case 3: return [2 /*return*/];
553
+ }
554
+ });
555
+ });
556
+ }
140
557
  //# sourceMappingURL=fs.utils.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"fs.utils.js","sourceRoot":"","sources":["../../../src/utils/fs.utils.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,OAAO,EAAE,MAAM,aAAa,CAAC;AAE7B;;;;;GAKG;AACH,MAAM,UAAgB,UAAU,CAAC,IAAY;;;;;;;oBAEzC,qBAAM,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,EAAA;;oBAArB,SAAqB,CAAC;oBACtB,sBAAO,IAAI,EAAC;;;oBAEZ,sBAAO,KAAK,EAAC;;;;;CAEhB;AAED;;;;;;GAMG;AACH,MAAM,UAAgB,YAAY,CAAU,IAAY;;;;;;;oBAEvC,qBAAM,EAAE,CAAC,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAC,EAAA;;oBAAvC,IAAI,GAAG,SAAgC;oBAC7C,sBAAO,IAAI,CAAC,KAAK,CAAC,IAAI,CAAM,EAAC;;;oBAE7B,sBAAO,IAAI,EAAC;;;;;CAEf;AAED;;;;;;;GAOG;AACH,MAAM,UAAgB,aAAa;wDACjC,IAAY,EACZ,IAAS,EACT,KAAS;;QAAT,sBAAA,EAAA,SAAS;;;;oBAEH,IAAI,GAAG,IAAI,CAAC,SAAS,CAAC,IAAI,EAAE,IAAI,EAAE,KAAK,CAAC,CAAC;oBAC/C,qBAAM,EAAE,CAAC,SAAS,CAAC,IAAI,EAAE,IAAI,EAAE,OAAO,CAAC,EAAA;;oBAAvC,SAAuC,CAAC;;;;;CACzC;AAED;;;;;GAKG;AACH,MAAM,UAAgB,kBAAkB,CAAC,IAAY;;;;;;;oBAEjD,qBAAM,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,EAAA;;oBAArB,SAAqB,CAAC;oBACtB,sBAAO,IAAI,EAAC;;;oBAEZ,IAAI,KAAG,CAAC,IAAI,KAAK,QAAQ;wBAAE,sBAAO,IAAI,EAAC;oBACvC,sBAAO,KAAK,EAAC;;;;;CAEhB","sourcesContent":["import fs from \"fs/promises\";\n\n/**\n * Checks whether a file or directory exists at the given path.\n *\n * @param {string} path - The file or directory path to check.\n * @returns {Promise<boolean>} Resolves to `true` if the path exists, `false` otherwise.\n */\nexport async function fileExists(path: string): Promise<boolean> {\n try {\n await fs.access(path);\n return true;\n } catch {\n return false;\n }\n}\n\n/**\n * Reads and parses a JSON file from the specified path.\n *\n * @typeParam T - The expected type of the parsed JSON object.\n * @param {string} path - The path to the JSON file.\n * @returns {Promise<T | null>} The parsed object, or `null` if reading or parsing fails.\n */\nexport async function readJsonFile<T = any>(path: string): Promise<T | null> {\n try {\n const data = await fs.readFile(path, \"utf-8\");\n return JSON.parse(data) as T;\n } catch {\n return null;\n }\n}\n\n/**\n * Writes a JavaScript object to a file as formatted JSON.\n *\n * @param {string} path - The destination file path.\n * @param {any} data - The object to serialize and write.\n * @param {number} [space=2] - Number of spaces for JSON formatting.\n * @returns {Promise<void>} Resolves when writing is complete.\n */\nexport async function writeJsonFile(\n path: string,\n data: any,\n space = 2,\n): Promise<void> {\n const json = JSON.stringify(data, null, space);\n await fs.writeFile(path, json, \"utf-8\");\n}\n\n/**\n * Deletes a file if it exists.\n *\n * @param {string} path - The file path to delete.\n * @returns {Promise<boolean>} Resolves to `true` if the file was deleted or didn't exist, `false` if deletion failed.\n */\nexport async function deleteFileIfExists(path: string): Promise<boolean> {\n try {\n await fs.unlink(path);\n return true;\n } catch (err: any) {\n if (err.code === \"ENOENT\") return true;\n return false;\n }\n}\n"]}
1
+ {"version":3,"file":"fs.utils.js","sourceRoot":"","sources":["../../../src/utils/fs.utils.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AACA,OAAO,EAAE,MAAM,aAAa,CAAC;AAC7B,OAAO,EAAE,gBAAgB,EAAE,iBAAiB,EAAS,MAAM,IAAI,CAAC;AAChE,OAAO,EAAE,MAAM,EAAE,MAAM,IAAI,CAAC;AAC5B,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAC3C,OAAO,EAAE,UAAU,EAAE,MAAM,QAAQ,CAAC;AAEpC;;;;;GAKG;AACH,MAAM,UAAgB,UAAU,CAAC,IAAY;;;;;;;oBAEzC,qBAAM,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,EAAA;;oBAArB,SAAqB,CAAC;oBACtB,sBAAO,IAAI,EAAC;;;oBAEZ,sBAAO,KAAK,EAAC;;;;;CAEhB;AAED;;;;;;GAMG;AACH,MAAM,UAAgB,YAAY,CAAU,IAAY;;;;;;;oBAEvC,qBAAM,EAAE,CAAC,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAC,EAAA;;oBAAvC,IAAI,GAAG,SAAgC;oBAC7C,sBAAO,IAAI,CAAC,KAAK,CAAC,IAAI,CAAM,EAAC;;;oBAE7B,sBAAO,IAAI,EAAC;;;;;CAEf;AAED;;;;;;;GAOG;AACH,MAAM,UAAgB,aAAa;wDAAC,IAAY,EAAE,IAAS,EAAE,KAAS;;QAAT,sBAAA,EAAA,SAAS;;;;oBAC9D,IAAI,GAAG,IAAI,CAAC,SAAS,CAAC,IAAI,EAAE,IAAI,EAAE,KAAK,CAAC,CAAC;oBAC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ACJ,CAAC;;;;oBAED,sBAAO;4BACL,IAAI,EAAE,IAAI;4BACV,KAAK,EAAE,IAAI,KAAK,CAAC,8BAAuB,IAAI,eAAK,OAAK,CAAC,OAAO,CAAE,CAAC;yBAClE,EAAC;;;;;CAEL;AAED;;;;;GAKG;AACH,MAAM,UAAgB,MAAM,CAAC,IAAY;;;;;;;oBAExB,qBAAM,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,EAAA;;oBAA1B,IAAI,GAAG,SAAmB;oBAChC,sBAAO,IAAI,CAAC,MAAM,EAAE,EAAC;;;oBAErB,sBAAO,KAAK,EAAC;;;;;CAEhB;AAED;;;;;GAKG;AACH,MAAM,UAAgB,WAAW,CAAC,IAAY;;;;;;;oBAE7B,qBAAM,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,EAAA;;oBAA1B,IAAI,GAAG,SAAmB;oBAChC,sBAAO,IAAI,CAAC,IAAI,EAAC;;;oBAEjB,sBAAO,CAAC,CAAC,EAAC;;;;;CAEb;AAED;;;;;GAKG;AACH,MAAM,UAAgB,cAAc,CAAC,IAAY;;;;;;;oBAEtC,qBAAM,EAAE,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAA;wBAA9B,sBAAO,SAAuB,EAAC;;;oBAE/B,sBAAO,IAAI,EAAC;;;;;CAEf","sourcesContent":["import { BufferEncoding } from './crypto.utils';\nimport fs from 'fs/promises';\nimport { createReadStream, createWriteStream, Stats } from 'fs';\nimport { tmpdir } from 'os';\nimport path from 'path';\nimport { pipeline } from 'stream/promises';\nimport { randomUUID } from 'crypto';\n\n/**\n * Checks whether a file or directory exists at the given path.\n *\n * @param {string} path - The file or directory path to check.\n * @returns {Promise<boolean>} Resolves to `true` if the path exists, `false` otherwise.\n */\nexport async function fileExists(path: string): Promise<boolean> {\n try {\n await fs.access(path);\n return true;\n } catch {\n return false;\n }\n}\n\n/**\n * Reads and parses a JSON file from the specified path.\n *\n * @typeParam T - The expected type of the parsed JSON object.\n * @param {string} path - The path to the JSON file.\n * @returns {Promise<T | null>} The parsed object, or `null` if reading or parsing fails.\n */\nexport async function readJsonFile<T = any>(path: string): Promise<T | null> {\n try {\n const data = await fs.readFile(path, 'utf-8');\n return JSON.parse(data) as T;\n } catch {\n return null;\n }\n}\n\n/**\n * Writes a JavaScript object to a file as formatted JSON.\n *\n * @param {string} path - The destination file path.\n * @param {any} data - The object to serialize and write.\n * @param {number} [space=2] - Number of spaces for JSON formatting.\n * @returns {Promise<void>} Resolves when writing is complete.\n */\nexport async function writeJsonFile(path: string, data: any, space = 2): Promise<void> {\n const json = JSON.stringify(data, null, space);\n await fs.writeFile(path, json, 'utf-8');\n}\n\n/**\n * Deletes a file if it exists.\n *\n * @param {string} path - The file path to delete.\n * @returns {Promise<boolean>} Resolves to `true` if the file was deleted or didn't exist, `false` if deletion failed.\n */\nexport async function deleteFileIfExists(path: string): Promise<boolean> {\n try {\n await fs.unlink(path);\n return true;\n } catch (err: any) {\n if (err.code === 'ENOENT') return true;\n return false;\n }\n}\n\n/**\n * Reads a text file from the specified path.\n *\n * @param {string} path - The path to the text file.\n * @param {BufferEncoding} [encoding=\"utf-8\"] - The encoding to use.\n * @returns {Promise<string | null>} The file contents, or `null` if reading fails.\n */\nexport async function readTextFile(path: string, encoding: BufferEncoding = 'utf-8'): Promise<string | null> {\n try {\n return await fs.readFile(path, encoding);\n } catch {\n return null;\n }\n}\n\n/**\n * Writes text content to a file.\n *\n * @param {string} path - The destination file path.\n * @param {string} content - The text content to write.\n * @param {BufferEncoding} [encoding=\"utf-8\"] - The encoding to use.\n * @returns {Promise<boolean>} Resolves to `true` if successful, `false` otherwise.\n */\nexport async function writeTextFile(\n path: string,\n content: string,\n encoding: BufferEncoding = 'utf-8'\n): Promise<boolean> {\n try {\n await fs.writeFile(path, content, encoding);\n return true;\n } catch {\n return false;\n }\n}\n\n/**\n * Appends text content to a file.\n *\n * @param {string} path - The file path to append to.\n * @param {string} content - The text content to append.\n * @param {BufferEncoding} [encoding=\"utf-8\"] - The encoding to use.\n * @returns {Promise<boolean>} Resolves to `true` if successful, `false` otherwise.\n */\nexport async function appendTextFile(\n path: string,\n content: string,\n encoding: BufferEncoding = 'utf-8'\n): Promise<boolean> {\n try {\n await fs.appendFile(path, content, encoding);\n return true;\n } catch {\n return false;\n }\n}\n\n/**\n * Copies a file from source to destination.\n *\n * @param {string} source - Source file path.\n * @param {string} destination - Destination file path.\n * @param {boolean} [overwrite=false] - Whether to overwrite if destination exists.\n * @returns {Promise<boolean>} Resolves to `true` if successful, `false` otherwise.\n */\nexport async function copyFile(source: string, destination: string, overwrite = false): Promise<boolean> {\n try {\n const flags = overwrite ? 0 : fs.constants.COPYFILE_EXCL;\n await fs.copyFile(source, destination, flags);\n return true;\n } catch {\n return false;\n }\n}\n\n/**\n * Renames/moves a file.\n *\n * @param {string} oldPath - Current file path.\n * @param {string} newPath - New file path.\n * @returns {Promise<boolean>} Resolves to `true` if successful, `false` otherwise.\n */\nexport async function moveFile(oldPath: string, newPath: string): Promise<boolean> {\n try {\n await fs.rename(oldPath, newPath);\n return true;\n } catch {\n // If rename fails (e.g., across devices), try copy+delete\n try {\n await fs.copyFile(oldPath, newPath);\n await fs.unlink(oldPath);\n return true;\n } catch {\n return false;\n }\n }\n}\n\n/**\n * Gets file stats if the file exists.\n *\n * @param {string} path - Path to the file.\n * @returns {Promise<fs.Stats | null>} File stats object or null if file doesn't exist.\n */\nexport async function getFileStats(path: string): Promise<Stats | null> {\n try {\n return await fs.stat(path);\n } catch {\n return null;\n }\n}\n\n/**\n * Creates a temporary file with optional content.\n *\n * @param {object} [options] - Options for creating the temp file.\n * @param {string} [options.prefix=\"tmp-\"] - Filename prefix.\n * @param {string} [options.extension=\"\"] - File extension.\n * @param {string} [options.dir] - Directory to create the file in (defaults to OS temp dir).\n * @param {string | Buffer} [options.content] - Optional content to write to the file.\n * @returns {Promise<string>} Path to the created temporary file.\n */\nexport async function createTempFile({\n prefix = 'tmp-',\n extension = '',\n dir = tmpdir(),\n content\n}: {\n prefix?: string;\n extension?: string;\n dir?: string;\n content?: string | Buffer;\n} = {}): Promise<string> {\n const ext = extension ? (extension.startsWith('.') ? extension : `.${extension}`) : '';\n const filename = `${prefix}${randomUUID()}${ext}`;\n const filepath = path.join(dir, filename);\n\n if (content) {\n await fs.writeFile(filepath, content);\n } else {\n await fs.writeFile(filepath, '');\n }\n\n return filepath;\n}\n\n/**\n * Streams a file from source to destination.\n * Useful for large files to avoid loading the entire file into memory.\n *\n * @param {string} source - Source file path.\n * @param {string} destination - Destination file path.\n * @returns {Promise<void>} Resolves when streaming completes.\n * @throws {Error} If streaming fails.\n */\nexport async function streamFile(source: string, destination: string): Promise<void> {\n const readStream = createReadStream(source);\n const writeStream = createWriteStream(destination);\n\n try {\n await pipeline(readStream, writeStream);\n } catch (error) {\n // Ensure both streams are closed in case of error\n readStream.destroy();\n writeStream.destroy();\n throw error;\n }\n}\n\n/**\n * Reads a directory and returns file names.\n *\n * @param {string} dirPath - Path to the directory.\n * @param {object} [options] - Options for reading the directory.\n * @param {boolean} [options.fullPaths=false] - Whether to return full paths.\n * @param {RegExp} [options.filter] - Optional regex to filter files.\n * @returns {Promise<string[]>} Array of file names or paths.\n * @throws {Error} If directory cannot be read.\n */\nexport async function readDirectory(\n dirPath: string,\n options: { fullPaths?: boolean; filter?: RegExp } = {}\n): Promise<string[]> {\n const files = await fs.readdir(dirPath);\n\n let result = files;\n\n if (options.filter) {\n result = result.filter(file => options.filter!.test(file));\n }\n\n if (options.fullPaths) {\n result = result.map(file => path.join(dirPath, file));\n }\n\n return result;\n}\n\n/**\n * Creates a directory if it doesn't exist.\n *\n * @param {string} dirPath - Path to the directory.\n * @param {boolean} [recursive=true] - Whether to create parent directories.\n * @returns {Promise<boolean>} Resolves to `true` if successful, `false` otherwise.\n */\nexport async function createDirectory(dirPath: string, recursive = true): Promise<boolean> {\n try {\n await fs.mkdir(dirPath, { recursive });\n return true;\n } catch (err: any) {\n // Consider it success if directory already exists\n return err.code === 'EEXIST';\n }\n}\n\n/**\n * Safely reads and parses a JSON file with error details.\n *\n * @typeParam T - The expected type of the parsed JSON object.\n * @param {string} path - The path to the JSON file.\n * @returns {Promise<{ data: T | null; error: Error | null }>} Object with data and error properties.\n */\nexport async function safeReadJsonFile<T = any>(path: string): Promise<{ data: T | null; error: Error | null }> {\n try {\n const content = await fs.readFile(path, 'utf-8');\n try {\n const data = JSON.parse(content) as T;\n return { data, error: null };\n } catch (error: any) {\n return {\n data: null,\n error: new Error(`Invalid JSON in file ${path}: ${error.message}`)\n };\n }\n } catch (error: any) {\n return {\n data: null,\n error: new Error(`Failed to read file ${path}: ${error.message}`)\n };\n }\n}\n\n/**\n * Checks if a path points to a file (not a directory).\n *\n * @param {string} path - Path to check.\n * @returns {Promise<boolean>} True if the path is a file, false otherwise.\n */\nexport async function isFile(path: string): Promise<boolean> {\n try {\n const stat = await fs.stat(path);\n return stat.isFile();\n } catch {\n return false;\n }\n}\n\n/**\n * Gets the size of a file in bytes.\n *\n * @param {string} path - Path to the file.\n * @returns {Promise<number>} Size in bytes or -1 if file doesn't exist.\n */\nexport async function getFileSize(path: string): Promise<number> {\n try {\n const stat = await fs.stat(path);\n return stat.size;\n } catch {\n return -1;\n }\n}\n\n/**\n * Reads a file as a Buffer.\n *\n * @param {string} path - Path to the file.\n * @returns {Promise<Buffer | null>} File contents as Buffer or null if reading fails.\n */\nexport async function readFileBuffer(path: string): Promise<Buffer | null> {\n try {\n return await fs.readFile(path);\n } catch {\n return null;\n }\n}\n"]}