@simplysm/core-node 13.0.100 → 14.0.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 (41) hide show
  1. package/dist/features/fs-watcher.d.ts +21 -21
  2. package/dist/features/fs-watcher.d.ts.map +1 -1
  3. package/dist/features/fs-watcher.js +176 -114
  4. package/dist/features/fs-watcher.js.map +1 -6
  5. package/dist/index.js +6 -7
  6. package/dist/index.js.map +1 -6
  7. package/dist/utils/fs.d.ts +96 -96
  8. package/dist/utils/fs.d.ts.map +1 -1
  9. package/dist/utils/fs.js +437 -272
  10. package/dist/utils/fs.js.map +1 -6
  11. package/dist/utils/path.d.ts +22 -22
  12. package/dist/utils/path.js +103 -45
  13. package/dist/utils/path.js.map +1 -6
  14. package/dist/worker/create-worker.d.ts +3 -3
  15. package/dist/worker/create-worker.js +106 -81
  16. package/dist/worker/create-worker.js.map +1 -6
  17. package/dist/worker/types.d.ts +14 -14
  18. package/dist/worker/types.js +4 -1
  19. package/dist/worker/types.js.map +1 -6
  20. package/dist/worker/worker.d.ts +5 -5
  21. package/dist/worker/worker.js +168 -132
  22. package/dist/worker/worker.js.map +1 -6
  23. package/lib/worker-dev-proxy.js +15 -0
  24. package/package.json +8 -6
  25. package/src/features/fs-watcher.ts +53 -42
  26. package/src/index.ts +3 -3
  27. package/src/utils/fs.ts +111 -120
  28. package/src/utils/path.ts +26 -26
  29. package/src/worker/create-worker.ts +10 -10
  30. package/src/worker/types.ts +14 -14
  31. package/src/worker/worker.ts +29 -29
  32. package/README.md +0 -112
  33. package/docs/features.md +0 -91
  34. package/docs/fs.md +0 -309
  35. package/docs/path.md +0 -120
  36. package/docs/worker.md +0 -168
  37. package/tests/utils/fs-watcher.spec.ts +0 -286
  38. package/tests/utils/fs.spec.ts +0 -705
  39. package/tests/utils/path.spec.ts +0 -179
  40. package/tests/worker/fixtures/test-worker.ts +0 -35
  41. package/tests/worker/sd-worker.spec.ts +0 -174
