wyt-cli 1.0.22 → 1.0.23
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/bin/commands/run.js +58 -1
- package/package.json +1 -1
package/bin/commands/run.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import path from 'path';
|
|
2
|
-
import { execa } from 'execa';
|
|
2
|
+
import { execa, execaSync } from 'execa';
|
|
3
3
|
import { Command } from 'commander';
|
|
4
4
|
import fs from 'fs-extra';
|
|
5
5
|
import inquirer from 'inquirer';
|
|
@@ -8,6 +8,8 @@ import readline from 'readline';
|
|
|
8
8
|
import { log_info, log_error, getInquirerOperationText } from '../lib/logger.js';
|
|
9
9
|
import { checkTargetDir, getProjects } from '../lib/dir.js';
|
|
10
10
|
|
|
11
|
+
const MEMORY_LIMIT = 90; // 建议内存阈值
|
|
12
|
+
|
|
11
13
|
export default function () {
|
|
12
14
|
const command = new Command('run');
|
|
13
15
|
|
|
@@ -84,6 +86,7 @@ async function runProject(projects = [], appsDir) {
|
|
|
84
86
|
NODE_ENV: 'development',
|
|
85
87
|
PROJECT_RUN_MODE: 'wyt-cli',
|
|
86
88
|
};
|
|
89
|
+
|
|
87
90
|
// 更新 packages 最新版本
|
|
88
91
|
log_info(`🔄 检查并更新 packages 最新版本...`);
|
|
89
92
|
await execa('pnpm', ['install'], {
|
|
@@ -96,6 +99,9 @@ async function runProject(projects = [], appsDir) {
|
|
|
96
99
|
env: commandEnv,
|
|
97
100
|
});
|
|
98
101
|
|
|
102
|
+
// 检测内存占用率
|
|
103
|
+
checkSystemMemoryUsage();
|
|
104
|
+
|
|
99
105
|
// 运行项目文档
|
|
100
106
|
runDocsProjects(commandEnv);
|
|
101
107
|
// log_info(`🚀 启动项目文档...`);
|
|
@@ -310,3 +316,54 @@ function runDocsProjects(commandEnv) {
|
|
|
310
316
|
log_error(`❌ 启动文档终端失败: ${error.message}`);
|
|
311
317
|
}
|
|
312
318
|
}
|
|
319
|
+
|
|
320
|
+
// 检查系统内存占用率
|
|
321
|
+
function checkSystemMemoryUsage() {
|
|
322
|
+
const memoryUsage = getSystemMemoryUsage();
|
|
323
|
+
if (memoryUsage !== -1 && memoryUsage > MEMORY_LIMIT) {
|
|
324
|
+
log_error(`⚠️ 系统内存占用率过高,当前占用率: ${memoryUsage.toFixed(2)}%,不建议运行 Hrp3.0 项目!`);
|
|
325
|
+
process.exit(1);
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
/**
|
|
330
|
+
* 获取系统内存占用率
|
|
331
|
+
* @returns {Promise<number>} 返回 0-100 之间的数字,代表内存占用百分比
|
|
332
|
+
*/
|
|
333
|
+
function getSystemMemoryUsage() {
|
|
334
|
+
try {
|
|
335
|
+
if (process.platform === 'win32') {
|
|
336
|
+
// Windows: 使用 wmic 命令获取总内存和可用内存
|
|
337
|
+
const totalMemCmd = execaSync('wmic', ['OS', 'get', 'TotalVisibleMemorySize', '/value']);
|
|
338
|
+
const freeMemCmd = execaSync('wmic', ['OS', 'get', 'FreePhysicalMemory', '/value']);
|
|
339
|
+
|
|
340
|
+
// 解析命令输出,提取数值 (输出格式类似:TotalVisibleMemorySize=16384000)
|
|
341
|
+
const totalMem = parseInt(totalMemCmd.stdout.split('=')[1], 10);
|
|
342
|
+
const freeMem = parseInt(freeMemCmd.stdout.split('=')[1], 10);
|
|
343
|
+
|
|
344
|
+
const usedMem = totalMem - freeMem;
|
|
345
|
+
return (usedMem / totalMem) * 100;
|
|
346
|
+
} else {
|
|
347
|
+
// Linux / macOS: 使用 free -b 命令 (以字节为单位)
|
|
348
|
+
// -b 确保所有系统输出格式一致,便于解析
|
|
349
|
+
const { stdout } = execaSync('free', ['-b']);
|
|
350
|
+
|
|
351
|
+
// 输出格式类似:
|
|
352
|
+
// total used free shared buff/cache available
|
|
353
|
+
// Mem: 16777216000 8388608000 4194304000 209715200 4194304000 5242880000
|
|
354
|
+
const lines = stdout.split('\n');
|
|
355
|
+
const memLine = lines[1].split(/\s+/);
|
|
356
|
+
|
|
357
|
+
const totalMem = parseInt(memLine[1], 10);
|
|
358
|
+
// 注意:在 Linux 中,看可用内存应该看 available 列(第7个,索引6),而不是 free 列
|
|
359
|
+
// 因为 buff/cache 中的内存是可以被快速释放使用的
|
|
360
|
+
const availableMem = parseInt(memLine[6], 10);
|
|
361
|
+
|
|
362
|
+
const usedMem = totalMem - availableMem;
|
|
363
|
+
return (usedMem / totalMem) * 100;
|
|
364
|
+
}
|
|
365
|
+
} catch (error) {
|
|
366
|
+
console.warn('⚠️ 无法获取系统内存信息,跳过内存检测:', error.message);
|
|
367
|
+
return -1; // 返回 -1 表示检测失败,后续逻辑可以放行
|
|
368
|
+
}
|
|
369
|
+
}
|