Skip to content

Commit 3c763d2

Browse files
committed
feat(@unimolecule/utils): ✨ disk & memory check
1 parent 196a531 commit 3c763d2

13 files changed

Lines changed: 608 additions & 75 deletions

File tree

.changeset/many-spies-watch.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@unimolecule/utils": patch
3+
---
4+
5+
feat disk & memory check
Lines changed: 10 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,13 @@
11
import process from "node:process";
2-
import { outputEntryBuilder } from "@unimolecule/utils/node";
2+
import { outputEntryBuilder } from "@unimolecule/utils/node/output-entry-builder";
33
import { defineConfig } from "tsdown";
44

5-
export default defineConfig([
6-
{
7-
entry: ["./src/index.ts"],
8-
format: ["esm", "cjs"],
9-
platform: "node",
10-
dts: true,
11-
tsconfig: "./tsconfig.json",
12-
watch: process.env.NODE_ENV === "development",
13-
shims: true,
14-
},
15-
{
16-
entry: outputEntryBuilder("./src/cli"),
17-
format: ["esm", "cjs"],
18-
platform: "node",
19-
dts: true,
20-
tsconfig: "./tsconfig.json",
21-
outDir: "dist/cli",
22-
watch: process.env.NODE_ENV === "development",
23-
shims: true,
24-
clean: false,
25-
},
26-
{
27-
entry: outputEntryBuilder("./src/core"),
28-
format: ["esm", "cjs"],
29-
platform: "node",
30-
dts: true,
31-
tsconfig: "./tsconfig.json",
32-
outDir: "dist/core",
33-
watch: process.env.NODE_ENV === "development",
34-
shims: true,
35-
clean: false,
36-
},
37-
{
38-
entry: outputEntryBuilder("./src/node"),
39-
format: ["esm", "cjs"],
40-
platform: "node",
41-
dts: true,
42-
tsconfig: "./tsconfig.json",
43-
outDir: "dist/node",
44-
watch: process.env.NODE_ENV === "development",
45-
shims: true,
46-
clean: false,
47-
},
48-
]);
5+
export default defineConfig({
6+
entry: outputEntryBuilder("./src"),
7+
format: ["esm", "cjs"],
8+
platform: "node",
9+
dts: true,
10+
tsconfig: "./tsconfig.json",
11+
watch: process.env.NODE_ENV === "development",
12+
shims: true,
13+
});

packages/utils/src/node/README.md

Lines changed: 50 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -82,19 +82,44 @@ Probe filesystem and path values:
8282
```ts
8383
import {
8484
checkProcessDiskAccess,
85+
checkProcessDiskUsage,
8586
formatPath,
87+
getProcessDiskUsage,
8688
pathExists,
8789
} from "@unimolecule/utils/node";
8890

8991
async function main() {
9092
await checkProcessDiskAccess(process.cwd());
93+
const disk = await getProcessDiskUsage({ path: process.cwd() });
94+
const diskCheck = await checkProcessDiskUsage({
95+
maxUsedPercent: 0.9,
96+
minAvailableBytes: 1024 * 1024 * 1024,
97+
});
9198
await pathExists("package.json");
9299
formatPath(String.raw`C:\Users\i7eo\app`);
100+
console.log({ disk, diskCheck });
93101
}
94102

95103
main().catch(console.error);
96104
```
97105

106+
Check process memory usage:
107+
108+
```ts
109+
import {
110+
checkProcessMemoryUsage,
111+
getProcessMemoryUsage,
112+
} from "@unimolecule/utils/node";
113+
114+
const memory = getProcessMemoryUsage();
115+
const memoryCheck = checkProcessMemoryUsage({
116+
maxHeapUsedBytes: 150 * 1024 * 1024,
117+
maxRssBytes: 512 * 1024 * 1024,
118+
});
119+
120+
console.log({ memory, memoryCheck });
121+
```
122+
98123
Read local host addresses:
99124

