@simplysm/core-node 13.0.76 → 13.0.78
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 +353 -44
- package/dist/features/fs-watcher.d.ts.map +1 -1
- package/dist/features/fs-watcher.js +2 -2
- package/dist/features/fs-watcher.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +6 -2
- package/dist/index.js.map +1 -1
- package/dist/utils/fs.d.ts +30 -30
- package/dist/utils/fs.d.ts.map +1 -1
- package/dist/utils/fs.js +101 -99
- package/dist/utils/fs.js.map +1 -1
- package/dist/utils/path.d.ts +19 -19
- package/dist/utils/path.d.ts.map +1 -1
- package/dist/utils/path.js +17 -17
- package/dist/utils/path.js.map +1 -1
- package/dist/worker/create-worker.d.ts +1 -1
- package/dist/worker/create-worker.d.ts.map +1 -1
- package/dist/worker/create-worker.js +8 -8
- package/dist/worker/create-worker.js.map +1 -1
- package/dist/worker/types.d.ts +2 -2
- package/dist/worker/types.d.ts.map +1 -1
- package/dist/worker/worker.js +4 -4
- package/dist/worker/worker.js.map +1 -1
- package/package.json +2 -2
- package/src/features/fs-watcher.ts +2 -2
- package/src/index.ts +2 -2
- package/src/utils/fs.ts +570 -562
- package/src/utils/path.ts +24 -24
- package/src/worker/create-worker.ts +9 -9
- package/src/worker/types.ts +6 -6
- package/src/worker/worker.ts +4 -4
- package/tests/utils/fs.spec.ts +123 -123
- package/tests/utils/path.spec.ts +55 -55
package/src/utils/path.ts
CHANGED
|
@@ -7,7 +7,7 @@ const NORM = Symbol("NormPath");
|
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
9
|
* Brand type representing a normalized path.
|
|
10
|
-
* Can only be created through
|
|
10
|
+
* Can only be created through norm().
|
|
11
11
|
*/
|
|
12
12
|
export type NormPath = string & {
|
|
13
13
|
[NORM]: never;
|
|
@@ -21,10 +21,10 @@ export type NormPath = string & {
|
|
|
21
21
|
* Converts to POSIX-style path (backslash → forward slash).
|
|
22
22
|
*
|
|
23
23
|
* @example
|
|
24
|
-
*
|
|
25
|
-
*
|
|
24
|
+
* posix("C:\\Users\\test"); // "C:/Users/test"
|
|
25
|
+
* posix("src", "index.ts"); // "src/index.ts"
|
|
26
26
|
*/
|
|
27
|
-
export function
|
|
27
|
+
export function posix(...args: string[]): string {
|
|
28
28
|
const resolvedPath = path.join(...args);
|
|
29
29
|
return resolvedPath.replace(/\\/g, "/");
|
|
30
30
|
}
|
|
@@ -33,12 +33,12 @@ export function pathPosix(...args: string[]): string {
|
|
|
33
33
|
* Changes the directory of a file path.
|
|
34
34
|
*
|
|
35
35
|
* @example
|
|
36
|
-
*
|
|
36
|
+
* changeFileDirectory("/a/b/c.txt", "/a", "/x");
|
|
37
37
|
* // → "/x/b/c.txt"
|
|
38
38
|
*
|
|
39
39
|
* @throws Error if the file is not inside fromDirectory
|
|
40
40
|
*/
|
|
41
|
-
export function
|
|
41
|
+
export function changeFileDirectory(
|
|
42
42
|
filePath: string,
|
|
43
43
|
fromDirectory: string,
|
|
44
44
|
toDirectory: string,
|
|
@@ -47,7 +47,7 @@ export function pathChangeFileDirectory(
|
|
|
47
47
|
return toDirectory;
|
|
48
48
|
}
|
|
49
49
|
|
|
50
|
-
if (!
|
|
50
|
+
if (!isChildPath(filePath, fromDirectory)) {
|
|
51
51
|
throw new ArgumentError(`'${filePath}' is not inside ${fromDirectory}.`, {
|
|
52
52
|
filePath,
|
|
53
53
|
fromDirectory,
|
|
@@ -61,10 +61,10 @@ export function pathChangeFileDirectory(
|
|
|
61
61
|
* Returns the filename (basename) without extension.
|
|
62
62
|
*
|
|
63
63
|
* @example
|
|
64
|
-
*
|
|
65
|
-
*
|
|
64
|
+
* basenameWithoutExt("file.txt"); // "file"
|
|
65
|
+
* basenameWithoutExt("/path/to/file.spec.ts"); // "file.spec"
|
|
66
66
|
*/
|
|
67
|
-
export function
|
|
67
|
+
export function basenameWithoutExt(filePath: string): string {
|
|
68
68
|
return path.basename(filePath, path.extname(filePath));
|
|
69
69
|
}
|
|
70
70
|
|
|
@@ -72,17 +72,17 @@ export function pathBasenameWithoutExt(filePath: string): string {
|
|
|
72
72
|
* Checks if childPath is a child path of parentPath.
|
|
73
73
|
* Returns false if the paths are the same.
|
|
74
74
|
*
|
|
75
|
-
* Paths are internally normalized using `
|
|
75
|
+
* Paths are internally normalized using `norm()` and compared using
|
|
76
76
|
* platform-specific path separators (Windows: `\`, Unix: `/`).
|
|
77
77
|
*
|
|
78
78
|
* @example
|
|
79
|
-
*
|
|
80
|
-
*
|
|
81
|
-
*
|
|
79
|
+
* isChildPath("/a/b/c", "/a/b"); // true
|
|
80
|
+
* isChildPath("/a/b", "/a/b/c"); // false
|
|
81
|
+
* isChildPath("/a/b", "/a/b"); // false (same path)
|
|
82
82
|
*/
|
|
83
|
-
export function
|
|
84
|
-
const normalizedChild =
|
|
85
|
-
const normalizedParent =
|
|
83
|
+
export function isChildPath(childPath: string, parentPath: string): boolean {
|
|
84
|
+
const normalizedChild = norm(childPath);
|
|
85
|
+
const normalizedParent = norm(parentPath);
|
|
86
86
|
|
|
87
87
|
// Same path returns false
|
|
88
88
|
if (normalizedChild === normalizedParent) {
|
|
@@ -102,10 +102,10 @@ export function pathIsChildPath(childPath: string, parentPath: string): boolean
|
|
|
102
102
|
* Converts to absolute path and normalizes using platform-specific separators.
|
|
103
103
|
*
|
|
104
104
|
* @example
|
|
105
|
-
*
|
|
106
|
-
*
|
|
105
|
+
* norm("/some/path"); // NormPath
|
|
106
|
+
* norm("relative", "path"); // NormPath (converted to absolute path)
|
|
107
107
|
*/
|
|
108
|
-
export function
|
|
108
|
+
export function norm(...paths: string[]): NormPath {
|
|
109
109
|
return path.resolve(...paths) as NormPath;
|
|
110
110
|
}
|
|
111
111
|
|
|
@@ -122,14 +122,14 @@ export function pathNorm(...paths: string[]): NormPath {
|
|
|
122
122
|
*
|
|
123
123
|
* @example
|
|
124
124
|
* const files = ["/proj/src/a.ts", "/proj/src/b.ts", "/proj/tests/c.ts"];
|
|
125
|
-
*
|
|
125
|
+
* filterByTargets(files, ["src"], "/proj");
|
|
126
126
|
* // → ["/proj/src/a.ts", "/proj/src/b.ts"]
|
|
127
127
|
*/
|
|
128
|
-
export function
|
|
128
|
+
export function filterByTargets(files: string[], targets: string[], cwd: string): string[] {
|
|
129
129
|
if (targets.length === 0) return files;
|
|
130
|
-
const normalizedTargets = targets.map((t) =>
|
|
130
|
+
const normalizedTargets = targets.map((t) => posix(t));
|
|
131
131
|
return files.filter((file) => {
|
|
132
|
-
const relativePath =
|
|
132
|
+
const relativePath = posix(path.relative(cwd, file));
|
|
133
133
|
return normalizedTargets.some(
|
|
134
134
|
(target) => relativePath === target || relativePath.startsWith(target + "/"),
|
|
135
135
|
);
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { parentPort } from "worker_threads";
|
|
2
|
-
import { SdError,
|
|
2
|
+
import { SdError, transfer } from "@simplysm/core-common";
|
|
3
3
|
import type { WorkerRequest, WorkerResponse } from "./types";
|
|
4
4
|
|
|
5
5
|
//#region createWorker
|
|
@@ -27,7 +27,7 @@ export function createWorker<
|
|
|
27
27
|
>(
|
|
28
28
|
methods: TMethods,
|
|
29
29
|
): {
|
|
30
|
-
send<
|
|
30
|
+
send<TEventName extends keyof TEvents & string>(event: TEventName, data?: TEvents[TEventName]): void;
|
|
31
31
|
__methods: TMethods;
|
|
32
32
|
__events: TEvents;
|
|
33
33
|
} {
|
|
@@ -46,7 +46,7 @@ export function createWorker<
|
|
|
46
46
|
): boolean => {
|
|
47
47
|
const body = typeof chunk === "string" ? chunk : new TextDecoder().decode(chunk);
|
|
48
48
|
const response: WorkerResponse = { type: "log", body };
|
|
49
|
-
const serialized =
|
|
49
|
+
const serialized = transfer.encode(response);
|
|
50
50
|
port.postMessage(serialized.result, serialized.transferList);
|
|
51
51
|
|
|
52
52
|
const cb = typeof encodingOrCallback === "function" ? encodingOrCallback : callback;
|
|
@@ -58,7 +58,7 @@ export function createWorker<
|
|
|
58
58
|
};
|
|
59
59
|
|
|
60
60
|
port.on("message", async (serializedRequest: unknown) => {
|
|
61
|
-
const decoded =
|
|
61
|
+
const decoded = transfer.decode(serializedRequest);
|
|
62
62
|
|
|
63
63
|
// Validate request structure
|
|
64
64
|
if (
|
|
@@ -79,7 +79,7 @@ export function createWorker<
|
|
|
79
79
|
request: { id: "unknown", method: "unknown", params: [] },
|
|
80
80
|
body: new SdError(`Invalid worker request format: ${decodedStr}`),
|
|
81
81
|
};
|
|
82
|
-
const serialized =
|
|
82
|
+
const serialized = transfer.encode(errorResponse);
|
|
83
83
|
port.postMessage(serialized.result, serialized.transferList);
|
|
84
84
|
return;
|
|
85
85
|
}
|
|
@@ -94,7 +94,7 @@ export function createWorker<
|
|
|
94
94
|
body: new SdError(`Unknown method: ${request.method}`),
|
|
95
95
|
};
|
|
96
96
|
|
|
97
|
-
const serialized =
|
|
97
|
+
const serialized = transfer.encode(response);
|
|
98
98
|
port.postMessage(serialized.result, serialized.transferList);
|
|
99
99
|
return;
|
|
100
100
|
}
|
|
@@ -108,7 +108,7 @@ export function createWorker<
|
|
|
108
108
|
body: result,
|
|
109
109
|
};
|
|
110
110
|
|
|
111
|
-
const serialized =
|
|
111
|
+
const serialized = transfer.encode(response);
|
|
112
112
|
port.postMessage(serialized.result, serialized.transferList);
|
|
113
113
|
} catch (err) {
|
|
114
114
|
const response: WorkerResponse = {
|
|
@@ -117,7 +117,7 @@ export function createWorker<
|
|
|
117
117
|
body: err instanceof Error ? err : new Error(String(err)),
|
|
118
118
|
};
|
|
119
119
|
|
|
120
|
-
const serialized =
|
|
120
|
+
const serialized = transfer.encode(response);
|
|
121
121
|
port.postMessage(serialized.result, serialized.transferList);
|
|
122
122
|
}
|
|
123
123
|
});
|
|
@@ -132,7 +132,7 @@ export function createWorker<
|
|
|
132
132
|
body: data,
|
|
133
133
|
};
|
|
134
134
|
|
|
135
|
-
const serialized =
|
|
135
|
+
const serialized = transfer.encode(response);
|
|
136
136
|
port.postMessage(serialized.result, serialized.transferList);
|
|
137
137
|
},
|
|
138
138
|
};
|
package/src/worker/types.ts
CHANGED
|
@@ -35,17 +35,17 @@ export type WorkerProxy<TModule extends WorkerModule> = PromisifyMethods<
|
|
|
35
35
|
/**
|
|
36
36
|
* Registers a worker event listener.
|
|
37
37
|
*/
|
|
38
|
-
on<
|
|
39
|
-
event:
|
|
40
|
-
listener: (data: TModule["default"]["__events"][
|
|
38
|
+
on<TEventName extends keyof TModule["default"]["__events"] & string>(
|
|
39
|
+
event: TEventName,
|
|
40
|
+
listener: (data: TModule["default"]["__events"][TEventName]) => void,
|
|
41
41
|
): void;
|
|
42
42
|
|
|
43
43
|
/**
|
|
44
44
|
* Unregisters a worker event listener.
|
|
45
45
|
*/
|
|
46
|
-
off<
|
|
47
|
-
event:
|
|
48
|
-
listener: (data: TModule["default"]["__events"][
|
|
46
|
+
off<TEventName extends keyof TModule["default"]["__events"] & string>(
|
|
47
|
+
event: TEventName,
|
|
48
|
+
listener: (data: TModule["default"]["__events"][TEventName]) => void,
|
|
49
49
|
): void;
|
|
50
50
|
|
|
51
51
|
/**
|
package/src/worker/worker.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { EventEmitter,
|
|
1
|
+
import { EventEmitter, transfer, Uuid } from "@simplysm/core-common";
|
|
2
2
|
import consola from "consola";
|
|
3
3
|
import path from "path";
|
|
4
4
|
import { fileURLToPath } from "url";
|
|
@@ -85,7 +85,7 @@ class WorkerInternal extends EventEmitter<Record<string, unknown>> {
|
|
|
85
85
|
});
|
|
86
86
|
|
|
87
87
|
this._worker.on("message", (serializedResponse: unknown) => {
|
|
88
|
-
const decoded =
|
|
88
|
+
const decoded = transfer.decode(serializedResponse);
|
|
89
89
|
|
|
90
90
|
// Validate response structure
|
|
91
91
|
if (decoded == null || typeof decoded !== "object" || !("type" in decoded)) {
|
|
@@ -131,14 +131,14 @@ class WorkerInternal extends EventEmitter<Record<string, unknown>> {
|
|
|
131
131
|
call(method: string, params: unknown[]): Promise<unknown> {
|
|
132
132
|
return new Promise((resolve, reject) => {
|
|
133
133
|
const request: WorkerRequest = {
|
|
134
|
-
id: Uuid.
|
|
134
|
+
id: Uuid.generate().toString(),
|
|
135
135
|
method,
|
|
136
136
|
params,
|
|
137
137
|
};
|
|
138
138
|
|
|
139
139
|
this._pendingRequests.set(request.id, { method, resolve, reject });
|
|
140
140
|
|
|
141
|
-
const serialized =
|
|
141
|
+
const serialized = transfer.encode(request);
|
|
142
142
|
this._worker.postMessage(serialized.result, serialized.transferList);
|
|
143
143
|
});
|
|
144
144
|
}
|