@reliverse/relifso 1.4.4 → 2.2.7
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/README.md +371 -367
- package/dist/copy.d.ts +3 -0
- package/dist/copy.js +95 -0
- package/dist/dir.d.ts +13 -0
- package/dist/dir.js +113 -0
- package/dist/file.d.ts +12 -0
- package/dist/file.js +169 -0
- package/dist/helpers.d.ts +3 -0
- package/dist/helpers.js +38 -0
- package/dist/internal/fs.d.ts +16 -0
- package/dist/internal/fs.js +32 -0
- package/dist/internal/path.d.ts +3 -0
- package/dist/internal/path.js +11 -0
- package/dist/links.d.ts +3 -0
- package/dist/links.js +74 -0
- package/dist/mod.d.ts +40 -0
- package/dist/mod.js +77 -0
- package/dist/types.d.ts +50 -0
- package/dist/types.js +0 -0
- package/package.json +15 -42
- package/LICENSE +0 -21
- package/LICENSES +0 -16
- package/bin/impl/bun.d.ts +0 -11
- package/bin/impl/bun.js +0 -23
- package/bin/impl/copy.d.ts +0 -40
- package/bin/impl/copy.js +0 -229
- package/bin/impl/create.d.ts +0 -34
- package/bin/impl/create.js +0 -54
- package/bin/impl/dive.d.ts +0 -27
- package/bin/impl/dive.js +0 -145
- package/bin/impl/empty.d.ts +0 -28
- package/bin/impl/empty.js +0 -75
- package/bin/impl/extras.d.ts +0 -35
- package/bin/impl/extras.js +0 -112
- package/bin/impl/json-utils.d.ts +0 -30
- package/bin/impl/json-utils.js +0 -46
- package/bin/impl/mkdirs.d.ts +0 -7
- package/bin/impl/mkdirs.js +0 -28
- package/bin/impl/move.d.ts +0 -28
- package/bin/impl/move.js +0 -140
- package/bin/impl/output-file.d.ts +0 -17
- package/bin/impl/output-file.js +0 -15
- package/bin/impl/output-json.d.ts +0 -21
- package/bin/impl/output-json.js +0 -77
- package/bin/impl/path-exists.d.ts +0 -2
- package/bin/impl/path-exists.js +0 -16
- package/bin/impl/read-file.d.ts +0 -31
- package/bin/impl/read-file.js +0 -165
- package/bin/impl/read-json.d.ts +0 -31
- package/bin/impl/read-json.js +0 -241
- package/bin/impl/remove.d.ts +0 -2
- package/bin/impl/remove.js +0 -8
- package/bin/impl/stats.d.ts +0 -31
- package/bin/impl/stats.js +0 -141
- package/bin/impl/write-file.d.ts +0 -31
- package/bin/impl/write-file.js +0 -177
- package/bin/impl/write-json.d.ts +0 -41
- package/bin/impl/write-json.js +0 -135
- package/bin/mod.d.ts +0 -253
- package/bin/mod.js +0 -514
- package/bin/utils/json/helpers/JSONRepairError.d.ts +0 -4
- package/bin/utils/json/helpers/JSONRepairError.js +0 -7
- package/bin/utils/json/helpers/JsonSchemaError.d.ts +0 -6
- package/bin/utils/json/helpers/JsonSchemaError.js +0 -6
- package/bin/utils/json/helpers/stringUtils.d.ts +0 -64
- package/bin/utils/json/helpers/stringUtils.js +0 -87
- package/bin/utils/json/regular/jsonc.d.ts +0 -45
- package/bin/utils/json/regular/jsonc.js +0 -88
- package/bin/utils/json/regular/jsonrepair.d.ts +0 -17
- package/bin/utils/json/regular/jsonrepair.js +0 -576
- package/bin/utils/json/regular/validate.d.ts +0 -22
- package/bin/utils/json/regular/validate.js +0 -52
- package/bin/utils/json/stream/JsonStreamError.d.ts +0 -6
- package/bin/utils/json/stream/JsonStreamError.js +0 -6
- package/bin/utils/json/stream/buffer/InputBuffer.d.ts +0 -13
- package/bin/utils/json/stream/buffer/InputBuffer.js +0 -68
- package/bin/utils/json/stream/buffer/OutputBuffer.d.ts +0 -17
- package/bin/utils/json/stream/buffer/OutputBuffer.js +0 -101
- package/bin/utils/json/stream/core.d.ts +0 -10
- package/bin/utils/json/stream/core.js +0 -695
- package/bin/utils/json/stream/jsonl.d.ts +0 -21
- package/bin/utils/json/stream/jsonl.js +0 -55
- package/bin/utils/json/stream/parser.d.ts +0 -14
- package/bin/utils/json/stream/parser.js +0 -81
- package/bin/utils/json/stream/stack.d.ts +0 -19
- package/bin/utils/json/stream/stack.js +0 -43
- package/bin/utils/json/stream/stream.d.ts +0 -6
- package/bin/utils/json/stream/stream.js +0 -30
- package/bin/utils/json/stream/writer.d.ts +0 -14
- package/bin/utils/json/stream/writer.js +0 -44
- package/bin/utils/log.d.ts +0 -1
- package/bin/utils/log.js +0 -7
package/dist/copy.d.ts
ADDED
package/dist/copy.js
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { dirname, join } from "node:path";
|
|
2
|
+
import { ensureDir, pathExists, remove } from "./dir.js";
|
|
3
|
+
import {
|
|
4
|
+
fsLStat,
|
|
5
|
+
fsReaddir,
|
|
6
|
+
fsReadlink,
|
|
7
|
+
fsRename,
|
|
8
|
+
fsStat,
|
|
9
|
+
fsSymlink
|
|
10
|
+
} from "./internal/fs.js";
|
|
11
|
+
import { toPathString } from "./internal/path.js";
|
|
12
|
+
const shouldCopyEntry = async (path, filter) => {
|
|
13
|
+
if (!filter) {
|
|
14
|
+
return true;
|
|
15
|
+
}
|
|
16
|
+
const result = filter(path);
|
|
17
|
+
if (result instanceof Promise) {
|
|
18
|
+
return result;
|
|
19
|
+
}
|
|
20
|
+
return result;
|
|
21
|
+
};
|
|
22
|
+
const copyFile = async (source, destination, overwrite) => {
|
|
23
|
+
await ensureDir(dirname(destination));
|
|
24
|
+
if (!overwrite && await pathExists(destination)) {
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
await Bun.write(destination, Bun.file(source));
|
|
28
|
+
};
|
|
29
|
+
const copySymlink = async (source, destination) => {
|
|
30
|
+
const target = await fsReadlink(source);
|
|
31
|
+
try {
|
|
32
|
+
await fsSymlink(target, destination);
|
|
33
|
+
} catch (error) {
|
|
34
|
+
if (error.code === "EEXIST") {
|
|
35
|
+
await remove(destination);
|
|
36
|
+
await fsSymlink(target, destination);
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
throw error;
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
const copyDirectory = async (source, destination, options) => {
|
|
43
|
+
await ensureDir(destination);
|
|
44
|
+
const entries = await fsReaddir(source, { withFileTypes: true });
|
|
45
|
+
for (const entry of entries) {
|
|
46
|
+
const entrySource = join(source, entry.name);
|
|
47
|
+
const entryDest = join(destination, entry.name);
|
|
48
|
+
if (!await shouldCopyEntry(entrySource, options.filter)) {
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
if (entry.isDirectory()) {
|
|
52
|
+
await copyDirectory(entrySource, entryDest, options);
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
if (entry.isSymbolicLink() && !options.dereference) {
|
|
56
|
+
await copySymlink(entrySource, entryDest);
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
await copyFile(entrySource, entryDest, options.overwrite ?? true);
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
export const copy = async (from, to, options = {}) => {
|
|
63
|
+
const source = toPathString(from);
|
|
64
|
+
const destination = toPathString(to);
|
|
65
|
+
const stats = options.dereference ? await fsStat(source) : await fsLStat(source);
|
|
66
|
+
if (stats.isDirectory()) {
|
|
67
|
+
await copyDirectory(source, destination, options);
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
if (stats.isSymbolicLink() && !options.dereference) {
|
|
71
|
+
await copySymlink(source, destination);
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
await copyFile(source, destination, options.overwrite ?? true);
|
|
75
|
+
};
|
|
76
|
+
export const move = async (from, to, options) => {
|
|
77
|
+
const source = toPathString(from);
|
|
78
|
+
const destination = toPathString(to);
|
|
79
|
+
if (!options?.overwrite && await pathExists(destination)) {
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
try {
|
|
83
|
+
await ensureDir(dirname(destination));
|
|
84
|
+
await fsRename(source, destination);
|
|
85
|
+
} catch (error) {
|
|
86
|
+
if (error.code === "EXDEV") {
|
|
87
|
+
await copy(source, destination, {
|
|
88
|
+
overwrite: options?.overwrite ?? true
|
|
89
|
+
});
|
|
90
|
+
await remove(source);
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
throw error;
|
|
94
|
+
}
|
|
95
|
+
};
|
package/dist/dir.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { EmptyDirOptions, ListFilesOptions, PathLike, ReaddirOptions, SizeOfOptions } from "./types.js";
|
|
2
|
+
export declare const pathExists: (path: PathLike) => Promise<boolean>;
|
|
3
|
+
export declare const ensureDir: (path: PathLike) => Promise<void>;
|
|
4
|
+
export declare const mkdirp: (path: PathLike) => Promise<void>;
|
|
5
|
+
export declare const remove: (path: PathLike, options?: {
|
|
6
|
+
readonly force?: boolean;
|
|
7
|
+
}) => Promise<void>;
|
|
8
|
+
export declare const emptyDir: (path: PathLike, options?: EmptyDirOptions) => Promise<void>;
|
|
9
|
+
export declare const sizeOf: (path: PathLike, options?: SizeOfOptions) => Promise<number>;
|
|
10
|
+
export declare const readdir: (path: PathLike, options?: ReaddirOptions) => Promise<string[]>;
|
|
11
|
+
export declare const readdirRecursive: (path: PathLike, options?: ListFilesOptions) => Promise<string[]>;
|
|
12
|
+
export declare const listFiles: (path: PathLike, options?: ListFilesOptions) => Promise<string[]>;
|
|
13
|
+
export declare const ensureFile: (path: PathLike) => Promise<void>;
|
package/dist/dir.js
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { dirname, join } from "node:path";
|
|
2
|
+
import {
|
|
3
|
+
fsLStat,
|
|
4
|
+
fsMkdir,
|
|
5
|
+
fsReaddir,
|
|
6
|
+
fsRm,
|
|
7
|
+
fsStat,
|
|
8
|
+
fsUnlink
|
|
9
|
+
} from "./internal/fs.js";
|
|
10
|
+
import { toPathString } from "./internal/path.js";
|
|
11
|
+
export const pathExists = async (path) => {
|
|
12
|
+
const pathStr = toPathString(path);
|
|
13
|
+
const file = Bun.file(pathStr);
|
|
14
|
+
return file.exists();
|
|
15
|
+
};
|
|
16
|
+
export const ensureDir = async (path) => {
|
|
17
|
+
await fsMkdir(toPathString(path), { recursive: true });
|
|
18
|
+
};
|
|
19
|
+
export const mkdirp = ensureDir;
|
|
20
|
+
export const remove = async (path, options) => {
|
|
21
|
+
await fsRm(toPathString(path), {
|
|
22
|
+
recursive: true,
|
|
23
|
+
force: options?.force ?? true
|
|
24
|
+
});
|
|
25
|
+
};
|
|
26
|
+
export const emptyDir = async (path, options) => {
|
|
27
|
+
const target = toPathString(path);
|
|
28
|
+
await ensureDir(target);
|
|
29
|
+
const entries = await fsReaddir(target, { withFileTypes: true });
|
|
30
|
+
for (const entry of entries) {
|
|
31
|
+
const entryPath = join(target, entry.name);
|
|
32
|
+
if (options?.filter) {
|
|
33
|
+
const shouldKeep = await options.filter(entryPath);
|
|
34
|
+
if (shouldKeep) {
|
|
35
|
+
continue;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
if (entry.isDirectory()) {
|
|
39
|
+
await remove(entryPath);
|
|
40
|
+
continue;
|
|
41
|
+
}
|
|
42
|
+
await fsUnlink(entryPath);
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
export const sizeOf = async (path, options) => {
|
|
46
|
+
const target = toPathString(path);
|
|
47
|
+
const stats = options?.followSymlinks ? await fsStat(target) : await fsLStat(target);
|
|
48
|
+
if (stats.isDirectory()) {
|
|
49
|
+
let total = 0;
|
|
50
|
+
const entries = await fsReaddir(target, { withFileTypes: true });
|
|
51
|
+
for (const entry of entries) {
|
|
52
|
+
total += await sizeOf(join(target, entry.name), options);
|
|
53
|
+
}
|
|
54
|
+
return total;
|
|
55
|
+
}
|
|
56
|
+
return stats.size;
|
|
57
|
+
};
|
|
58
|
+
export const readdir = async (path, options) => {
|
|
59
|
+
const target = toPathString(path);
|
|
60
|
+
if (options?.withFileTypes) {
|
|
61
|
+
const entries2 = await fsReaddir(target, { withFileTypes: true });
|
|
62
|
+
return entries2.map((entry) => entry.name);
|
|
63
|
+
}
|
|
64
|
+
const entries = await fsReaddir(target);
|
|
65
|
+
return entries;
|
|
66
|
+
};
|
|
67
|
+
export const readdirRecursive = async (path, options) => {
|
|
68
|
+
const target = toPathString(path);
|
|
69
|
+
const queue = [target];
|
|
70
|
+
const files = [];
|
|
71
|
+
const follow = options?.followSymlinks ?? false;
|
|
72
|
+
const extensions = options?.extensions?.map(
|
|
73
|
+
(ext) => ext.startsWith(".") ? ext : `.${ext}`
|
|
74
|
+
);
|
|
75
|
+
while (queue.length > 0) {
|
|
76
|
+
const current = queue.pop();
|
|
77
|
+
const entries = await fsReaddir(current, { withFileTypes: true });
|
|
78
|
+
for (const entry of entries) {
|
|
79
|
+
const entryPath = join(current, entry.name);
|
|
80
|
+
if (entry.isDirectory()) {
|
|
81
|
+
queue.push(entryPath);
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
if (entry.isSymbolicLink() && !follow) {
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
if (extensions && extensions.length > 0) {
|
|
88
|
+
const match = extensions.some((ext) => entry.name.endsWith(ext));
|
|
89
|
+
if (!match) {
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
files.push(entryPath);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return files;
|
|
97
|
+
};
|
|
98
|
+
export const listFiles = readdirRecursive;
|
|
99
|
+
export const ensureFile = async (path) => {
|
|
100
|
+
const fullPath = toPathString(path);
|
|
101
|
+
const dir = dirname(fullPath);
|
|
102
|
+
if (dir.length > 0) {
|
|
103
|
+
await ensureDir(dir);
|
|
104
|
+
}
|
|
105
|
+
try {
|
|
106
|
+
await Bun.write(fullPath, "");
|
|
107
|
+
} catch (error) {
|
|
108
|
+
const code = error.code;
|
|
109
|
+
if (code !== "EEXIST") {
|
|
110
|
+
throw error;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
};
|
package/dist/file.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { FileEncoding, FileInput, JsonWriteOptions, PathLike, ReadFileOptions, WriteFileOptions } from "./types.js";
|
|
2
|
+
export declare function readFile(path: PathLike, options: ReadFileOptions & {
|
|
3
|
+
readonly encoding: FileEncoding;
|
|
4
|
+
}): Promise<string>;
|
|
5
|
+
export declare function readFile(path: PathLike): Promise<Uint8Array>;
|
|
6
|
+
export declare const readJson: <T>(path: PathLike, _options?: ReadFileOptions) => Promise<T>;
|
|
7
|
+
export declare const readJSONSync: <T>(path: PathLike) => T;
|
|
8
|
+
export declare const writeFile: (destination: PathLike, input: FileInput, options?: WriteFileOptions) => Promise<void>;
|
|
9
|
+
export declare const writeJson: (destination: PathLike, data: unknown, options?: JsonWriteOptions) => Promise<void>;
|
|
10
|
+
export declare const outputFile: (destination: PathLike, input: FileInput, options?: WriteFileOptions) => Promise<void>;
|
|
11
|
+
export declare const outputJson: (destination: PathLike, data: unknown, options?: JsonWriteOptions) => Promise<void>;
|
|
12
|
+
export declare const appendFile: (file: PathLike, data: FileInput, options?: WriteFileOptions) => Promise<void>;
|
package/dist/file.js
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import { dirname } from "node:path";
|
|
3
|
+
import { ensureDir } from "./dir.js";
|
|
4
|
+
import { fsAppendFile, fsWriteFile } from "./internal/fs.js";
|
|
5
|
+
import { toPathString } from "./internal/path.js";
|
|
6
|
+
const toBunWritable = async (input) => {
|
|
7
|
+
if (typeof input === "string" || input instanceof Uint8Array) {
|
|
8
|
+
return input;
|
|
9
|
+
}
|
|
10
|
+
if (ArrayBuffer.isView(input)) {
|
|
11
|
+
return new Uint8Array(input.buffer, input.byteOffset, input.byteLength);
|
|
12
|
+
}
|
|
13
|
+
if (input instanceof Blob) {
|
|
14
|
+
const buffer = await input.arrayBuffer();
|
|
15
|
+
return new Uint8Array(buffer);
|
|
16
|
+
}
|
|
17
|
+
if (input instanceof SharedArrayBuffer) {
|
|
18
|
+
return new Uint8Array(input);
|
|
19
|
+
}
|
|
20
|
+
if (input instanceof ArrayBuffer) {
|
|
21
|
+
return new Uint8Array(input);
|
|
22
|
+
}
|
|
23
|
+
return new Uint8Array(input);
|
|
24
|
+
};
|
|
25
|
+
const toNodeWritable = async (input) => {
|
|
26
|
+
if (typeof input === "string") {
|
|
27
|
+
return input;
|
|
28
|
+
}
|
|
29
|
+
if (input instanceof Blob) {
|
|
30
|
+
const buffer = await input.arrayBuffer();
|
|
31
|
+
return new Uint8Array(buffer);
|
|
32
|
+
}
|
|
33
|
+
if (input instanceof Uint8Array) {
|
|
34
|
+
return input;
|
|
35
|
+
}
|
|
36
|
+
if (ArrayBuffer.isView(input)) {
|
|
37
|
+
return new Uint8Array(input.buffer, input.byteOffset, input.byteLength);
|
|
38
|
+
}
|
|
39
|
+
if (input instanceof SharedArrayBuffer) {
|
|
40
|
+
return new Uint8Array(input);
|
|
41
|
+
}
|
|
42
|
+
return new Uint8Array(input);
|
|
43
|
+
};
|
|
44
|
+
const ensureOutputDestination = async (path) => {
|
|
45
|
+
const resolved = toPathString(path);
|
|
46
|
+
const directory = dirname(resolved);
|
|
47
|
+
if (directory.length > 0) {
|
|
48
|
+
await ensureDir(directory);
|
|
49
|
+
}
|
|
50
|
+
return resolved;
|
|
51
|
+
};
|
|
52
|
+
export async function readFile(path, options) {
|
|
53
|
+
const file = Bun.file(toPathString(path));
|
|
54
|
+
const encoding = options?.encoding;
|
|
55
|
+
if (!encoding || encoding === "binary") {
|
|
56
|
+
return file.bytes();
|
|
57
|
+
}
|
|
58
|
+
if (encoding === "utf8" || encoding === "utf-8") {
|
|
59
|
+
return file.text();
|
|
60
|
+
}
|
|
61
|
+
const bytes = await file.bytes();
|
|
62
|
+
const buffer = Buffer.from(bytes);
|
|
63
|
+
switch (encoding) {
|
|
64
|
+
case "ascii":
|
|
65
|
+
return buffer.toString("ascii");
|
|
66
|
+
case "base64":
|
|
67
|
+
return buffer.toString("base64");
|
|
68
|
+
case "hex":
|
|
69
|
+
return buffer.toString("hex");
|
|
70
|
+
case "latin1":
|
|
71
|
+
return buffer.toString("latin1");
|
|
72
|
+
case "ucs-2":
|
|
73
|
+
case "ucs2":
|
|
74
|
+
return buffer.toString("ucs2");
|
|
75
|
+
case "utf16le":
|
|
76
|
+
return buffer.toString("utf16le");
|
|
77
|
+
default:
|
|
78
|
+
return buffer.toString("utf8");
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
export const readJson = async (path, _options) => {
|
|
82
|
+
const file = Bun.file(toPathString(path));
|
|
83
|
+
return file.json();
|
|
84
|
+
};
|
|
85
|
+
export const readJSONSync = (path) => {
|
|
86
|
+
const pathStr = toPathString(path);
|
|
87
|
+
const content = readFileSync(pathStr, "utf8");
|
|
88
|
+
return JSON.parse(content);
|
|
89
|
+
};
|
|
90
|
+
export const writeFile = async (destination, input, options) => {
|
|
91
|
+
const encoding = options?.encoding;
|
|
92
|
+
const hasSpecialOptions = options?.mode !== void 0 || options?.signal !== void 0;
|
|
93
|
+
if (typeof input === "string" && encoding && encoding !== "utf8" && encoding !== "utf-8" && encoding !== "binary") {
|
|
94
|
+
const buffer = Buffer.from(input, encoding);
|
|
95
|
+
const bytes = new Uint8Array(buffer);
|
|
96
|
+
if (!hasSpecialOptions) {
|
|
97
|
+
await Bun.write(destination, bytes);
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
const path2 = toPathString(destination);
|
|
101
|
+
await fsWriteFile(path2, bytes, {
|
|
102
|
+
mode: options.mode,
|
|
103
|
+
signal: options.signal
|
|
104
|
+
});
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
if (!hasSpecialOptions) {
|
|
108
|
+
if (typeof input === "string" || input instanceof Uint8Array || input instanceof Blob) {
|
|
109
|
+
await Bun.write(destination, input);
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
const payload2 = await toBunWritable(input);
|
|
113
|
+
await Bun.write(destination, payload2);
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
const payload = await toNodeWritable(input);
|
|
117
|
+
const path = toPathString(destination);
|
|
118
|
+
await fsWriteFile(path, payload, {
|
|
119
|
+
mode: options.mode,
|
|
120
|
+
signal: options.signal
|
|
121
|
+
});
|
|
122
|
+
};
|
|
123
|
+
export const writeJson = async (destination, data, options) => {
|
|
124
|
+
const spaces = options?.spaces ?? 2;
|
|
125
|
+
const text = `${JSON.stringify(data, void 0, spaces)}
|
|
126
|
+
`;
|
|
127
|
+
await writeFile(destination, text, options);
|
|
128
|
+
};
|
|
129
|
+
export const outputFile = async (destination, input, options) => {
|
|
130
|
+
const target = await ensureOutputDestination(destination);
|
|
131
|
+
await writeFile(target, input, options);
|
|
132
|
+
};
|
|
133
|
+
export const outputJson = async (destination, data, options) => {
|
|
134
|
+
const target = await ensureOutputDestination(destination);
|
|
135
|
+
await writeJson(target, data, options);
|
|
136
|
+
};
|
|
137
|
+
export const appendFile = async (file, data, options) => {
|
|
138
|
+
const pathStr = toPathString(file);
|
|
139
|
+
const encoding = options?.encoding;
|
|
140
|
+
const dir = dirname(pathStr);
|
|
141
|
+
if (dir.length > 0) {
|
|
142
|
+
await ensureDir(dir);
|
|
143
|
+
}
|
|
144
|
+
if (typeof data === "string" && encoding && encoding !== "utf8" && encoding !== "utf-8" && encoding !== "binary") {
|
|
145
|
+
const buffer = Buffer.from(data, encoding);
|
|
146
|
+
await fsAppendFile(pathStr, buffer, {
|
|
147
|
+
mode: options?.mode
|
|
148
|
+
});
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
let payload;
|
|
152
|
+
if (typeof data === "string") {
|
|
153
|
+
payload = data;
|
|
154
|
+
} else if (data instanceof Uint8Array) {
|
|
155
|
+
payload = data;
|
|
156
|
+
} else if (data instanceof Blob) {
|
|
157
|
+
const buffer = await data.arrayBuffer();
|
|
158
|
+
payload = new Uint8Array(buffer);
|
|
159
|
+
} else if (ArrayBuffer.isView(data)) {
|
|
160
|
+
payload = new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
|
|
161
|
+
} else if (data instanceof SharedArrayBuffer) {
|
|
162
|
+
payload = new Uint8Array(data);
|
|
163
|
+
} else {
|
|
164
|
+
payload = new Uint8Array(data);
|
|
165
|
+
}
|
|
166
|
+
await fsAppendFile(pathStr, payload, {
|
|
167
|
+
mode: options?.mode
|
|
168
|
+
});
|
|
169
|
+
};
|
package/dist/helpers.js
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { ensureFile } from "./dir.js";
|
|
2
|
+
import { readFile } from "./file.js";
|
|
3
|
+
import { fsStat, fsUtimes } from "./internal/fs.js";
|
|
4
|
+
import { toPathString } from "./internal/path.js";
|
|
5
|
+
export const touch = async (path, options) => {
|
|
6
|
+
const target = toPathString(path);
|
|
7
|
+
await ensureFile(target);
|
|
8
|
+
const stats = await fsStat(target);
|
|
9
|
+
if (options instanceof Date) {
|
|
10
|
+
await fsUtimes(target, stats.atime, options);
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
const mtime = options?.mtime ?? /* @__PURE__ */ new Date();
|
|
14
|
+
const atime = options?.atime ?? stats.atime;
|
|
15
|
+
await fsUtimes(target, atime, mtime);
|
|
16
|
+
};
|
|
17
|
+
export const readLines = async (path, options) => {
|
|
18
|
+
const text = await readFile(path, { encoding: "utf8" });
|
|
19
|
+
const lines = text.split(/\r?\n/);
|
|
20
|
+
const result = [];
|
|
21
|
+
for (let index = 0; index < lines.length; index += 1) {
|
|
22
|
+
let line = lines[index] ?? "";
|
|
23
|
+
if (options?.trim) {
|
|
24
|
+
line = line.trim();
|
|
25
|
+
}
|
|
26
|
+
if (options?.skipEmpty && line === "") {
|
|
27
|
+
continue;
|
|
28
|
+
}
|
|
29
|
+
if (options?.maxLines && result.length >= options.maxLines) {
|
|
30
|
+
break;
|
|
31
|
+
}
|
|
32
|
+
if (line === "" && index === lines.length - 1) {
|
|
33
|
+
continue;
|
|
34
|
+
}
|
|
35
|
+
result.push(line);
|
|
36
|
+
}
|
|
37
|
+
return result;
|
|
38
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { access, appendFile, link, lstat, mkdir, readdir, readlink, rename, rm, stat, symlink, unlink, utimes, writeFile } from "node:fs/promises";
|
|
2
|
+
export declare const fsAccess: typeof access;
|
|
3
|
+
export declare const fsAppendFile: typeof appendFile;
|
|
4
|
+
export declare const fsConstants: typeof import("fs").constants;
|
|
5
|
+
export declare const fsLink: typeof link;
|
|
6
|
+
export declare const fsLStat: typeof lstat;
|
|
7
|
+
export declare const fsMkdir: typeof mkdir;
|
|
8
|
+
export declare const fsReaddir: typeof readdir;
|
|
9
|
+
export declare const fsReadlink: typeof readlink;
|
|
10
|
+
export declare const fsRename: typeof rename;
|
|
11
|
+
export declare const fsRm: typeof rm;
|
|
12
|
+
export declare const fsStat: typeof stat;
|
|
13
|
+
export declare const fsSymlink: typeof symlink;
|
|
14
|
+
export declare const fsUnlink: typeof unlink;
|
|
15
|
+
export declare const fsUtimes: typeof utimes;
|
|
16
|
+
export declare const fsWriteFile: typeof writeFile;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import {
|
|
2
|
+
access,
|
|
3
|
+
appendFile,
|
|
4
|
+
constants,
|
|
5
|
+
link,
|
|
6
|
+
lstat,
|
|
7
|
+
mkdir,
|
|
8
|
+
readdir,
|
|
9
|
+
readlink,
|
|
10
|
+
rename,
|
|
11
|
+
rm,
|
|
12
|
+
stat,
|
|
13
|
+
symlink,
|
|
14
|
+
unlink,
|
|
15
|
+
utimes,
|
|
16
|
+
writeFile
|
|
17
|
+
} from "node:fs/promises";
|
|
18
|
+
export const fsAccess = access;
|
|
19
|
+
export const fsAppendFile = appendFile;
|
|
20
|
+
export const fsConstants = constants;
|
|
21
|
+
export const fsLink = link;
|
|
22
|
+
export const fsLStat = lstat;
|
|
23
|
+
export const fsMkdir = mkdir;
|
|
24
|
+
export const fsReaddir = readdir;
|
|
25
|
+
export const fsReadlink = readlink;
|
|
26
|
+
export const fsRename = rename;
|
|
27
|
+
export const fsRm = rm;
|
|
28
|
+
export const fsStat = stat;
|
|
29
|
+
export const fsSymlink = symlink;
|
|
30
|
+
export const fsUnlink = unlink;
|
|
31
|
+
export const fsUtimes = utimes;
|
|
32
|
+
export const fsWriteFile = writeFile;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { dirname, resolve } from "node:path";
|
|
2
|
+
import { fileURLToPath } from "node:url";
|
|
3
|
+
export const toPathString = (input) => {
|
|
4
|
+
if (typeof input === "string") {
|
|
5
|
+
return resolve(input);
|
|
6
|
+
}
|
|
7
|
+
return resolve(fileURLToPath(input));
|
|
8
|
+
};
|
|
9
|
+
export const getPathDirname = (input) => {
|
|
10
|
+
return dirname(toPathString(input));
|
|
11
|
+
};
|
package/dist/links.d.ts
ADDED
package/dist/links.js
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { dirname } from "node:path";
|
|
2
|
+
import { ensureDir, remove } from "./dir.js";
|
|
3
|
+
import { fsLink, fsLStat, fsSymlink, fsUnlink } from "./internal/fs.js";
|
|
4
|
+
import { toPathString } from "./internal/path.js";
|
|
5
|
+
const ensureParentDirectory = async (path) => {
|
|
6
|
+
const directory = dirname(path);
|
|
7
|
+
if (directory.length === 0) {
|
|
8
|
+
return;
|
|
9
|
+
}
|
|
10
|
+
await ensureDir(directory);
|
|
11
|
+
};
|
|
12
|
+
const removeExistingPath = async (path) => {
|
|
13
|
+
try {
|
|
14
|
+
await fsUnlink(path);
|
|
15
|
+
} catch (error) {
|
|
16
|
+
const code = error.code;
|
|
17
|
+
if (code === "ENOENT") {
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
if (code === "EISDIR" || code === "EPERM") {
|
|
21
|
+
await remove(path);
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
throw error;
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
const resolveSymlinkType = async (source, type) => {
|
|
28
|
+
if (type) {
|
|
29
|
+
if (type === "dir" && process.platform === "win32") {
|
|
30
|
+
return "junction";
|
|
31
|
+
}
|
|
32
|
+
return type;
|
|
33
|
+
}
|
|
34
|
+
if (process.platform !== "win32") {
|
|
35
|
+
return void 0;
|
|
36
|
+
}
|
|
37
|
+
try {
|
|
38
|
+
const stats = await fsLStat(source);
|
|
39
|
+
return stats.isDirectory() ? "junction" : "file";
|
|
40
|
+
} catch {
|
|
41
|
+
return "file";
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
export const ensureLink = async (from, to) => {
|
|
45
|
+
const source = toPathString(from);
|
|
46
|
+
const destination = toPathString(to);
|
|
47
|
+
await ensureParentDirectory(destination);
|
|
48
|
+
try {
|
|
49
|
+
await fsLink(source, destination);
|
|
50
|
+
} catch (error) {
|
|
51
|
+
if (error.code === "EEXIST") {
|
|
52
|
+
await removeExistingPath(destination);
|
|
53
|
+
await fsLink(source, destination);
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
throw error;
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
export const ensureSymlink = async (from, to, options) => {
|
|
60
|
+
const source = toPathString(from);
|
|
61
|
+
const destination = toPathString(to);
|
|
62
|
+
const type = await resolveSymlinkType(source, options?.type);
|
|
63
|
+
await ensureParentDirectory(destination);
|
|
64
|
+
try {
|
|
65
|
+
await fsSymlink(source, destination, type);
|
|
66
|
+
} catch (error) {
|
|
67
|
+
if (error.code === "EEXIST") {
|
|
68
|
+
await remove(destination);
|
|
69
|
+
await fsSymlink(source, destination, type);
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
throw error;
|
|
73
|
+
}
|
|
74
|
+
};
|
package/dist/mod.d.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { copy, move } from "./copy.js";
|
|
2
|
+
import { emptyDir, ensureDir, ensureFile, listFiles, mkdirp, pathExists, readdir, readdirRecursive, remove, sizeOf } from "./dir.js";
|
|
3
|
+
import { appendFile, outputFile, outputJson, readFile, readJSONSync, readJson, writeFile, writeJson } from "./file.js";
|
|
4
|
+
import { readLines, touch } from "./helpers.js";
|
|
5
|
+
import { ensureLink, ensureSymlink } from "./links.js";
|
|
6
|
+
export type { CopyOptions, EmptyDirOptions, EnsureSymlinkOptions, FileEncoding, JsonWriteOptions, ListFilesOptions, MoveOptions, PathLike, ReaddirOptions, ReadFileOptions, ReadLinesOptions, SizeOfOptions, TouchOptions, WriteFileOptions, } from "./types.js";
|
|
7
|
+
export { copy, move };
|
|
8
|
+
export { emptyDir, ensureDir, ensureFile, listFiles, mkdirp, pathExists, readdir, readdirRecursive, remove, sizeOf, };
|
|
9
|
+
export { appendFile, outputFile, outputJson, readFile, readJson, readJSONSync, writeFile, writeJson, };
|
|
10
|
+
export { readLines, touch };
|
|
11
|
+
export { ensureLink, ensureSymlink };
|
|
12
|
+
declare const fs: {
|
|
13
|
+
readonly appendFile: (file: import("./types.js").PathLike, data: import("./types.js").FileInput, options?: import("./types.js").WriteFileOptions) => Promise<void>;
|
|
14
|
+
readonly copy: (from: import("./types.js").PathLike, to: import("./types.js").PathLike, options?: import("./types.js").CopyOptions) => Promise<void>;
|
|
15
|
+
readonly move: (from: import("./types.js").PathLike, to: import("./types.js").PathLike, options?: import("./types.js").MoveOptions) => Promise<void>;
|
|
16
|
+
readonly emptyDir: (path: import("./types.js").PathLike, options?: import("./types.js").EmptyDirOptions) => Promise<void>;
|
|
17
|
+
readonly ensureDir: (path: import("./types.js").PathLike) => Promise<void>;
|
|
18
|
+
readonly ensureFile: (path: import("./types.js").PathLike) => Promise<void>;
|
|
19
|
+
readonly listFiles: (path: import("./types.js").PathLike, options?: import("./types.js").ListFilesOptions) => Promise<string[]>;
|
|
20
|
+
readonly mkdirp: (path: import("./types.js").PathLike) => Promise<void>;
|
|
21
|
+
readonly pathExists: (path: import("./types.js").PathLike) => Promise<boolean>;
|
|
22
|
+
readonly readdir: (path: import("./types.js").PathLike, options?: import("./types.js").ReaddirOptions) => Promise<string[]>;
|
|
23
|
+
readonly readdirRecursive: (path: import("./types.js").PathLike, options?: import("./types.js").ListFilesOptions) => Promise<string[]>;
|
|
24
|
+
readonly remove: (path: import("./types.js").PathLike, options?: {
|
|
25
|
+
readonly force?: boolean;
|
|
26
|
+
}) => Promise<void>;
|
|
27
|
+
readonly sizeOf: (path: import("./types.js").PathLike, options?: import("./types.js").SizeOfOptions) => Promise<number>;
|
|
28
|
+
readonly outputFile: (destination: import("./types.js").PathLike, input: import("./types.js").FileInput, options?: import("./types.js").WriteFileOptions) => Promise<void>;
|
|
29
|
+
readonly outputJson: (destination: import("./types.js").PathLike, data: unknown, options?: import("./types.js").JsonWriteOptions) => Promise<void>;
|
|
30
|
+
readonly readFile: typeof readFile;
|
|
31
|
+
readonly readJson: <T>(path: import("./types.js").PathLike, _options?: import("./types.js").ReadFileOptions) => Promise<T>;
|
|
32
|
+
readonly readJSONSync: <T>(path: import("./types.js").PathLike) => T;
|
|
33
|
+
readonly writeFile: (destination: import("./types.js").PathLike, input: import("./types.js").FileInput, options?: import("./types.js").WriteFileOptions) => Promise<void>;
|
|
34
|
+
readonly writeJson: (destination: import("./types.js").PathLike, data: unknown, options?: import("./types.js").JsonWriteOptions) => Promise<void>;
|
|
35
|
+
readonly readLines: (path: import("./types.js").PathLike, options?: import("./types.js").ReadLinesOptions) => Promise<string[]>;
|
|
36
|
+
readonly touch: (path: import("./types.js").PathLike, options?: import("./types.js").TouchOptions | Date) => Promise<void>;
|
|
37
|
+
readonly ensureLink: (from: import("./types.js").PathLike, to: import("./types.js").PathLike) => Promise<void>;
|
|
38
|
+
readonly ensureSymlink: (from: import("./types.js").PathLike, to: import("./types.js").PathLike, options?: import("./types.js").EnsureSymlinkOptions) => Promise<void>;
|
|
39
|
+
};
|
|
40
|
+
export default fs;
|