@reliverse/relifso 1.4.5 → 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 +365 -447
- 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/bin/impl/stats.js
DELETED
|
@@ -1,141 +0,0 @@
|
|
|
1
|
-
import { statSync } from "node:fs";
|
|
2
|
-
import { stat } from "node:fs/promises";
|
|
3
|
-
import { logInternal } from "../utils/log.js";
|
|
4
|
-
import { isBun } from "./bun.js";
|
|
5
|
-
export async function toNodeStats(path) {
|
|
6
|
-
if (!isBun) {
|
|
7
|
-
return stat(path);
|
|
8
|
-
}
|
|
9
|
-
try {
|
|
10
|
-
const file = Bun.file(path);
|
|
11
|
-
const size = file.size;
|
|
12
|
-
const lastModified = new Date(file.lastModified);
|
|
13
|
-
return {
|
|
14
|
-
dev: 0,
|
|
15
|
-
ino: 0,
|
|
16
|
-
mode: 0,
|
|
17
|
-
nlink: 0,
|
|
18
|
-
uid: 0,
|
|
19
|
-
gid: 0,
|
|
20
|
-
rdev: 0,
|
|
21
|
-
size,
|
|
22
|
-
blksize: 0,
|
|
23
|
-
blocks: 0,
|
|
24
|
-
atimeMs: lastModified.getTime(),
|
|
25
|
-
mtimeMs: lastModified.getTime(),
|
|
26
|
-
ctimeMs: lastModified.getTime(),
|
|
27
|
-
birthtimeMs: lastModified.getTime(),
|
|
28
|
-
atime: lastModified,
|
|
29
|
-
mtime: lastModified,
|
|
30
|
-
ctime: lastModified,
|
|
31
|
-
birthtime: lastModified,
|
|
32
|
-
isDirectory: () => false,
|
|
33
|
-
// Bun doesn't provide this info directly
|
|
34
|
-
isFile: () => true,
|
|
35
|
-
isBlockDevice: () => false,
|
|
36
|
-
isCharacterDevice: () => false,
|
|
37
|
-
isSymbolicLink: () => false,
|
|
38
|
-
isFIFO: () => false,
|
|
39
|
-
isSocket: () => false
|
|
40
|
-
};
|
|
41
|
-
} catch (error) {
|
|
42
|
-
return stat(path);
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
export async function getStats(path) {
|
|
46
|
-
if (!isBun) {
|
|
47
|
-
return stat(path);
|
|
48
|
-
}
|
|
49
|
-
try {
|
|
50
|
-
return await toNodeStats(path);
|
|
51
|
-
} catch (error) {
|
|
52
|
-
return stat(path);
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
export function getStatsSync(path) {
|
|
56
|
-
if (!isBun) {
|
|
57
|
-
return statSync(path);
|
|
58
|
-
}
|
|
59
|
-
try {
|
|
60
|
-
const file = Bun.file(path);
|
|
61
|
-
const size = file.size;
|
|
62
|
-
const lastModified = new Date(file.lastModified);
|
|
63
|
-
return {
|
|
64
|
-
dev: 0,
|
|
65
|
-
ino: 0,
|
|
66
|
-
mode: 0,
|
|
67
|
-
nlink: 0,
|
|
68
|
-
uid: 0,
|
|
69
|
-
gid: 0,
|
|
70
|
-
rdev: 0,
|
|
71
|
-
size,
|
|
72
|
-
blksize: 0,
|
|
73
|
-
blocks: 0,
|
|
74
|
-
atimeMs: lastModified.getTime(),
|
|
75
|
-
mtimeMs: lastModified.getTime(),
|
|
76
|
-
ctimeMs: lastModified.getTime(),
|
|
77
|
-
birthtimeMs: lastModified.getTime(),
|
|
78
|
-
atime: lastModified,
|
|
79
|
-
mtime: lastModified,
|
|
80
|
-
ctime: lastModified,
|
|
81
|
-
birthtime: lastModified,
|
|
82
|
-
isDirectory: () => false,
|
|
83
|
-
isFile: () => true,
|
|
84
|
-
isBlockDevice: () => false,
|
|
85
|
-
isCharacterDevice: () => false,
|
|
86
|
-
isSymbolicLink: () => false,
|
|
87
|
-
isFIFO: () => false,
|
|
88
|
-
isSocket: () => false
|
|
89
|
-
};
|
|
90
|
-
} catch (error) {
|
|
91
|
-
return statSync(path);
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
export async function getFileExists(path) {
|
|
95
|
-
if (!isBun) {
|
|
96
|
-
try {
|
|
97
|
-
await stat(path);
|
|
98
|
-
return true;
|
|
99
|
-
} catch {
|
|
100
|
-
return false;
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
try {
|
|
104
|
-
const file = Bun.file(path);
|
|
105
|
-
return await file.exists();
|
|
106
|
-
} catch (error) {
|
|
107
|
-
return false;
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
export async function getFileSize(path) {
|
|
111
|
-
if (!isBun) {
|
|
112
|
-
try {
|
|
113
|
-
const stats = await stat(path);
|
|
114
|
-
return stats.size;
|
|
115
|
-
} catch (error) {
|
|
116
|
-
return 0;
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
try {
|
|
120
|
-
const file = Bun.file(path);
|
|
121
|
-
return file.size;
|
|
122
|
-
} catch (error) {
|
|
123
|
-
return 0;
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
export async function getFileLastModified(path) {
|
|
127
|
-
if (!isBun) {
|
|
128
|
-
try {
|
|
129
|
-
const stats = await stat(path);
|
|
130
|
-
return stats.mtime;
|
|
131
|
-
} catch (error) {
|
|
132
|
-
return /* @__PURE__ */ new Date(0);
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
try {
|
|
136
|
-
const file = Bun.file(path);
|
|
137
|
-
return new Date(file.lastModified);
|
|
138
|
-
} catch (error) {
|
|
139
|
-
return /* @__PURE__ */ new Date(0);
|
|
140
|
-
}
|
|
141
|
-
}
|
package/bin/impl/write-file.d.ts
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import { type PathLike, type WriteStream } from "node:fs";
|
|
2
|
-
import { URL } from "node:url";
|
|
3
|
-
export interface WriteFileOptions {
|
|
4
|
-
encoding?: BufferEncoding | null;
|
|
5
|
-
mode?: number;
|
|
6
|
-
flag?: string;
|
|
7
|
-
isJson?: boolean;
|
|
8
|
-
useStreaming?: boolean;
|
|
9
|
-
replacer?: (key: string, value: unknown) => unknown;
|
|
10
|
-
spaces?: string | number;
|
|
11
|
-
}
|
|
12
|
-
type WriteTarget = PathLike | number | URL | WriteStream;
|
|
13
|
-
/**
|
|
14
|
-
* Synchronously writes data to a file, replacing the file if it already exists.
|
|
15
|
-
* Ensures the directory exists before writing.
|
|
16
|
-
*
|
|
17
|
-
* @param file - Path to the file, file descriptor, URL, or special streams (stdout/stderr).
|
|
18
|
-
* @param data - The data to write. Will be converted to string if not already a string.
|
|
19
|
-
* @param options - Options for writing the file. Can be an encoding string or an object.
|
|
20
|
-
*/
|
|
21
|
-
export declare function writeFileSync(file: WriteTarget, data: string | NodeJS.ArrayBufferView | Blob | Response | unknown, options?: WriteFileOptions): void;
|
|
22
|
-
/**
|
|
23
|
-
* Asynchronously writes data to a file, replacing the file if it already exists.
|
|
24
|
-
* Ensures the directory exists before writing.
|
|
25
|
-
*
|
|
26
|
-
* @param file - Path to the file, file descriptor, URL, or special streams (stdout/stderr).
|
|
27
|
-
* @param data - The data to write. Will be converted to string if not already a string.
|
|
28
|
-
* @param options - Options for writing the file. Can be an encoding string or an object.
|
|
29
|
-
*/
|
|
30
|
-
export declare function writeFile(file: WriteTarget, data: string | NodeJS.ArrayBufferView | Blob | Response | unknown, options?: WriteFileOptions): Promise<void>;
|
|
31
|
-
export {};
|
package/bin/impl/write-file.js
DELETED
|
@@ -1,177 +0,0 @@
|
|
|
1
|
-
import { writeFileSync as nodeWriteFileSync, createWriteStream } from "node:fs";
|
|
2
|
-
import { mkdir, writeFile as nodeWriteFileAsync } from "node:fs/promises";
|
|
3
|
-
import path from "node:path";
|
|
4
|
-
import { Transform } from "node:stream";
|
|
5
|
-
import { pipeline } from "node:stream/promises";
|
|
6
|
-
import { URL } from "node:url";
|
|
7
|
-
import { logInternal } from "../utils/log.js";
|
|
8
|
-
import { isBun } from "./bun.js";
|
|
9
|
-
import { mkdirsSync } from "./mkdirs.js";
|
|
10
|
-
function isStdStream(target) {
|
|
11
|
-
return typeof target === "object" && target !== null && "write" in target && typeof target.write === "function";
|
|
12
|
-
}
|
|
13
|
-
function isFileDescriptor(target) {
|
|
14
|
-
return typeof target === "number";
|
|
15
|
-
}
|
|
16
|
-
function isPathLike(target) {
|
|
17
|
-
return !isStdStream(target) && !isFileDescriptor(target) && !(target instanceof URL);
|
|
18
|
-
}
|
|
19
|
-
export function writeFileSync(file, data, options) {
|
|
20
|
-
let isJson = false;
|
|
21
|
-
let replacer;
|
|
22
|
-
let spaces;
|
|
23
|
-
let encoding;
|
|
24
|
-
let mode;
|
|
25
|
-
let flag;
|
|
26
|
-
if (options) {
|
|
27
|
-
isJson = options.isJson ?? isJson;
|
|
28
|
-
replacer = options.replacer;
|
|
29
|
-
spaces = options.spaces;
|
|
30
|
-
encoding = options.encoding;
|
|
31
|
-
mode = options.mode ? Number(options.mode) : void 0;
|
|
32
|
-
flag = options.flag;
|
|
33
|
-
}
|
|
34
|
-
let stringData;
|
|
35
|
-
if (typeof data === "string") {
|
|
36
|
-
stringData = data;
|
|
37
|
-
} else if (data instanceof Uint8Array) {
|
|
38
|
-
stringData = Buffer.from(data).toString(encoding || "utf8");
|
|
39
|
-
} else if (data instanceof Blob || data instanceof Response) {
|
|
40
|
-
throw new Error("Blob and Response are not supported in sync context");
|
|
41
|
-
} else if (data instanceof ArrayBuffer) {
|
|
42
|
-
stringData = Buffer.from(new Uint8Array(data)).toString(encoding || "utf8");
|
|
43
|
-
} else if (ArrayBuffer.isView(data)) {
|
|
44
|
-
stringData = Buffer.from(data.buffer, data.byteOffset, data.byteLength).toString(encoding || "utf8");
|
|
45
|
-
} else if (isJson) {
|
|
46
|
-
try {
|
|
47
|
-
stringData = JSON.stringify(data, replacer, spaces);
|
|
48
|
-
if (stringData === void 0) {
|
|
49
|
-
throw new Error("Failed to stringify JSON object");
|
|
50
|
-
}
|
|
51
|
-
} catch (err) {
|
|
52
|
-
throw new Error(`Failed to stringify JSON data: ${err}`);
|
|
53
|
-
}
|
|
54
|
-
} else {
|
|
55
|
-
stringData = String(data);
|
|
56
|
-
}
|
|
57
|
-
if (isStdStream(file)) {
|
|
58
|
-
file.write(stringData);
|
|
59
|
-
return;
|
|
60
|
-
}
|
|
61
|
-
if (isFileDescriptor(file)) {
|
|
62
|
-
if (isBun) {
|
|
63
|
-
Bun.write(Bun.file(file), stringData);
|
|
64
|
-
return;
|
|
65
|
-
}
|
|
66
|
-
nodeWriteFileSync(file, stringData, { encoding: encoding || "utf8", mode, flag });
|
|
67
|
-
return;
|
|
68
|
-
}
|
|
69
|
-
if (file instanceof URL) {
|
|
70
|
-
if (file.protocol !== "file:") {
|
|
71
|
-
throw new Error("Only file:// URLs are supported");
|
|
72
|
-
}
|
|
73
|
-
file = file.pathname;
|
|
74
|
-
}
|
|
75
|
-
if (!isPathLike(file)) {
|
|
76
|
-
throw new Error("Invalid file target");
|
|
77
|
-
}
|
|
78
|
-
const dir = path.dirname(file.toString());
|
|
79
|
-
mkdirsSync(dir);
|
|
80
|
-
if (isBun) {
|
|
81
|
-
try {
|
|
82
|
-
const filePath = file.toString();
|
|
83
|
-
Bun.write(filePath, stringData);
|
|
84
|
-
return;
|
|
85
|
-
} catch (_error) {
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
nodeWriteFileSync(file, stringData, { encoding: encoding || "utf8", mode, flag });
|
|
89
|
-
}
|
|
90
|
-
export async function writeFile(file, data, options) {
|
|
91
|
-
let isJson = false;
|
|
92
|
-
let useStreaming = false;
|
|
93
|
-
let replacer;
|
|
94
|
-
let spaces;
|
|
95
|
-
let encoding;
|
|
96
|
-
let mode;
|
|
97
|
-
let flag;
|
|
98
|
-
if (options) {
|
|
99
|
-
isJson = options.isJson ?? isJson;
|
|
100
|
-
useStreaming = options.useStreaming ?? useStreaming;
|
|
101
|
-
replacer = options.replacer;
|
|
102
|
-
spaces = options.spaces;
|
|
103
|
-
encoding = options.encoding;
|
|
104
|
-
mode = options.mode ? Number(options.mode) : void 0;
|
|
105
|
-
flag = options.flag;
|
|
106
|
-
}
|
|
107
|
-
let stringData;
|
|
108
|
-
if (typeof data === "string") {
|
|
109
|
-
stringData = data;
|
|
110
|
-
} else if (data instanceof Uint8Array) {
|
|
111
|
-
stringData = Buffer.from(data).toString(encoding || "utf8");
|
|
112
|
-
} else if (data instanceof Blob) {
|
|
113
|
-
stringData = await data.text();
|
|
114
|
-
} else if (data instanceof Response) {
|
|
115
|
-
stringData = await data.text();
|
|
116
|
-
} else if (data instanceof ArrayBuffer) {
|
|
117
|
-
stringData = Buffer.from(new Uint8Array(data)).toString(encoding || "utf8");
|
|
118
|
-
} else if (ArrayBuffer.isView(data)) {
|
|
119
|
-
stringData = Buffer.from(data.buffer, data.byteOffset, data.byteLength).toString(encoding || "utf8");
|
|
120
|
-
} else if (isJson) {
|
|
121
|
-
try {
|
|
122
|
-
stringData = JSON.stringify(data, replacer, spaces);
|
|
123
|
-
if (stringData === void 0) {
|
|
124
|
-
throw new Error("Failed to stringify JSON object");
|
|
125
|
-
}
|
|
126
|
-
} catch (err) {
|
|
127
|
-
throw new Error(`Failed to stringify JSON data: ${err}`);
|
|
128
|
-
}
|
|
129
|
-
} else {
|
|
130
|
-
stringData = String(data);
|
|
131
|
-
}
|
|
132
|
-
if (isStdStream(file)) {
|
|
133
|
-
file.write(stringData);
|
|
134
|
-
return;
|
|
135
|
-
}
|
|
136
|
-
if (isFileDescriptor(file)) {
|
|
137
|
-
if (isBun) {
|
|
138
|
-
await Bun.write(Bun.file(file), stringData);
|
|
139
|
-
return;
|
|
140
|
-
}
|
|
141
|
-
await nodeWriteFileAsync(file, stringData, { encoding: encoding || "utf8", mode, flag });
|
|
142
|
-
return;
|
|
143
|
-
}
|
|
144
|
-
if (file instanceof URL) {
|
|
145
|
-
if (file.protocol !== "file:") {
|
|
146
|
-
throw new Error("Only file:// URLs are supported");
|
|
147
|
-
}
|
|
148
|
-
file = file.pathname;
|
|
149
|
-
}
|
|
150
|
-
if (!isPathLike(file)) {
|
|
151
|
-
throw new Error("Invalid file target");
|
|
152
|
-
}
|
|
153
|
-
const dir = path.dirname(file.toString());
|
|
154
|
-
await mkdir(dir, { recursive: true });
|
|
155
|
-
if (useStreaming) {
|
|
156
|
-
try {
|
|
157
|
-
const writeStream = createWriteStream(file, { encoding: encoding || "utf8", mode, flags: flag });
|
|
158
|
-
const transform = new Transform({
|
|
159
|
-
transform(chunk, _encoding, callback) {
|
|
160
|
-
callback(null, chunk);
|
|
161
|
-
}
|
|
162
|
-
});
|
|
163
|
-
await pipeline(stringData, transform, writeStream);
|
|
164
|
-
return;
|
|
165
|
-
} catch (streamError) {
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
if (isBun) {
|
|
169
|
-
try {
|
|
170
|
-
const filePath = file.toString();
|
|
171
|
-
await Bun.write(filePath, stringData);
|
|
172
|
-
return;
|
|
173
|
-
} catch (_error) {
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
await nodeWriteFileAsync(file, stringData, { encoding: encoding || "utf8", mode, flag });
|
|
177
|
-
}
|
package/bin/impl/write-json.d.ts
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
export interface JsonStringifyOptions {
|
|
2
|
-
replacer?: (key: string, value: unknown) => unknown | (number | string)[] | null;
|
|
3
|
-
spaces?: string | number;
|
|
4
|
-
/**
|
|
5
|
-
* Whether to include comments when writing JSONC files
|
|
6
|
-
* @default false
|
|
7
|
-
*/
|
|
8
|
-
includeComments?: boolean;
|
|
9
|
-
}
|
|
10
|
-
export interface WriteJsonOptions {
|
|
11
|
-
encoding?: BufferEncoding | null;
|
|
12
|
-
mode?: number;
|
|
13
|
-
flag?: string;
|
|
14
|
-
replacer?: (key: string, value: unknown) => unknown | (number | string)[] | null;
|
|
15
|
-
spaces?: string | number;
|
|
16
|
-
throws?: boolean;
|
|
17
|
-
useStreaming?: boolean;
|
|
18
|
-
chunkSize?: number;
|
|
19
|
-
/**
|
|
20
|
-
* Whether to include comments when writing JSONC files
|
|
21
|
-
* @default false
|
|
22
|
-
*/
|
|
23
|
-
includeComments?: boolean;
|
|
24
|
-
}
|
|
25
|
-
/**
|
|
26
|
-
* Synchronously writes a JSON file.
|
|
27
|
-
*
|
|
28
|
-
* @param file - The path to the file.
|
|
29
|
-
* @param object - The object to write.
|
|
30
|
-
* @param options - Options for writing the file or stringifying JSON. Can be an encoding string or an object.
|
|
31
|
-
*/
|
|
32
|
-
export declare function writeJsonSync(file: string, object: unknown, options?: BufferEncoding | WriteJsonOptions): void;
|
|
33
|
-
/**
|
|
34
|
-
* Asynchronously writes a JSON file.
|
|
35
|
-
*
|
|
36
|
-
* @param file - The path to the file.
|
|
37
|
-
* @param object - The object to write.
|
|
38
|
-
* @param options - Options for writing the file or stringifying JSON. Can be an encoding string or an object.
|
|
39
|
-
* @returns A promise that resolves when the file has been written.
|
|
40
|
-
*/
|
|
41
|
-
export declare function writeJson(file: string, object: unknown, options?: BufferEncoding | WriteJsonOptions): Promise<void>;
|
package/bin/impl/write-json.js
DELETED
|
@@ -1,135 +0,0 @@
|
|
|
1
|
-
import { createWriteStream } from "node:fs";
|
|
2
|
-
import { extname } from "node:path";
|
|
3
|
-
import { Transform } from "node:stream";
|
|
4
|
-
import { pipeline } from "node:stream/promises";
|
|
5
|
-
import { stringifyJsonc } from "../utils/json/regular/jsonc.js";
|
|
6
|
-
import { logInternal } from "../utils/log.js";
|
|
7
|
-
import { isBun, getFileBun } from "./bun.js";
|
|
8
|
-
import { writeFileSync } from "./write-file.js";
|
|
9
|
-
import { writeFile } from "./write-file.js";
|
|
10
|
-
export function writeJsonSync(file, object, options) {
|
|
11
|
-
let encoding = "utf8";
|
|
12
|
-
let replacer;
|
|
13
|
-
let spaces;
|
|
14
|
-
let flag;
|
|
15
|
-
let mode;
|
|
16
|
-
let throws = true;
|
|
17
|
-
let includeComments = false;
|
|
18
|
-
if (typeof options === "string") {
|
|
19
|
-
encoding = options;
|
|
20
|
-
} else if (options) {
|
|
21
|
-
encoding = options.encoding ?? encoding;
|
|
22
|
-
replacer = options.replacer;
|
|
23
|
-
spaces = options.spaces;
|
|
24
|
-
flag = options.flag;
|
|
25
|
-
mode = options.mode;
|
|
26
|
-
includeComments = options.includeComments ?? includeComments;
|
|
27
|
-
if (options.throws !== void 0) {
|
|
28
|
-
throws = options.throws;
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
try {
|
|
32
|
-
if (object === void 0) {
|
|
33
|
-
throw new Error("Cannot write undefined as JSON");
|
|
34
|
-
}
|
|
35
|
-
const isJsonc = extname(file).toLowerCase() === ".jsonc";
|
|
36
|
-
if (isBun) {
|
|
37
|
-
try {
|
|
38
|
-
const jsonString2 = isJsonc ? stringifyJsonc(object, { includeComments, spaces: typeof spaces === "number" ? spaces : 2 }) : JSON.stringify(object, replacer, spaces);
|
|
39
|
-
if (jsonString2 === void 0) {
|
|
40
|
-
throw new Error("Failed to stringify JSON object");
|
|
41
|
-
}
|
|
42
|
-
writeFileSync(file, jsonString2, { encoding, flag, mode });
|
|
43
|
-
return;
|
|
44
|
-
} catch (error) {
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
const jsonString = isJsonc ? stringifyJsonc(object, { includeComments, spaces: typeof spaces === "number" ? spaces : 2 }) : JSON.stringify(object, replacer, spaces);
|
|
48
|
-
if (jsonString === void 0) {
|
|
49
|
-
throw new Error("Failed to stringify JSON object");
|
|
50
|
-
}
|
|
51
|
-
writeFileSync(file, jsonString, { encoding, flag, mode });
|
|
52
|
-
} catch (err) {
|
|
53
|
-
if (throws) {
|
|
54
|
-
throw err;
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
export async function writeJson(file, object, options) {
|
|
59
|
-
let encoding = "utf8";
|
|
60
|
-
let replacer;
|
|
61
|
-
let spaces;
|
|
62
|
-
let flag;
|
|
63
|
-
let mode;
|
|
64
|
-
let throws = true;
|
|
65
|
-
let useStreaming = false;
|
|
66
|
-
let chunkSize = 1024 * 1024;
|
|
67
|
-
let includeComments = false;
|
|
68
|
-
if (typeof options === "string") {
|
|
69
|
-
encoding = options;
|
|
70
|
-
} else if (options) {
|
|
71
|
-
encoding = options.encoding ?? encoding;
|
|
72
|
-
replacer = options.replacer;
|
|
73
|
-
spaces = options.spaces;
|
|
74
|
-
flag = options.flag;
|
|
75
|
-
mode = options.mode;
|
|
76
|
-
useStreaming = options.useStreaming ?? useStreaming;
|
|
77
|
-
chunkSize = options.chunkSize ?? chunkSize;
|
|
78
|
-
includeComments = options.includeComments ?? includeComments;
|
|
79
|
-
if (options.throws !== void 0) {
|
|
80
|
-
throws = options.throws;
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
try {
|
|
84
|
-
if (object === void 0) {
|
|
85
|
-
throw new Error("Cannot write undefined as JSON");
|
|
86
|
-
}
|
|
87
|
-
const isJsonc = extname(file).toLowerCase() === ".jsonc";
|
|
88
|
-
if (isBun) {
|
|
89
|
-
try {
|
|
90
|
-
const fileRef = getFileBun(file);
|
|
91
|
-
const jsonString2 = isJsonc ? stringifyJsonc(object, { includeComments, spaces: typeof spaces === "number" ? spaces : 2 }) : JSON.stringify(object, replacer, spaces);
|
|
92
|
-
if (jsonString2 === void 0) {
|
|
93
|
-
throw new Error("Failed to stringify JSON object");
|
|
94
|
-
}
|
|
95
|
-
await Bun.write(fileRef, jsonString2);
|
|
96
|
-
return;
|
|
97
|
-
} catch (error) {
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
if (useStreaming) {
|
|
101
|
-
try {
|
|
102
|
-
const writeStream = createWriteStream(file, {
|
|
103
|
-
encoding,
|
|
104
|
-
mode: mode ? Number(mode) : void 0,
|
|
105
|
-
flags: flag,
|
|
106
|
-
highWaterMark: chunkSize
|
|
107
|
-
// Use chunkSize for stream buffer size
|
|
108
|
-
});
|
|
109
|
-
const jsonString2 = isJsonc ? stringifyJsonc(object, { includeComments, spaces: typeof spaces === "number" ? spaces : 2 }) : JSON.stringify(object, replacer, spaces);
|
|
110
|
-
if (jsonString2 === void 0) {
|
|
111
|
-
throw new Error("Failed to stringify JSON object");
|
|
112
|
-
}
|
|
113
|
-
const transform = new Transform({
|
|
114
|
-
transform(chunk, _encoding, callback) {
|
|
115
|
-
callback(null, chunk);
|
|
116
|
-
}
|
|
117
|
-
});
|
|
118
|
-
transform.write(jsonString2);
|
|
119
|
-
transform.end();
|
|
120
|
-
await pipeline(transform, writeStream);
|
|
121
|
-
return;
|
|
122
|
-
} catch (streamError) {
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
const jsonString = isJsonc ? stringifyJsonc(object, { includeComments, spaces: typeof spaces === "number" ? spaces : 2 }) : JSON.stringify(object, replacer, spaces);
|
|
126
|
-
if (jsonString === void 0) {
|
|
127
|
-
throw new Error("Failed to stringify JSON object");
|
|
128
|
-
}
|
|
129
|
-
await writeFile(file, jsonString, { encoding, flag, mode });
|
|
130
|
-
} catch (err) {
|
|
131
|
-
if (throws) {
|
|
132
|
-
throw err;
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
}
|