@reliverse/relifso 1.2.0 → 1.2.1
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/bin/impl/copy.d.ts +8 -0
- package/bin/impl/copy.js +36 -0
- package/bin/impl/create-file.d.ts +1 -0
- package/bin/impl/create-file.js +13 -0
- package/bin/impl/empty-dir.d.ts +1 -0
- package/bin/impl/empty-dir.js +14 -0
- package/bin/impl/mkdirs.d.ts +1 -0
- package/bin/impl/mkdirs.js +12 -0
- package/bin/impl/move.d.ts +1 -0
- package/bin/impl/move.js +51 -0
- package/bin/impl/output-file.d.ts +8 -0
- package/bin/impl/output-file.js +7 -0
- package/bin/impl/output-json.d.ts +8 -0
- package/bin/impl/output-json.js +7 -0
- package/bin/impl/path-exists.d.ts +1 -0
- package/bin/impl/path-exists.js +12 -0
- package/bin/impl/read-file.d.ts +19 -1
- package/bin/impl/read-file.js +20 -2
- package/bin/impl/read-json.d.ts +8 -0
- package/bin/impl/read-json.js +26 -0
- package/bin/impl/remove.d.ts +1 -0
- package/bin/impl/remove.js +4 -0
- package/bin/impl/write-file.d.ts +9 -0
- package/bin/impl/write-file.js +14 -0
- package/bin/impl/write-json.d.ts +8 -0
- package/bin/impl/write-json.js +11 -0
- package/bin/mod.d.ts +20 -34
- package/bin/mod.js +18 -32
- package/package.json +6 -2
- package/bin/impl/async.d.ts +0 -7
- package/bin/impl/async.js +0 -9
package/bin/impl/copy.d.ts
CHANGED
|
@@ -18,3 +18,11 @@ export interface CopyOptions {
|
|
|
18
18
|
* @param options - Options for the copy operation.
|
|
19
19
|
*/
|
|
20
20
|
export declare function copySync(src: string, dest: string, options?: CopyOptions): void;
|
|
21
|
+
/**
|
|
22
|
+
* Asynchronously copies a file or directory. The directory can have contents. Like `cp -r`.
|
|
23
|
+
*
|
|
24
|
+
* @param src - The source path.
|
|
25
|
+
* @param dest - The destination path.
|
|
26
|
+
* @param options - Options for the copy operation.
|
|
27
|
+
*/
|
|
28
|
+
export declare function copy(src: string, dest: string, options?: CopyOptions): Promise<void>;
|
package/bin/impl/copy.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { copyFileSync, statSync, constants as fsConstants } from "node:fs";
|
|
2
|
+
import { stat as statAsync, copyFile as copyFileAsync, constants as fsConstantsAsync } from "node:fs/promises";
|
|
2
3
|
import { dirname, join as joinPath, basename as basenamePath } from "node:path";
|
|
3
4
|
import { mkdirsSync } from "./mkdirs.js";
|
|
5
|
+
import { mkdirs } from "./mkdirs.js";
|
|
4
6
|
export function copySync(src, dest, options = {}) {
|
|
5
7
|
const { overwrite = options.clobber || false, preserveTimestamps = false } = options;
|
|
6
8
|
const srcStat = statSync(src, { throwIfNoEntry: true });
|
|
@@ -26,3 +28,37 @@ export function copySync(src, dest, options = {}) {
|
|
|
26
28
|
}
|
|
27
29
|
}
|
|
28
30
|
}
|
|
31
|
+
export async function copy(src, dest, options = {}) {
|
|
32
|
+
const { overwrite = options.clobber || false, preserveTimestamps = false } = options;
|
|
33
|
+
const srcStat = await statAsync(src).catch((e) => {
|
|
34
|
+
if (e.code === "ENOENT") return null;
|
|
35
|
+
throw e;
|
|
36
|
+
});
|
|
37
|
+
if (!srcStat) {
|
|
38
|
+
}
|
|
39
|
+
let destFinal = dest;
|
|
40
|
+
const destStat = await statAsync(dest).catch((e) => {
|
|
41
|
+
if (e.code === "ENOENT") return null;
|
|
42
|
+
throw e;
|
|
43
|
+
});
|
|
44
|
+
if (destStat?.isDirectory()) {
|
|
45
|
+
destFinal = joinPath(dest, basenamePath(src));
|
|
46
|
+
}
|
|
47
|
+
const destExists = await statAsync(destFinal).catch((e) => {
|
|
48
|
+
if (e.code === "ENOENT") return null;
|
|
49
|
+
throw e;
|
|
50
|
+
});
|
|
51
|
+
if (destExists && !overwrite) {
|
|
52
|
+
throw new Error(`Destination ${destFinal} already exists and overwrite is false.`);
|
|
53
|
+
}
|
|
54
|
+
const destDir = dirname(destFinal);
|
|
55
|
+
await mkdirs(destDir);
|
|
56
|
+
if (srcStat?.isDirectory()) {
|
|
57
|
+
await mkdirs(destFinal);
|
|
58
|
+
} else {
|
|
59
|
+
await copyFileAsync(src, destFinal, preserveTimestamps ? fsConstantsAsync.COPYFILE_FICLONE : 0);
|
|
60
|
+
if (preserveTimestamps) {
|
|
61
|
+
console.warn("preserveTimestamps: utimes is not implemented for the moment.");
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
package/bin/impl/create-file.js
CHANGED
|
@@ -1,8 +1,21 @@
|
|
|
1
1
|
import { existsSync } from "node:fs";
|
|
2
|
+
import { stat } from "node:fs/promises";
|
|
2
3
|
import { writeFileSync } from "./write-file.js";
|
|
4
|
+
import { writeFile } from "./write-file.js";
|
|
3
5
|
export function createFileSync(file, content = "") {
|
|
4
6
|
if (existsSync(file)) {
|
|
5
7
|
return;
|
|
6
8
|
}
|
|
7
9
|
return writeFileSync(file, content);
|
|
8
10
|
}
|
|
11
|
+
export async function createFile(file, content = "") {
|
|
12
|
+
try {
|
|
13
|
+
await stat(file);
|
|
14
|
+
return;
|
|
15
|
+
} catch (error) {
|
|
16
|
+
if (error.code === "ENOENT") {
|
|
17
|
+
return writeFile(file, content);
|
|
18
|
+
}
|
|
19
|
+
throw error;
|
|
20
|
+
}
|
|
21
|
+
}
|
package/bin/impl/empty-dir.d.ts
CHANGED
package/bin/impl/empty-dir.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { existsSync, readdirSync, rmSync } from "node:fs";
|
|
2
|
+
import { readdir, rm, stat } from "node:fs/promises";
|
|
2
3
|
import path from "node:path";
|
|
3
4
|
export function emptyDirSync(dir) {
|
|
4
5
|
if (!existsSync(dir)) {
|
|
@@ -8,3 +9,16 @@ export function emptyDirSync(dir) {
|
|
|
8
9
|
rmSync(path.resolve(dir, file), { recursive: true, force: true });
|
|
9
10
|
}
|
|
10
11
|
}
|
|
12
|
+
export async function emptyDir(dir) {
|
|
13
|
+
try {
|
|
14
|
+
await stat(dir);
|
|
15
|
+
} catch (error) {
|
|
16
|
+
if (error.code === "ENOENT") {
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
throw error;
|
|
20
|
+
}
|
|
21
|
+
for (const file of await readdir(dir)) {
|
|
22
|
+
await rm(path.resolve(dir, file), { recursive: true, force: true });
|
|
23
|
+
}
|
|
24
|
+
}
|
package/bin/impl/mkdirs.d.ts
CHANGED
package/bin/impl/mkdirs.js
CHANGED
|
@@ -1,7 +1,19 @@
|
|
|
1
1
|
import { mkdirSync, existsSync } from "node:fs";
|
|
2
|
+
import { mkdir, stat } from "node:fs/promises";
|
|
2
3
|
export function mkdirsSync(dir) {
|
|
3
4
|
if (existsSync(dir)) {
|
|
4
5
|
return;
|
|
5
6
|
}
|
|
6
7
|
return mkdirSync(dir, { recursive: true });
|
|
7
8
|
}
|
|
9
|
+
export async function mkdirs(dir) {
|
|
10
|
+
try {
|
|
11
|
+
await stat(dir);
|
|
12
|
+
return void 0;
|
|
13
|
+
} catch (error) {
|
|
14
|
+
if (error.code === "ENOENT") {
|
|
15
|
+
return mkdir(dir, { recursive: true });
|
|
16
|
+
}
|
|
17
|
+
throw error;
|
|
18
|
+
}
|
|
19
|
+
}
|
package/bin/impl/move.d.ts
CHANGED
|
@@ -12,3 +12,4 @@ export interface MoveOptions {
|
|
|
12
12
|
* @param options - Options for the move operation.
|
|
13
13
|
*/
|
|
14
14
|
export declare function moveSync(src: string, dest: string, options?: MoveOptions): void;
|
|
15
|
+
export declare function move(src: string, dest: string, options?: MoveOptions): Promise<void>;
|
package/bin/impl/move.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { renameSync, statSync, unlinkSync, copyFileSync } from "node:fs";
|
|
2
|
+
import { rename, stat, unlink, copyFile } from "node:fs/promises";
|
|
2
3
|
import { dirname, basename, join as joinPath } from "node:path";
|
|
3
4
|
import { mkdirsSync } from "./mkdirs.js";
|
|
5
|
+
import { mkdirs } from "./mkdirs.js";
|
|
4
6
|
export function moveSync(src, dest, options = {}) {
|
|
5
7
|
let overwrite;
|
|
6
8
|
if (options.overwrite !== void 0) {
|
|
@@ -40,3 +42,52 @@ export function moveSync(src, dest, options = {}) {
|
|
|
40
42
|
}
|
|
41
43
|
}
|
|
42
44
|
}
|
|
45
|
+
export async function move(src, dest, options = {}) {
|
|
46
|
+
let overwrite;
|
|
47
|
+
if (options.overwrite !== void 0) {
|
|
48
|
+
overwrite = options.overwrite;
|
|
49
|
+
} else if (options.clobber !== void 0) {
|
|
50
|
+
console.warn(
|
|
51
|
+
"Warning: The 'clobber' option in move is deprecated and will be removed in a future version. Please use 'overwrite' instead."
|
|
52
|
+
);
|
|
53
|
+
overwrite = options.clobber;
|
|
54
|
+
} else {
|
|
55
|
+
overwrite = false;
|
|
56
|
+
}
|
|
57
|
+
const srcStat = await stat(src).catch((e) => {
|
|
58
|
+
if (e.code === "ENOENT") return null;
|
|
59
|
+
throw e;
|
|
60
|
+
});
|
|
61
|
+
if (!srcStat) {
|
|
62
|
+
}
|
|
63
|
+
let destFinal = dest;
|
|
64
|
+
const destStat = await stat(dest).catch((e) => {
|
|
65
|
+
if (e.code === "ENOENT") return null;
|
|
66
|
+
throw e;
|
|
67
|
+
});
|
|
68
|
+
if (destStat?.isDirectory()) {
|
|
69
|
+
destFinal = joinPath(dest, basename(src));
|
|
70
|
+
}
|
|
71
|
+
const destFinalStat = await stat(destFinal).catch((e) => {
|
|
72
|
+
if (e.code === "ENOENT") return null;
|
|
73
|
+
throw e;
|
|
74
|
+
});
|
|
75
|
+
if (destFinalStat && !overwrite) {
|
|
76
|
+
throw new Error(`Destination ${destFinal} already exists and overwrite is false.`);
|
|
77
|
+
}
|
|
78
|
+
const destDir = dirname(destFinal);
|
|
79
|
+
await mkdirs(destDir);
|
|
80
|
+
try {
|
|
81
|
+
await rename(src, destFinal);
|
|
82
|
+
} catch (err) {
|
|
83
|
+
if (err.code === "EXDEV") {
|
|
84
|
+
await copyFile(src, destFinal);
|
|
85
|
+
await unlink(src);
|
|
86
|
+
} else if (err.code === "EISDIR" || err.code === "EPERM") {
|
|
87
|
+
await copyFile(src, destFinal);
|
|
88
|
+
await unlink(src);
|
|
89
|
+
} else {
|
|
90
|
+
throw err;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
@@ -6,3 +6,11 @@
|
|
|
6
6
|
* @param options - Options for writing the file (e.g., encoding, mode, flag).
|
|
7
7
|
*/
|
|
8
8
|
export declare function outputFileSync(file: string, data: string | Uint8Array, options?: unknown): void;
|
|
9
|
+
/**
|
|
10
|
+
* Ensures that the directory for the file exists and then asynchronously writes the data to the file.
|
|
11
|
+
*
|
|
12
|
+
* @param file - The path to the file.
|
|
13
|
+
* @param data - The data to write.
|
|
14
|
+
* @param options - Options for writing the file (e.g., encoding, mode, flag).
|
|
15
|
+
*/
|
|
16
|
+
export declare function outputFile(file: string, data: string | Uint8Array, options?: unknown): Promise<void>;
|
package/bin/impl/output-file.js
CHANGED
|
@@ -1,8 +1,15 @@
|
|
|
1
1
|
import { dirname } from "node:path";
|
|
2
2
|
import { mkdirsSync } from "./mkdirs.js";
|
|
3
|
+
import { mkdirs } from "./mkdirs.js";
|
|
3
4
|
import { writeFileSync } from "./write-file.js";
|
|
5
|
+
import { writeFile } from "./write-file.js";
|
|
4
6
|
export function outputFileSync(file, data, options) {
|
|
5
7
|
const dir = dirname(file);
|
|
6
8
|
mkdirsSync(dir);
|
|
7
9
|
writeFileSync(file, data, options);
|
|
8
10
|
}
|
|
11
|
+
export async function outputFile(file, data, options) {
|
|
12
|
+
const dir = dirname(file);
|
|
13
|
+
await mkdirs(dir);
|
|
14
|
+
await writeFile(file, data, options);
|
|
15
|
+
}
|
|
@@ -6,3 +6,11 @@
|
|
|
6
6
|
* @param options - Options for writing the JSON file (e.g., replacer, space).
|
|
7
7
|
*/
|
|
8
8
|
export declare function outputJsonSync(file: string, data: unknown, options?: unknown): void;
|
|
9
|
+
/**
|
|
10
|
+
* Ensures that the directory for the JSON file exists and then asynchronously writes the JSON data to the file.
|
|
11
|
+
*
|
|
12
|
+
* @param file - The path to the file.
|
|
13
|
+
* @param data - The JSON data to write.
|
|
14
|
+
* @param options - Options for writing the JSON file (e.g., replacer, space).
|
|
15
|
+
*/
|
|
16
|
+
export declare function outputJson(file: string, data: unknown, options?: unknown): Promise<void>;
|
package/bin/impl/output-json.js
CHANGED
|
@@ -1,8 +1,15 @@
|
|
|
1
1
|
import { dirname } from "node:path";
|
|
2
2
|
import { mkdirsSync } from "./mkdirs.js";
|
|
3
|
+
import { mkdirs } from "./mkdirs.js";
|
|
3
4
|
import { writeJsonSync } from "./write-json.js";
|
|
5
|
+
import { writeJson } from "./write-json.js";
|
|
4
6
|
export function outputJsonSync(file, data, options) {
|
|
5
7
|
const dir = dirname(file);
|
|
6
8
|
mkdirsSync(dir);
|
|
7
9
|
writeJsonSync(file, data, options);
|
|
8
10
|
}
|
|
11
|
+
export async function outputJson(file, data, options) {
|
|
12
|
+
const dir = dirname(file);
|
|
13
|
+
await mkdirs(dir);
|
|
14
|
+
await writeJson(file, data, options);
|
|
15
|
+
}
|
package/bin/impl/path-exists.js
CHANGED
|
@@ -1,4 +1,16 @@
|
|
|
1
1
|
import { existsSync } from "node:fs";
|
|
2
|
+
import { stat } from "node:fs/promises";
|
|
2
3
|
export function pathExistsSync(path) {
|
|
3
4
|
return existsSync(path);
|
|
4
5
|
}
|
|
6
|
+
export async function pathExists(path) {
|
|
7
|
+
try {
|
|
8
|
+
await stat(path);
|
|
9
|
+
return true;
|
|
10
|
+
} catch (error) {
|
|
11
|
+
if (error.code === "ENOENT") {
|
|
12
|
+
return false;
|
|
13
|
+
}
|
|
14
|
+
throw error;
|
|
15
|
+
}
|
|
16
|
+
}
|
package/bin/impl/read-file.d.ts
CHANGED
|
@@ -9,4 +9,22 @@ export interface ReadFileOptions {
|
|
|
9
9
|
* @param options - Options for reading the file. Can be an encoding string or an object.
|
|
10
10
|
* @returns The file content as a string or Buffer.
|
|
11
11
|
*/
|
|
12
|
-
export declare function readFileSync(path: string, options
|
|
12
|
+
export declare function readFileSync(path: string, options: BufferEncoding | (ReadFileOptions & {
|
|
13
|
+
encoding: BufferEncoding;
|
|
14
|
+
})): string;
|
|
15
|
+
export declare function readFileSync(path: string, options?: (ReadFileOptions & {
|
|
16
|
+
encoding?: null;
|
|
17
|
+
}) | null): Buffer;
|
|
18
|
+
/**
|
|
19
|
+
* Asynchronously reads the entire contents of a file.
|
|
20
|
+
*
|
|
21
|
+
* @param path - The path to the file.
|
|
22
|
+
* @param options - Options for reading the file. Can be an encoding string or an object.
|
|
23
|
+
* @returns A promise that resolves with the file content as a string or Buffer.
|
|
24
|
+
*/
|
|
25
|
+
export declare function readFile(path: string, options: BufferEncoding | (ReadFileOptions & {
|
|
26
|
+
encoding: BufferEncoding;
|
|
27
|
+
})): Promise<string>;
|
|
28
|
+
export declare function readFile(path: string, options?: (ReadFileOptions & {
|
|
29
|
+
encoding?: null;
|
|
30
|
+
}) | null): Promise<Buffer>;
|
package/bin/impl/read-file.js
CHANGED
|
@@ -1,12 +1,30 @@
|
|
|
1
1
|
import { readFileSync as nodeReadFileSync } from "node:fs";
|
|
2
|
+
import { readFile as nodeReadFileAsync } from "node:fs/promises";
|
|
2
3
|
export function readFileSync(path, options) {
|
|
3
4
|
let encoding;
|
|
4
5
|
let flag;
|
|
5
6
|
if (typeof options === "string") {
|
|
6
7
|
encoding = options;
|
|
7
8
|
} else if (options) {
|
|
8
|
-
encoding = options.encoding
|
|
9
|
+
encoding = options.encoding;
|
|
9
10
|
flag = options.flag;
|
|
10
11
|
}
|
|
11
|
-
|
|
12
|
+
if (encoding) {
|
|
13
|
+
return nodeReadFileSync(path, { encoding, flag });
|
|
14
|
+
}
|
|
15
|
+
return nodeReadFileSync(path, { encoding: null, flag });
|
|
16
|
+
}
|
|
17
|
+
export async function readFile(path, options) {
|
|
18
|
+
let encoding;
|
|
19
|
+
let flag;
|
|
20
|
+
if (typeof options === "string") {
|
|
21
|
+
encoding = options;
|
|
22
|
+
} else if (options) {
|
|
23
|
+
encoding = options.encoding;
|
|
24
|
+
flag = options.flag;
|
|
25
|
+
}
|
|
26
|
+
if (encoding) {
|
|
27
|
+
return nodeReadFileAsync(path, { encoding, flag });
|
|
28
|
+
}
|
|
29
|
+
return nodeReadFileAsync(path, { encoding: null, flag });
|
|
12
30
|
}
|
package/bin/impl/read-json.d.ts
CHANGED
|
@@ -12,3 +12,11 @@ export interface ReadJsonOptions {
|
|
|
12
12
|
* @returns The parsed JSON object.
|
|
13
13
|
*/
|
|
14
14
|
export declare function readJsonSync<T = unknown>(file: string, options?: BufferEncoding | ReadJsonOptions): T;
|
|
15
|
+
/**
|
|
16
|
+
* Asynchronously reads a JSON file and then parses it into an object.
|
|
17
|
+
*
|
|
18
|
+
* @param file - The path to the file.
|
|
19
|
+
* @param options - Options for reading the file or parsing JSON. Can be an encoding string or an object.
|
|
20
|
+
* @returns A promise that resolves with the parsed JSON object.
|
|
21
|
+
*/
|
|
22
|
+
export declare function readJson<T = unknown>(file: string, options?: BufferEncoding | ReadJsonOptions): Promise<T>;
|
package/bin/impl/read-json.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { readFileSync } from "node:fs";
|
|
2
|
+
import { readFile } from "node:fs/promises";
|
|
2
3
|
export function readJsonSync(file, options) {
|
|
3
4
|
let encoding = "utf8";
|
|
4
5
|
let reviver;
|
|
@@ -22,3 +23,28 @@ export function readJsonSync(file, options) {
|
|
|
22
23
|
return void 0;
|
|
23
24
|
}
|
|
24
25
|
}
|
|
26
|
+
export async function readJson(file, options) {
|
|
27
|
+
let encoding = "utf8";
|
|
28
|
+
let reviver;
|
|
29
|
+
let throws = true;
|
|
30
|
+
let flag;
|
|
31
|
+
if (typeof options === "string") {
|
|
32
|
+
encoding = options;
|
|
33
|
+
} else if (options) {
|
|
34
|
+
encoding = options.encoding ?? encoding;
|
|
35
|
+
reviver = options.reviver;
|
|
36
|
+
flag = options.flag;
|
|
37
|
+
if (options.throws !== void 0) {
|
|
38
|
+
throws = options.throws;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
try {
|
|
42
|
+
const data = await readFile(file, { encoding, flag });
|
|
43
|
+
return JSON.parse(data, reviver);
|
|
44
|
+
} catch (err) {
|
|
45
|
+
if (throws) {
|
|
46
|
+
throw err;
|
|
47
|
+
}
|
|
48
|
+
return void 0;
|
|
49
|
+
}
|
|
50
|
+
}
|
package/bin/impl/remove.d.ts
CHANGED
package/bin/impl/remove.js
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
1
|
import { rmSync } from "node:fs";
|
|
2
|
+
import { rm } from "node:fs/promises";
|
|
2
3
|
export function removeSync(path) {
|
|
3
4
|
return rmSync(path, { recursive: true, force: true });
|
|
4
5
|
}
|
|
6
|
+
export async function remove(path) {
|
|
7
|
+
return rm(path, { recursive: true, force: true });
|
|
8
|
+
}
|
package/bin/impl/write-file.d.ts
CHANGED
|
@@ -9,3 +9,12 @@ export type WriteFileOptions = import("node:fs").WriteFileOptions;
|
|
|
9
9
|
* @param options - Options for writing the file. Can be an encoding string or an object.
|
|
10
10
|
*/
|
|
11
11
|
export declare function writeFileSync(file: PathLike | number, data: string | NodeJS.ArrayBufferView, options?: WriteFileOptions): void;
|
|
12
|
+
/**
|
|
13
|
+
* Asynchronously writes data to a file, replacing the file if it already exists.
|
|
14
|
+
* Ensures the directory exists before writing.
|
|
15
|
+
*
|
|
16
|
+
* @param file - Path to the file.
|
|
17
|
+
* @param data - The data to write. If something other than a Buffer or Uint8Array is provided, it is converted to a string.
|
|
18
|
+
* @param options - Options for writing the file. Can be an encoding string or an object.
|
|
19
|
+
*/
|
|
20
|
+
export declare function writeFile(file: PathLike | number, data: string | NodeJS.ArrayBufferView, options?: WriteFileOptions): Promise<void>;
|
package/bin/impl/write-file.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { existsSync, mkdirSync, writeFileSync as nodeWriteFileSync } from "node:fs";
|
|
2
|
+
import { mkdir, writeFile as nodeWriteFileAsync, stat } from "node:fs/promises";
|
|
2
3
|
import path from "node:path";
|
|
3
4
|
export function writeFileSync(file, data, options) {
|
|
4
5
|
const dir = path.dirname(file.toString());
|
|
@@ -7,3 +8,16 @@ export function writeFileSync(file, data, options) {
|
|
|
7
8
|
}
|
|
8
9
|
nodeWriteFileSync(file, data, options);
|
|
9
10
|
}
|
|
11
|
+
export async function writeFile(file, data, options) {
|
|
12
|
+
const dir = path.dirname(file.toString());
|
|
13
|
+
try {
|
|
14
|
+
await stat(dir);
|
|
15
|
+
} catch (error) {
|
|
16
|
+
if (error.code === "ENOENT") {
|
|
17
|
+
await mkdir(dir, { recursive: true });
|
|
18
|
+
} else {
|
|
19
|
+
throw error;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
return nodeWriteFileAsync(file, data, options);
|
|
23
|
+
}
|
package/bin/impl/write-json.d.ts
CHANGED
|
@@ -18,3 +18,11 @@ export interface WriteJsonOptions {
|
|
|
18
18
|
* @param options - Options for stringifying JSON or writing the file.
|
|
19
19
|
*/
|
|
20
20
|
export declare function writeJsonSync(file: string, object: unknown, options?: WriteJsonOptions): void;
|
|
21
|
+
/**
|
|
22
|
+
* Asynchronously writes an object to a JSON file.
|
|
23
|
+
*
|
|
24
|
+
* @param file - The path to the file.
|
|
25
|
+
* @param object - The object to stringify and write.
|
|
26
|
+
* @param options - Options for stringifying JSON or writing the file.
|
|
27
|
+
*/
|
|
28
|
+
export declare function writeJson(file: string, object: unknown, options?: WriteJsonOptions): Promise<void>;
|
package/bin/impl/write-json.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { writeFileSync } from "./write-file.js";
|
|
2
|
+
import { writeFile } from "./write-file.js";
|
|
2
3
|
export function writeJsonSync(file, object, options = {}) {
|
|
3
4
|
const replacer = options.replacer === void 0 ? null : options.replacer;
|
|
4
5
|
const space = options.space === void 0 ? 2 : options.space;
|
|
@@ -9,3 +10,13 @@ export function writeJsonSync(file, object, options = {}) {
|
|
|
9
10
|
const str = JSON.stringify(object, replacer, space);
|
|
10
11
|
writeFileSync(file, str, fileWriteOpts);
|
|
11
12
|
}
|
|
13
|
+
export async function writeJson(file, object, options = {}) {
|
|
14
|
+
const replacer = options.replacer === void 0 ? null : options.replacer;
|
|
15
|
+
const space = options.space === void 0 ? 2 : options.space;
|
|
16
|
+
const fileWriteOpts = {};
|
|
17
|
+
if (options.encoding !== void 0) fileWriteOpts.encoding = options.encoding;
|
|
18
|
+
if (options.mode !== void 0) fileWriteOpts.mode = options.mode;
|
|
19
|
+
if (options.flag !== void 0) fileWriteOpts.flag = options.flag.toString();
|
|
20
|
+
const str = JSON.stringify(object, replacer, space);
|
|
21
|
+
await writeFile(file, str, fileWriteOpts);
|
|
22
|
+
}
|
package/bin/mod.d.ts
CHANGED
|
@@ -1,47 +1,33 @@
|
|
|
1
1
|
import type { Stats } from "node:fs";
|
|
2
|
+
import { readdir, stat } from "node:fs/promises";
|
|
2
3
|
import type { DiveOptions } from "./impl/dive.js";
|
|
3
|
-
import { copySync } from "./impl/copy.js";
|
|
4
|
-
import { createFileSync } from "./impl/create-file.js";
|
|
4
|
+
import { copy, copySync } from "./impl/copy.js";
|
|
5
|
+
import { createFile, createFileSync } from "./impl/create-file.js";
|
|
5
6
|
import { diveSync } from "./impl/dive.js";
|
|
6
|
-
import { emptyDirSync } from "./impl/empty-dir.js";
|
|
7
|
-
import { mkdirsSync } from "./impl/mkdirs.js";
|
|
8
|
-
import { moveSync } from "./impl/move.js";
|
|
9
|
-
import { outputFileSync } from "./impl/output-file.js";
|
|
10
|
-
import { outputJsonSync } from "./impl/output-json.js";
|
|
11
|
-
import { pathExistsSync } from "./impl/path-exists.js";
|
|
12
|
-
import { readFileSync } from "./impl/read-file.js";
|
|
13
|
-
import {
|
|
14
|
-
import { removeSync } from "./impl/remove.js";
|
|
15
|
-
import { writeFileSync } from "./impl/write-file.js";
|
|
16
|
-
import { writeJsonSync } from "./impl/write-json.js";
|
|
17
|
-
declare const readJson: <T>(file: string, options?: BufferEncoding | ReadJsonOptions) => Promise<T>;
|
|
18
|
-
declare const writeJson: (file: string, object: unknown, options?: import("./impl/write-json.js").WriteJsonOptions | undefined) => Promise<void>;
|
|
19
|
-
declare const createFile: (file: string, content?: string | undefined) => Promise<void>;
|
|
20
|
-
declare const writeFile: (file: number | import("fs").PathLike, data: string | NodeJS.ArrayBufferView<ArrayBufferLike>, options?: import("fs").WriteFileOptions | undefined) => Promise<void>;
|
|
21
|
-
declare const readFile: (path: string, options?: BufferEncoding | import("./impl/read-file.js").ReadFileOptions | undefined) => Promise<string | Buffer<ArrayBufferLike>>;
|
|
22
|
-
declare const mkdirs: (dir: string) => Promise<string | undefined>;
|
|
23
|
-
declare const emptyDir: (dir: string) => Promise<void>;
|
|
24
|
-
declare const pathExists: (path: string) => Promise<boolean>;
|
|
25
|
-
declare const copy: (src: string, dest: string, options?: import("./impl/copy.js").CopyOptions | undefined) => Promise<void>;
|
|
26
|
-
declare const move: (src: string, dest: string, options?: import("./impl/move.js").MoveOptions | undefined) => Promise<void>;
|
|
27
|
-
declare const remove: (path: string) => Promise<void>;
|
|
28
|
-
declare const outputJson: (file: string, data: unknown, options?: unknown) => Promise<void>;
|
|
29
|
-
declare const outputFile: (file: string, data: string | Uint8Array<ArrayBufferLike>, options?: unknown) => Promise<void>;
|
|
7
|
+
import { emptyDir, emptyDirSync } from "./impl/empty-dir.js";
|
|
8
|
+
import { mkdirs, mkdirsSync } from "./impl/mkdirs.js";
|
|
9
|
+
import { move, moveSync } from "./impl/move.js";
|
|
10
|
+
import { outputFile, outputFileSync } from "./impl/output-file.js";
|
|
11
|
+
import { outputJson, outputJsonSync } from "./impl/output-json.js";
|
|
12
|
+
import { pathExists, pathExistsSync } from "./impl/path-exists.js";
|
|
13
|
+
import { readFile, readFileSync } from "./impl/read-file.js";
|
|
14
|
+
import { readJson, readJsonSync } from "./impl/read-json.js";
|
|
15
|
+
import { remove, removeSync } from "./impl/remove.js";
|
|
16
|
+
import { writeFile, writeFileSync } from "./impl/write-file.js";
|
|
17
|
+
import { writeJson, writeJsonSync } from "./impl/write-json.js";
|
|
30
18
|
export declare function dive(directory: string, action: (file: string, stat: Stats) => void | Promise<void>, options?: DiveOptions): Promise<void>;
|
|
31
19
|
export declare function dive(directory: string, options?: DiveOptions): Promise<string[]>;
|
|
32
|
-
declare const mkdirp:
|
|
33
|
-
declare const ensureDir:
|
|
34
|
-
declare const ensureFile:
|
|
35
|
-
declare const rimraf:
|
|
36
|
-
declare const ncp:
|
|
20
|
+
declare const mkdirp: typeof mkdirs;
|
|
21
|
+
declare const ensureDir: typeof mkdirs;
|
|
22
|
+
declare const ensureFile: typeof createFile;
|
|
23
|
+
declare const rimraf: typeof remove;
|
|
24
|
+
declare const ncp: typeof copy;
|
|
37
25
|
declare const mkdirpSync: typeof mkdirsSync;
|
|
38
26
|
declare const ensureDirSync: typeof mkdirsSync;
|
|
39
27
|
declare const ensureFileSync: typeof createFileSync;
|
|
40
28
|
declare const rimrafSync: typeof removeSync;
|
|
41
29
|
declare const ncpSync: typeof copySync;
|
|
42
|
-
export
|
|
43
|
-
export { default } from "node:fs";
|
|
44
|
-
export { readJson, writeJson, createFile, writeFile, readFile, mkdirs, emptyDir, pathExists, copy, move, remove, readJsonSync, writeJsonSync, createFileSync, writeFileSync, readFileSync, mkdirsSync, emptyDirSync, pathExistsSync, copySync, moveSync, removeSync, mkdirp, mkdirpSync, rimraf, rimrafSync, ncp, ncpSync, ensureDir, ensureDirSync, ensureFile, ensureFileSync, outputJson, outputJsonSync, outputFile, outputFileSync, diveSync, };
|
|
30
|
+
export { readJson, writeJson, createFile, writeFile, readFile, mkdirs, emptyDir, pathExists, copy, move, remove, readdir, stat, readJsonSync, writeJsonSync, createFileSync, writeFileSync, readFileSync, mkdirsSync, emptyDirSync, pathExistsSync, copySync, moveSync, removeSync, mkdirp, mkdirpSync, rimraf, rimrafSync, ncp, ncpSync, ensureDir, ensureDirSync, ensureFile, ensureFileSync, outputJson, outputJsonSync, outputFile, outputFileSync, diveSync, };
|
|
45
31
|
export type { CopyOptions } from "./impl/copy.js";
|
|
46
32
|
export type { MoveOptions } from "./impl/move.js";
|
|
47
33
|
export type { ReadFileOptions } from "./impl/read-file.js";
|
package/bin/mod.js
CHANGED
|
@@ -1,33 +1,19 @@
|
|
|
1
|
-
import { readdir, stat } from "node:fs/promises";
|
|
1
|
+
import { readdir as nodeReaddirInternal, stat as nodeStatInternal, readdir, stat } from "node:fs/promises";
|
|
2
2
|
import { join as pathJoin } from "node:path";
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import { createFileSync } from "./impl/create-file.js";
|
|
3
|
+
import { copy, copySync } from "./impl/copy.js";
|
|
4
|
+
import { createFile, createFileSync } from "./impl/create-file.js";
|
|
6
5
|
import { diveSync } from "./impl/dive.js";
|
|
7
|
-
import { emptyDirSync } from "./impl/empty-dir.js";
|
|
8
|
-
import { mkdirsSync } from "./impl/mkdirs.js";
|
|
9
|
-
import { moveSync } from "./impl/move.js";
|
|
10
|
-
import { outputFileSync } from "./impl/output-file.js";
|
|
11
|
-
import { outputJsonSync } from "./impl/output-json.js";
|
|
12
|
-
import { pathExistsSync } from "./impl/path-exists.js";
|
|
13
|
-
import { readFileSync } from "./impl/read-file.js";
|
|
14
|
-
import { readJsonSync } from "./impl/read-json.js";
|
|
15
|
-
import { removeSync } from "./impl/remove.js";
|
|
16
|
-
import { writeFileSync } from "./impl/write-file.js";
|
|
17
|
-
import { writeJsonSync } from "./impl/write-json.js";
|
|
18
|
-
const readJson = toAsync(readJsonSync);
|
|
19
|
-
const writeJson = toAsync(writeJsonSync);
|
|
20
|
-
const createFile = toAsync(createFileSync);
|
|
21
|
-
const writeFile = toAsync(writeFileSync);
|
|
22
|
-
const readFile = toAsync(readFileSync);
|
|
23
|
-
const mkdirs = toAsync(mkdirsSync);
|
|
24
|
-
const emptyDir = toAsync(emptyDirSync);
|
|
25
|
-
const pathExists = toAsync(pathExistsSync);
|
|
26
|
-
const copy = toAsync(copySync);
|
|
27
|
-
const move = toAsync(moveSync);
|
|
28
|
-
const remove = toAsync(removeSync);
|
|
29
|
-
const outputJson = toAsync(outputJsonSync);
|
|
30
|
-
const outputFile = toAsync(outputFileSync);
|
|
6
|
+
import { emptyDir, emptyDirSync } from "./impl/empty-dir.js";
|
|
7
|
+
import { mkdirs, mkdirsSync } from "./impl/mkdirs.js";
|
|
8
|
+
import { move, moveSync } from "./impl/move.js";
|
|
9
|
+
import { outputFile, outputFileSync } from "./impl/output-file.js";
|
|
10
|
+
import { outputJson, outputJsonSync } from "./impl/output-json.js";
|
|
11
|
+
import { pathExists, pathExistsSync } from "./impl/path-exists.js";
|
|
12
|
+
import { readFile, readFileSync } from "./impl/read-file.js";
|
|
13
|
+
import { readJson, readJsonSync } from "./impl/read-json.js";
|
|
14
|
+
import { remove, removeSync } from "./impl/remove.js";
|
|
15
|
+
import { writeFile, writeFileSync } from "./impl/write-file.js";
|
|
16
|
+
import { writeJson, writeJsonSync } from "./impl/write-json.js";
|
|
31
17
|
async function* _diveWorker(currentPath, options, currentDepth) {
|
|
32
18
|
const maxDepth = options.depth ?? Number.POSITIVE_INFINITY;
|
|
33
19
|
if (currentDepth > maxDepth) {
|
|
@@ -35,7 +21,7 @@ async function* _diveWorker(currentPath, options, currentDepth) {
|
|
|
35
21
|
}
|
|
36
22
|
let entries;
|
|
37
23
|
try {
|
|
38
|
-
entries = await
|
|
24
|
+
entries = await nodeReaddirInternal(currentPath, { withFileTypes: true });
|
|
39
25
|
} catch (_err) {
|
|
40
26
|
return;
|
|
41
27
|
}
|
|
@@ -54,7 +40,7 @@ async function* _diveWorker(currentPath, options, currentDepth) {
|
|
|
54
40
|
}
|
|
55
41
|
let entryStat;
|
|
56
42
|
try {
|
|
57
|
-
entryStat = await
|
|
43
|
+
entryStat = await nodeStatInternal(entryPath);
|
|
58
44
|
} catch (_err) {
|
|
59
45
|
continue;
|
|
60
46
|
}
|
|
@@ -115,8 +101,6 @@ const ensureDirSync = mkdirsSync;
|
|
|
115
101
|
const ensureFileSync = createFileSync;
|
|
116
102
|
const rimrafSync = removeSync;
|
|
117
103
|
const ncpSync = copySync;
|
|
118
|
-
export * from "node:fs";
|
|
119
|
-
export { default } from "node:fs";
|
|
120
104
|
export {
|
|
121
105
|
readJson,
|
|
122
106
|
writeJson,
|
|
@@ -129,6 +113,8 @@ export {
|
|
|
129
113
|
copy,
|
|
130
114
|
move,
|
|
131
115
|
remove,
|
|
116
|
+
readdir,
|
|
117
|
+
stat,
|
|
132
118
|
readJsonSync,
|
|
133
119
|
writeJsonSync,
|
|
134
120
|
createFileSync,
|
package/package.json
CHANGED
|
@@ -1,14 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"dependencies": {
|
|
3
|
+
"@reliverse/relinka": "^1.4.5",
|
|
3
4
|
"fs-extra": "^11.3.0",
|
|
4
|
-
"jsonfile": "^6.1.0"
|
|
5
|
+
"jsonfile": "^6.1.0",
|
|
6
|
+
"magic-string": "^0.30.17",
|
|
7
|
+
"p-map": "^7.0.3",
|
|
8
|
+
"zeptomatch": "^2.0.1"
|
|
5
9
|
},
|
|
6
10
|
"description": "@reliverse/relifso is a modern filesystem toolkit for builders. Drop-in replacement for node:fs and fs-extra — powered by native promises, built with ES modules, and packed with DX-focused utilities.",
|
|
7
11
|
"homepage": "https://docs.reliverse.org",
|
|
8
12
|
"license": "MIT",
|
|
9
13
|
"name": "@reliverse/relifso",
|
|
10
14
|
"type": "module",
|
|
11
|
-
"version": "1.2.
|
|
15
|
+
"version": "1.2.1",
|
|
12
16
|
"keywords": [
|
|
13
17
|
"fs",
|
|
14
18
|
"file",
|
package/bin/impl/async.d.ts
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Converts a synchronous function into an asynchronous function that returns a Promise.
|
|
3
|
-
*
|
|
4
|
-
* @param fn - The synchronous function to convert.
|
|
5
|
-
* @returns An asynchronous function that wraps the original synchronous function.
|
|
6
|
-
*/
|
|
7
|
-
export declare function toAsync<T extends (...args: any[]) => any>(fn: T): (...args: Parameters<T>) => Promise<Awaited<ReturnType<T>>>;
|