@stonyx/utils 0.2.3-beta.24 → 0.2.3-beta.25
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.d.ts +8 -0
- package/dist/file.js +26 -2
- package/package.json +1 -1
package/dist/file.d.ts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
1
2
|
interface FileOptions {
|
|
2
3
|
json?: boolean;
|
|
3
4
|
}
|
|
4
5
|
interface ReadFileOptions extends FileOptions {
|
|
5
6
|
missingFileCallback?: (filePath: string) => string | Record<string, unknown>;
|
|
7
|
+
encoding?: BufferEncoding | null;
|
|
6
8
|
}
|
|
7
9
|
interface DeleteFileOptions {
|
|
8
10
|
ignoreAccessFailure?: boolean;
|
|
@@ -26,13 +28,19 @@ interface CopyFileOptions {
|
|
|
26
28
|
overwrite?: boolean;
|
|
27
29
|
}
|
|
28
30
|
export declare function copyFile(sourcePath: string, targetPath: string, options?: CopyFileOptions): Promise<boolean>;
|
|
31
|
+
export declare function readFile(filePath: string, options: ReadFileOptions & {
|
|
32
|
+
encoding: null;
|
|
33
|
+
}): Promise<Buffer>;
|
|
29
34
|
export declare function readFile(filePath: string, options: ReadFileOptions & {
|
|
30
35
|
json: true;
|
|
31
36
|
}): Promise<Record<string, unknown>>;
|
|
32
37
|
export declare function readFile(filePath: string, options?: ReadFileOptions): Promise<string>;
|
|
33
38
|
export declare function deleteFile(filePath: string, options?: DeleteFileOptions): Promise<void>;
|
|
39
|
+
export declare function deleteFileSync(filePath: string, options?: DeleteFileOptions): void;
|
|
34
40
|
export declare function deleteDirectory(dir: string): Promise<void>;
|
|
35
41
|
export declare function createDirectory(dir: string): Promise<void>;
|
|
42
|
+
export declare function createReadStream(filePath: string, options?: Parameters<typeof fs.createReadStream>[1]): fs.ReadStream;
|
|
43
|
+
export declare function createWriteStream(filePath: string, options?: Parameters<typeof fs.createWriteStream>[1]): fs.WriteStream;
|
|
36
44
|
export declare function forEachFileImport(dir: string, callback: (output: unknown, meta: FileImportMeta) => void | Promise<void>, options?: ForEachFileImportOptions): Promise<void>;
|
|
37
45
|
export declare function fileExists(filePath: string): Promise<boolean>;
|
|
38
46
|
export {};
|
package/dist/file.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { getTimestamp } from './date.js';
|
|
2
2
|
import { kebabCaseToCamelCase } from './string.js';
|
|
3
3
|
import { objToJson } from './object.js';
|
|
4
|
+
import fs from 'fs';
|
|
4
5
|
import { promises as fsp } from 'fs';
|
|
5
6
|
import path from 'path';
|
|
6
7
|
function isNodeError(error) {
|
|
@@ -58,8 +59,13 @@ export async function readFile(filePath, options = {}) {
|
|
|
58
59
|
try {
|
|
59
60
|
filePath = path.resolve(filePath);
|
|
60
61
|
await fsp.access(filePath);
|
|
61
|
-
const
|
|
62
|
-
|
|
62
|
+
const encoding = options.encoding === undefined ? 'utf8' : options.encoding;
|
|
63
|
+
const fileData = await fsp.readFile(filePath, { encoding: encoding });
|
|
64
|
+
if (encoding === null)
|
|
65
|
+
return fileData;
|
|
66
|
+
if (options.json)
|
|
67
|
+
return JSON.parse(fileData);
|
|
68
|
+
return fileData;
|
|
63
69
|
}
|
|
64
70
|
catch (error) {
|
|
65
71
|
const { missingFileCallback } = options;
|
|
@@ -81,12 +87,30 @@ export async function deleteFile(filePath, options) {
|
|
|
81
87
|
}
|
|
82
88
|
await fsp.unlink(filePath);
|
|
83
89
|
}
|
|
90
|
+
export function deleteFileSync(filePath, options) {
|
|
91
|
+
try {
|
|
92
|
+
filePath = path.resolve(filePath);
|
|
93
|
+
fs.accessSync(filePath);
|
|
94
|
+
}
|
|
95
|
+
catch (error) {
|
|
96
|
+
if (options?.ignoreAccessFailure)
|
|
97
|
+
return;
|
|
98
|
+
throw error;
|
|
99
|
+
}
|
|
100
|
+
fs.unlinkSync(filePath);
|
|
101
|
+
}
|
|
84
102
|
export async function deleteDirectory(dir) {
|
|
85
103
|
await fsp.rm(dir, { recursive: true, force: true });
|
|
86
104
|
}
|
|
87
105
|
export async function createDirectory(dir) {
|
|
88
106
|
await fsp.mkdir(dir, { recursive: true });
|
|
89
107
|
}
|
|
108
|
+
export function createReadStream(filePath, options) {
|
|
109
|
+
return fs.createReadStream(path.resolve(filePath), options);
|
|
110
|
+
}
|
|
111
|
+
export function createWriteStream(filePath, options) {
|
|
112
|
+
return fs.createWriteStream(path.resolve(filePath), options);
|
|
113
|
+
}
|
|
90
114
|
export async function forEachFileImport(dir, callback, options = {}) {
|
|
91
115
|
if (typeof callback !== 'function')
|
|
92
116
|
throw new Error('Callback must be valid function');
|