@reliverse/relifso 1.1.3 → 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.
Files changed (122) hide show
  1. package/README.md +6 -1
  2. package/bin/impl/copy.d.ts +28 -0
  3. package/bin/impl/copy.js +64 -0
  4. package/bin/impl/create-file.d.ts +2 -0
  5. package/bin/impl/create-file.js +21 -0
  6. package/bin/impl/dive.d.ts +17 -0
  7. package/bin/impl/dive.js +56 -0
  8. package/bin/impl/empty-dir.d.ts +2 -0
  9. package/bin/impl/empty-dir.js +24 -0
  10. package/bin/impl/mkdirs.d.ts +2 -0
  11. package/bin/impl/mkdirs.js +19 -0
  12. package/bin/impl/move.d.ts +15 -0
  13. package/bin/impl/move.js +93 -0
  14. package/bin/impl/output-file.d.ts +16 -0
  15. package/bin/impl/output-file.js +15 -0
  16. package/bin/impl/output-json.d.ts +16 -0
  17. package/bin/impl/output-json.js +15 -0
  18. package/bin/impl/path-exists.d.ts +2 -0
  19. package/bin/impl/path-exists.js +16 -0
  20. package/bin/impl/read-file.d.ts +30 -0
  21. package/bin/impl/read-file.js +30 -0
  22. package/bin/impl/read-json.d.ts +22 -0
  23. package/bin/impl/read-json.js +50 -0
  24. package/bin/impl/remove.d.ts +2 -0
  25. package/bin/impl/remove.js +8 -0
  26. package/bin/impl/write-file.d.ts +20 -0
  27. package/bin/impl/write-file.js +23 -0
  28. package/bin/impl/write-json.d.ts +28 -0
  29. package/bin/impl/write-json.js +22 -0
  30. package/bin/mod.d.ts +36 -2
  31. package/bin/mod.js +144 -2
  32. package/package.json +11 -9
  33. package/bin/d.ts.txt +0 -1000
  34. package/bin/impl/args-impl.d.ts +0 -0
  35. package/bin/impl/args-impl.js +0 -0
  36. package/bin/impl/array-polyfill.d.ts +0 -15
  37. package/bin/impl/external/path-is-inside.d.ts +0 -1
  38. package/bin/impl/external/path-is-inside.js +0 -16
  39. package/bin/impl/external/vacuum.d.ts +0 -26
  40. package/bin/impl/external/vacuum.js +0 -95
  41. package/bin/impl/fs/dive.d.ts +0 -8
  42. package/bin/impl/fs/dive.js +0 -91
  43. package/bin/impl/fs/exists.d.ts +0 -31
  44. package/bin/impl/fs/exists.js +0 -10
  45. package/bin/impl/fs/for-each-child.d.ts +0 -34
  46. package/bin/impl/fs/for-each-child.js +0 -37
  47. package/bin/impl/fs/index.d.ts +0 -1118
  48. package/bin/impl/fs/index.js +0 -66
  49. package/bin/impl/fs/is-directory.d.ts +0 -14
  50. package/bin/impl/fs/is-directory.js +0 -16
  51. package/bin/impl/fs/map-children.d.ts +0 -17
  52. package/bin/impl/fs/map-children.js +0 -31
  53. package/bin/impl/fs/map-structure.d.ts +0 -35
  54. package/bin/impl/fs/map-structure.js +0 -44
  55. package/bin/impl/fs/read-lines.d.ts +0 -16
  56. package/bin/impl/fs/read-lines.js +0 -32
  57. package/bin/impl/fs/read-text.d.ts +0 -13
  58. package/bin/impl/fs/read-text.js +0 -10
  59. package/bin/impl/fs/universalify.d.ts +0 -18
  60. package/bin/impl/fs/universalify.js +0 -38
  61. package/bin/impl/fs/write-file.d.ts +0 -93
  62. package/bin/impl/fs/write-file.js +0 -47
  63. package/bin/impl/helpers.d.ts +0 -1
  64. package/bin/impl/helpers.js +0 -37
  65. package/bin/impl/json.d.ts +0 -16
  66. package/bin/impl/json.js +0 -90
  67. package/bin/impl/lib/copy/copy-sync.d.ts +0 -25
  68. package/bin/impl/lib/copy/copy-sync.js +0 -119
  69. package/bin/impl/lib/copy/copy.d.ts +0 -7
  70. package/bin/impl/lib/copy/copy.js +0 -144
  71. package/bin/impl/lib/copy/index.d.ts +0 -5
  72. package/bin/impl/lib/copy/index.js +0 -2
  73. package/bin/impl/lib/empty/index.d.ts +0 -1
  74. package/bin/impl/lib/empty/index.js +0 -29
  75. package/bin/impl/lib/ensure/file.d.ts +0 -2
  76. package/bin/impl/lib/ensure/file.js +0 -56
  77. package/bin/impl/lib/ensure/index.d.ts +0 -8
  78. package/bin/impl/lib/ensure/index.js +0 -12
  79. package/bin/impl/lib/ensure/link.d.ts +0 -2
  80. package/bin/impl/lib/ensure/link.js +0 -53
  81. package/bin/impl/lib/ensure/symlink-paths.d.ts +0 -5
  82. package/bin/impl/lib/ensure/symlink-paths.js +0 -63
  83. package/bin/impl/lib/ensure/symlink-type.d.ts +0 -2
  84. package/bin/impl/lib/ensure/symlink-type.js +0 -23
  85. package/bin/impl/lib/ensure/symlink.d.ts +0 -2
  86. package/bin/impl/lib/ensure/symlink.js +0 -59
  87. package/bin/impl/lib/fs/index.d.ts +0 -1
  88. package/bin/impl/lib/fs/index.js +0 -1
  89. package/bin/impl/lib/index.d.ts +0 -15
  90. package/bin/impl/lib/index.js +0 -34
  91. package/bin/impl/lib/index_rollup.d.ts +0 -10
  92. package/bin/impl/lib/index_rollup.js +0 -10
  93. package/bin/impl/lib/json/index.d.ts +0 -3
  94. package/bin/impl/lib/json/index.js +0 -10
  95. package/bin/impl/lib/json/output-json-sync.d.ts +0 -1
  96. package/bin/impl/lib/json/output-json-sync.js +0 -6
  97. package/bin/impl/lib/json/output-json.d.ts +0 -1
  98. package/bin/impl/lib/json/output-json.js +0 -6
  99. package/bin/impl/lib/mkdirs/index.d.ts +0 -1
  100. package/bin/impl/lib/mkdirs/index.js +0 -4
  101. package/bin/impl/lib/mkdirs/make-dir.d.ts +0 -2
  102. package/bin/impl/lib/mkdirs/make-dir.js +0 -21
  103. package/bin/impl/lib/mkdirs/utils.d.ts +0 -1
  104. package/bin/impl/lib/mkdirs/utils.js +0 -13
  105. package/bin/impl/lib/move/index.d.ts +0 -30
  106. package/bin/impl/lib/move/index.js +0 -2
  107. package/bin/impl/lib/move/move-sync.d.ts +0 -8
  108. package/bin/impl/lib/move/move-sync.js +0 -51
  109. package/bin/impl/lib/move/move.d.ts +0 -6
  110. package/bin/impl/lib/move/move.js +0 -48
  111. package/bin/impl/lib/output-file/index.d.ts +0 -1
  112. package/bin/impl/lib/output-file/index.js +0 -24
  113. package/bin/impl/lib/remove/index.d.ts +0 -53
  114. package/bin/impl/lib/remove/index.js +0 -8
  115. package/bin/impl/lib/util/stat.d.ts +0 -39
  116. package/bin/impl/lib/util/stat.js +0 -140
  117. package/bin/impl/lib/util/utimes.d.ts +0 -7
  118. package/bin/impl/lib/util/utimes.js +0 -31
  119. package/bin/impl/subindex.d.ts +0 -43
  120. package/bin/impl/subindex.js +0 -161
  121. package/bin/types.d.ts +0 -100
  122. package/bin/types.js +0 -1