package/docs/fs.md DELETED
@@ -1,309 +0,0 @@
1
- # File System Utilities
2
-
3
- File system utilities exported as the `fsx` namespace.
4
-
5
- ```typescript
6
- import { fsx } from "@simplysm/core-node";
7
- ```
8
-
9
- ## Existence Check
10
-
11
- ### `existsSync`
12
-
13
- Checks if a file or directory exists (synchronous).
14
-
15
- ```typescript
16
- function existsSync(targetPath: string): boolean;
17
- ```
18
-
19
- ### `exists`
20
-
21
- Checks if a file or directory exists (asynchronous).
22
-
23
- ```typescript
24
- function exists(targetPath: string): Promise<boolean>;
25
- ```
26
-
27
- ## Create Directory
28
-
29
- ### `mkdirSync`
30
-
31
- Creates a directory (recursive).
32
-
33
- ```typescript
34
- function mkdirSync(targetPath: string): void;
35
- ```
36
-
37
- ### `mkdir`
38
-
39
- Creates a directory (recursive, asynchronous).
40
-
41
- ```typescript
42
- function mkdir(targetPath: string): Promise<void>;
43
- ```
44
-
45
- ## Delete
46
-
47
- ### `rmSync`
48
-
49
- Deletes a file or directory.
50
-
51
- The synchronous version fails immediately without retries. Use `rm` for cases with potential transient errors like file locks.
52
-
53
- ```typescript
54
- function rmSync(targetPath: string): void;
55
- ```
56
-
57
- ### `rm`
58
-
59
- Deletes a file or directory (asynchronous).
60
-
61
- The asynchronous version retries up to 6 times (500ms interval) for transient errors like file locks.
62
-
63
- ```typescript
64
- function rm(targetPath: string): Promise<void>;
65
- ```
66
-
67
- ## Copy
68
-
69
- ### `copySync`
70
-
71
- Copies a file or directory.
72
-
73
- If `sourcePath` does not exist, no action is performed and the function returns.
74
-
75
- ```typescript
76
- function copySync(
77
- sourcePath: string,
78
- targetPath: string,
79
- filter?: (absolutePath: string) => boolean,
80
- ): void;
81
- ```
82
-
83
- **Parameters:**
84
- - `sourcePath` -- Path of the source to copy
85
- - `targetPath` -- Destination path for the copy
86
- - `filter` -- A filter function that determines whether to copy. The **absolute path** of each file/directory is passed. Returns `true` to copy, `false` to exclude. The top-level `sourcePath` is not subject to filtering; the filter function is applied recursively to all children. Returning `false` for a directory skips that directory and all its contents.
87
-
88
- ### `copy`
89
-
90
- Copies a file or directory (asynchronous). Same behavior as `copySync`.
91
-
92
- ```typescript
93
- function copy(
94
- sourcePath: string,
95
- targetPath: string,
96
- filter?: (absolutePath: string) => boolean,
97
- ): Promise<void>;
98
- ```
99
-
100
- ## Read File
101
-
102
- ### `readSync`
103
-
104
- Reads a file as a UTF-8 string.
105
-
106
- ```typescript
107
- function readSync(targetPath: string): string;
108
- ```
109
-
110
- ### `read`
111
-
112
- Reads a file as a UTF-8 string (asynchronous).
113
-
114
- ```typescript
115
- function read(targetPath: string): Promise<string>;
116
- ```
117
-
118
- ### `readBufferSync`
119
-
120
- Reads a file as a Buffer.
121
-
122
- ```typescript
123
- function readBufferSync(targetPath: string): Buffer;
124
- ```
125
-
126
- ### `readBuffer`
127
-
128
- Reads a file as a Buffer (asynchronous).
129
-
130
- ```typescript
131
- function readBuffer(targetPath: string): Promise<Buffer>;
132
- ```
133
-
134
- ### `readJsonSync`
135
-
136
- Reads a JSON file (using JsonConvert).
137
-
138
- ```typescript
139
- function readJsonSync<TData = unknown>(targetPath: string): TData;
140
- ```
141
-
142
- ### `readJson`
143
-
144
- Reads a JSON file (using JsonConvert, asynchronous).
145
-
146
- ```typescript
147
- function readJson<TData = unknown>(targetPath: string): Promise<TData>;
148
- ```
149
-
150
- ## Write File
151
-
152
- ### `writeSync`
153
-
154
- Writes data to a file (auto-creates parent directories).
155
-
156
- ```typescript
157
- function writeSync(targetPath: string, data: string | Uint8Array): void;
158
- ```
159
-
160
- ### `write`
161
-
162
- Writes data to a file (auto-creates parent directories, asynchronous).
163
-
164
- ```typescript
165
- function write(targetPath: string, data: string | Uint8Array): Promise<void>;
166
- ```
167
-
168
- ### `writeJsonSync`
169
-
170
- Writes data to a JSON file (using JsonConvert).
171
-
172
- ```typescript
173
- function writeJsonSync(
174
- targetPath: string,
175
- data: unknown,
176
- options?: {
177
- replacer?: (this: unknown, key: string | undefined, value: unknown) => unknown;
178
- space?: string | number;
179
- },
180
- ): void;
181
- ```
182
-
183
- ### `writeJson`
184
-
185
- Writes data to a JSON file (using JsonConvert, asynchronous).
186
-
187
- ```typescript
188
- function writeJson(
189
- targetPath: string,
190
- data: unknown,
191
- options?: {
192
- replacer?: (this: unknown, key: string | undefined, value: unknown) => unknown;
193
- space?: string | number;
194
- },
195
- ): Promise<void>;
196
- ```
197
-
198
- ## Read Directory
199
-
200
- ### `readdirSync`
201
-
202
- Reads the contents of a directory.
203
-
204
- ```typescript
205
- function readdirSync(targetPath: string): string[];
206
- ```
207
-
208
- ### `readdir`
209
-
210
- Reads the contents of a directory (asynchronous).
211
-
212
- ```typescript
213
- function readdir(targetPath: string): Promise<string[]>;
214
- ```
215
-
216
- ## File Information
217
-
218
- ### `statSync`
219
-
220
- Gets file/directory information (follows symbolic links).
221
-
222
- ```typescript
223
- function statSync(targetPath: string): fs.Stats;
224
- ```
225
-
226
- ### `stat`
227
-
228
- Gets file/directory information (follows symbolic links, asynchronous).
229
-
230
- ```typescript
231
- function stat(targetPath: string): Promise<fs.Stats>;
232
- ```
233
-
234
- ### `lstatSync`
235
-
236
- Gets file/directory information (does not follow symbolic links).
237
-
238
- ```typescript
239
- function lstatSync(targetPath: string): fs.Stats;
240
- ```
241
-
242
- ### `lstat`
243
-
244
- Gets file/directory information (does not follow symbolic links, asynchronous).
245
-
246
- ```typescript
247
- function lstat(targetPath: string): Promise<fs.Stats>;
248
- ```
249
-
250
- ## Glob
251
-
252
- ### `globSync`
253
-
254
- Searches for files using a glob pattern.
255
-
256
- ```typescript
257
- function globSync(pattern: string, options?: GlobOptions): string[];
258
- ```
259
-
260
- Returns an array of absolute paths for matched files.
261
-
262
- ### `glob`
263
-
264
- Searches for files using a glob pattern (asynchronous).
265
-
266
- ```typescript
267
- function glob(pattern: string, options?: GlobOptions): Promise<string[]>;
268
- ```
269
-
270
- Returns an array of absolute paths for matched files.
271
-
272
- ## Utilities
273
-
274
- ### `clearEmptyDirectory`
275
-
276
- Recursively searches and deletes empty directories under a specified directory. If all child directories are deleted and a parent becomes empty, it will also be deleted.
277
-
278
- ```typescript
279
- function clearEmptyDirectory(dirPath: string): Promise<void>;
280
- ```
281
-
282
- ### `findAllParentChildPathsSync`
283
-
284
- Searches for files matching a glob pattern by traversing parent directories from a start path towards the root. Collects all file paths matching the `childGlob` pattern in each directory.
285
-
286
- ```typescript
287
- function findAllParentChildPathsSync(
288
- childGlob: string,
289
- fromPath: string,
290
- rootPath?: string,
291
- ): string[];
292
- ```
293
-
294
- **Parameters:**
295
- - `childGlob` -- Glob pattern to search for in each directory
296
- - `fromPath` -- Path to start searching from
297
- - `rootPath` -- Path to stop searching at (if not specified, searches to filesystem root). `fromPath` must be a child path of `rootPath`. Otherwise, searches to the filesystem root.
298
-
299
- ### `findAllParentChildPaths`
300
-
301
- Asynchronous version of `findAllParentChildPathsSync`.
302
-
303
- ```typescript
304
- function findAllParentChildPaths(
305
- childGlob: string,
306
- fromPath: string,
307
- rootPath?: string,
308
- ): Promise<string[]>;
309
- ```
package/docs/path.md DELETED
@@ -1,120 +0,0 @@
1
- # Path Utilities
2
-
3
- Path utilities exported as the `pathx` namespace.
4
-
5
- ```typescript
6
- import { pathx } from "@simplysm/core-node";
7
- ```
8
-
9
- ## Types
10
-
11
- ### `NormPath`
12
-
13
- Brand type representing a normalized path. Can only be created through `norm()`.
14
-
15
- ```typescript
16
- type NormPath = string & { [NORM]: never };
17
- ```
18
-
19
- ## Functions
20
-
21
- ### `posix`
22
-
23
- Converts to POSIX-style path (backslash to forward slash).
24
-
25
- ```typescript
26
- function posix(...args: string[]): string;
27
- ```
28
-
29
- **Examples:**
30
- ```typescript
31
- posix("C:\\Users\\test"); // "C:/Users/test"
32
- posix("src", "index.ts"); // "src/index.ts"
33
- ```
34
-
35
- ### `changeFileDirectory`
36
-
37
- Changes the directory of a file path.
38
-
39
- ```typescript
40
- function changeFileDirectory(
41
- filePath: string,
42
- fromDirectory: string,
43
- toDirectory: string,
44
- ): string;
45
- ```
46
-
47
- Throws an error if the file is not inside `fromDirectory`.
48
-
49
- **Example:**
50
- ```typescript
51
- changeFileDirectory("/a/b/c.txt", "/a", "/x");
52
- // "/x/b/c.txt"
53
- ```
54
-
55
- ### `basenameWithoutExt`
56
-
57
- Returns the filename (basename) without extension.
58
-
59
- ```typescript
60
- function basenameWithoutExt(filePath: string): string;
61
- ```
62
-
63
- **Examples:**
64
- ```typescript
65
- basenameWithoutExt("file.txt"); // "file"
66
- basenameWithoutExt("/path/to/file.spec.ts"); // "file.spec"
67
- ```
68
-
69
- ### `isChildPath`
70
-
71
- Checks if `childPath` is a child path of `parentPath`. Returns `false` if the paths are the same.
72
-
73
- Paths are internally normalized using `norm()` and compared using platform-specific path separators.
74
-
75
- ```typescript
76
- function isChildPath(childPath: string, parentPath: string): boolean;
77
- ```
78
-
79
- **Examples:**
80
- ```typescript
81
- isChildPath("/a/b/c", "/a/b"); // true
82
- isChildPath("/a/b", "/a/b/c"); // false
83
- isChildPath("/a/b", "/a/b"); // false (same path)
84
- ```
85
-
86
- ### `norm`
87
-
88
- Normalizes the path and returns it as `NormPath`. Converts to absolute path and normalizes using platform-specific separators.
89
-
90
- ```typescript
91
- function norm(...paths: string[]): NormPath;
92
- ```
93
-
94
- **Examples:**
95
- ```typescript
96
- norm("/some/path"); // NormPath
97
- norm("relative", "path"); // NormPath (converted to absolute path)
98
- ```
99
-
100
- ### `filterByTargets`
101
-
102
- Filters files based on a list of target paths. Includes files that match or are children of a target path.
103
-
104
- ```typescript
105
- function filterByTargets(files: string[], targets: string[], cwd: string): string[];
106
- ```
107
-
108
- **Parameters:**
109
- - `files` -- File paths to filter. Must be absolute paths under `cwd`.
110
- - `targets` -- Target paths (relative to `cwd`, POSIX style recommended)
111
- - `cwd` -- Current working directory (absolute path)
112
-
113
- If `targets` is empty, returns `files` as-is; otherwise returns only files under target paths.
114
-
115
- **Example:**
116
- ```typescript
117
- const files = ["/proj/src/a.ts", "/proj/src/b.ts", "/proj/tests/c.ts"];
118
- filterByTargets(files, ["src"], "/proj");
119
- // ["/proj/src/a.ts", "/proj/src/b.ts"]
120
- ```
package/docs/worker.md DELETED
@@ -1,168 +0,0 @@
1
- # Worker
2
-
3
- Type-safe `worker_threads` wrapper providing a proxy-based RPC interface for worker threads.
4
-
5
- ```typescript
6
- import { Worker, createWorker } from "@simplysm/core-node";
7
- ```
8
-
9
- ## Types
10
-
11
- ### `WorkerModule`
12
-
13
- Type structure of the worker module returned by `createWorker()`. Used for type inference in `Worker.create<typeof import("./worker")>()`.
14
-
15
- ```typescript
16
- interface WorkerModule {
17
- default: {
18
- __methods: Record<string, (...args: any[]) => unknown>;
19
- __events: Record<string, unknown>;
20
- };
21
- }
22
- ```
23
-
24
- ### `PromisifyMethods<TMethods>`
25
-
26
- Mapping type that wraps method return values in `Promise`. Worker methods operate based on `postMessage` and are always asynchronous, so synchronous method types are also converted to `Promise<Awaited<R>>`.
27
-
28
- ```typescript
29
- type PromisifyMethods<TMethods> = {
30
- [K in keyof TMethods]: TMethods[K] extends (...args: infer P) => infer R
31
- ? (...args: P) => Promise<Awaited<R>>
32
- : never;
33
- };
34
- ```
35
-
36
- ### `WorkerProxy<TModule>`
37
-
38
- Proxy type returned by `Worker.create()`. Provides promisified methods + `on()` + `off()` + `terminate()`.
39
-
40
- ```typescript
41
- type WorkerProxy<TModule extends WorkerModule> = PromisifyMethods<
42
- TModule["default"]["__methods"]
43
- > & {
44
- /** Registers a worker event listener. */
45
- on<TEventName extends keyof TModule["default"]["__events"] & string>(
46
- event: TEventName,
47
- listener: (data: TModule["default"]["__events"][TEventName]) => void,
48
- ): void;
49
-
50
- /** Unregisters a worker event listener. */
51
- off<TEventName extends keyof TModule["default"]["__events"] & string>(
52
- event: TEventName,
53
- listener: (data: TModule["default"]["__events"][TEventName]) => void,
54
- ): void;
55
-
56
- /** Terminates the worker. */
57
- terminate(): Promise<void>;
58
- };
59
- ```
60
-
61
- ### `WorkerRequest`
62
-
63
- Internal worker request message.
64
-
65
- ```typescript
66
- interface WorkerRequest {
67
- id: string;
68
- method: string;
69
- params: unknown[];
70
- }
71
- ```
72
-
73
- ### `WorkerResponse`
74
-
75
- Internal worker response message.
76
-
77
- ```typescript
78
- type WorkerResponse =
79
- | { request: WorkerRequest; type: "return"; body?: unknown }
80
- | { request: WorkerRequest; type: "error"; body: Error }
81
- | { type: "event"; event: string; body?: unknown }
82
- | { type: "log"; body: string };
83
- ```
84
-
85
- ## `Worker.create`
86
-
87
- Creates a type-safe Worker Proxy.
88
-
89
- ```typescript
90
- const Worker = {
91
- create<TModule extends WorkerModule>(
92
- filePath: string,
93
- opt?: Omit<WorkerOptions, "stdout" | "stderr">,
94
- ): WorkerProxy<TModule>;
95
- };
96
- ```
97
-
98
- **Parameters:**
99
- - `filePath` -- Worker file path (file:// URL or absolute path)
100
- - `opt` -- Worker options
101
-
102
- **Returns:** Proxy object (supports direct method calls, `on()`, `off()`, and `terminate()`)
103
-
104
- In development (`.ts` files), TypeScript worker files are executed via tsx. In production (`.js` files), Worker is created directly.
105
-
106
- ## `createWorker`
107
-
108
- Worker factory for use in worker threads. This is the function called inside the worker file.
109
-
110
- ```typescript
111
- function createWorker<
112
- TMethods extends Record<string, (...args: any[]) => unknown>,
113
- TEvents extends Record<string, unknown> = Record<string, never>,
114
- >(
115
- methods: TMethods,
116
- ): {
117
- send<TEventName extends keyof TEvents & string>(event: TEventName, data?: TEvents[TEventName]): void;
118
- __methods: TMethods;
119
- __events: TEvents;
120
- };
121
- ```
122
-
123
- ## Example
124
-
125
- ```typescript
126
- // worker.ts
127
- import { createWorker } from "@simplysm/core-node";
128
-
129
- export default createWorker({
130
- add: (a: number, b: number) => a + b,
131
- });
132
-
133
- // main.ts
134
- import { Worker } from "@simplysm/core-node";
135
-
136
- const worker = Worker.create<typeof import("./worker")>("./worker.ts");
137
- const result = await worker.add(10, 20); // 30
138
- await worker.terminate();
139
- ```
140
-
141
- ### Worker with Events
142
-
143
- ```typescript
144
- // worker.ts
145
- import { createWorker } from "@simplysm/core-node";
146
-
147
- interface MyEvents {
148
- progress: number;
149
- }
150
-
151
- const methods = {
152
- calc: (x: number) => {
153
- sender.send("progress", 50);
154
- return x * 2;
155
- },
156
- };
157
-
158
- const sender = createWorker<typeof methods, MyEvents>(methods);
159
- export default sender;
160
-
161
- // main.ts
162
- const worker = Worker.create<typeof import("./worker")>("./worker.ts");
163
- worker.on("progress", (value) => {
164
- console.log(`Progress: ${value}%`);
165
- });
166
- const result = await worker.calc(5); // 10
167
- await worker.terminate();
168
- ```