@reliverse/relifso 1.1.2 → 1.2.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.
Files changed (124) hide show
  1. package/README.md +6 -1
  2. package/bin/impl/async.d.ts +7 -0
  3. package/bin/impl/async.js +9 -0
  4. package/bin/impl/copy.d.ts +20 -0
  5. package/bin/impl/copy.js +28 -0
  6. package/bin/impl/create-file.d.ts +1 -0
  7. package/bin/impl/create-file.js +8 -0
  8. package/bin/impl/dive.d.ts +17 -0
  9. package/bin/impl/dive.js +56 -0
  10. package/bin/impl/empty-dir.d.ts +1 -0
  11. package/bin/impl/empty-dir.js +10 -0
  12. package/bin/impl/mkdirs.d.ts +1 -0
  13. package/bin/impl/mkdirs.js +7 -0
  14. package/bin/impl/move.d.ts +14 -0
  15. package/bin/impl/move.js +42 -0
  16. package/bin/impl/output-file.d.ts +8 -0
  17. package/bin/impl/output-file.js +8 -0
  18. package/bin/impl/output-json.d.ts +8 -0
  19. package/bin/impl/output-json.js +8 -0
  20. package/bin/impl/path-exists.d.ts +1 -0
  21. package/bin/impl/path-exists.js +4 -0
  22. package/bin/impl/read-file.d.ts +12 -0
  23. package/bin/impl/read-file.js +12 -0
  24. package/bin/impl/read-json.d.ts +14 -0
  25. package/bin/impl/read-json.js +24 -0
  26. package/bin/impl/remove.d.ts +1 -0
  27. package/bin/impl/remove.js +4 -0
  28. package/bin/impl/write-file.d.ts +11 -0
  29. package/bin/impl/write-file.js +9 -0
  30. package/bin/impl/write-json.d.ts +20 -0
  31. package/bin/impl/write-json.js +11 -0
  32. package/bin/mod.d.ts +50 -2
  33. package/bin/mod.js +158 -2
  34. package/package.json +7 -9
  35. package/bin/d.ts.txt +0 -1000
  36. package/bin/impl/args-impl.d.ts +0 -0
  37. package/bin/impl/args-impl.js +0 -0
  38. package/bin/impl/array-polyfill.d.ts +0 -15
  39. package/bin/impl/external/path-is-inside.d.ts +0 -1
  40. package/bin/impl/external/path-is-inside.js +0 -16
  41. package/bin/impl/external/vacuum.d.ts +0 -26
  42. package/bin/impl/external/vacuum.js +0 -95
  43. package/bin/impl/fs/dive.d.ts +0 -8
  44. package/bin/impl/fs/dive.js +0 -91
  45. package/bin/impl/fs/exists.d.ts +0 -31
  46. package/bin/impl/fs/exists.js +0 -10
  47. package/bin/impl/fs/for-each-child.d.ts +0 -34
  48. package/bin/impl/fs/for-each-child.js +0 -37
  49. package/bin/impl/fs/index.d.ts +0 -1118
  50. package/bin/impl/fs/index.js +0 -66
  51. package/bin/impl/fs/is-directory.d.ts +0 -14
  52. package/bin/impl/fs/is-directory.js +0 -16
  53. package/bin/impl/fs/map-children.d.ts +0 -17
  54. package/bin/impl/fs/map-children.js +0 -31
  55. package/bin/impl/fs/map-structure.d.ts +0 -35
  56. package/bin/impl/fs/map-structure.js +0 -44
  57. package/bin/impl/fs/read-lines.d.ts +0 -16
  58. package/bin/impl/fs/read-lines.js +0 -32
  59. package/bin/impl/fs/read-text.d.ts +0 -13
  60. package/bin/impl/fs/read-text.js +0 -10
  61. package/bin/impl/fs/universalify.d.ts +0 -18
  62. package/bin/impl/fs/universalify.js +0 -38
  63. package/bin/impl/fs/write-file.d.ts +0 -93
  64. package/bin/impl/fs/write-file.js +0 -47
  65. package/bin/impl/helpers.d.ts +0 -1
  66. package/bin/impl/helpers.js +0 -37
  67. package/bin/impl/json.d.ts +0 -16
  68. package/bin/impl/json.js +0 -90
  69. package/bin/impl/lib/copy/copy-sync.d.ts +0 -25
  70. package/bin/impl/lib/copy/copy-sync.js +0 -119
  71. package/bin/impl/lib/copy/copy.d.ts +0 -7
  72. package/bin/impl/lib/copy/copy.js +0 -144
  73. package/bin/impl/lib/copy/index.d.ts +0 -5
  74. package/bin/impl/lib/copy/index.js +0 -2
  75. package/bin/impl/lib/empty/index.d.ts +0 -1
  76. package/bin/impl/lib/empty/index.js +0 -29
  77. package/bin/impl/lib/ensure/file.d.ts +0 -2
  78. package/bin/impl/lib/ensure/file.js +0 -56
  79. package/bin/impl/lib/ensure/index.d.ts +0 -8
  80. package/bin/impl/lib/ensure/index.js +0 -12
  81. package/bin/impl/lib/ensure/link.d.ts +0 -2
  82. package/bin/impl/lib/ensure/link.js +0 -53
  83. package/bin/impl/lib/ensure/symlink-paths.d.ts +0 -5
  84. package/bin/impl/lib/ensure/symlink-paths.js +0 -63
  85. package/bin/impl/lib/ensure/symlink-type.d.ts +0 -2
  86. package/bin/impl/lib/ensure/symlink-type.js +0 -23
  87. package/bin/impl/lib/ensure/symlink.d.ts +0 -2
  88. package/bin/impl/lib/ensure/symlink.js +0 -59
  89. package/bin/impl/lib/fs/index.d.ts +0 -1
  90. package/bin/impl/lib/fs/index.js +0 -1
  91. package/bin/impl/lib/index.d.ts +0 -15
  92. package/bin/impl/lib/index.js +0 -34
  93. package/bin/impl/lib/index_rollup.d.ts +0 -10
  94. package/bin/impl/lib/index_rollup.js +0 -10
  95. package/bin/impl/lib/json/index.d.ts +0 -3
  96. package/bin/impl/lib/json/index.js +0 -10
  97. package/bin/impl/lib/json/output-json-sync.d.ts +0 -1
  98. package/bin/impl/lib/json/output-json-sync.js +0 -6
  99. package/bin/impl/lib/json/output-json.d.ts +0 -1
  100. package/bin/impl/lib/json/output-json.js +0 -6
  101. package/bin/impl/lib/mkdirs/index.d.ts +0 -1
  102. package/bin/impl/lib/mkdirs/index.js +0 -4
  103. package/bin/impl/lib/mkdirs/make-dir.d.ts +0 -2
  104. package/bin/impl/lib/mkdirs/make-dir.js +0 -21
  105. package/bin/impl/lib/mkdirs/utils.d.ts +0 -1
  106. package/bin/impl/lib/mkdirs/utils.js +0 -13
  107. package/bin/impl/lib/move/index.d.ts +0 -30
  108. package/bin/impl/lib/move/index.js +0 -2
  109. package/bin/impl/lib/move/move-sync.d.ts +0 -8
  110. package/bin/impl/lib/move/move-sync.js +0 -51
  111. package/bin/impl/lib/move/move.d.ts +0 -6
  112. package/bin/impl/lib/move/move.js +0 -48
  113. package/bin/impl/lib/output-file/index.d.ts +0 -1
  114. package/bin/impl/lib/output-file/index.js +0 -24
  115. package/bin/impl/lib/remove/index.d.ts +0 -53
  116. package/bin/impl/lib/remove/index.js +0 -8
  117. package/bin/impl/lib/util/stat.d.ts +0 -39
  118. package/bin/impl/lib/util/stat.js +0 -140
  119. package/bin/impl/lib/util/utimes.d.ts +0 -7
  120. package/bin/impl/lib/util/utimes.js +0 -31
  121. package/bin/impl/subindex.d.ts +0 -38
  122. package/bin/impl/subindex.js +0 -148
  123. package/bin/types.d.ts +0 -100
  124. 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,7 @@
