@hypernym/utils 3.1.0 → 3.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.
package/dist/fs/index.mjs CHANGED
@@ -1,17 +1,21 @@
1
- import { access, constants, mkdir, writeFile as writeFile$1, cp } from 'node:fs/promises';
1
+ import { access, constants, mkdir as mkdir$1, writeFile as writeFile$1, cp, rm } from 'node:fs/promises';
2
2
  import { dirname } from 'node:path';
3
- import { isString, isURL } from '../index.mjs';
3
+ import { fileURLToPath } from 'node:url';
4
+ import { isURL, isString } from '../index.mjs';
4
5
 
5
6
  async function exists(path) {
6
7
  return await access(path, constants.F_OK).then(() => true).catch(() => false);
7
8
  }
8
9
 
9
- async function writeFile(path, data, options) {
10
- await mkdir(dirname(path), { recursive: true });
11
- return await writeFile$1(path, data, options);
10
+ async function write(path, data, options) {
11
+ await mkdir$1(dirname(isURL(path) ? fileURLToPath(path) : path), {
12
+ recursive: true
13
+ });
14
+ await writeFile$1(path, data, options);
12
15
  }
16
+ const writeFile = write;
13
17
 
14
- async function copy(source, destination, options) {
18
+ async function copy(source, destination, options = {}) {
15
19
  const { recursive = true, filter } = options;
16
20
  const sources = isString(source) || isURL(source) ? [source] : source;
17
21
  for (const src of sources) {
@@ -22,4 +26,20 @@ async function copy(source, destination, options) {
22
26
  }
23
27
  }
24
28
 
25
- export { copy, exists, writeFile };
29
+ async function mkdir(path, options = {}) {
30
+ const { recursive = true, mode } = options;
31
+ const paths = isString(path) || isURL(path) ? [path] : path;
32
+ for (const p of paths) {
33
+ await mkdir$1(p, { recursive, mode });
34
+ }
35
+ }
36
+
37
+ async function remove(path, options = {}) {
38
+ const { recursive = true, force = true } = options;
39
+ const paths = isString(path) || isURL(path) ? [path] : path;
40
+ for (const p of paths) {
41
+ await rm(p, { recursive, force, ...options });
42
+ }
43
+ }
44
+
45
+ export { copy, exists, mkdir, remove, write, writeFile };
@@ -13,19 +13,26 @@ import { writeFile as writeFile$1 } from 'node:fs/promises';
13
13
  */
14
14
  declare function exists(path: string): Promise<boolean>;
15
15
 
16
+ type WritePath = string | URL;
16
17
  /**
17
18
  * Writes data to a file recursively.
18
19
  *
19
20
  * @example
20
21
  *
21
22
  * ```ts
22
- * import { writeFile } from '@hypernym/utils/fs'
23
+ * import { write } from '@hypernym/utils/fs'
23
24
  *
24
- * await writeFile('dir/subdir/file.ts', `console.log('Hello World!')`)
25
+ * await write('dir/subdir/file.ts', `console.log('Hello World!')`)
25
26
  * ```
26
27
  */
27
- declare function writeFile(path: string, data: Parameters<typeof writeFile$1>[1], options?: Parameters<typeof writeFile$1>[2]): Promise<void>;
28
+ declare function write(path: WritePath, data: Parameters<typeof writeFile$1>[1], options?: Parameters<typeof writeFile$1>[2]): Promise<void>;
29
+ /**
30
+ * @deprecated Use `write` instead.
31
+ */
32
+ declare const writeFile: typeof write;
28
33
 
34
+ type CopySource = string | URL;
35
+ type CopyDestination = string | URL;
29
36
  interface CopyOptions {
30
37
  /**
31
38
  * Copies files or directories recursively.
@@ -55,6 +62,88 @@ interface CopyOptions {
55
62
  * await copy('src/subdir/file.ts', './dist/subdir')
56
63
  * ```
57
64
  */
58
- declare function copy(source: string | URL | (string | URL)[], destination: string | URL, options: CopyOptions): Promise<void>;
65
+ declare function copy(source: CopySource | CopySource[], destination: CopyDestination, options?: CopyOptions): Promise<void>;
66
+
67
+ type MakeDirPath = string | URL;
68
+ interface MakeDirOptions {
69
+ /**
70
+ * Indicates whether parent folders should be created.
71
+ *
72
+ * If a folder was created, the path to the first created folder will be returned.
73
+ *
74
+ * @default true
75
+ */
76
+ recursive?: boolean;
77
+ /**
78
+ * A file mode. If a string is passed, it is parsed as an octal integer.
79
+ *
80
+ * @default 0o777
81
+ */
82
+ mode?: string | number;
83
+ }
84
+ /**
85
+ * Creates a `directory` recursively.
86
+ *
87
+ * Accepts a single path or a range of paths.
88
+ *
89
+ * @example
90
+ *
91
+ * ```ts
92
+ * import { mkdir } from '@hypernym/utils/fs'
93
+ *
94
+ * await mkdir('src/subdir')
95
+ * ```
96
+ */
97
+ declare function mkdir(path: MakeDirPath | MakeDirPath[], options?: MakeDirOptions): Promise<void>;
98
+
99
+ type RemovePath = string | URL;
100
+ interface RemoveOptions {
101
+ /**
102
+ * If `true`, perform a recursive directory removal.
103
+ *
104
+ * In recursive mode, operations are retried on failure.
105
+ *
106
+ * @default true
107
+ */
108
+ recursive?: boolean;
109
+ /**
110
+ * When `true`, exceptions will be ignored if `path` does not exist.
111
+ *
112
+ * @default true
113
+ */
114
+ force?: boolean;
115
+ /**
116
+ * If an `EBUSY`, `EMFILE`, `ENFILE`, `ENOTEMPTY`, or
117
+ * `EPERM` error is encountered, Node.js will retry the operation with a linear
118
+ * backoff wait of `retryDelay` ms longer on each try. This option represents the
119
+ * number of retries. This option is ignored if the `recursive` option is not
120
+ * `true`.
121
+ *
122
+ * @default 0
123
+ */
124
+ maxRetries?: number;
125
+ /**
126
+ * The amount of time in milliseconds to wait between retries.
127
+ *
128
+ * This option is ignored if the `recursive` option is not `true`.
129
+ *
130
+ * @default 100
131
+ */
132
+ retryDelay?: number;
133
+ }
134
+ /**
135
+ * Removes `files` and `directories` recursively.
136
+ *
137
+ * Accepts a single path or a range of paths.
138
+ *
139
+ * @example
140
+ *
141
+ * ```ts
142
+ * import { remove } from '@hypernym/utils/fs'
143
+ *
144
+ * await remove('src/subdir/file.ts')
145
+ * ```
146
+ */
147
+ declare function remove(path: RemovePath | RemovePath[], options?: RemoveOptions): Promise<void>;
59
148
 
60
- export { type CopyOptions, copy, exists, writeFile };
149
+ export { type CopyDestination, type CopyOptions, type CopySource, type MakeDirOptions, type MakeDirPath, type RemoveOptions, type RemovePath, type WritePath, copy, exists, mkdir, remove, write, writeFile };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hypernym/utils",
3
- "version": "3.1.0",
3
+ "version": "3.2.0",
4
4
  "author": "Hypernym Studio",
5
5
  "description": "A collection of reusable utilities.",
6
6
  "license": "MIT",