package/README.md CHANGED
@@ -16,6 +16,12 @@
16
16
  - ✌️ **Soon**: Bun v1.2+ ready — ships with Bun-aware enhancements out of the box
17
17
  - 🔥 **Soon**: Bun-specific features are exposed via `fs.*` when running on Bun
18
18
 
19
+ ## Heads Up
20
+
21
+ - **Most of the things** mentioned in this doc **aren’t implemented yet** — they’re part of the vision for ~`v1.3.0`.
22
+ - Got thoughts? Ideas? Send your feedback in [Discord](https://discord.gg/Pb8uKbwpsJ) or use [GitHub Issues](https://github.com/reliverse/relifso/issues).
23
+ - Your feedback means the world and helps shape where this project goes next. Thank you!
24
+
19
25
  ## Install
20
26
 
21
27
  ```bash
@@ -282,7 +288,6 @@ Relifso wouldn’t be so cool without these gems:
282
288
  - [node:fs](https://nodejs.org/api/fs.html)+[node:path](https://nodejs.org/api/path.html) — origins
283
289
  - [`fs-extra`](https://github.com/jprichardson/node-fs-extra) — classic, reliable
284
290
  - [`fsxt`](https://github.com/uwx-node-modules/fsxt) — full fs-extra overhaul
285
- - [`fs-lite`](https://github.com/baooab/node-fs-lite) — no-deps, fully-typed
286
291
 
287
292
  ## Show Some Love
288
293
 
@@ -0,0 +1,28 @@
1
+ export interface CopyOptions {
2
+ overwrite?: boolean;
3
+ /** @deprecated Use `overwrite`. */
4
+ clobber?: boolean;
5
+ preserveTimestamps?: boolean;
6
+ /** @deprecated Not used. */
7
+ errorOnExist?: boolean;
8
+ /** @deprecated Not used. */
9
+ dereference?: boolean;
10
+ /** @deprecated Not used. */
11
+ filter?: (src: string, dest: string) => boolean;
12
+ }
13
+ /**
14
+ * Copies a file or directory. The directory can have contents. Like `cp -r`.
15
+ *
16
+ * @param src - The source path.
17
+ * @param dest - The destination path.
18
+ * @param options - Options for the copy operation.
19
+ */
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>;
@@ -0,0 +1,64 @@
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";
3
+ import { dirname, join as joinPath, basename as basenamePath } from "node:path";
4
+ import { mkdirsSync } from "./mkdirs.js";
5
+ import { mkdirs } from "./mkdirs.js";
6
+ export function copySync(src, dest, options = {}) {
7
+ const { overwrite = options.clobber || false, preserveTimestamps = false } = options;
8
+ const srcStat = statSync(src, { throwIfNoEntry: true });
9
+ if (!srcStat) {
10
+ }
11
+ let destFinal = dest;
12
+ const destStat = statSync(dest, { throwIfNoEntry: false });
13
+ if (destStat?.isDirectory()) {
14
+ destFinal = joinPath(dest, basenamePath(src));
15
+ }
16
+ const destExists = statSync(destFinal, { throwIfNoEntry: false });
17
+ if (destExists && !overwrite) {
18
+ throw new Error(`Destination ${destFinal} already exists and overwrite is false.`);
19
+ }
20
+ const destDir = dirname(destFinal);
21
+ mkdirsSync(destDir);
22
+ if (srcStat.isDirectory()) {
23
+ mkdirsSync(destFinal);
24
+ } else {
25
+ copyFileSync(src, destFinal, preserveTimestamps ? fsConstants.COPYFILE_FICLONE : 0);
26
+ if (preserveTimestamps) {
27
+ console.warn("preserveTimestamps: utimesSync is not implemented for the moment.");
28
+ }
29
+ }
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
+ }
@@ -0,0 +1,2 @@
1
+ export declare function createFileSync(file: string, content?: string): void;
2
+ export declare function createFile(file: string, content?: string): Promise<void>;
@@ -0,0 +1,21 @@
1
+ import { existsSync } from "node:fs";
2
+ import { stat } from "node:fs/promises";
3
+ import { writeFileSync } from "./write-file.js";
4
+ import { writeFile } from "./write-file.js";
5
+ export function createFileSync(file, content = "") {
6
+ if (existsSync(file)) {
7
+ return;
8
+ }
9
+ return writeFileSync(file, content);
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
+ }
@@ -0,0 +1,17 @@
1
+ import { statSync } from "node:fs";
2
+ export interface DiveOptions {
3
+ all?: boolean;
4
+ recursive?: boolean;
5
+ directories?: boolean;
6
+ files?: boolean;
7
+ ignore?: string[] | RegExp;
8
+ depth?: number;
9
+ }
10
+ /**
11
+ * Synchronously walks a directory and yields each file and/or directory path.
12
+ *
13
+ * @param dir - The directory to walk.
14
+ * @param callbackOrOptions - A callback function to execute for each entry, or options object.
15
+ * @param options - Options for the dive operation if a callback is provided as the second argument.
16
+ */
17
+ export declare function diveSync(dir: string, callbackOrOptions?: ((path: string, stat: ReturnType<typeof statSync>) => void) | DiveOptions, options?: DiveOptions): Generator<string, void, unknown>;
@@ -0,0 +1,56 @@
1
+ import { readdirSync, statSync } from "node:fs";
2
+ import { join } from "node:path";
3
+ export function* diveSync(dir, callbackOrOptions, options) {
4
+ const currentDepth = 0;
5
+ let actualOptions = {
6
+ recursive: true,
7
+ files: true,
8
+ directories: false
9
+ // fs-extra's dive by default only yields files
10
+ };
11
+ let callback;
12
+ if (typeof callbackOrOptions === "function") {
13
+ callback = callbackOrOptions;
14
+ if (options) {
15
+ actualOptions = { ...actualOptions, ...options };
16
+ }
17
+ } else if (typeof callbackOrOptions === "object") {
18
+ actualOptions = { ...actualOptions, ...callbackOrOptions };
19
+ }
20
+ function* walk(currentPath, depth) {
21
+ if (actualOptions.depth !== void 0 && depth > actualOptions.depth) {
22
+ return;
23
+ }
24
+ const entries = readdirSync(currentPath, { withFileTypes: true });
25
+ for (const entry of entries) {
26
+ const entryPath = join(currentPath, entry.name);
27
+ if (!actualOptions.all && entry.name.startsWith(".")) {
28
+ continue;
29
+ }
30
+ if (actualOptions.ignore) {
31
+ if (Array.isArray(actualOptions.ignore) && actualOptions.ignore.includes(entry.name)) {
32
+ continue;
33
+ }
34
+ if (actualOptions.ignore instanceof RegExp && actualOptions.ignore.test(entryPath)) {
35
+ continue;
36
+ }
37
+ }
38
+ const stat = statSync(entryPath);
39
+ if (entry.isFile()) {
40
+ if (actualOptions.files) {
41
+ if (callback) callback(entryPath, stat);
42
+ yield entryPath;
43
+ }
44
+ } else if (entry.isDirectory()) {
45
+ if (actualOptions.directories) {
46
+ if (callback) callback(entryPath, stat);
47
+ yield entryPath;
48
+ }
49
+ if (actualOptions.recursive) {
50
+ yield* walk(entryPath, depth + 1);
51
+ }
52
+ }
53
+ }
54
+ }
55
+ yield* walk(dir, currentDepth);
56
+ }
@@ -0,0 +1,2 @@
1
+ export declare function emptyDirSync(dir: string): void;
2
+ export declare function emptyDir(dir: string): Promise<void>;
@@ -0,0 +1,24 @@
1
+ import { existsSync, readdirSync, rmSync } from "node:fs";
2
+ import { readdir, rm, stat } from "node:fs/promises";
3
+ import path from "node:path";
4
+ export function emptyDirSync(dir) {
5
+ if (!existsSync(dir)) {
6
+ return;
7
+ }
8
+ for (const file of readdirSync(dir)) {
9
+ rmSync(path.resolve(dir, file), { recursive: true, force: true });
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
+ }
@@ -0,0 +1,2 @@
1
+ export declare function mkdirsSync(dir: string): string | undefined;
2
+ export declare function mkdirs(dir: string): Promise<string | undefined>;
@@ -0,0 +1,19 @@
1
+ import { mkdirSync, existsSync } from "node:fs";
2
+ import { mkdir, stat } from "node:fs/promises";
3
+ export function mkdirsSync(dir) {
4
+ if (existsSync(dir)) {
5
+ return;
6
+ }
7
+ return mkdirSync(dir, { recursive: true });
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
+ }
@@ -0,0 +1,15 @@
1
+ export interface MoveOptions {
2
+ overwrite?: boolean;
3
+ /** @deprecated Use `overwrite`. */
4
+ clobber?: boolean;
5
+ }
6
+ /**
7
+ * Moves a file or directory. If the destination is a directory, the source is moved into it.
8
+ * If the destination exists and is a file, it will be overwritten if `overwrite` is true.
9
+ *
10
+ * @param src - The source path.
11
+ * @param dest - The destination path.
12
+ * @param options - Options for the move operation.
13
+ */
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>;
@@ -0,0 +1,93 @@
1
+ import { renameSync, statSync, unlinkSync, copyFileSync } from "node:fs";
2
+ import { rename, stat, unlink, copyFile } from "node:fs/promises";
3
+ import { dirname, basename, join as joinPath } from "node:path";
4
+ import { mkdirsSync } from "./mkdirs.js";
5
+ import { mkdirs } from "./mkdirs.js";
6
+ export function moveSync(src, dest, options = {}) {
7
+ let overwrite;
8
+ if (options.overwrite !== void 0) {
9
+ overwrite = options.overwrite;
10
+ } else if (options.clobber !== void 0) {
11
+ console.warn(
12
+ "Warning: The 'clobber' option in moveSync is deprecated and will be removed in a future version. Please use 'overwrite' instead."
13
+ );
14
+ overwrite = options.clobber;
15
+ } else {
16
+ overwrite = false;
17
+ }
18
+ const srcStat = statSync(src, { throwIfNoEntry: true });
19
+ if (!srcStat) {
20
+ }
21
+ let destFinal = dest;
22
+ const destStat = statSync(dest, { throwIfNoEntry: false });
23
+ if (destStat?.isDirectory()) {
24
+ destFinal = joinPath(dest, basename(src));
25
+ }
26
+ if (statSync(destFinal, { throwIfNoEntry: false }) && !overwrite) {
27
+ throw new Error(`Destination ${destFinal} already exists and overwrite is false.`);
28
+ }
29
+ const destDir = dirname(destFinal);
30
+ mkdirsSync(destDir);
31
+ try {
32
+ renameSync(src, destFinal);
33
+ } catch (err) {
34
+ if (err.code === "EXDEV") {
35
+ copyFileSync(src, destFinal);
36
+ unlinkSync(src);
37
+ } else if (err.code === "EISDIR" || err.code === "EPERM") {
38
+ copyFileSync(src, destFinal);
39
+ unlinkSync(src);
40
+ } else {
41
+ throw err;
42
+ }
43
+ }
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
+ }
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Ensures that the directory for the file exists and then writes the data to the file.
3
+ *
4
+ * @param file - The path to the file.
5
+ * @param data - The data to write.
6
+ * @param options - Options for writing the file (e.g., encoding, mode, flag).
7
+ */
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>;
@@ -0,0 +1,15 @@
1
+ import { dirname } from "node:path";
2
+ import { mkdirsSync } from "./mkdirs.js";
3
+ import { mkdirs } from "./mkdirs.js";
4
+ import { writeFileSync } from "./write-file.js";
5
+ import { writeFile } from "./write-file.js";
6
+ export function outputFileSync(file, data, options) {
7
+ const dir = dirname(file);
8
+ mkdirsSync(dir);
9
+ writeFileSync(file, data, options);
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
+ }
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Ensures that the directory for the JSON file exists and then writes the JSON data to the file.
3
+ *
4
+ * @param file - The path to the file.
5
+ * @param data - The JSON data to write.
6
+ * @param options - Options for writing the JSON file (e.g., replacer, space).
7
+ */
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>;
@@ -0,0 +1,15 @@
1
+ import { dirname } from "node:path";
2
+ import { mkdirsSync } from "./mkdirs.js";
3
+ import { mkdirs } from "./mkdirs.js";
4
+ import { writeJsonSync } from "./write-json.js";
5
+ import { writeJson } from "./write-json.js";
6
+ export function outputJsonSync(file, data, options) {
7
+ const dir = dirname(file);
8
+ mkdirsSync(dir);
9
+ writeJsonSync(file, data, options);
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
+ }
@@ -0,0 +1,2 @@
1
+ export declare function pathExistsSync(path: string): boolean;
2
+ export declare function pathExists(path: string): Promise<boolean>;
@@ -0,0 +1,16 @@
1
+ import { existsSync } from "node:fs";
2
+ import { stat } from "node:fs/promises";
3
+ export function pathExistsSync(path) {
4
+ return existsSync(path);
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
+ }
@@ -0,0 +1,30 @@
1
+ export interface ReadFileOptions {
2
+ encoding?: BufferEncoding | null;
3
+ flag?: string;
4
+ }
5
+ /**
6
+ * Reads the entire contents of a file.
7
+ *
8
+ * @param path - The path to the file.
9
+ * @param options - Options for reading the file. Can be an encoding string or an object.
10
+ * @returns The file content as a string or Buffer.
11
+ */
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>;
@@ -0,0 +1,30 @@
1
+ import { readFileSync as nodeReadFileSync } from "node:fs";
2
+ import { readFile as nodeReadFileAsync } from "node:fs/promises";
3
+ export function readFileSync(path, options) {
4
+ let encoding;
5
+ let flag;
6
+ if (typeof options === "string") {
7
+ encoding = options;
8
+ } else if (options) {
9
+ encoding = options.encoding;
10
+ flag = options.flag;
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 });
30
+ }
@@ -0,0 +1,22 @@
1
+ export interface ReadJsonOptions {
2
+ encoding?: BufferEncoding | null;
3
+ flag?: string;
4
+ reviver?: (key: string, value: unknown) => unknown;
5
+ throws?: boolean;
6
+ }
7
+ /**
8
+ * Reads a JSON file and then parses it into an object.
9
+ *
10
+ * @param file - The path to the file.
11
+ * @param options - Options for reading the file or parsing JSON. Can be an encoding string or an object.
12
+ * @returns The parsed JSON object.
13
+ */
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>;
@@ -0,0 +1,50 @@
1
+ import { readFileSync } from "node:fs";
2
+ import { readFile } from "node:fs/promises";
3
+ export function readJsonSync(file, options) {
4
+ let encoding = "utf8";
5
+ let reviver;
6
+ let throws = true;
7
+ if (typeof options === "string") {
8
+ encoding = options;
9
+ } else if (options) {
10
+ encoding = options.encoding ?? encoding;
11
+ reviver = options.reviver;
12
+ if (options.throws !== void 0) {
13
+ throws = options.throws;
14
+ }
15
+ }
16
+ try {
17
+ const data = readFileSync(file, { encoding });
18
+ return JSON.parse(data, reviver);
19
+ } catch (err) {
20
+ if (throws) {
21
+ throw err;
22
+ }
23
+ return void 0;
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
+ }
@@ -0,0 +1,2 @@
1
+ export declare function removeSync(path: string): void;
2
+ export declare function remove(path: string): Promise<void>;
@@ -0,0 +1,8 @@
1
+ import { rmSync } from "node:fs";
2
+ import { rm } from "node:fs/promises";
3
+ export function removeSync(path) {
4
+ return rmSync(path, { recursive: true, force: true });
5
+ }
6
+ export async function remove(path) {
7
+ return rm(path, { recursive: true, force: true });
8
+ }
@@ -0,0 +1,20 @@
1
+ import { type PathLike } from "node:fs";
2
+ export type WriteFileOptions = import("node:fs").WriteFileOptions;
3
+ /**
4
+ * Synchronously writes data to a file, replacing the file if it already exists.
5
+ * Ensures the directory exists before writing.
6
+ *
7
+ * @param file - Path to the file.
8
+ * @param data - The data to write. If something other than a Buffer or Uint8Array is provided, it is converted to a string.
9
+ * @param options - Options for writing the file. Can be an encoding string or an object.
10
+ */
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>;
@@ -0,0 +1,23 @@
1
+ import { existsSync, mkdirSync, writeFileSync as nodeWriteFileSync } from "node:fs";
2
+ import { mkdir, writeFile as nodeWriteFileAsync, stat } from "node:fs/promises";
3
+ import path from "node:path";
4
+ export function writeFileSync(file, data, options) {
5
+ const dir = path.dirname(file.toString());
6
+ if (!existsSync(dir)) {
7
+ mkdirSync(dir, { recursive: true });
8
+ }
9
+ nodeWriteFileSync(file, data, options);
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
+ }