1
+ /**
2
+ * Converts a synchronous function into an asynchronous function that returns a Promise.
3
+ *
4
+ * @param fn - The synchronous function to convert.
5
+ * @returns An asynchronous function that wraps the original synchronous function.
6
+ */
7
+ export declare function toAsync<T extends (...args: any[]) => any>(fn: T): (...args: Parameters<T>) => Promise<Awaited<ReturnType<T>>>;
@@ -0,0 +1,9 @@
1
+ export function toAsync(fn) {
2
+ return (...args) => new Promise((resolve, reject) => {
3
+ try {
4
+ resolve(fn(...args));
5
+ } catch (error) {
6
+ reject(error);
7
+ }
8
+ });
9
+ }
@@ -0,0 +1,20 @@
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;
@@ -0,0 +1,28 @@
1
+ import { copyFileSync, statSync, constants as fsConstants } from "node:fs";
2
+ import { dirname, join as joinPath, basename as basenamePath } from "node:path";
3
+ import { mkdirsSync } from "./mkdirs.js";
4
+ export function copySync(src, dest, options = {}) {
5
+ const { overwrite = options.clobber || false, preserveTimestamps = false } = options;
6
+ const srcStat = statSync(src, { throwIfNoEntry: true });
7
+ if (!srcStat) {
8
+ }
9
+ let destFinal = dest;
10
+ const destStat = statSync(dest, { throwIfNoEntry: false });
11
+ if (destStat?.isDirectory()) {
12
+ destFinal = joinPath(dest, basenamePath(src));
13
+ }
14
+ const destExists = statSync(destFinal, { throwIfNoEntry: false });
15
+ if (destExists && !overwrite) {
16
+ throw new Error(`Destination ${destFinal} already exists and overwrite is false.`);
17
+ }
18
+ const destDir = dirname(destFinal);
19
+ mkdirsSync(destDir);
20
+ if (srcStat.isDirectory()) {
21
+ mkdirsSync(destFinal);
22
+ } else {
23
+ copyFileSync(src, destFinal, preserveTimestamps ? fsConstants.COPYFILE_FICLONE : 0);
24
+ if (preserveTimestamps) {
25
+ console.warn("preserveTimestamps: utimesSync is not implemented for the moment.");
26
+ }
27
+ }
28
+ }
@@ -0,0 +1 @@
1
+ export declare function createFileSync(file: string, content?: string): void;
@@ -0,0 +1,8 @@
1
+ import { existsSync } from "node:fs";
2
+ import { writeFileSync } from "./write-file.js";
3
+ export function createFileSync(file, content = "") {
4
+ if (existsSync(file)) {
5
+ return;
6
+ }
7
+ return writeFileSync(file, content);
8
+ }
@@ -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 @@
1
+ export declare function emptyDirSync(dir: string): void;
@@ -0,0 +1,10 @@
1
+ import { existsSync, readdirSync, rmSync } from "node:fs";
2
+ import path from "node:path";
3
+ export function emptyDirSync(dir) {
4
+ if (!existsSync(dir)) {
5
+ return;
6
+ }
7
+ for (const file of readdirSync(dir)) {
8
+ rmSync(path.resolve(dir, file), { recursive: true, force: true });
9
+ }
10
+ }
@@ -0,0 +1 @@
1
+ export declare function mkdirsSync(dir: string): string | undefined;
@@ -0,0 +1,7 @@
1
+ import { mkdirSync, existsSync } from "node:fs";
2
+ export function mkdirsSync(dir) {
3
+ if (existsSync(dir)) {
4
+ return;
5
+ }
6
+ return mkdirSync(dir, { recursive: true });
7
+ }
@@ -0,0 +1,14 @@
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;
@@ -0,0 +1,42 @@
1
+ import { renameSync, statSync, unlinkSync, copyFileSync } from "node:fs";
2
+ import { dirname, basename, join as joinPath } from "node:path";
3
+ import { mkdirsSync } from "./mkdirs.js";
4
+ export function moveSync(src, dest, options = {}) {
5
+ let overwrite;
6
+ if (options.overwrite !== void 0) {
7
+ overwrite = options.overwrite;
8
+ } else if (options.clobber !== void 0) {
9
+ console.warn(
10
+ "Warning: The 'clobber' option in moveSync is deprecated and will be removed in a future version. Please use 'overwrite' instead."
11
+ );
12
+ overwrite = options.clobber;
13
+ } else {
14
+ overwrite = false;
15
+ }
16
+ const srcStat = statSync(src, { throwIfNoEntry: true });
17
+ if (!srcStat) {
18
+ }
19
+ let destFinal = dest;
20
+ const destStat = statSync(dest, { throwIfNoEntry: false });
21
+ if (destStat?.isDirectory()) {
22
+ destFinal = joinPath(dest, basename(src));
23
+ }
24
+ if (statSync(destFinal, { throwIfNoEntry: false }) && !overwrite) {
25
+ throw new Error(`Destination ${destFinal} already exists and overwrite is false.`);
26
+ }
27
+ const destDir = dirname(destFinal);
28
+ mkdirsSync(destDir);
29
+ try {
30
+ renameSync(src, destFinal);
31
+ } catch (err) {
32
+ if (err.code === "EXDEV") {
33
+ copyFileSync(src, destFinal);
34
+ unlinkSync(src);
35
+ } else if (err.code === "EISDIR" || err.code === "EPERM") {
36
+ copyFileSync(src, destFinal);
37
+ unlinkSync(src);
38
+ } else {
39
+ throw err;
40
+ }
41
+ }
42
+ }
@@ -0,0 +1,8 @@
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;
@@ -0,0 +1,8 @@
1
+ import { dirname } from "node:path";
2
+ import { mkdirsSync } from "./mkdirs.js";
3
+ import { writeFileSync } from "./write-file.js";
4
+ export function outputFileSync(file, data, options) {
5
+ const dir = dirname(file);
6
+ mkdirsSync(dir);
7
+ writeFileSync(file, data, options);
8
+ }
@@ -0,0 +1,8 @@
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;
@@ -0,0 +1,8 @@
1
+ import { dirname } from "node:path";
2
+ import { mkdirsSync } from "./mkdirs.js";
3
+ import { writeJsonSync } from "./write-json.js";
4
+ export function outputJsonSync(file, data, options) {
5
+ const dir = dirname(file);
6
+ mkdirsSync(dir);
7
+ writeJsonSync(file, data, options);
8
+ }
@@ -0,0 +1 @@
1
+ export declare function pathExistsSync(path: string): boolean;
@@ -0,0 +1,4 @@
1
+ import { existsSync } from "node:fs";
2
+ export function pathExistsSync(path) {
3
+ return existsSync(path);
4
+ }
@@ -0,0 +1,12 @@
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): string | Buffer;
@@ -0,0 +1,12 @@
1
+ import { readFileSync as nodeReadFileSync } from "node:fs";
2
+ export function readFileSync(path, options) {
3
+ let encoding;
4
+ let flag;
5
+ if (typeof options === "string") {
6
+ encoding = options;
7
+ } else if (options) {
8
+ encoding = options.encoding ?? void 0;
9
+ flag = options.flag;
10
+ }
11
+ return nodeReadFileSync(path, { encoding, flag });
12
+ }
@@ -0,0 +1,14 @@
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;
@@ -0,0 +1,24 @@
1
+ import { readFileSync } from "node:fs";
2
+ export function readJsonSync(file, options) {
3
+ let encoding = "utf8";
4
+ let reviver;
5
+ let throws = true;
6
+ if (typeof options === "string") {
7
+ encoding = options;
8
+ } else if (options) {
9
+ encoding = options.encoding ?? encoding;
10
+ reviver = options.reviver;
11
+ if (options.throws !== void 0) {
12
+ throws = options.throws;
13
+ }
14
+ }
15
+ try {
16
+ const data = readFileSync(file, { encoding });
17
+ return JSON.parse(data, reviver);
18
+ } catch (err) {
19
+ if (throws) {
20
+ throw err;
21
+ }
22
+ return void 0;
23
+ }
24
+ }
@@ -0,0 +1 @@
1
+ export declare function removeSync(path: string): void;
@@ -0,0 +1,4 @@
1
+ import { rmSync } from "node:fs";
2
+ export function removeSync(path) {
3
+ return rmSync(path, { recursive: true, force: true });
4
+ }
@@ -0,0 +1,11 @@
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;
@@ -0,0 +1,9 @@
1
+ import { existsSync, mkdirSync, writeFileSync as nodeWriteFileSync } from "node:fs";
2
+ import path from "node:path";
3
+ export function writeFileSync(file, data, options) {
4
+ const dir = path.dirname(file.toString());
5
+ if (!existsSync(dir)) {
6
+ mkdirSync(dir, { recursive: true });
7
+ }
8
+ nodeWriteFileSync(file, data, options);
9
+ }
@@ -0,0 +1,20 @@
1
+ import type { Mode, OpenMode } from "node:fs";
2
+ export interface JsonStringifyOptions {
3
+ replacer?: (key: string, value: unknown) => unknown | (number | string)[] | null;
4
+ space?: string | number;
5
+ }
6
+ export interface WriteJsonOptions {
7
+ encoding?: BufferEncoding | null;
8
+ mode?: Mode;
9
+ flag?: OpenMode;
10
+ replacer?: (key: string, value: unknown) => unknown | (number | string)[] | null;
11
+ space?: string | number;
12
+ }
13
+ /**
14
+ * Synchronously writes an object to a JSON file.
15
+ *
16
+ * @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.
19
+ */
20
+ export declare function writeJsonSync(file: string, object: unknown, options?: WriteJsonOptions): void;
@@ -0,0 +1,11 @@
1
+ import { writeFileSync } from "./write-file.js";
2
+ export function writeJsonSync(file, object, options = {}) {
3
+ const replacer = options.replacer === void 0 ? null : options.replacer;
4
+ const space = options.space === void 0 ? 2 : options.space;
5
+ const fileWriteOpts = {};
6
+ if (options.encoding !== void 0) fileWriteOpts.encoding = options.encoding;
7
+ if (options.mode !== void 0) fileWriteOpts.mode = options.mode;
8
+ if (options.flag !== void 0) fileWriteOpts.flag = options.flag.toString();
9
+ const str = JSON.stringify(object, replacer, space);
10
+ writeFileSync(file, str, fileWriteOpts);
11
+ }
package/bin/mod.d.ts CHANGED
@@ -1,2 +1,50 @@
1
- export * from "./impl/subindex.js";
2
- export * as default from "./impl/subindex.js";
1
+ import type { Stats } from "node:fs";
2
+ import type { DiveOptions } from "./impl/dive.js";
3
+ import { copySync } from "./impl/copy.js";
4
+ import { createFileSync } from "./impl/create-file.js";
5
+ import { diveSync } from "./impl/dive.js";
6
+ import { emptyDirSync } from "./impl/empty-dir.js";
7
+ import { mkdirsSync } from "./impl/mkdirs.js";
8
+ import { moveSync } from "./impl/move.js";
9
+ import { outputFileSync } from "./impl/output-file.js";
10
+ import { outputJsonSync } from "./impl/output-json.js";
11
+ import { pathExistsSync } from "./impl/path-exists.js";
12
+ import { readFileSync } from "./impl/read-file.js";
13
+ import { readJsonSync, type ReadJsonOptions } from "./impl/read-json.js";
14
+ import { removeSync } from "./impl/remove.js";
15
+ import { writeFileSync } from "./impl/write-file.js";
16
+ import { writeJsonSync } from "./impl/write-json.js";
17
+ declare const readJson: <T>(file: string, options?: BufferEncoding | ReadJsonOptions) => Promise<T>;
18
+ declare const writeJson: (file: string, object: unknown, options?: import("./impl/write-json.js").WriteJsonOptions | undefined) => Promise<void>;
19
+ declare const createFile: (file: string, content?: string | undefined) => Promise<void>;
20
+ declare const writeFile: (file: number | import("fs").PathLike, data: string | NodeJS.ArrayBufferView<ArrayBufferLike>, options?: import("fs").WriteFileOptions | undefined) => Promise<void>;
21
+ declare const readFile: (path: string, options?: BufferEncoding | import("./impl/read-file.js").ReadFileOptions | undefined) => Promise<string | Buffer<ArrayBufferLike>>;
22
+ declare const mkdirs: (dir: string) => Promise<string | undefined>;
23
+ declare const emptyDir: (dir: string) => Promise<void>;
24
+ declare const pathExists: (path: string) => Promise<boolean>;
25
+ declare const copy: (src: string, dest: string, options?: import("./impl/copy.js").CopyOptions | undefined) => Promise<void>;
26
+ declare const move: (src: string, dest: string, options?: import("./impl/move.js").MoveOptions | undefined) => Promise<void>;
27
+ declare const remove: (path: string) => Promise<void>;
28
+ declare const outputJson: (file: string, data: unknown, options?: unknown) => Promise<void>;
29
+ declare const outputFile: (file: string, data: string | Uint8Array<ArrayBufferLike>, options?: unknown) => Promise<void>;
30
+ export declare function dive(directory: string, action: (file: string, stat: Stats) => void | Promise<void>, options?: DiveOptions): Promise<void>;
31
+ export declare function dive(directory: string, options?: DiveOptions): Promise<string[]>;
32
+ declare const mkdirp: (dir: string) => Promise<string | undefined>;
33
+ declare const ensureDir: (dir: string) => Promise<string | undefined>;
34
+ declare const ensureFile: (file: string, content?: string | undefined) => Promise<void>;
35
+ declare const rimraf: (path: string) => Promise<void>;
36
+ declare const ncp: (src: string, dest: string, options?: import("./impl/copy.js").CopyOptions | undefined) => Promise<void>;
37
+ declare const mkdirpSync: typeof mkdirsSync;
38
+ declare const ensureDirSync: typeof mkdirsSync;
39
+ declare const ensureFileSync: typeof createFileSync;
40
+ declare const rimrafSync: typeof removeSync;
41
+ declare const ncpSync: typeof copySync;
42
+ export * from "node:fs";
43
+ export { default } from "node:fs";
44
+ export { readJson, writeJson, createFile, writeFile, readFile, mkdirs, emptyDir, pathExists, copy, move, remove, readJsonSync, writeJsonSync, createFileSync, writeFileSync, readFileSync, mkdirsSync, emptyDirSync, pathExistsSync, copySync, moveSync, removeSync, mkdirp, mkdirpSync, rimraf, rimrafSync, ncp, ncpSync, ensureDir, ensureDirSync, ensureFile, ensureFileSync, outputJson, outputJsonSync, outputFile, outputFileSync, diveSync, };
45
+ export type { CopyOptions } from "./impl/copy.js";
46
+ export type { MoveOptions } from "./impl/move.js";
47
+ export type { ReadFileOptions } from "./impl/read-file.js";
48
+ export type { ReadJsonOptions } from "./impl/read-json.js";
49
+ export type { JsonStringifyOptions } from "./impl/write-json.js";
50
+ export type { WriteJsonOptions } from "./impl/write-json.js";
package/bin/mod.js CHANGED
@@ -1,2 +1,158 @@
1
- export * from "./impl/subindex.js";
2
- export * as default from "./impl/subindex.js";
1
+ import { readdir, stat } from "node:fs/promises";
2
+ import { join as pathJoin } from "node:path";
3
+ import { toAsync } from "./impl/async.js";
4
+ import { copySync } from "./impl/copy.js";
5
+ import { createFileSync } from "./impl/create-file.js";
6
+ import { diveSync } from "./impl/dive.js";
7
+ import { emptyDirSync } from "./impl/empty-dir.js";
8
+ import { mkdirsSync } from "./impl/mkdirs.js";
9
+ import { moveSync } from "./impl/move.js";
10
+ import { outputFileSync } from "./impl/output-file.js";
11
+ import { outputJsonSync } from "./impl/output-json.js";
12
+ import { pathExistsSync } from "./impl/path-exists.js";
13
+ import { readFileSync } from "./impl/read-file.js";
14
+ import { readJsonSync } from "./impl/read-json.js";
15
+ import { removeSync } from "./impl/remove.js";
16
+ import { writeFileSync } from "./impl/write-file.js";
17
+ import { writeJsonSync } from "./impl/write-json.js";
18
+ const readJson = toAsync(readJsonSync);
19
+ const writeJson = toAsync(writeJsonSync);
20
+ const createFile = toAsync(createFileSync);
21
+ const writeFile = toAsync(writeFileSync);
22
+ const readFile = toAsync(readFileSync);
23
+ const mkdirs = toAsync(mkdirsSync);
24
+ const emptyDir = toAsync(emptyDirSync);
25
+ const pathExists = toAsync(pathExistsSync);
26
+ const copy = toAsync(copySync);
27
+ const move = toAsync(moveSync);
28
+ const remove = toAsync(removeSync);
29
+ const outputJson = toAsync(outputJsonSync);
30
+ const outputFile = toAsync(outputFileSync);
31
+ async function* _diveWorker(currentPath, options, currentDepth) {
32
+ const maxDepth = options.depth ?? Number.POSITIVE_INFINITY;
33
+ if (currentDepth > maxDepth) {
34
+ return;
35
+ }
36
+ let entries;
37
+ try {
38
+ entries = await readdir(currentPath, { withFileTypes: true });
39
+ } catch (_err) {
40
+ return;
41
+ }
42
+ for (const entry of entries) {
43
+ const entryPath = pathJoin(currentPath, entry.name);
44
+ if (!(options.all ?? false) && entry.name.startsWith(".")) {
45
+ continue;
46
+ }
47
+ if (options.ignore) {
48
+ if (Array.isArray(options.ignore) && options.ignore.some((pattern) => entry.name.includes(pattern))) {
49
+ continue;
50
+ }
51
+ if (options.ignore instanceof RegExp && options.ignore.test(entryPath)) {
52
+ continue;
53
+ }
54
+ }
55
+ let entryStat;
56
+ try {
57
+ entryStat = await stat(entryPath);
58
+ } catch (_err) {
59
+ continue;
60
+ }
61
+ if (entry.isDirectory()) {
62
+ if (options.directories ?? false) {
63
+ yield { file: entryPath, stat: entryStat };
64
+ }
65
+ if (options.recursive ?? true) {
66
+ if (currentDepth < maxDepth) {
67
+ yield* _diveWorker(entryPath, options, currentDepth + 1);
68
+ }
69
+ }
70
+ } else if (entry.isFile()) {
71
+ if (options.files ?? true) {
72
+ yield { file: entryPath, stat: entryStat };
73
+ }
74
+ }
75
+ }
76
+ }
77
+ export async function dive(directory, actionOrOptions, optionsOnly) {
78
+ let action;
79
+ let options;
80
+ if (typeof actionOrOptions === "function") {
81
+ action = actionOrOptions;
82
+ options = optionsOnly;
83
+ } else {
84
+ options = actionOrOptions;
85
+ }
86
+ const currentOptions = {
87
+ recursive: true,
88
+ files: true,
89
+ directories: false,
90
+ all: false,
91
+ depth: Number.POSITIVE_INFINITY,
92
+ ...options
93
+ // User options override defaults
94
+ };
95
+ if (action) {
96
+ for await (const { file, stat: entryStat } of _diveWorker(directory, currentOptions, 0)) {
97
+ await action(file, entryStat);
98
+ }
99
+ return;
100
+ } else {
101
+ const results = [];
102
+ for await (const { file } of _diveWorker(directory, currentOptions, 0)) {
103
+ results.push(file);
104
+ }
105
+ return results;
106
+ }
107
+ }
108
+ const mkdirp = mkdirs;
109
+ const ensureDir = mkdirs;
110
+ const ensureFile = createFile;
111
+ const rimraf = remove;
112
+ const ncp = copy;
113
+ const mkdirpSync = mkdirsSync;
114
+ const ensureDirSync = mkdirsSync;
115
+ const ensureFileSync = createFileSync;
116
+ const rimrafSync = removeSync;
117
+ const ncpSync = copySync;
118
+ export * from "node:fs";
119
+ export { default } from "node:fs";
120
+ export {
121
+ readJson,
122
+ writeJson,
123
+ createFile,
124
+ writeFile,
125
+ readFile,
126
+ mkdirs,
127
+ emptyDir,
128
+ pathExists,
129
+ copy,
130
+ move,
131
+ remove,
132
+ readJsonSync,
133
+ writeJsonSync,
134
+ createFileSync,
135
+ writeFileSync,
136
+ readFileSync,
137
+ mkdirsSync,
138
+ emptyDirSync,
139
+ pathExistsSync,
140
+ copySync,
141
+ moveSync,
142
+ removeSync,
143
+ mkdirp,
144
+ mkdirpSync,
145
+ rimraf,
146
+ rimrafSync,
147
+ ncp,
148
+ ncpSync,
149
+ ensureDir,
150
+ ensureDirSync,
151
+ ensureFile,
152
+ ensureFileSync,
153
+ outputJson,
154
+ outputJsonSync,
155
+ outputFile,
156
+ outputFileSync,
157
+ diveSync
158
+ };