@hypernym/utils 3.0.1 → 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/README.md CHANGED
@@ -1,12 +1,22 @@
1
- # @hypernym/utils
1
+ <h1 align="center">Hyperutils</h1>
2
2
 
3
- A collection of reusable utilities.
3
+ <p align="center">A collection of reusable utilities.</p>
4
4
 
5
- <sub><a href="https://github.com/hypernym-studio/utils">Repository</a> | <a href="https://www.npmjs.com/package/@hypernym/utils">Package</a> | <a href="https://github.com/hypernym-studio/utils/releases">Releases</a> | <a href="https://github.com/hypernym-studio/utils/discussions">Discussions</a></sub>
5
+ <p align="center">
6
+ <a href="https://github.com/hypernym-studio/utils">Repository</a>
7
+ <span>+</span>
8
+ <a href="https://www.npmjs.com/package/@hypernym/utils">Package</a>
9
+ <span>+</span>
10
+ <a href="https://github.com/hypernym-studio/utils/releases">Releases</a>
11
+ <span>+</span>
12
+ <a href="https://github.com/hypernym-studio/utils/discussions">Discussions</a>
13
+ </p>
6
14
 
7
- ```sh
8
- pnpm add @hypernym/utils
9
- ```
15
+ <pre align="center">pnpm add @hypernym/utils</pre>
16
+
17
+ <h4 align="center">Hypernym Studio</h4>
18
+
19
+ <br>
10
20
 
11
21
  ## Features
12
22
 
package/dist/fs/index.mjs CHANGED
@@ -1,13 +1,45 @@
1
- import { access, constants, mkdir, writeFile as writeFile$1 } 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 { fileURLToPath } from 'node:url';
4
+ import { isURL, isString } from '../index.mjs';
3
5
 
4
6
  async function exists(path) {
5
7
  return await access(path, constants.F_OK).then(() => true).catch(() => false);
6
8
  }
7
9
 
8
- async function writeFile(path, data, options) {
9
- await mkdir(dirname(path), { recursive: true });
10
- 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);
11
15
  }
16
+ const writeFile = write;
12
17
 
13
- export { exists, writeFile };
18
+ async function copy(source, destination, options = {}) {
19
+ const { recursive = true, filter } = options;
20
+ const sources = isString(source) || isURL(source) ? [source] : source;
21
+ for (const src of sources) {
22
+ await cp(src, destination, {
23
+ recursive,
24
+ filter
25
+ });
26
+ }
27
+ }
28
+
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 };
@@ -8,22 +8,142 @@ import { writeFile as writeFile$1 } from 'node:fs/promises';
8
8
  * ```ts
9
9
  * import { exists } from '@hypernym/utils/fs'
10
10
  *
11
- * exists('dir/file.ts') // => true
11
+ * await exists('dir/file.ts') // => true
12
12
  * ```
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
- * 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;
33
+
34
+ type CopySource = string | URL;
35
+ type CopyDestination = string | URL;
36
+ interface CopyOptions {
37
+ /**
38
+ * Copies files or directories recursively.
39
+ *
40
+ * @default true
41
+ */
42
+ recursive?: boolean;
43
+ /**
44
+ * Filters copied `files` or `directories`.
45
+ *
46
+ * Returns `true` to copy the item, `false` to ignore it.
47
+ *
48
+ * @default undefined
49
+ */
50
+ filter?(source: string, destination: string): boolean | Promise<boolean>;
51
+ }
52
+ /**
53
+ * Copies `files` or `directories` recursively.
54
+ *
55
+ * Accepts a single source or a range of sources.
56
+ *
57
+ * @example
58
+ *
59
+ * ```ts
60
+ * import { copy } from '@hypernym/utils/fs'
61
+ *
62
+ * await copy('src/subdir/file.ts', './dist/subdir')
63
+ * ```
64
+ */
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>;
28
148
 
29
- export { 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.0.1",
3
+ "version": "3.2.0",
4
4
  "author": "Hypernym Studio",
5
5
  "description": "A collection of reusable utilities.",
6
6
  "license": "MIT",
@@ -45,7 +45,7 @@
45
45
  "prepublishOnly": "npm run build"
46
46
  },
47
47
  "sideEffects": false,
48
- "packageManager": "pnpm@9.9.0",
48
+ "packageManager": "pnpm@9.10.0",
49
49
  "engines": {
50
50
  "node": ">=20.0.0",
51
51
  "pnpm": ">=9.0.0"
@@ -63,14 +63,14 @@
63
63
  }
64
64
  },
65
65
  "devDependencies": {
66
- "@hypernym/bundler": "^0.10.0",
66
+ "@hypernym/bundler": "^0.11.0",
67
67
  "@hypernym/eslint-config": "^3.5.0",
68
68
  "@hypernym/prettier-config": "^3.2.0",
69
69
  "@hypernym/tsconfig": "^2.3.0",
70
- "@types/node": "^20.16.5",
70
+ "@types/node": "^22.5.5",
71
71
  "eslint": "^9.10.0",
72
72
  "prettier": "^3.3.3",
73
73
  "typescript": "^5.5.4",
74
- "vitest": "^2.0.5"
74
+ "vitest": "^2.1.1"
75
75
  }
76
76
  }