100125
```ts
@@ -118,27 +143,31 @@ export default {
118143

119144
## API
120145

121-
| Export | Description |
122-
| ---------------------------------------------- | ----------------------------------------------------------------------------------------- |
123-
| `appExists(app, options?)` | Async PATH probe for an executable app. |
124-
| `appExistsSync(app, options?)` | Sync PATH probe for an executable app. |
125-
| `checkProcessDiskAccess(path?)` | Verifies read/write access for a path, defaulting to `process.cwd()`. |
126-
| `executeCommand(command, args?, options?)` | Runs a command and resolves exit details when it succeeds. |
127-
| `executeCommandSync(command, args?, options?)` | Sync command runner with the same success/error behavior. |
128-
| `formatPath(path)` | Converts backslash separators to `/` while leaving Unix paths unchanged. |
129-
| `createProcessGracefulExit(logger?)` | Creates isolated process signal registration and shutdown helpers. |
130-
| `exitSignals` | Supported graceful shutdown signals: `SIGINT`, `SIGQUIT`, `SIGTERM`. |
131-
| `getLocalhostAddress()` | Returns non-internal IPv4 addresses plus `[::]`, with duplicates removed. |
132-
| `getPackages(cwd?)` | Async export from `@manypkg/get-packages`. |
133-
| `getPackagesSync(cwd?)` | Sync export from `@manypkg/get-packages`. |
134-
| `outputEntryBuilder(rootDir, options?)` | Builds tsdown entry objects by scanning files; `entries: "index"` keeps only index files. |
135-
| `pathExists(path)` | Async `fs.access` existence check. |
136-
| `pathExistsSync(path)` | Sync `fs.accessSync` existence check. |
137-
| `require` | `createRequire(import.meta.url)` for ESM modules that need Node require. |
138-
| `unifiedSpawn(command, args?, options?)` | Cross-platform `spawn` wrapper. |
139-
| `unifiedSpawnAsync(command, args?, options?)` | Promise wrapper resolving the child close code. |
140-
| `unifiedSpawnSync(command, args?, options?)` | Cross-platform `spawnSync` wrapper. |
141-
| `userHome` | Current `os.homedir()` value. |
146+
| Export | Description |
147+
| ---------------------------------------------- | ------------------------------------------------------------------------------------------------ |
148+
| `appExists(app, options?)` | Async PATH probe for an executable app. |
149+
| `appExistsSync(app, options?)` | Sync PATH probe for an executable app. |
150+
| `checkProcessDiskAccess(path?)` | Verifies read/write access for a path, defaulting to `process.cwd()`. |
151+
| `checkProcessDiskUsage(options?)` | Reads disk usage and returns structured `checks` for used-percent and available-byte thresholds. |
152+
| `checkProcessMemoryUsage(options?)` | Reads process memory usage and compares heap/RSS usage with optional byte thresholds. |
153+
| `executeCommand(command, args?, options?)` | Runs a command and resolves exit details when it succeeds. |
154+
| `executeCommandSync(command, args?, options?)` | Sync command runner with the same success/error behavior. |
155+
| `formatPath(path)` | Converts backslash separators to `/` while leaving Unix paths unchanged. |
156+
| `createProcessGracefulExit(logger?)` | Creates isolated process signal registration and shutdown helpers. |
157+
| `exitSignals` | Supported graceful shutdown signals: `SIGINT`, `SIGQUIT`, `SIGTERM`. |
158+
| `getLocalhostAddress()` | Returns non-internal IPv4 addresses plus `[::]`, with duplicates removed. |
159+
| `getPackages(cwd?)` | Async export from `@manypkg/get-packages`. |
160+
| `getPackagesSync(cwd?)` | Sync export from `@manypkg/get-packages`. |
161+
| `getProcessDiskUsage(options?)` | Reads filesystem capacity, free, available, used bytes, and used percent for a path. |
162+
| `getProcessMemoryUsage()` | Reads `process.memoryUsage()` with byte-oriented field names. |
163+
| `outputEntryBuilder(rootDir, options?)` | Builds tsdown entry objects by scanning files; `entries: "index"` keeps only index files. |
164+
| `pathExists(path)` | Async `fs.access` existence check. |
165+
| `pathExistsSync(path)` | Sync `fs.accessSync` existence check. |
166+
| `require` | `createRequire(import.meta.url)` for ESM modules that need Node require. |
167+
| `unifiedSpawn(command, args?, options?)` | Cross-platform `spawn` wrapper. |
168+
| `unifiedSpawnAsync(command, args?, options?)` | Promise wrapper resolving the child close code. |
169+
| `unifiedSpawnSync(command, args?, options?)` | Cross-platform `spawnSync` wrapper. |
170+
| `userHome` | Current `os.homedir()` value. |
142171

143172
## Runtime Notes
144173

packages/utils/src/node/README.zh-CN.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,19 +82,44 @@ gracefulExit.register(cleanup);
8282
```ts
8383
import {
8484
checkProcessDiskAccess,
85+
checkProcessDiskUsage,
8586
formatPath,
87+
getProcessDiskUsage,
8688
pathExists,
8789
} from "@unimolecule/utils/node";
8890

