@reliverse/relifso 1.3.0 → 1.4.0

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.
@@ -1,9 +1,15 @@
1
1
  import { readFileSync } from "node:fs";
2
2
  import { readFile } from "node:fs/promises";
3
+ import { isBun, getFileBun } from "./bun.js";
4
+ import { logInternal } from "./logger.js";
5
+ import { outputJsonSync } from "./output-json.js";
6
+ import { pathExistsSync } from "./path-exists.js";
3
7
  export function readJsonSync(file, options) {
4
8
  let encoding = "utf8";
5
9
  let reviver;
6
10
  let throws = true;
11
+ let defaultValue = { sync: true };
12
+ let ensure = true;
7
13
  if (typeof options === "string") {
8
14
  encoding = options;
9
15
  } else if (options) {
@@ -12,15 +18,42 @@ export function readJsonSync(file, options) {
12
18
  if (options.throws !== void 0) {
13
19
  throws = options.throws;
14
20
  }
21
+ if (options.defaultValue !== void 0) {
22
+ defaultValue = options.defaultValue;
23
+ }
24
+ if (options.ensure !== void 0) {
25
+ ensure = options.ensure;
26
+ }
15
27
  }
16
28
  try {
29
+ if (!pathExistsSync(file) && ensure) {
30
+ outputJsonSync(file, defaultValue);
31
+ return defaultValue;
32
+ }
17
33
  const data = readFileSync(file, { encoding });
18
- return JSON.parse(data, reviver);
34
+ if (!data || !data.trim()) {
35
+ return defaultValue;
36
+ }
37
+ try {
38
+ const parsed = JSON.parse(data, reviver);
39
+ if (parsed === null || typeof parsed !== "object") {
40
+ if (throws) {
41
+ throw new Error("Invalid JSON data: expected an object");
42
+ }
43
+ return defaultValue;
44
+ }
45
+ return parsed;
46
+ } catch (parseError) {
47
+ if (throws) {
48
+ throw parseError;
49
+ }
50
+ return defaultValue;
51
+ }
19
52
  } catch (err) {
20
53
  if (throws) {
21
54
  throw err;
22
55
  }
23
- return void 0;
56
+ return defaultValue;
24
57
  }
25
58
  }
26
59
  export async function readJson(file, options) {
@@ -28,6 +61,7 @@ export async function readJson(file, options) {
28
61
  let reviver;
29
62
  let throws = true;
30
63
  let flag;
64
+ let defaultValue = {};
31
65
  if (typeof options === "string") {
32
66
  encoding = options;
33
67
  } else if (options) {
@@ -37,14 +71,59 @@ export async function readJson(file, options) {
37
71
  if (options.throws !== void 0) {
38
72
  throws = options.throws;
39
73
  }
74
+ if (options.defaultValue !== void 0) {
75
+ defaultValue = options.defaultValue;
76
+ }
40
77
  }
41
78
  try {
79
+ if (isBun) {
80
+ try {
81
+ const fileRef = getFileBun(file);
82
+ try {
83
+ const content = await fileRef.text();
84
+ if (!content || !content.trim()) {
85
+ return defaultValue;
86
+ }
87
+ const parsed = JSON.parse(content, reviver);
88
+ if (parsed === null || typeof parsed !== "object") {
89
+ if (throws) {
90
+ throw new Error("Invalid JSON data: expected an object");
91
+ }
92
+ return defaultValue;
93
+ }
94
+ return parsed;
95
+ } catch (parseError) {
96
+ if (throws) {
97
+ throw parseError;
98
+ }
99
+ return defaultValue;
100
+ }
101
+ } catch (error) {
102
+ }
103
+ }
42
104
  const data = await readFile(file, { encoding, flag });
43
- return JSON.parse(data, reviver);
105
+ if (!data || !data.trim()) {
106
+ return defaultValue;
107
+ }
108
+ try {
109
+ const parsed = JSON.parse(data, reviver);
110
+ if (parsed === null || typeof parsed !== "object") {
111
+ if (throws) {
112
+ throw new Error("Invalid JSON data: expected an object");
113
+ }
114
+ return defaultValue;
115
+ }
116
+ return parsed;
117
+ } catch (parseError) {
118
+ if (throws) {
119
+ throw parseError;
120
+ }
121
+ return defaultValue;
122
+ }
44
123
  } catch (err) {
45
124
  if (throws) {
46
125
  throw err;
47
126
  }
48
- return void 0;
127
+ return defaultValue;
49
128
  }
50
129
  }
@@ -1,10 +1,18 @@
1
- import { existsSync, mkdirSync, writeFileSync as nodeWriteFileSync } from "node:fs";
1
+ import { writeFileSync as nodeWriteFileSync } from "node:fs";
2
2
  import { mkdir, writeFile as nodeWriteFileAsync, stat } from "node:fs/promises";
3
3
  import path from "node:path";
4
+ import { isBun, getFileBun } from "./bun.js";
5
+ import { logInternal } from "./logger.js";
6
+ import { mkdirsSync } from "./mkdirs.js";
4
7
  export function writeFileSync(file, data, options) {
5
8
  const dir = path.dirname(file.toString());
6
- if (!existsSync(dir)) {
7
- mkdirSync(dir, { recursive: true });
9
+ mkdirsSync(dir);
10
+ if (isBun) {
11
+ try {
12
+ nodeWriteFileSync(file, data, options);
13
+ return;
14
+ } catch (_error) {
15
+ }
8
16
  }
9
17
  nodeWriteFileSync(file, data, options);
10
18
  }
@@ -12,11 +20,28 @@ export async function writeFile(file, data, options) {
12
20
  const dir = path.dirname(file.toString());
13
21
  try {
14
22
  await stat(dir);
15
- } catch (error) {
16
- if (error.code === "ENOENT") {
23
+ } catch (_error) {
24
+ if (_error.code === "ENOENT") {
17
25
  await mkdir(dir, { recursive: true });
18
26
  } else {
19
- throw error;
27
+ throw _error;
28
+ }
29
+ }
30
+ if (isBun) {
31
+ try {
32
+ const filePath = file.toString();
33
+ const bunFile = getFileBun(filePath);
34
+ let writeData;
35
+ if (typeof data === "string") {
36
+ writeData = data;
37
+ } else if (data instanceof Uint8Array) {
38
+ writeData = data;
39
+ } else {
40
+ writeData = new Uint8Array(data.buffer);
41
+ }
42
+ await Bun.write(bunFile, writeData);
43
+ return;
44
+ } catch (_error) {
20
45
  }
21
46
  }
22
47
  return nodeWriteFileAsync(file, data, options);
@@ -1,4 +1,4 @@
1
- import type { Mode, OpenMode } from "node:fs";
1
+ import type { Mode } from "node:fs";
2
2
  export interface JsonStringifyOptions {
3
3
  replacer?: (key: string, value: unknown) => unknown | (number | string)[] | null;
4
4
  spaces?: string | number;
@@ -6,23 +6,25 @@ export interface JsonStringifyOptions {
6
6
  export interface WriteJsonOptions {
7
7
  encoding?: BufferEncoding | null;
8
8
  mode?: Mode;
9
- flag?: OpenMode;
9
+ flag?: string;
10
10
  replacer?: (key: string, value: unknown) => unknown | (number | string)[] | null;
11
11
  spaces?: string | number;
12
+ throws?: boolean;
12
13
  }
13
14
  /**
14
- * Synchronously writes an object to a JSON file.
15
+ * Synchronously writes a JSON file.
15
16
  *
16
17
  * @param file - The path to the file.
17
- * @param object - The object to stringify and write.
18
- * @param options - Options for stringifying JSON or writing the file.
18
+ * @param object - The object to write.
19
+ * @param options - Options for writing the file or stringifying JSON. Can be an encoding string or an object.
19
20
  */
20
- export declare function writeJsonSync(file: string, object: unknown, options?: WriteJsonOptions): void;
21
+ export declare function writeJsonSync(file: string, object: unknown, options?: BufferEncoding | WriteJsonOptions): void;
21
22
  /**
22
- * Asynchronously writes an object to a JSON file.
23
+ * Asynchronously writes a JSON file.
23
24
  *
24
25
  * @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.
26
+ * @param object - The object to write.
27
+ * @param options - Options for writing the file or stringifying JSON. Can be an encoding string or an object.
28
+ * @returns A promise that resolves when the file has been written.
27
29
  */
28
- export declare function writeJson(file: string, object: unknown, options?: WriteJsonOptions): Promise<void>;
30
+ export declare function writeJson(file: string, object: unknown, options?: BufferEncoding | WriteJsonOptions): Promise<void>;
@@ -1,22 +1,96 @@
1
+ import { isBun, getFileBun } from "./bun.js";
2
+ import { logInternal } from "./logger.js";
1
3
  import { writeFileSync } from "./write-file.js";
2
4
  import { writeFile } from "./write-file.js";
3
- export function writeJsonSync(file, object, options = {}) {
4
- const replacer = options.replacer === void 0 ? null : options.replacer;
5
- const spaces = options.spaces === void 0 ? 2 : options.spaces;
6
- const fileWriteOpts = {};
7
- if (options.encoding !== void 0) fileWriteOpts.encoding = options.encoding;
8
- if (options.mode !== void 0) fileWriteOpts.mode = options.mode;
9
- if (options.flag !== void 0) fileWriteOpts.flag = options.flag.toString();
10
- const str = JSON.stringify(object, replacer, spaces);
11
- writeFileSync(file, str, fileWriteOpts);
5
+ export function writeJsonSync(file, object, options) {
6
+ let encoding = "utf8";
7
+ let replacer;
8
+ let spaces;
9
+ let flag;
10
+ let mode;
11
+ let throws = true;
12
+ if (typeof options === "string") {
13
+ encoding = options;
14
+ } else if (options) {
15
+ encoding = options.encoding ?? encoding;
16
+ replacer = options.replacer;
17
+ spaces = options.spaces;
18
+ flag = options.flag;
19
+ mode = options.mode;
20
+ if (options.throws !== void 0) {
21
+ throws = options.throws;
22
+ }
23
+ }
24
+ try {
25
+ if (object === void 0) {
26
+ throw new Error("Cannot write undefined as JSON");
27
+ }
28
+ if (isBun) {
29
+ try {
30
+ const fileRef = getFileBun(file);
31
+ const jsonString2 = JSON.stringify(object, replacer, spaces);
32
+ if (jsonString2 === void 0) {
33
+ throw new Error("Failed to stringify JSON object");
34
+ }
35
+ Bun.write(fileRef, jsonString2);
36
+ return;
37
+ } catch (error) {
38
+ }
39
+ }
40
+ const jsonString = JSON.stringify(object, replacer, spaces);
41
+ if (jsonString === void 0) {
42
+ throw new Error("Failed to stringify JSON object");
43
+ }
44
+ writeFileSync(file, jsonString, { encoding, flag, mode });
45
+ } catch (err) {
46
+ if (throws) {
47
+ throw err;
48
+ }
49
+ }
12
50
  }
13
- export async function writeJson(file, object, options = {}) {
14
- const replacer = options.replacer === void 0 ? null : options.replacer;
15
- const spaces = options.spaces === void 0 ? 2 : options.spaces;
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, spaces);
21
- await writeFile(file, str, fileWriteOpts);
51
+ export async function writeJson(file, object, options) {
52
+ let encoding = "utf8";
53
+ let replacer;
54
+ let spaces;
55
+ let flag;
56
+ let mode;
57
+ let throws = true;
58
+ if (typeof options === "string") {
59
+ encoding = options;
60
+ } else if (options) {
61
+ encoding = options.encoding ?? encoding;
62
+ replacer = options.replacer;
63
+ spaces = options.spaces;
64
+ flag = options.flag;
65
+ mode = options.mode;
66
+ if (options.throws !== void 0) {
67
+ throws = options.throws;
68
+ }
69
+ }
70
+ try {
71
+ if (object === void 0) {
72
+ throw new Error("Cannot write undefined as JSON");
73
+ }
74
+ if (isBun) {
75
+ try {
76
+ const fileRef = getFileBun(file);
77
+ const jsonString2 = JSON.stringify(object, replacer, spaces);
78
+ if (jsonString2 === void 0) {
79
+ throw new Error("Failed to stringify JSON object");
80
+ }
81
+ await Bun.write(fileRef, jsonString2);
82
+ return;
83
+ } catch (error) {
84
+ }
85
+ }
86
+ const jsonString = JSON.stringify(object, replacer, spaces);
87
+ if (jsonString === void 0) {
88
+ throw new Error("Failed to stringify JSON object");
89
+ }
90
+ await writeFile(file, jsonString, { encoding, flag, mode });
91
+ } catch (err) {
92
+ if (throws) {
93
+ throw err;
94
+ }
95
+ }
22
96
  }
package/bin/mod.d.ts CHANGED
@@ -1,12 +1,14 @@
1
- import type { Stats } from "node:fs";
2
1
  import { renameSync as nodeRenameSync, unlinkSync as nodeUnlinkSync, accessSync, constants, readdirSync, statSync, copyFileSync, appendFileSync, chmodSync, chownSync, closeSync, createReadStream, createWriteStream, fchmodSync, fchownSync, fdatasyncSync, fstatSync, fsyncSync, ftruncateSync, futimesSync, lchmodSync, lchownSync, linkSync, lstatSync, lutimesSync, mkdtempSync, openSync, opendirSync, readSync, readlinkSync, realpathSync, rmSync, rmdirSync, statfsSync, symlinkSync, truncateSync, unwatchFile, utimesSync, watchFile, writeFileSync, writeSync, readvSync, writevSync } from "node:fs";
3
- import { readdir as nodeReaddirInternal, stat as nodeStatInternal, rename as nodeRename, unlink as nodeUnlink, access, appendFile, chmod, chown, copyFile, lchmod, lchown, link, lstat, lutimes, mkdtemp, open, opendir, readdir, readlink, realpath, rm, rmdir, stat, statfs, symlink, truncate, utimes, watch, writeFile } from "node:fs/promises";
2
+ import { rename as nodeRename, unlink as nodeUnlink, access, appendFile, chmod, chown, copyFile, lchmod, lchown, link, lstat, lutimes, mkdtemp, open, opendir, readdir, readlink, realpath, rm, rmdir, stat, statfs, symlink, truncate, utimes, watch, writeFile } from "node:fs/promises";
4
3
  import { resolve } from "node:path";
5
- import type { DiveOptions } from "./impl/dive.js";
4
+ import { isBun, getFileBun, existsBun, sizeBun, typeBun, lastModifiedBun, toNodeStatsBun, getStatsBun, getStatsSyncBun } from "./impl/bun.js";
6
5
  import { copy, copySync } from "./impl/copy.js";
7
6
  import { createFile, createFileSync } from "./impl/create-file.js";
7
+ import { dive } from "./impl/dive-async.js";
8
8
  import { diveSync } from "./impl/dive.js";
9
9
  import { emptyDir, emptyDirSync } from "./impl/empty-dir.js";
10
+ import { execAsync, setHiddenAttributeOnWindows, isHidden, isDirectoryEmpty, rmEnsureDir } from "./impl/extras.js";
11
+ import { readText, readTextSync, readLines, readLinesSync, isDirectory, isDirectorySync, isSymlink, isSymlinkSync } from "./impl/file-utils.js";
10
12
  import { mkdirs, mkdirsSync } from "./impl/mkdirs.js";
11
13
  import { move, moveSync } from "./impl/move.js";
12
14
  import { outputFile, outputFileSync } from "./impl/output-file.js";
@@ -16,15 +18,6 @@ import { readFile, readFileSync } from "./impl/read-file.js";
16
18
  import { readJson, readJsonSync } from "./impl/read-json.js";
17
19
  import { remove, removeSync } from "./impl/remove.js";
18
20
  import { writeJson, writeJsonSync } from "./impl/write-json.js";
19
- /**
20
- * Recursively dives into a directory and yields files and directories.
21
- * @param directory - The directory to dive into.
22
- * @param action - An optional callback function to execute for each file or directory.
23
- * @param options - An optional object containing options for the dive.
24
- * @returns A Promise that resolves to an array of file paths if no action is provided, or void if an action is provided.
25
- */
26
- export declare function dive(directory: string, action: (file: string, stat: Stats) => void | Promise<void>, options?: DiveOptions): Promise<void>;
27
- export declare function dive(directory: string, options?: DiveOptions): Promise<string[]>;
28
21
  declare const mkdirp: typeof mkdirs;
29
22
  declare const ensureDir: typeof mkdirs;
30
23
  declare const ensureFile: typeof createFile;
@@ -51,33 +44,15 @@ declare const cp: typeof copy;
51
44
  declare const cpSync: typeof copySync;
52
45
  declare const exists: typeof pathExists;
53
46
  declare const existsSync: typeof pathExistsSync;
54
- declare function readText(filePath: string, options?: BufferEncoding | {
55
- encoding?: BufferEncoding | null;
56
- flag?: string;
57
- }): Promise<string>;
58
- declare function readTextSync(filePath: string, options?: BufferEncoding | {
59
- encoding?: BufferEncoding | null;
60
- flag?: string;
61
- }): string;
62
- declare function readLines(filePath: string, options?: BufferEncoding | {
63
- encoding?: BufferEncoding | null;
64
- flag?: string;
65
- }): Promise<any>;
66
- declare function readLinesSync(filePath: string, options?: BufferEncoding | {
67
- encoding?: BufferEncoding | null;
68
- flag?: string;
69
- }): any;
70
- declare function isDirectory(filePath: string): Promise<boolean>;
71
- declare function isDirectorySync(filePath: string): boolean;
72
- declare function isSymlink(filePath: string): Promise<boolean>;
73
- declare function isSymlinkSync(filePath: string): boolean;
74
47
  export type { CopyOptions } from "./impl/copy.js";
75
48
  export type { MoveOptions } from "./impl/move.js";
76
49
  export type { ReadFileOptions } from "./impl/read-file.js";
77
50
  export type { ReadJsonOptions } from "./impl/read-json.js";
78
51
  export type { JsonStringifyOptions } from "./impl/write-json.js";
79
52
  export type { WriteJsonOptions } from "./impl/write-json.js";
80
- export { accessSync, appendFileSync, chmodSync, chownSync, closeSync, copyFileSync, createReadStream, createWriteStream, fchmodSync, fchownSync, fdatasyncSync, fstatSync, fsyncSync, ftruncateSync, futimesSync, lchmodSync, lchownSync, linkSync, lstatSync, lutimesSync, mkdtempSync, openSync, opendirSync, readFileSync, readlinkSync, readSync, readdirSync, realpathSync, nodeRenameSync, rmSync, rmdirSync, statSync, statfsSync, symlinkSync, truncateSync, nodeUnlinkSync, unwatchFile, utimesSync, watchFile, writeFileSync, writeSync, readvSync, writevSync, readJsonSync, writeJsonSync, createFileSync, mkdirsSync, emptyDirSync, pathExistsSync, copySync, moveSync, removeSync, outputJsonSync, outputFileSync, diveSync, cpSync, ensureDirSync as ensuredirSync, ensureDirSync, ensureFileSync, existsSync, mkdirpSync, mkdirSync, ncpSync, outputJSONSync, readJSONSync, renameSync, rimrafSync, unlinkSync, writeJSONSync, isDirectorySync, isSymlinkSync, readLinesSync, readTextSync, readJson, writeJson, createFile, mkdirs, emptyDir, pathExists, copy, move, remove, outputJson, outputFile, access, appendFile, chmod, chown, copyFile, lchmod, lchown, link, lstat, lutimes, mkdtemp, open, opendir, readFile, readdir, readlink, realpath, nodeRename, rm, rmdir, stat, statfs, symlink, truncate, nodeUnlink, utimes, watch, writeFile, constants, cp, ensureDir as ensuredir, ensureDir, ensureFile, exists, mkdir, mkdirp, ncp, outputJSON, readJSON, rename, resolve, rimraf, unlink, writeJSON, isDirectory, isSymlink, readLines, readText, };
53
+ export type { WriteFileOptions } from "./impl/write-file.js";
54
+ export type { DiveOptions } from "./impl/dive.js";
55
+ export { accessSync, appendFileSync, chmodSync, chownSync, closeSync, copyFileSync, createReadStream, createWriteStream, fchmodSync, fchownSync, fdatasyncSync, fstatSync, fsyncSync, ftruncateSync, futimesSync, lchmodSync, lchownSync, linkSync, lstatSync, lutimesSync, mkdtempSync, openSync, opendirSync, readFileSync, readlinkSync, readSync, readdirSync, realpathSync, nodeRenameSync, rmSync, rmdirSync, statSync, statfsSync, symlinkSync, truncateSync, nodeUnlinkSync, unwatchFile, utimesSync, watchFile, writeFileSync, writeSync, readvSync, writevSync, readJsonSync, writeJsonSync, createFileSync, mkdirsSync, emptyDirSync, pathExistsSync, copySync, moveSync, removeSync, outputJsonSync, outputFileSync, diveSync, cpSync, ensureDirSync as ensuredirSync, ensureDirSync, ensureFileSync, existsSync, mkdirpSync, mkdirSync, ncpSync, outputJSONSync, readJSONSync, renameSync, rimrafSync, unlinkSync, writeJSONSync, isDirectorySync, isSymlinkSync, readLinesSync, readTextSync, readJson, writeJson, createFile, mkdirs, emptyDir, pathExists, copy, move, remove, outputJson, outputFile, access, appendFile, chmod, chown, copyFile, lchmod, lchown, link, lstat, lutimes, mkdtemp, open, opendir, readFile, readdir, readlink, realpath, nodeRename, rm, rmdir, stat, statfs, symlink, truncate, nodeUnlink, utimes, watch, writeFile, constants, cp, ensureDir as ensuredir, ensureDir, ensureFile, exists, mkdir, mkdirp, ncp, outputJSON, readJSON, rename, resolve, rimraf, unlink, writeJSON, isDirectory, isSymlink, readLines, readText, execAsync, setHiddenAttributeOnWindows, isHidden, isDirectoryEmpty, rmEnsureDir, dive, isBun, getFileBun, existsBun, sizeBun, typeBun, lastModifiedBun, toNodeStatsBun, getStatsBun, getStatsSyncBun, };
81
56
  declare const fs: {
82
57
  accessSync: typeof accessSync;
83
58
  appendFileSync: typeof appendFileSync;
@@ -177,13 +152,13 @@ declare const fs: {
177
152
  open: typeof open;
178
153
  opendir: typeof opendir;
179
154
  readFile: typeof readFile;
180
- readdir: typeof nodeReaddirInternal;
155
+ readdir: typeof readdir;
181
156
  readlink: typeof readlink;
182
157
  realpath: typeof realpath;
183
158
  nodeRename: typeof nodeRename;
184
159
  rm: typeof rm;
185
160
  rmdir: typeof rmdir;
186
- stat: typeof nodeStatInternal;
161
+ stat: typeof stat;
187
162
  statfs: typeof statfs;
188
163
  symlink: typeof symlink;
189
164
  truncate: typeof truncate;
@@ -211,5 +186,20 @@ declare const fs: {
211
186
  isSymlink: typeof isSymlink;
212
187
  readLines: typeof readLines;
213
188
  readText: typeof readText;
189
+ execAsync: typeof import("child_process").exec.__promisify__;
190
+ setHiddenAttributeOnWindows: typeof setHiddenAttributeOnWindows;
191
+ isHidden: typeof isHidden;
192
+ isDirectoryEmpty: typeof isDirectoryEmpty;
193
+ rmEnsureDir: typeof rmEnsureDir;
194
+ dive: typeof dive;
195
+ isBun: string | false;
196
+ getFileBun: typeof getFileBun;
197
+ existsBun: typeof existsBun;
198
+ sizeBun: typeof sizeBun;
199
+ typeBun: typeof typeBun;
200
+ lastModifiedBun: typeof lastModifiedBun;
201
+ toNodeStatsBun: typeof toNodeStatsBun;
202
+ getStatsBun: typeof getStatsBun;
203
+ getStatsSyncBun: typeof getStatsSyncBun;
214
204
  };
215
205
  export default fs;