77 lines
1.9 KiB
TypeScript
77 lines
1.9 KiB
TypeScript
import type { CAC } from 'cac';
|
|
|
|
import { execSync } from 'node:child_process';
|
|
|
|
import { execaCommand } from '@vben/node-utils';
|
|
|
|
interface LintCommandOptions {
|
|
/**
|
|
* Format lint problem.
|
|
*/
|
|
format?: boolean;
|
|
/**
|
|
* Number of threads for oxfmt and oxlint (default: 2).
|
|
*/
|
|
threads?: number;
|
|
}
|
|
|
|
async function runLint({ format, threads }: LintCommandOptions) {
|
|
// process.env.FORCE_COLOR = '3';
|
|
const threadsArg = threads ? ` --threads=${threads}` : ` --threads=2`;
|
|
|
|
if (format) {
|
|
await execaCommand(`stylelint "**/*.{vue,css,less,scss}" --cache --fix`, {
|
|
stdio: 'inherit',
|
|
});
|
|
await execaCommand(`oxfmt${threadsArg}`, {
|
|
stdio: 'inherit',
|
|
});
|
|
await execaCommand(`oxlint --fix${threadsArg}`, {
|
|
stdio: 'inherit',
|
|
});
|
|
await execaCommand(`eslint . --cache --fix`, {
|
|
stdio: 'inherit',
|
|
});
|
|
return;
|
|
}
|
|
const subprocesses = [
|
|
execaCommand(`oxfmt --check${threadsArg}`, { stdio: 'inherit' }),
|
|
execaCommand(`oxlint${threadsArg}`, { stdio: 'inherit' }),
|
|
execaCommand(`eslint . --cache`, { stdio: 'inherit' }),
|
|
execaCommand(`stylelint "**/*.{vue,css,less,scss}" --cache`, {
|
|
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;
|
|
}
|
|
}
|
|
|
|
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 };
|