8991
async function main() {
9092
await checkProcessDiskAccess(process.cwd());
93+
const disk = await getProcessDiskUsage({ path: process.cwd() });
94+
const diskCheck = await checkProcessDiskUsage({
95+
maxUsedPercent: 0.9,
96+
minAvailableBytes: 1024 * 1024 * 1024,
97+
});
9198
await pathExists("package.json");
9299
formatPath(String.raw`C:\Users\i7eo\app`);
100+
console.log({ disk, diskCheck });
93101
}
94102

95103
main().catch(console.error);
96104
```
97105

106+
检查进程内存使用:
107+
108+
```ts
109+
import {
110+
checkProcessMemoryUsage,
111+
getProcessMemoryUsage,
112+
} from "@unimolecule/utils/node";
113+
114+
const memory = getProcessMemoryUsage();
115+
const memoryCheck = checkProcessMemoryUsage({
116+
maxHeapUsedBytes: 150 * 1024 * 1024,
117+
maxRssBytes: 512 * 1024 * 1024,
118+
});
119+
120+
console.log({ memory, memoryCheck });
121+
```
122+
98123
读取本机地址:
99124

100125
```ts
@@ -123,6 +148,8 @@ export default {
123148
| `appExists(app, options?)` | 异步探测 PATH 中是否存在可执行命令。 |
124149
| `appExistsSync(app, options?)` | 同步探测 PATH 中是否存在可执行命令。 |
125150
| `checkProcessDiskAccess(path?)` | 验证路径读写权限,默认使用 `process.cwd()`|
151+
| `checkProcessDiskUsage(options?)` | 读取磁盘使用情况,并返回使用率与可用字节阈值的结构化 `checks`|
152+
| `checkProcessMemoryUsage(options?)` | 读取进程内存使用情况,并按可选 heap/RSS 字节阈值判断。 |
126153
| `executeCommand(command, args?, options?)` | 执行命令,成功时返回退出信息。 |
127154
| `executeCommandSync(command, args?, options?)` | 同步命令执行器,行为与异步版本一致。 |
128155
| `formatPath(path)` | 将反斜杠路径分隔符转换为 `/`,Unix 路径保持不变。 |
@@ -131,6 +158,8 @@ export default {
131158
| `getLocalhostAddress()` | 返回非 internal IPv4 地址和 `[::]`,并去重。 |
132159
| `getPackages(cwd?)` |`@manypkg/get-packages` 直接导出的异步函数。 |
133160
| `getPackagesSync(cwd?)` |`@manypkg/get-packages` 直接导出的同步函数。 |
161+
| `getProcessDiskUsage(options?)` | 读取指定路径的文件系统容量、空闲、可用、已用字节和使用率。 |
162+
| `getProcessMemoryUsage()` | 读取 `process.memoryUsage()`,并返回以字节命名的字段。 |
134163
| `outputEntryBuilder(rootDir, options?)` | 扫描文件生成 tsdown entry 对象;`entries: "index"` 只保留 index 文件。 |
135164
| `pathExists(path)` | 异步 `fs.access` 存在性检查。 |
136165
| `pathExistsSync(path)` | 同步 `fs.accessSync` 存在性检查。 |

0 commit comments

Comments
 (0)