@simplysm/core-node 14.0.4 → 14.0.5
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 +105 -76
- package/docs/fs-watcher.md +15 -17
- package/docs/fsx.md +71 -44
- package/docs/pathx.md +20 -13
- package/docs/worker.md +16 -16
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @simplysm/core-node
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Node.js-specific core utilities for the Simplysm framework. Provides enhanced file system operations, path utilities, file watching, and a type-safe worker thread abstraction.
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
@@ -8,99 +8,122 @@ Core Node.js utilities -- file system operations, path utilities, file watcher,
|
|
|
8
8
|
npm install @simplysm/core-node
|
|
9
9
|
```
|
|
10
10
|
|
|
11
|
-
## API
|
|
12
|
-
|
|
13
|
-
###
|
|
14
|
-
|
|
15
|
-
Namespace
|
|
16
|
-
|
|
17
|
-
|
|
|
18
|
-
|
|
19
|
-
| `
|
|
20
|
-
| `
|
|
21
|
-
| `
|
|
22
|
-
| `
|
|
23
|
-
| `
|
|
24
|
-
| `
|
|
25
|
-
| `
|
|
26
|
-
| `
|
|
27
|
-
| `
|
|
28
|
-
| `
|
|
29
|
-
| `
|
|
30
|
-
| `
|
|
31
|
-
| `
|
|
32
|
-
| `
|
|
33
|
-
| `
|
|
34
|
-
| `
|
|
35
|
-
| `
|
|
36
|
-
| `
|
|
37
|
-
| `
|
|
38
|
-
| `
|
|
39
|
-
| `
|
|
40
|
-
| `
|
|
41
|
-
| `
|
|
42
|
-
| `
|
|
43
|
-
| `
|
|
44
|
-
| `
|
|
45
|
-
| `clearEmptyDirectory` |
|
|
46
|
-
| `
|
|
47
|
-
| `
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
|
11
|
+
## API Overview
|
|
12
|
+
|
|
13
|
+
### Utilities / fsx
|
|
14
|
+
|
|
15
|
+
Namespace `fsx` -- Enhanced file system functions (sync and async pairs).
|
|
16
|
+
|
|
17
|
+
| API | Type | Description |
|
|
18
|
+
|-----|------|-------------|
|
|
19
|
+
| `exists` | function | Check if a path exists (async) |
|
|
20
|
+
| `existsSync` | function | Check if a path exists (sync) |
|
|
21
|
+
| `mkdir` | function | Create directory recursively (async) |
|
|
22
|
+
| `mkdirSync` | function | Create directory recursively (sync) |
|
|
23
|
+
| `rm` | function | Remove file/directory with retry (async) |
|
|
24
|
+
| `rmSync` | function | Remove file/directory (sync) |
|
|
25
|
+
| `copy` | function | Copy file/directory with filter (async) |
|
|
26
|
+
| `copySync` | function | Copy file/directory with filter (sync) |
|
|
27
|
+
| `read` | function | Read file as UTF-8 string (async) |
|
|
28
|
+
| `readSync` | function | Read file as UTF-8 string (sync) |
|
|
29
|
+
| `readBuffer` | function | Read file as Buffer (async) |
|
|
30
|
+
| `readBufferSync` | function | Read file as Buffer (sync) |
|
|
31
|
+
| `readJson` | function | Read and parse JSON file (async) |
|
|
32
|
+
| `readJsonSync` | function | Read and parse JSON file (sync) |
|
|
33
|
+
| `write` | function | Write data to file (async) |
|
|
34
|
+
| `writeSync` | function | Write data to file (sync) |
|
|
35
|
+
| `writeJson` | function | Write data as JSON file (async) |
|
|
36
|
+
| `writeJsonSync` | function | Write data as JSON file (sync) |
|
|
37
|
+
| `readdir` | function | List directory contents (async) |
|
|
38
|
+
| `readdirSync` | function | List directory contents (sync) |
|
|
39
|
+
| `stat` | function | Get file stats, follows symlinks (async) |
|
|
40
|
+
| `statSync` | function | Get file stats, follows symlinks (sync) |
|
|
41
|
+
| `lstat` | function | Get file stats, no symlink follow (async) |
|
|
42
|
+
| `lstatSync` | function | Get file stats, no symlink follow (sync) |
|
|
43
|
+
| `glob` | function | Search files by glob pattern (async) |
|
|
44
|
+
| `globSync` | function | Search files by glob pattern (sync) |
|
|
45
|
+
| `clearEmptyDirectory` | function | Recursively remove empty directories (async) |
|
|
46
|
+
| `findAllParentChildPaths` | function | Search parent dirs for glob matches (async) |
|
|
47
|
+
| `findAllParentChildPathsSync` | function | Search parent dirs for glob matches (sync) |
|
|
48
|
+
|
|
49
|
+
> See [docs/fsx.md](./docs/fsx.md) for details.
|
|
50
|
+
|
|
51
|
+
### Utilities / pathx
|
|
52
|
+
|
|
53
|
+
Namespace `pathx` -- Path manipulation utilities.
|
|
54
|
+
|
|
55
|
+
| API | Type | Description |
|
|
56
|
+
|-----|------|-------------|
|
|
57
|
+
| `NormPath` | type | Branded string type for normalized paths |
|
|
56
58
|
| `posix` | function | Convert path to POSIX style (backslash to slash) |
|
|
57
|
-
| `changeFileDirectory` | function | Change
|
|
59
|
+
| `changeFileDirectory` | function | Change a file's parent directory |
|
|
58
60
|
| `basenameWithoutExt` | function | Get filename without extension |
|
|
59
61
|
| `isChildPath` | function | Check if a path is a child of another |
|
|
60
62
|
| `norm` | function | Normalize and resolve path to `NormPath` |
|
|
61
63
|
| `filterByTargets` | function | Filter file paths by target directory prefixes |
|
|
62
64
|
|
|
63
|
-
|
|
65
|
+
> See [docs/pathx.md](./docs/pathx.md) for details.
|
|
64
66
|
|
|
65
|
-
|
|
67
|
+
### Features
|
|
66
68
|
|
|
67
|
-
|
|
|
68
|
-
|
|
69
|
-
| `FsWatcherEvent` | type |
|
|
70
|
-
| `FsWatcherChangeInfo` | interface |
|
|
71
|
-
| `FsWatcher` | class | Debounced file system watcher |
|
|
69
|
+
| API | Type | Description |
|
|
70
|
+
|-----|------|-------------|
|
|
71
|
+
| `FsWatcherEvent` | type | File change event type union |
|
|
72
|
+
| `FsWatcherChangeInfo` | interface | File change event info |
|
|
73
|
+
| `FsWatcher` | class | Debounced file system watcher (chokidar-based) |
|
|
72
74
|
|
|
73
|
-
|
|
75
|
+
> See [docs/fs-watcher.md](./docs/fs-watcher.md) for details.
|
|
74
76
|
|
|
75
|
-
|
|
77
|
+
### Worker
|
|
76
78
|
|
|
77
|
-
|
|
|
78
|
-
|
|
79
|
+
| API | Type | Description |
|
|
80
|
+
|-----|------|-------------|
|
|
79
81
|
| `WorkerModule` | interface | Type structure for worker modules |
|
|
80
|
-
| `PromisifyMethods` | type | Maps sync
|
|
82
|
+
| `PromisifyMethods` | type | Maps sync methods to async (Promise) |
|
|
81
83
|
| `WorkerProxy` | type | Proxy type returned by `Worker.create()` |
|
|
82
84
|
| `WorkerRequest` | interface | Internal worker request message |
|
|
83
|
-
| `WorkerResponse` | type | Internal worker response message
|
|
84
|
-
| `Worker` |
|
|
85
|
-
| `createWorker` | function |
|
|
85
|
+
| `WorkerResponse` | type | Internal worker response message |
|
|
86
|
+
| `Worker` | object | Type-safe worker thread factory |
|
|
87
|
+
| `createWorker` | function | Create a worker module in the worker thread |
|
|
86
88
|
|
|
87
|
-
|
|
89
|
+
> See [docs/worker.md](./docs/worker.md) for details.
|
|
88
90
|
|
|
89
|
-
|
|
90
|
-
import { fsx, pathx } from "@simplysm/core-node";
|
|
91
|
+
## Usage Examples
|
|
91
92
|
|
|
92
|
-
|
|
93
|
-
await fsx.mkdir("/tmp/mydir");
|
|
94
|
-
await fsx.write("/tmp/mydir/hello.txt", "Hello, world!");
|
|
95
|
-
const content = await fsx.read("/tmp/mydir/hello.txt");
|
|
96
|
-
const config = await fsx.readJson<{ port: number }>("config.json");
|
|
93
|
+
### File system operations
|
|
97
94
|
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
95
|
+
```typescript
|
|
96
|
+
import { fsx } from "@simplysm/core-node";
|
|
97
|
+
|
|
98
|
+
// Read/write files
|
|
99
|
+
const content = await fsx.read("/path/to/file.txt");
|
|
100
|
+
await fsx.write("/path/to/output.txt", "hello");
|
|
101
|
+
|
|
102
|
+
// JSON
|
|
103
|
+
const data = await fsx.readJson<{ name: string }>("/path/to/config.json");
|
|
104
|
+
await fsx.writeJson("/path/to/out.json", data, { space: 2 });
|
|
105
|
+
|
|
106
|
+
// Copy with filter
|
|
107
|
+
await fsx.copy("/src", "/dest", (p) => !p.endsWith(".tmp"));
|
|
108
|
+
|
|
109
|
+
// Glob
|
|
110
|
+
const tsFiles = await fsx.glob("/project/src/**/*.ts");
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
### Path utilities
|
|
114
|
+
|
|
115
|
+
```typescript
|
|
116
|
+
import { pathx } from "@simplysm/core-node";
|
|
117
|
+
|
|
118
|
+
const p = pathx.posix("C:\\Users\\test"); // "C:/Users/test"
|
|
119
|
+
const name = pathx.basenameWithoutExt("file.spec.ts"); // "file.spec"
|
|
101
120
|
const isChild = pathx.isChildPath("/a/b/c", "/a/b"); // true
|
|
121
|
+
const norm = pathx.norm("/some/path"); // NormPath
|
|
122
|
+
```
|
|
102
123
|
|
|
103
|
-
|
|
124
|
+
### File watcher
|
|
125
|
+
|
|
126
|
+
```typescript
|
|
104
127
|
import { FsWatcher } from "@simplysm/core-node";
|
|
105
128
|
|
|
106
129
|
const watcher = await FsWatcher.watch(["src/**/*.ts"]);
|
|
@@ -109,17 +132,23 @@ watcher.onChange({ delay: 300 }, (changes) => {
|
|
|
109
132
|
console.log(`${event}: ${path}`);
|
|
110
133
|
}
|
|
111
134
|
});
|
|
135
|
+
|
|
112
136
|
await watcher.close();
|
|
137
|
+
```
|
|
113
138
|
|
|
114
|
-
|
|
115
|
-
import { Worker, createWorker } from "@simplysm/core-node";
|
|
139
|
+
### Worker threads
|
|
116
140
|
|
|
141
|
+
```typescript
|
|
117
142
|
// worker.ts
|
|
143
|
+
import { createWorker } from "@simplysm/core-node";
|
|
144
|
+
|
|
118
145
|
export default createWorker({
|
|
119
146
|
add: (a: number, b: number) => a + b,
|
|
120
147
|
});
|
|
121
148
|
|
|
122
149
|
// main.ts
|
|
150
|
+
import { Worker } from "@simplysm/core-node";
|
|
151
|
+
|
|
123
152
|
const worker = Worker.create<typeof import("./worker")>("./worker.ts");
|
|
124
153
|
const result = await worker.add(10, 20); // 30
|
|
125
154
|
await worker.terminate();
|
package/docs/fs-watcher.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
# FsWatcher
|
|
1
|
+
# FsWatcher
|
|
2
2
|
|
|
3
|
-
Chokidar-based file system watcher with debounced event
|
|
3
|
+
Chokidar-based file system watcher with debounced event merging. Short-lived events on the same file are consolidated into a single callback invocation.
|
|
4
4
|
|
|
5
5
|
```ts
|
|
6
6
|
import { FsWatcher } from "@simplysm/core-node";
|
|
@@ -17,7 +17,7 @@ type FsWatcherEvent = "add" | "addDir" | "change" | "unlink" | "unlinkDir"
|
|
|
17
17
|
|
|
18
18
|
Supported file change event types.
|
|
19
19
|
|
|
20
|
-
### FsWatcherChangeInfo
|
|
20
|
+
### FsWatcherChangeInfo
|
|
21
21
|
|
|
22
22
|
```ts
|
|
23
23
|
interface FsWatcherChangeInfo {
|
|
@@ -31,17 +31,17 @@ interface FsWatcherChangeInfo {
|
|
|
31
31
|
| `event` | `FsWatcherEvent` | The type of change event |
|
|
32
32
|
| `path` | `NormPath` | Normalized absolute path of the changed file/directory |
|
|
33
33
|
|
|
34
|
-
## FsWatcher
|
|
34
|
+
## FsWatcher
|
|
35
35
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
Event merging strategy:
|
|
36
|
+
Debounced file system watcher. Events occurring within the debounce window are merged using the following strategy:
|
|
39
37
|
- `add` + `change` --> `add` (modification right after creation is treated as creation)
|
|
40
38
|
- `add` + `unlink` --> removed (creation then deletion cancels out)
|
|
41
39
|
- `unlink` + `add` --> `add` (deletion then recreation is treated as creation)
|
|
42
40
|
- Other combinations --> latest event wins
|
|
43
41
|
|
|
44
|
-
|
|
42
|
+
The constructor is private; use the static `watch` method.
|
|
43
|
+
|
|
44
|
+
**Note:** `ignoreInitial` is always forced to `true` internally. If you pass `ignoreInitial: false`, the first `onChange` callback fires with an empty array (initial file listing is not included).
|
|
45
45
|
|
|
46
46
|
### Static Methods
|
|
47
47
|
|
|
@@ -54,14 +54,12 @@ static async watch(
|
|
|
54
54
|
): Promise<FsWatcher>
|
|
55
55
|
```
|
|
56
56
|
|
|
57
|
-
Start watching files/directories. Resolves when
|
|
57
|
+
Start watching files/directories (async). Resolves when chokidar emits the `ready` event.
|
|
58
58
|
|
|
59
59
|
| Parameter | Type | Description |
|
|
60
60
|
|-----------|------|-------------|
|
|
61
|
-
| `paths` | `string[]` | File
|
|
62
|
-
| `options` | `chokidar.ChokidarOptions
|
|
63
|
-
|
|
64
|
-
Glob patterns in `paths` are automatically decomposed: the base directory is watched, and events are filtered by the glob pattern using minimatch.
|
|
61
|
+
| `paths` | `string[]` | File/directory paths or glob patterns to watch |
|
|
62
|
+
| `options` | `chokidar.ChokidarOptions` | Chokidar options (except `ignoreInitial`, which is forced `true`) |
|
|
65
63
|
|
|
66
64
|
### Instance Methods
|
|
67
65
|
|
|
@@ -74,14 +72,14 @@ onChange(
|
|
|
74
72
|
): this
|
|
75
73
|
```
|
|
76
74
|
|
|
77
|
-
Register a change event handler. Events are collected for the specified delay
|
|
75
|
+
Register a file change event handler. Events are collected for the specified delay, then delivered as a batch.
|
|
78
76
|
|
|
79
77
|
| Parameter | Type | Description |
|
|
80
78
|
|-----------|------|-------------|
|
|
81
|
-
| `opt.delay` | `number
|
|
79
|
+
| `opt.delay` | `number` | Debounce delay in milliseconds |
|
|
82
80
|
| `cb` | `(changeInfos: FsWatcherChangeInfo[]) => void \| Promise<void>` | Callback receiving batched change events |
|
|
83
81
|
|
|
84
|
-
Returns `this` for chaining.
|
|
82
|
+
**Returns:** `this` for chaining.
|
|
85
83
|
|
|
86
84
|
#### close
|
|
87
85
|
|
|
@@ -89,7 +87,7 @@ Returns `this` for chaining.
|
|
|
89
87
|
async close(): Promise<void>
|
|
90
88
|
```
|
|
91
89
|
|
|
92
|
-
Stop
|
|
90
|
+
Stop the watcher and dispose all debounce queues.
|
|
93
91
|
|
|
94
92
|
## Usage
|
|
95
93
|
|
package/docs/fsx.md
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
|
-
# fsx
|
|
1
|
+
# fsx
|
|
2
2
|
|
|
3
|
-
Namespace
|
|
3
|
+
Namespace of enhanced file system functions. All functions wrap Node.js `fs` with error handling (wrapping in `SdError`) and convenience features like recursive directory creation. Most functions come in sync/async pairs.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
Imported as:
|
|
6
|
+
```typescript
|
|
6
7
|
import { fsx } from "@simplysm/core-node";
|
|
7
8
|
```
|
|
8
9
|
|
|
9
|
-
##
|
|
10
|
+
## Existence
|
|
10
11
|
|
|
11
12
|
### existsSync
|
|
12
13
|
|
|
@@ -14,7 +15,7 @@ import { fsx } from "@simplysm/core-node";
|
|
|
14
15
|
function existsSync(targetPath: string): boolean
|
|
15
16
|
```
|
|
16
17
|
|
|
17
|
-
Check if a file or directory exists (
|
|
18
|
+
Check if a file or directory exists (sync).
|
|
18
19
|
|
|
19
20
|
### exists
|
|
20
21
|
|
|
@@ -22,7 +23,9 @@ Check if a file or directory exists (synchronous).
|
|
|
22
23
|
async function exists(targetPath: string): Promise<boolean>
|
|
23
24
|
```
|
|
24
25
|
|
|
25
|
-
Check if a file or directory exists (
|
|
26
|
+
Check if a file or directory exists (async).
|
|
27
|
+
|
|
28
|
+
## Directory Creation
|
|
26
29
|
|
|
27
30
|
### mkdirSync
|
|
28
31
|
|
|
@@ -30,7 +33,7 @@ Check if a file or directory exists (asynchronous).
|
|
|
30
33
|
function mkdirSync(targetPath: string): void
|
|
31
34
|
```
|
|
32
35
|
|
|
33
|
-
Create a directory recursively (
|
|
36
|
+
Create a directory recursively (sync).
|
|
34
37
|
|
|
35
38
|
### mkdir
|
|
36
39
|
|
|
@@ -38,7 +41,9 @@ Create a directory recursively (synchronous). Equivalent to `mkdir -p`.
|
|
|
38
41
|
async function mkdir(targetPath: string): Promise<void>
|
|
39
42
|
```
|
|
40
43
|
|
|
41
|
-
Create a directory recursively (
|
|
44
|
+
Create a directory recursively (async).
|
|
45
|
+
|
|
46
|
+
## Removal
|
|
42
47
|
|
|
43
48
|
### rmSync
|
|
44
49
|
|
|
@@ -46,7 +51,7 @@ Create a directory recursively (asynchronous). Equivalent to `mkdir -p`.
|
|
|
46
51
|
function rmSync(targetPath: string): void
|
|
47
52
|
```
|
|
48
53
|
|
|
49
|
-
Remove a file or directory
|
|
54
|
+
Remove a file or directory (sync). No retry -- fails immediately on error.
|
|
50
55
|
|
|
51
56
|
### rm
|
|
52
57
|
|
|
@@ -54,7 +59,9 @@ Remove a file or directory recursively (synchronous). Does not retry on failure
|
|
|
54
59
|
async function rm(targetPath: string): Promise<void>
|
|
55
60
|
```
|
|
56
61
|
|
|
57
|
-
Remove a file or directory
|
|
62
|
+
Remove a file or directory (async). Retries up to 6 times with 500ms delay for transient errors (e.g., file locks).
|
|
63
|
+
|
|
64
|
+
## Copy
|
|
58
65
|
|
|
59
66
|
### copySync
|
|
60
67
|
|
|
@@ -66,12 +73,13 @@ function copySync(
|
|
|
66
73
|
): void
|
|
67
74
|
```
|
|
68
75
|
|
|
69
|
-
Copy a file or directory
|
|
76
|
+
Copy a file or directory (sync). If `sourcePath` does not exist, returns silently. Directories are copied recursively. An optional filter function controls which children are included.
|
|
70
77
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
78
|
+
| Parameter | Type | Description |
|
|
79
|
+
|-----------|------|-------------|
|
|
80
|
+
| `sourcePath` | `string` | Source path to copy |
|
|
81
|
+
| `targetPath` | `string` | Destination path |
|
|
82
|
+
| `filter` | `(absolutePath: string) => boolean` | Optional filter applied to all children (not the root). Return `true` to include. If a directory returns `false`, it and all contents are skipped. |
|
|
75
83
|
|
|
76
84
|
### copy
|
|
77
85
|
|
|
@@ -83,7 +91,9 @@ async function copy(
|
|
|
83
91
|
): Promise<void>
|
|
84
92
|
```
|
|
85
93
|
|
|
86
|
-
Copy a file or directory
|
|
94
|
+
Copy a file or directory (async). Same behavior as `copySync`.
|
|
95
|
+
|
|
96
|
+
## File Reading
|
|
87
97
|
|
|
88
98
|
### readSync
|
|
89
99
|
|
|
@@ -91,7 +101,7 @@ Copy a file or directory recursively (asynchronous). Same semantics as `copySync
|
|
|
91
101
|
function readSync(targetPath: string): string
|
|
92
102
|
```
|
|
93
103
|
|
|
94
|
-
Read a file as a UTF-8 string (
|
|
104
|
+
Read a file as a UTF-8 string (sync).
|
|
95
105
|
|
|
96
106
|
### read
|
|
97
107
|
|
|
@@ -99,7 +109,7 @@ Read a file as a UTF-8 string (synchronous).
|
|
|
99
109
|
async function read(targetPath: string): Promise<string>
|
|
100
110
|
```
|
|
101
111
|
|
|
102
|
-
Read a file as a UTF-8 string (
|
|
112
|
+
Read a file as a UTF-8 string (async).
|
|
103
113
|
|
|
104
114
|
### readBufferSync
|
|
105
115
|
|
|
@@ -107,7 +117,7 @@ Read a file as a UTF-8 string (asynchronous).
|
|
|
107
117
|
function readBufferSync(targetPath: string): Buffer
|
|
108
118
|
```
|
|
109
119
|
|
|
110
|
-
Read a file as a
|
|
120
|
+
Read a file as a Buffer (sync).
|
|
111
121
|
|
|
112
122
|
### readBuffer
|
|
113
123
|
|
|
@@ -115,7 +125,7 @@ Read a file as a `Buffer` (synchronous).
|
|
|
115
125
|
async function readBuffer(targetPath: string): Promise<Buffer>
|
|
116
126
|
```
|
|
117
127
|
|
|
118
|
-
Read a file as a
|
|
128
|
+
Read a file as a Buffer (async).
|
|
119
129
|
|
|
120
130
|
### readJsonSync
|
|
121
131
|
|
|
@@ -123,7 +133,7 @@ Read a file as a `Buffer` (asynchronous).
|
|
|
123
133
|
function readJsonSync<TData = unknown>(targetPath: string): TData
|
|
124
134
|
```
|
|
125
135
|
|
|
126
|
-
Read and parse a JSON file using `
|
|
136
|
+
Read and parse a JSON file using `JsonConvert` (sync). On parse failure, the error message includes a preview of the file contents.
|
|
127
137
|
|
|
128
138
|
### readJson
|
|
129
139
|
|
|
@@ -131,7 +141,9 @@ Read and parse a JSON file using `json.parse` from `@simplysm/core-common` (sync
|
|
|
131
141
|
async function readJson<TData = unknown>(targetPath: string): Promise<TData>
|
|
132
142
|
```
|
|
133
143
|
|
|
134
|
-
Read and parse a JSON file using `
|
|
144
|
+
Read and parse a JSON file using `JsonConvert` (async). On parse failure, the error message includes a preview of the file contents.
|
|
145
|
+
|
|
146
|
+
## File Writing
|
|
135
147
|
|
|
136
148
|
### writeSync
|
|
137
149
|
|
|
@@ -139,7 +151,7 @@ Read and parse a JSON file using `json.parse` from `@simplysm/core-common` (asyn
|
|
|
139
151
|
function writeSync(targetPath: string, data: string | Uint8Array): void
|
|
140
152
|
```
|
|
141
153
|
|
|
142
|
-
Write data to a file (
|
|
154
|
+
Write data to a file (sync). Parent directories are created automatically. Uses `flush: true`.
|
|
143
155
|
|
|
144
156
|
### write
|
|
145
157
|
|
|
@@ -147,7 +159,7 @@ Write data to a file (synchronous). Parent directories are created automatically
|
|
|
147
159
|
async function write(targetPath: string, data: string | Uint8Array): Promise<void>
|
|
148
160
|
```
|
|
149
161
|
|
|
150
|
-
Write data to a file (
|
|
162
|
+
Write data to a file (async). Parent directories are created automatically. Uses `flush: true`.
|
|
151
163
|
|
|
152
164
|
### writeJsonSync
|
|
153
165
|
|
|
@@ -162,12 +174,14 @@ function writeJsonSync(
|
|
|
162
174
|
): void
|
|
163
175
|
```
|
|
164
176
|
|
|
165
|
-
|
|
177
|
+
Write data as a JSON file using `JsonConvert` (sync).
|
|
166
178
|
|
|
167
|
-
|
|
|
168
|
-
|
|
169
|
-
| `
|
|
170
|
-
| `
|
|
179
|
+
| Parameter | Type | Description |
|
|
180
|
+
|-----------|------|-------------|
|
|
181
|
+
| `targetPath` | `string` | Target file path |
|
|
182
|
+
| `data` | `unknown` | Data to serialize |
|
|
183
|
+
| `options.replacer` | `function` | Custom JSON replacer |
|
|
184
|
+
| `options.space` | `string \| number` | Indentation |
|
|
171
185
|
|
|
172
186
|
### writeJson
|
|
173
187
|
|
|
@@ -182,7 +196,9 @@ async function writeJson(
|
|
|
182
196
|
): Promise<void>
|
|
183
197
|
```
|
|
184
198
|
|
|
185
|
-
|
|
199
|
+
Write data as a JSON file using `JsonConvert` (async). Same options as `writeJsonSync`.
|
|
200
|
+
|
|
201
|
+
## Directory Reading
|
|
186
202
|
|
|
187
203
|
### readdirSync
|
|
188
204
|
|
|
@@ -190,7 +206,7 @@ Serialize data to JSON and write to a file (asynchronous). Uses `json.stringify`
|
|
|
190
206
|
function readdirSync(targetPath: string): string[]
|
|
191
207
|
```
|
|
192
208
|
|
|
193
|
-
|
|
209
|
+
List the contents of a directory (sync). Returns entry names (not full paths).
|
|
194
210
|
|
|
195
211
|
### readdir
|
|
196
212
|
|
|
@@ -198,7 +214,9 @@ Read directory contents (synchronous). Returns an array of entry names.
|
|
|
198
214
|
async function readdir(targetPath: string): Promise<string[]>
|
|
199
215
|
```
|
|
200
216
|
|
|
201
|
-
|
|
217
|
+
List the contents of a directory (async). Returns entry names (not full paths).
|
|
218
|
+
|
|
219
|
+
## File Stats
|
|
202
220
|
|
|
203
221
|
### statSync
|
|
204
222
|
|
|
@@ -206,7 +224,7 @@ Read directory contents (asynchronous). Returns an array of entry names.
|
|
|
206
224
|
function statSync(targetPath: string): fs.Stats
|
|
207
225
|
```
|
|
208
226
|
|
|
209
|
-
Get file/directory
|
|
227
|
+
Get file/directory stats, following symlinks (sync).
|
|
210
228
|
|
|
211
229
|
### stat
|
|
212
230
|
|
|
@@ -214,7 +232,7 @@ Get file/directory info (synchronous). Follows symbolic links.
|
|
|
214
232
|
async function stat(targetPath: string): Promise<fs.Stats>
|
|
215
233
|
```
|
|
216
234
|
|
|
217
|
-
Get file/directory
|
|
235
|
+
Get file/directory stats, following symlinks (async).
|
|
218
236
|
|
|
219
237
|
### lstatSync
|
|
220
238
|
|
|
@@ -222,7 +240,7 @@ Get file/directory info (asynchronous). Follows symbolic links.
|
|
|
222
240
|
function lstatSync(targetPath: string): fs.Stats
|
|
223
241
|
```
|
|
224
242
|
|
|
225
|
-
Get file/directory
|
|
243
|
+
Get file/directory stats without following symlinks (sync).
|
|
226
244
|
|
|
227
245
|
### lstat
|
|
228
246
|
|
|
@@ -230,7 +248,9 @@ Get file/directory info (synchronous). Does **not** follow symbolic links.
|
|
|
230
248
|
async function lstat(targetPath: string): Promise<fs.Stats>
|
|
231
249
|
```
|
|
232
250
|
|
|
233
|
-
Get file/directory
|
|
251
|
+
Get file/directory stats without following symlinks (async).
|
|
252
|
+
|
|
253
|
+
## Glob
|
|
234
254
|
|
|
235
255
|
### globSync
|
|
236
256
|
|
|
@@ -238,7 +258,12 @@ Get file/directory info (asynchronous). Does **not** follow symbolic links.
|
|
|
238
258
|
function globSync(pattern: string, options?: GlobOptions): string[]
|
|
239
259
|
```
|
|
240
260
|
|
|
241
|
-
Search
|
|
261
|
+
Search files by glob pattern (sync). Returns absolute paths.
|
|
262
|
+
|
|
263
|
+
| Parameter | Type | Description |
|
|
264
|
+
|-----------|------|-------------|
|
|
265
|
+
| `pattern` | `string` | Glob pattern (e.g., `"**/*.ts"`) |
|
|
266
|
+
| `options` | `GlobOptions` | Options passed to the `glob` library |
|
|
242
267
|
|
|
243
268
|
### glob
|
|
244
269
|
|
|
@@ -246,7 +271,9 @@ Search for files matching a glob pattern (synchronous). Returns absolute paths.
|
|
|
246
271
|
async function glob(pattern: string, options?: GlobOptions): Promise<string[]>
|
|
247
272
|
```
|
|
248
273
|
|
|
249
|
-
Search
|
|
274
|
+
Search files by glob pattern (async). Returns absolute paths.
|
|
275
|
+
|
|
276
|
+
## Utilities
|
|
250
277
|
|
|
251
278
|
### clearEmptyDirectory
|
|
252
279
|
|
|
@@ -254,7 +281,7 @@ Search for files matching a glob pattern (asynchronous). Returns absolute paths.
|
|
|
254
281
|
async function clearEmptyDirectory(dirPath: string): Promise<void>
|
|
255
282
|
```
|
|
256
283
|
|
|
257
|
-
Recursively find and remove empty directories under
|
|
284
|
+
Recursively find and remove empty directories under a given path (async). When all subdirectories are removed and a parent becomes empty, it is also removed.
|
|
258
285
|
|
|
259
286
|
### findAllParentChildPathsSync
|
|
260
287
|
|
|
@@ -266,13 +293,13 @@ function findAllParentChildPathsSync(
|
|
|
266
293
|
): string[]
|
|
267
294
|
```
|
|
268
295
|
|
|
269
|
-
Walk from `fromPath` toward the filesystem root,
|
|
296
|
+
Walk from `fromPath` upward toward the filesystem root, running a glob pattern at each level and collecting all matches (sync).
|
|
270
297
|
|
|
271
298
|
| Parameter | Type | Description |
|
|
272
299
|
|-----------|------|-------------|
|
|
273
|
-
| `childGlob` | `string` | Glob pattern to
|
|
274
|
-
| `fromPath` | `string` | Starting path
|
|
275
|
-
| `rootPath` | `string
|
|
300
|
+
| `childGlob` | `string` | Glob pattern to search at each directory level |
|
|
301
|
+
| `fromPath` | `string` | Starting path |
|
|
302
|
+
| `rootPath` | `string` | Optional stop path (must be an ancestor of `fromPath`) |
|
|
276
303
|
|
|
277
304
|
### findAllParentChildPaths
|
|
278
305
|
|
|
@@ -284,4 +311,4 @@ async function findAllParentChildPaths(
|
|
|
284
311
|
): Promise<string[]>
|
|
285
312
|
```
|
|
286
313
|
|
|
287
|
-
|
|
314
|
+
Same as `findAllParentChildPathsSync` but asynchronous.
|
package/docs/pathx.md
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
# pathx
|
|
1
|
+
# pathx
|
|
2
2
|
|
|
3
|
-
Namespace
|
|
3
|
+
Namespace of path manipulation utilities. Provides normalized path types, POSIX conversion, and directory-based filtering.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
Imported as:
|
|
6
|
+
```typescript
|
|
6
7
|
import { pathx } from "@simplysm/core-node";
|
|
7
8
|
```
|
|
8
9
|
|
|
@@ -14,7 +15,7 @@ import { pathx } from "@simplysm/core-node";
|
|
|
14
15
|
type NormPath = string & { [NORM]: never }
|
|
15
16
|
```
|
|
16
17
|
|
|
17
|
-
Branded type representing a normalized absolute path. Can only be created via `norm()`.
|
|
18
|
+
Branded string type representing a normalized, resolved absolute path. Can only be created via `norm()`.
|
|
18
19
|
|
|
19
20
|
## Functions
|
|
20
21
|
|
|
@@ -24,7 +25,7 @@ Branded type representing a normalized absolute path. Can only be created via `n
|
|
|
24
25
|
function posix(...args: string[]): string
|
|
25
26
|
```
|
|
26
27
|
|
|
27
|
-
Convert a path to POSIX style
|
|
28
|
+
Convert a path to POSIX style (backslashes replaced with forward slashes). Accepts multiple segments which are joined with `path.join`.
|
|
28
29
|
|
|
29
30
|
```ts
|
|
30
31
|
pathx.posix("C:\\Users\\test"); // "C:/Users/test"
|
|
@@ -41,7 +42,15 @@ function changeFileDirectory(
|
|
|
41
42
|
): string
|
|
42
43
|
```
|
|
43
44
|
|
|
44
|
-
Change the directory
|
|
45
|
+
Change the parent directory of a file path. The file must be inside `fromDirectory`.
|
|
46
|
+
|
|
47
|
+
| Parameter | Type | Description |
|
|
48
|
+
|-----------|------|-------------|
|
|
49
|
+
| `filePath` | `string` | File path to transform |
|
|
50
|
+
| `fromDirectory` | `string` | Original parent directory |
|
|
51
|
+
| `toDirectory` | `string` | New parent directory |
|
|
52
|
+
|
|
53
|
+
**Throws:** `ArgumentError` if `filePath` is not inside `fromDirectory`.
|
|
45
54
|
|
|
46
55
|
```ts
|
|
47
56
|
pathx.changeFileDirectory("/a/b/c.txt", "/a", "/x");
|
|
@@ -54,7 +63,7 @@ pathx.changeFileDirectory("/a/b/c.txt", "/a", "/x");
|
|
|
54
63
|
function basenameWithoutExt(filePath: string): string
|
|
55
64
|
```
|
|
56
65
|
|
|
57
|
-
|
|
66
|
+
Get the filename without its extension (last extension only).
|
|
58
67
|
|
|
59
68
|
```ts
|
|
60
69
|
pathx.basenameWithoutExt("file.txt"); // "file"
|
|
@@ -81,7 +90,7 @@ pathx.isChildPath("/a/b", "/a/b"); // false (same path)
|
|
|
81
90
|
function norm(...paths: string[]): NormPath
|
|
82
91
|
```
|
|
83
92
|
|
|
84
|
-
Normalize and resolve path segments into
|
|
93
|
+
Normalize and resolve path segments into a `NormPath` (absolute, platform-native separators).
|
|
85
94
|
|
|
86
95
|
```ts
|
|
87
96
|
pathx.norm("/some/path"); // NormPath
|
|
@@ -98,16 +107,14 @@ function filterByTargets(
|
|
|
98
107
|
): string[]
|
|
99
108
|
```
|
|
100
109
|
|
|
101
|
-
Filter file paths
|
|
110
|
+
Filter a list of file paths to only include those under the specified target directories. If `targets` is empty, all files are returned.
|
|
102
111
|
|
|
103
112
|
| Parameter | Type | Description |
|
|
104
113
|
|-----------|------|-------------|
|
|
105
|
-
| `files` | `string[]` | Absolute file paths to filter (
|
|
106
|
-
| `targets` | `string[]` | Target directory
|
|
114
|
+
| `files` | `string[]` | Absolute file paths to filter (should be under `cwd`) |
|
|
115
|
+
| `targets` | `string[]` | Target directory paths relative to `cwd` (POSIX style recommended) |
|
|
107
116
|
| `cwd` | `string` | Current working directory (absolute path) |
|
|
108
117
|
|
|
109
|
-
Returns `files` unchanged if `targets` is empty. Otherwise returns only files whose relative path matches or is a child of a target.
|
|
110
|
-
|
|
111
118
|
```ts
|
|
112
119
|
const files = ["/proj/src/a.ts", "/proj/src/b.ts", "/proj/tests/c.ts"];
|
|
113
120
|
pathx.filterByTargets(files, ["src"], "/proj");
|
package/docs/worker.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
# Worker
|
|
1
|
+
# Worker
|
|
2
2
|
|
|
3
|
-
Type-safe worker thread
|
|
3
|
+
Type-safe worker thread abstraction built on Node.js `worker_threads`. Provides a Proxy-based API where worker methods are called as if they were local async functions.
|
|
4
4
|
|
|
5
5
|
```ts
|
|
6
6
|
import { Worker, createWorker } from "@simplysm/core-node";
|
|
@@ -15,7 +15,7 @@ import type {
|
|
|
15
15
|
|
|
16
16
|
## Types
|
|
17
17
|
|
|
18
|
-
### WorkerModule
|
|
18
|
+
### WorkerModule
|
|
19
19
|
|
|
20
20
|
```ts
|
|
21
21
|
interface WorkerModule {
|
|
@@ -26,7 +26,7 @@ interface WorkerModule {
|
|
|
26
26
|
}
|
|
27
27
|
```
|
|
28
28
|
|
|
29
|
-
Type structure
|
|
29
|
+
Type structure that `createWorker()` returns. Used for type inference in `Worker.create<typeof import("./worker")>()`.
|
|
30
30
|
|
|
31
31
|
| Field | Type | Description |
|
|
32
32
|
|-------|------|-------------|
|
|
@@ -43,7 +43,7 @@ type PromisifyMethods<TMethods> = {
|
|
|
43
43
|
}
|
|
44
44
|
```
|
|
45
45
|
|
|
46
|
-
|
|
46
|
+
Mapped type that wraps all method return types with `Promise<Awaited<R>>`. Worker methods always return promises because they communicate via `postMessage`.
|
|
47
47
|
|
|
48
48
|
### WorkerProxy
|
|
49
49
|
|
|
@@ -65,7 +65,7 @@ type WorkerProxy<TModule extends WorkerModule> = PromisifyMethods<
|
|
|
65
65
|
}
|
|
66
66
|
```
|
|
67
67
|
|
|
68
|
-
The
|
|
68
|
+
The type returned by `Worker.create()`. Combines promisified methods with event subscription and termination.
|
|
69
69
|
|
|
70
70
|
| Method | Description |
|
|
71
71
|
|--------|-------------|
|
|
@@ -73,7 +73,7 @@ The proxy type returned by `Worker.create()`. Combines promisified worker method
|
|
|
73
73
|
| `off(event, listener)` | Remove an event listener |
|
|
74
74
|
| `terminate()` | Terminate the worker thread |
|
|
75
75
|
|
|
76
|
-
### WorkerRequest
|
|
76
|
+
### WorkerRequest
|
|
77
77
|
|
|
78
78
|
```ts
|
|
79
79
|
interface WorkerRequest {
|
|
@@ -91,7 +91,7 @@ Internal message format sent from the main thread to the worker.
|
|
|
91
91
|
| `method` | `string` | Name of the method to invoke |
|
|
92
92
|
| `params` | `unknown[]` | Method arguments |
|
|
93
93
|
|
|
94
|
-
### WorkerResponse
|
|
94
|
+
### WorkerResponse
|
|
95
95
|
|
|
96
96
|
```ts
|
|
97
97
|
type WorkerResponse =
|
|
@@ -101,7 +101,7 @@ type WorkerResponse =
|
|
|
101
101
|
| { type: "log"; body: string }
|
|
102
102
|
```
|
|
103
103
|
|
|
104
|
-
Internal message format sent from the worker to the main thread.
|
|
104
|
+
Internal message format sent from the worker to the main thread. Discriminated union on `type`.
|
|
105
105
|
|
|
106
106
|
| Variant | Fields | Description |
|
|
107
107
|
|---------|--------|-------------|
|
|
@@ -110,9 +110,9 @@ Internal message format sent from the worker to the main thread. A discriminated
|
|
|
110
110
|
| `event` | `event`, `body?` | Worker-emitted event |
|
|
111
111
|
| `log` | `body` | Redirected stdout output |
|
|
112
112
|
|
|
113
|
-
## Worker
|
|
113
|
+
## Worker
|
|
114
114
|
|
|
115
|
-
|
|
115
|
+
Static factory object for creating type-safe worker proxies.
|
|
116
116
|
|
|
117
117
|
### Worker.create
|
|
118
118
|
|
|
@@ -130,11 +130,11 @@ Create a type-safe worker proxy.
|
|
|
130
130
|
| `filePath` | `string` | Worker file path (`file://` URL or absolute path) |
|
|
131
131
|
| `opt` | `Omit<WorkerRawOptions, "stdout" \| "stderr">?` | Node.js `WorkerOptions` (excluding stdout/stderr which are managed internally) |
|
|
132
132
|
|
|
133
|
-
In development (`.ts` files), the worker is
|
|
133
|
+
In development (`.ts` files), the worker is run through `tsx`. In production (`.js` files), the worker is created directly.
|
|
134
134
|
|
|
135
|
-
|
|
135
|
+
**Returns:** `WorkerProxy<TModule>` -- Proxy object supporting direct method calls, `on()`, `off()`, and `terminate()`.
|
|
136
136
|
|
|
137
|
-
## createWorker
|
|
137
|
+
## createWorker
|
|
138
138
|
|
|
139
139
|
```ts
|
|
140
140
|
function createWorker<
|
|
@@ -152,7 +152,7 @@ function createWorker<
|
|
|
152
152
|
}
|
|
153
153
|
```
|
|
154
154
|
|
|
155
|
-
|
|
155
|
+
Factory function used inside the worker thread file. Registers method handlers and sets up the message protocol. Returns a sender object for emitting events back to the main thread.
|
|
156
156
|
|
|
157
157
|
| Parameter | Type | Description |
|
|
158
158
|
|-----------|------|-------------|
|
|
@@ -166,7 +166,7 @@ The returned object exposes:
|
|
|
166
166
|
| `__methods` | Type-level reference to the methods map (used for type inference) |
|
|
167
167
|
| `__events` | Type-level reference to the events map (used for type inference) |
|
|
168
168
|
|
|
169
|
-
Throws if not running in a worker thread (`parentPort`
|
|
169
|
+
**Throws:** `SdError` if not running in a worker thread (no `parentPort`).
|
|
170
170
|
|
|
171
171
|
## Usage
|
|
172
172
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@simplysm/core-node",
|
|
3
|
-
"version": "14.0.
|
|
3
|
+
"version": "14.0.5",
|
|
4
4
|
"description": "심플리즘 패키지 - 코어 (node)",
|
|
5
5
|
"author": "심플리즘",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
"glob": "^13.0.6",
|
|
26
26
|
"minimatch": "^10.2.4",
|
|
27
27
|
"tsx": "^4.21.0",
|
|
28
|
-
"@simplysm/core-common": "14.0.
|
|
28
|
+
"@simplysm/core-common": "14.0.5"
|
|
29
29
|
},
|
|
30
30
|
"devDependencies": {
|
|
31
31
|
"@types/node": "^20.19.37"
|