@stonyx/utils 0.2.3-beta.17 → 0.2.3-beta.19
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/dist/file.js +11 -7
- package/package.json +1 -1
package/dist/file.js
CHANGED
|
@@ -13,18 +13,18 @@ export async function createFile(filePath, data, options = {}) {
|
|
|
13
13
|
await fsp.writeFile(filePath, options.json ? objToJson(data) : String(data), 'utf8');
|
|
14
14
|
}
|
|
15
15
|
catch (error) {
|
|
16
|
-
throw new Error(String(error));
|
|
16
|
+
throw error instanceof Error ? error : new Error(String(error));
|
|
17
17
|
}
|
|
18
18
|
}
|
|
19
19
|
export async function updateFile(filePath, data, options = {}) {
|
|
20
20
|
try {
|
|
21
21
|
await fsp.access(filePath);
|
|
22
22
|
const swapFile = `${filePath}.temp-${getTimestamp()}`;
|
|
23
|
-
await fsp.writeFile(swapFile, options.json ? objToJson(data) : String(data));
|
|
23
|
+
await fsp.writeFile(swapFile, options.json ? objToJson(data) : String(data), 'utf8');
|
|
24
24
|
await fsp.rename(swapFile, filePath);
|
|
25
25
|
}
|
|
26
26
|
catch (error) {
|
|
27
|
-
throw new Error(String(error));
|
|
27
|
+
throw error instanceof Error ? error : new Error(String(error));
|
|
28
28
|
}
|
|
29
29
|
}
|
|
30
30
|
export async function copyFile(sourcePath, targetPath, options = {}) {
|
|
@@ -34,19 +34,23 @@ export async function copyFile(sourcePath, targetPath, options = {}) {
|
|
|
34
34
|
await fsp.access(sourcePath);
|
|
35
35
|
}
|
|
36
36
|
catch (error) {
|
|
37
|
-
throw new Error(String(error));
|
|
37
|
+
throw error instanceof Error ? error : new Error(String(error));
|
|
38
38
|
}
|
|
39
39
|
try {
|
|
40
40
|
await fsp.access(targetPath);
|
|
41
41
|
if (!options.overwrite)
|
|
42
42
|
return false;
|
|
43
43
|
}
|
|
44
|
-
catch {
|
|
44
|
+
catch (error) {
|
|
45
|
+
if (isNodeError(error) && error.code === 'ENOENT') { /* file doesn't exist — proceed with copy */ }
|
|
46
|
+
else
|
|
47
|
+
throw error;
|
|
48
|
+
}
|
|
45
49
|
try {
|
|
46
50
|
await fsp.copyFile(sourcePath, targetPath);
|
|
47
51
|
}
|
|
48
52
|
catch (error) {
|
|
49
|
-
throw new Error(String(error));
|
|
53
|
+
throw error instanceof Error ? error : new Error(String(error));
|
|
50
54
|
}
|
|
51
55
|
return true;
|
|
52
56
|
}
|
|
@@ -62,7 +66,7 @@ export async function readFile(filePath, options = {}) {
|
|
|
62
66
|
if (isNodeError(error) && error.code === 'ENOENT' && missingFileCallback) {
|
|
63
67
|
return missingFileCallback(filePath);
|
|
64
68
|
}
|
|
65
|
-
throw new Error(String(error));
|
|
69
|
+
throw error instanceof Error ? error : new Error(String(error));
|
|
66
70
|
}
|
|
67
71
|
}
|
|
68
72
|
export async function deleteFile(filePath, options) {
|