Files
huachuang/nl-ui/nl-ui-admin-vben/yudao-ui-admin-vben/scripts/vsh/src/lint/index.ts

134 lines
3.6 KiB
TypeScript
Raw Normal View History

2026-07-08 15:08:41 +08:00
import type { CAC } from 'cac';
import { execSync } from 'node:child_process';
import { availableParallelism, freemem } from 'node:os';
import { execaCommand } from '@vben/node-utils';
interface LintCommandOptions {
/**
* Format lint problem.
*/
format?: boolean;
/**
* Number of threads for oxfmt and oxlint (default: 2).
*/
threads?: number;
}
/**
* CPU 4
* -
*/
const CPU_CORE_THRESHOLD = 4;
/**
* 8 GB
*
* oxfmt / oxlint 线 4使 2
*/
const FREE_MEMORY_THRESHOLD = 8 * 1024 ** 3;
/**
*
*
*/
async function runSerial(commands: string[]) {
const failed: string[] = [];
for (const command of commands) {
try {
await execaCommand(command, { stdio: 'inherit' });
} catch {
failed.push(command);
}
}
if (failed.length > 0) {
throw new Error(
`Lint failed:\n${failed.map((command) => ` - ${command}`).join('\n')}`,
);
}
}
/**
*
*
*/
async function runParallel(commands: string[]) {
const subprocesses = commands.map((command) =>
execaCommand(command, { stdio: 'inherit' }),
);
try {
await Promise.all(subprocesses);
} catch (error) {
for (const subprocess of subprocesses) {
try {
if (process.platform === 'win32' && subprocess.pid) {
execSync(`taskkill /F /T /PID ${subprocess.pid}`, {
stdio: 'ignore',
});
} else {
subprocess.kill('SIGKILL');
}
} catch {
// process may have already exited
}
}
await Promise.allSettled(subprocesses);
throw error;
}
}
async function runLint({ format, threads }: LintCommandOptions) {
// process.env.FORCE_COLOR = '3';
const cpuCores = availableParallelism();
// CPU 核心数充足且可用内存充足时,默认线程数提升到 4否则维持 2
// 用户通过 --threads 显式指定时优先使用其值。
const defaultThreads =
cpuCores > CPU_CORE_THRESHOLD && freemem() > FREE_MEMORY_THRESHOLD ? 4 : 2;
const threadsArg = ` --threads=${threads || defaultThreads}`;
if (format) {
await execaCommand(`stylelint "**/*.{vue,css,less,scss}" --cache --fix`, {
stdio: 'inherit',
});
await execaCommand(`oxfmt${threadsArg}`, {
stdio: 'inherit',
});
await execaCommand(`oxlint --fix --type-aware${threadsArg}`, {
stdio: 'inherit',
});
await execaCommand(`eslint . --cache --fix`, {
stdio: 'inherit',
});
return;
}
const commands = [
`oxfmt --check${threadsArg}`,
`oxlint --type-aware${threadsArg}`,
`eslint . --cache`,
`stylelint "**/*.{vue,css,less,scss}" --cache`,
];
// 低配机器CPU 核心数较少)串行执行,避免多进程并发导致瞬时占用飙升;
// 高配机器并行执行以缩短整体耗时。
await (cpuCores <= CPU_CORE_THRESHOLD
? runSerial(commands)
: runParallel(commands));
}
function defineLintCommand(cac: CAC) {
cac
.command('lint')
.usage('Batch execute project lint check.')
.option('--format', 'Format lint problem.')
.option('--threads <count>', 'Number of threads for oxfmt and oxlint.')
.action(runLint);
}
export { defineLintCommand };