物业代码生成
This commit is contained in:
170
scripts/vsh/src/check-circular/index.ts
Normal file
170
scripts/vsh/src/check-circular/index.ts
Normal file
@@ -0,0 +1,170 @@
|
||||
import type { CAC } from 'cac';
|
||||
|
||||
import { extname } from 'node:path';
|
||||
|
||||
import { getStagedFiles } from '@vben/node-utils';
|
||||
|
||||
import { circularDepsDetect } from 'circular-dependency-scanner';
|
||||
|
||||
// 默认配置
|
||||
const DEFAULT_CONFIG = {
|
||||
allowedExtensions: ['.cjs', '.js', '.jsx', '.mjs', '.ts', '.tsx', '.vue'],
|
||||
ignoreDirs: [
|
||||
'dist',
|
||||
'.turbo',
|
||||
'output',
|
||||
'.cache',
|
||||
'scripts',
|
||||
'internal',
|
||||
'packages/effects/request/src/',
|
||||
'packages/@core/ui-kit/menu-ui/src/',
|
||||
'packages/@core/ui-kit/popup-ui/src/',
|
||||
],
|
||||
threshold: 0, // 循环依赖的阈值
|
||||
} as const;
|
||||
|
||||
// 类型定义
|
||||
type CircularDependencyResult = string[];
|
||||
|
||||
interface CheckCircularConfig {
|
||||
allowedExtensions?: string[];
|
||||
ignoreDirs?: string[];
|
||||
threshold?: number;
|
||||
}
|
||||
|
||||
interface CommandOptions {
|
||||
config?: CheckCircularConfig;
|
||||
staged: boolean;
|
||||
verbose: boolean;
|
||||
}
|
||||
|
||||
// 缓存机制
|
||||
const cache = new Map<string, CircularDependencyResult[]>();
|
||||
|
||||
/**
|
||||
* 格式化循环依赖的输出
|
||||
* @param circles - 循环依赖结果
|
||||
*/
|
||||
function formatCircles(circles: CircularDependencyResult[]): void {
|
||||
if (circles.length === 0) {
|
||||
console.log('✅ No circular dependencies found');
|
||||
return;
|
||||
}
|
||||
|
||||
console.log('⚠️ Circular dependencies found:');
|
||||
circles.forEach((circle, index) => {
|
||||
console.log(`\nCircular dependency #${index + 1}:`);
|
||||
circle.forEach((file) => console.log(` → ${file}`));
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查项目中的循环依赖
|
||||
* @param options - 检查选项
|
||||
* @param options.staged - 是否只检查暂存区文件
|
||||
* @param options.verbose - 是否显示详细信息
|
||||
* @param options.config - 自定义配置
|
||||
* @returns Promise<void>
|
||||
*/
|
||||
async function checkCircular({
|
||||
config = {},
|
||||
staged,
|
||||
verbose,
|
||||
}: CommandOptions): Promise<void> {
|
||||
try {
|
||||
// 合并配置
|
||||
const finalConfig = {
|
||||
...DEFAULT_CONFIG,
|
||||
...config,
|
||||
};
|
||||
|
||||
// 生成忽略模式
|
||||
const ignorePattern = `**/{${finalConfig.ignoreDirs.join(',')}}/**`;
|
||||
|
||||
// 检查缓存
|
||||
const cacheKey = `${staged}-${process.cwd()}-${ignorePattern}`;
|
||||
if (cache.has(cacheKey)) {
|
||||
const cachedResults = cache.get(cacheKey);
|
||||
if (cachedResults) {
|
||||
verbose && formatCircles(cachedResults);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// 检测循环依赖
|
||||
const results = await circularDepsDetect({
|
||||
absolute: staged,
|
||||
cwd: process.cwd(),
|
||||
ignore: [ignorePattern],
|
||||
});
|
||||
|
||||
if (staged) {
|
||||
let files = await getStagedFiles();
|
||||
const allowedExtensions = new Set(finalConfig.allowedExtensions);
|
||||
|
||||
// 过滤文件列表
|
||||
files = files.filter((file) => allowedExtensions.has(extname(file)));
|
||||
|
||||
const circularFiles: CircularDependencyResult[] = [];
|
||||
|
||||
for (const file of files) {
|
||||
for (const result of results) {
|
||||
const resultFiles = result.flat();
|
||||
if (resultFiles.includes(file)) {
|
||||
circularFiles.push(result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 更新缓存
|
||||
cache.set(cacheKey, circularFiles);
|
||||
verbose && formatCircles(circularFiles);
|
||||
} else {
|
||||
// 更新缓存
|
||||
cache.set(cacheKey, results);
|
||||
verbose && formatCircles(results);
|
||||
}
|
||||
|
||||
// 如果发现循环依赖,只输出警告信息
|
||||
if (results.length > 0) {
|
||||
console.log(
|
||||
'\n⚠️ Warning: Circular dependencies found, please check and fix',
|
||||
);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(
|
||||
'❌ Error checking circular dependencies:',
|
||||
error instanceof Error ? error.message : error,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 定义检查循环依赖的命令
|
||||
* @param cac - CAC实例
|
||||
*/
|
||||
function defineCheckCircularCommand(cac: CAC): void {
|
||||
cac
|
||||
.command('check-circular')
|
||||
.option('--staged', 'Only check staged files')
|
||||
.option('--verbose', 'Show detailed information')
|
||||
.option('--threshold <number>', 'Threshold for circular dependencies', {
|
||||
default: 0,
|
||||
})
|
||||
.option('--ignore-dirs <dirs>', 'Directories to ignore, comma separated')
|
||||
.usage('Analyze project circular dependencies')
|
||||
.action(async ({ ignoreDirs, staged, threshold, verbose }) => {
|
||||
const config: CheckCircularConfig = {
|
||||
threshold: Number(threshold),
|
||||
...(ignoreDirs && { ignoreDirs: ignoreDirs.split(',') }),
|
||||
};
|
||||
|
||||
await checkCircular({
|
||||
config,
|
||||
staged,
|
||||
verbose: verbose ?? true,
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
export { type CheckCircularConfig, defineCheckCircularCommand };
|
194
scripts/vsh/src/check-dep/index.ts
Normal file
194
scripts/vsh/src/check-dep/index.ts
Normal file
@@ -0,0 +1,194 @@
|
||||
import type { CAC } from 'cac';
|
||||
|
||||
import { getPackages } from '@vben/node-utils';
|
||||
|
||||
import depcheck from 'depcheck';
|
||||
|
||||
// 默认配置
|
||||
const DEFAULT_CONFIG = {
|
||||
// 需要忽略的依赖匹配
|
||||
ignoreMatches: [
|
||||
'vite',
|
||||
'vitest',
|
||||
'unbuild',
|
||||
'@vben/tsconfig',
|
||||
'@vben/vite-config',
|
||||
'@vben/tailwind-config',
|
||||
'@types/*',
|
||||
'@vben-core/design',
|
||||
],
|
||||
// 需要忽略的包
|
||||
ignorePackages: [
|
||||
'@vben/backend-mock',
|
||||
'@vben/commitlint-config',
|
||||
'@vben/eslint-config',
|
||||
'@vben/node-utils',
|
||||
'@vben/prettier-config',
|
||||
'@vben/stylelint-config',
|
||||
'@vben/tailwind-config',
|
||||
'@vben/tsconfig',
|
||||
'@vben/vite-config',
|
||||
'@vben/vsh',
|
||||
],
|
||||
// 需要忽略的文件模式
|
||||
ignorePatterns: ['dist', 'node_modules', 'public'],
|
||||
};
|
||||
|
||||
interface DepcheckResult {
|
||||
dependencies: string[];
|
||||
devDependencies: string[];
|
||||
missing: Record<string, string[]>;
|
||||
}
|
||||
|
||||
interface DepcheckConfig {
|
||||
ignoreMatches?: string[];
|
||||
ignorePackages?: string[];
|
||||
ignorePatterns?: string[];
|
||||
}
|
||||
|
||||
interface PackageInfo {
|
||||
dir: string;
|
||||
packageJson: {
|
||||
name: string;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 清理依赖检查结果
|
||||
* @param unused - 依赖检查结果
|
||||
*/
|
||||
function cleanDepcheckResult(unused: DepcheckResult): void {
|
||||
// 删除file:前缀的依赖提示,该依赖是本地依赖
|
||||
Reflect.deleteProperty(unused.missing, 'file:');
|
||||
|
||||
// 清理路径依赖
|
||||
Object.keys(unused.missing).forEach((key) => {
|
||||
unused.missing[key] = (unused.missing[key] || []).filter(
|
||||
(item: string) => !item.startsWith('/'),
|
||||
);
|
||||
if (unused.missing[key].length === 0) {
|
||||
Reflect.deleteProperty(unused.missing, key);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化依赖检查结果
|
||||
* @param pkgName - 包名
|
||||
* @param unused - 依赖检查结果
|
||||
*/
|
||||
function formatDepcheckResult(pkgName: string, unused: DepcheckResult): void {
|
||||
const hasIssues =
|
||||
Object.keys(unused.missing).length > 0 ||
|
||||
unused.dependencies.length > 0 ||
|
||||
unused.devDependencies.length > 0;
|
||||
|
||||
if (!hasIssues) {
|
||||
return;
|
||||
}
|
||||
|
||||
console.log('\n📦 Package:', pkgName);
|
||||
|
||||
if (Object.keys(unused.missing).length > 0) {
|
||||
console.log('❌ Missing dependencies:');
|
||||
Object.entries(unused.missing).forEach(([dep, files]) => {
|
||||
console.log(` - ${dep}:`);
|
||||
files.forEach((file) => console.log(` → ${file}`));
|
||||
});
|
||||
}
|
||||
|
||||
if (unused.dependencies.length > 0) {
|
||||
console.log('⚠️ Unused dependencies:');
|
||||
unused.dependencies.forEach((dep) => console.log(` - ${dep}`));
|
||||
}
|
||||
|
||||
if (unused.devDependencies.length > 0) {
|
||||
console.log('⚠️ Unused devDependencies:');
|
||||
unused.devDependencies.forEach((dep) => console.log(` - ${dep}`));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 运行依赖检查
|
||||
* @param config - 配置选项
|
||||
*/
|
||||
async function runDepcheck(config: DepcheckConfig = {}): Promise<void> {
|
||||
try {
|
||||
const finalConfig = {
|
||||
...DEFAULT_CONFIG,
|
||||
...config,
|
||||
};
|
||||
|
||||
const { packages } = await getPackages();
|
||||
|
||||
let hasIssues = false;
|
||||
|
||||
await Promise.all(
|
||||
packages.map(async (pkg: PackageInfo) => {
|
||||
// 跳过需要忽略的包
|
||||
if (finalConfig.ignorePackages.includes(pkg.packageJson.name)) {
|
||||
return;
|
||||
}
|
||||
|
||||
const unused = await depcheck(pkg.dir, {
|
||||
ignoreMatches: finalConfig.ignoreMatches,
|
||||
ignorePatterns: finalConfig.ignorePatterns,
|
||||
});
|
||||
|
||||
cleanDepcheckResult(unused);
|
||||
|
||||
const pkgHasIssues =
|
||||
Object.keys(unused.missing).length > 0 ||
|
||||
unused.dependencies.length > 0 ||
|
||||
unused.devDependencies.length > 0;
|
||||
|
||||
if (pkgHasIssues) {
|
||||
hasIssues = true;
|
||||
formatDepcheckResult(pkg.packageJson.name, unused);
|
||||
}
|
||||
}),
|
||||
);
|
||||
|
||||
if (!hasIssues) {
|
||||
console.log('\n✅ Dependency check completed, no issues found');
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(
|
||||
'❌ Dependency check failed:',
|
||||
error instanceof Error ? error.message : error,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 定义依赖检查命令
|
||||
* @param cac - CAC实例
|
||||
*/
|
||||
function defineDepcheckCommand(cac: CAC): void {
|
||||
cac
|
||||
.command('check-dep')
|
||||
.option(
|
||||
'--ignore-packages <packages>',
|
||||
'Packages to ignore, comma separated',
|
||||
)
|
||||
.option(
|
||||
'--ignore-matches <matches>',
|
||||
'Dependency patterns to ignore, comma separated',
|
||||
)
|
||||
.option(
|
||||
'--ignore-patterns <patterns>',
|
||||
'File patterns to ignore, comma separated',
|
||||
)
|
||||
.usage('Analyze project dependencies')
|
||||
.action(async ({ ignoreMatches, ignorePackages, ignorePatterns }) => {
|
||||
const config: DepcheckConfig = {
|
||||
...(ignorePackages && { ignorePackages: ignorePackages.split(',') }),
|
||||
...(ignoreMatches && { ignoreMatches: ignoreMatches.split(',') }),
|
||||
...(ignorePatterns && { ignorePatterns: ignorePatterns.split(',') }),
|
||||
};
|
||||
|
||||
await runDepcheck(config);
|
||||
});
|
||||
}
|
||||
|
||||
export { defineDepcheckCommand, type DepcheckConfig };
|
78
scripts/vsh/src/code-workspace/index.ts
Normal file
78
scripts/vsh/src/code-workspace/index.ts
Normal file
@@ -0,0 +1,78 @@
|
||||
import type { CAC } from 'cac';
|
||||
|
||||
import { join, relative } from 'node:path';
|
||||
|
||||
import {
|
||||
colors,
|
||||
consola,
|
||||
findMonorepoRoot,
|
||||
getPackages,
|
||||
gitAdd,
|
||||
outputJSON,
|
||||
prettierFormat,
|
||||
toPosixPath,
|
||||
} from '@vben/node-utils';
|
||||
|
||||
const CODE_WORKSPACE_FILE = join('vben-admin.code-workspace');
|
||||
|
||||
interface CodeWorkspaceCommandOptions {
|
||||
autoCommit?: boolean;
|
||||
spaces?: number;
|
||||
}
|
||||
|
||||
async function createCodeWorkspace({
|
||||
autoCommit = false,
|
||||
spaces = 2,
|
||||
}: CodeWorkspaceCommandOptions) {
|
||||
const { packages, rootDir } = await getPackages();
|
||||
|
||||
let folders = packages.map((pkg) => {
|
||||
const { dir, packageJson } = pkg;
|
||||
return {
|
||||
name: packageJson.name,
|
||||
path: toPosixPath(relative(rootDir, dir)),
|
||||
};
|
||||
});
|
||||
|
||||
folders = folders.filter(Boolean);
|
||||
|
||||
const monorepoRoot = findMonorepoRoot();
|
||||
const outputPath = join(monorepoRoot, CODE_WORKSPACE_FILE);
|
||||
await outputJSON(outputPath, { folders }, spaces);
|
||||
|
||||
await prettierFormat(outputPath);
|
||||
if (autoCommit) {
|
||||
await gitAdd(CODE_WORKSPACE_FILE, monorepoRoot);
|
||||
}
|
||||
}
|
||||
|
||||
async function runCodeWorkspace({
|
||||
autoCommit,
|
||||
spaces,
|
||||
}: CodeWorkspaceCommandOptions) {
|
||||
await createCodeWorkspace({
|
||||
autoCommit,
|
||||
spaces,
|
||||
});
|
||||
if (autoCommit) {
|
||||
return;
|
||||
}
|
||||
consola.log('');
|
||||
consola.success(colors.green(`${CODE_WORKSPACE_FILE} is updated!`));
|
||||
consola.log('');
|
||||
}
|
||||
|
||||
function defineCodeWorkspaceCommand(cac: CAC) {
|
||||
cac
|
||||
.command('code-workspace')
|
||||
.usage('Update the `.code-workspace` file')
|
||||
.option('--spaces [number]', '.code-workspace JSON file spaces.', {
|
||||
default: 2,
|
||||
})
|
||||
.option('--auto-commit', 'auto commit .code-workspace JSON file.', {
|
||||
default: false,
|
||||
})
|
||||
.action(runCodeWorkspace);
|
||||
}
|
||||
|
||||
export { defineCodeWorkspaceCommand };
|
74
scripts/vsh/src/index.ts
Normal file
74
scripts/vsh/src/index.ts
Normal file
@@ -0,0 +1,74 @@
|
||||
import { colors, consola } from '@vben/node-utils';
|
||||
|
||||
import { cac } from 'cac';
|
||||
|
||||
import { version } from '../package.json';
|
||||
import { defineCheckCircularCommand } from './check-circular';
|
||||
import { defineDepcheckCommand } from './check-dep';
|
||||
import { defineCodeWorkspaceCommand } from './code-workspace';
|
||||
import { defineLintCommand } from './lint';
|
||||
import { definePubLintCommand } from './publint';
|
||||
|
||||
// 命令描述
|
||||
const COMMAND_DESCRIPTIONS = {
|
||||
'check-circular': 'Check for circular dependencies',
|
||||
'check-dep': 'Check for unused dependencies',
|
||||
'code-workspace': 'Manage VS Code workspace settings',
|
||||
lint: 'Run linting on the project',
|
||||
publint: 'Check package.json files for publishing standards',
|
||||
} as const;
|
||||
|
||||
/**
|
||||
* Initialize and run the CLI
|
||||
*/
|
||||
async function main(): Promise<void> {
|
||||
try {
|
||||
const vsh = cac('vsh');
|
||||
|
||||
// Register commands
|
||||
defineLintCommand(vsh);
|
||||
definePubLintCommand(vsh);
|
||||
defineCodeWorkspaceCommand(vsh);
|
||||
defineCheckCircularCommand(vsh);
|
||||
defineDepcheckCommand(vsh);
|
||||
|
||||
// Handle invalid commands
|
||||
vsh.on('command:*', ([cmd]) => {
|
||||
consola.error(
|
||||
colors.red(`Invalid command: ${cmd}`),
|
||||
'\n',
|
||||
colors.yellow('Available commands:'),
|
||||
'\n',
|
||||
Object.entries(COMMAND_DESCRIPTIONS)
|
||||
.map(([cmd, desc]) => ` ${colors.cyan(cmd)} - ${desc}`)
|
||||
.join('\n'),
|
||||
);
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
// Set up CLI
|
||||
vsh.usage('vsh <command> [options]');
|
||||
vsh.help();
|
||||
vsh.version(version);
|
||||
|
||||
// Parse arguments
|
||||
vsh.parse();
|
||||
} catch (error) {
|
||||
consola.error(
|
||||
colors.red('An unexpected error occurred:'),
|
||||
'\n',
|
||||
error instanceof Error ? error.message : error,
|
||||
);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
// Run the CLI
|
||||
main().catch((error) => {
|
||||
consola.error(
|
||||
colors.red('Failed to start CLI:'),
|
||||
'\n',
|
||||
error instanceof Error ? error.message : error,
|
||||
);
|
||||
process.exit(1);
|
||||
});
|
48
scripts/vsh/src/lint/index.ts
Normal file
48
scripts/vsh/src/lint/index.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
import type { CAC } from 'cac';
|
||||
|
||||
import { execaCommand } from '@vben/node-utils';
|
||||
|
||||
interface LintCommandOptions {
|
||||
/**
|
||||
* Format lint problem.
|
||||
*/
|
||||
format?: boolean;
|
||||
}
|
||||
|
||||
async function runLint({ format }: LintCommandOptions) {
|
||||
// process.env.FORCE_COLOR = '3';
|
||||
|
||||
if (format) {
|
||||
await execaCommand(`stylelint "**/*.{vue,css,less,scss}" --cache --fix`, {
|
||||
stdio: 'inherit',
|
||||
});
|
||||
await execaCommand(`eslint . --cache --fix`, {
|
||||
stdio: 'inherit',
|
||||
});
|
||||
await execaCommand(`prettier . --write --cache --log-level warn`, {
|
||||
stdio: 'inherit',
|
||||
});
|
||||
return;
|
||||
}
|
||||
await Promise.all([
|
||||
execaCommand(`eslint . --cache`, {
|
||||
stdio: 'inherit',
|
||||
}),
|
||||
execaCommand(`prettier . --ignore-unknown --check --cache`, {
|
||||
stdio: 'inherit',
|
||||
}),
|
||||
execaCommand(`stylelint "**/*.{vue,css,less,scss}" --cache`, {
|
||||
stdio: 'inherit',
|
||||
}),
|
||||
]);
|
||||
}
|
||||
|
||||
function defineLintCommand(cac: CAC) {
|
||||
cac
|
||||
.command('lint')
|
||||
.usage('Batch execute project lint check.')
|
||||
.option('--format', 'Format lint problem.')
|
||||
.action(runLint);
|
||||
}
|
||||
|
||||
export { defineLintCommand };
|
184
scripts/vsh/src/publint/index.ts
Normal file
184
scripts/vsh/src/publint/index.ts
Normal file
@@ -0,0 +1,184 @@
|
||||
import type { CAC } from 'cac';
|
||||
import type { Result } from 'publint';
|
||||
|
||||
import { basename, dirname, join } from 'node:path';
|
||||
|
||||
import {
|
||||
colors,
|
||||
consola,
|
||||
ensureFile,
|
||||
findMonorepoRoot,
|
||||
generatorContentHash,
|
||||
getPackages,
|
||||
outputJSON,
|
||||
readJSON,
|
||||
UNICODE,
|
||||
} from '@vben/node-utils';
|
||||
import { publint } from 'publint';
|
||||
import { formatMessage } from 'publint/utils';
|
||||
|
||||
const CACHE_FILE = join(
|
||||
'node_modules',
|
||||
'.cache',
|
||||
'publint',
|
||||
'.pkglintcache.json',
|
||||
);
|
||||
|
||||
interface PubLintCommandOptions {
|
||||
/**
|
||||
* Only errors are checked, no program exit is performed
|
||||
*/
|
||||
check?: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get files that require lint
|
||||
* @param files
|
||||
*/
|
||||
async function getLintFiles(files: string[] = []) {
|
||||
const lintFiles: string[] = [];
|
||||
|
||||
if (files?.length > 0) {
|
||||
return files.filter((file) => basename(file) === 'package.json');
|
||||
}
|
||||
|
||||
const { packages } = await getPackages();
|
||||
|
||||
for (const { dir } of packages) {
|
||||
lintFiles.push(join(dir, 'package.json'));
|
||||
}
|
||||
return lintFiles;
|
||||
}
|
||||
|
||||
function getCacheFile() {
|
||||
const root = findMonorepoRoot();
|
||||
return join(root, CACHE_FILE);
|
||||
}
|
||||
|
||||
async function readCache(cacheFile: string) {
|
||||
try {
|
||||
await ensureFile(cacheFile);
|
||||
return await readJSON(cacheFile);
|
||||
} catch {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
async function runPublint(files: string[], { check }: PubLintCommandOptions) {
|
||||
const lintFiles = await getLintFiles(files);
|
||||
const cacheFile = getCacheFile();
|
||||
|
||||
const cacheData = await readCache(cacheFile);
|
||||
const cache: Record<string, { hash: string; result: Result }> = cacheData;
|
||||
|
||||
const results = await Promise.all(
|
||||
lintFiles.map(async (file) => {
|
||||
try {
|
||||
const pkgJson = await readJSON(file);
|
||||
|
||||
if (pkgJson.private) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Reflect.deleteProperty(pkgJson, 'dependencies');
|
||||
Reflect.deleteProperty(pkgJson, 'devDependencies');
|
||||
Reflect.deleteProperty(pkgJson, 'peerDependencies');
|
||||
const content = JSON.stringify(pkgJson);
|
||||
const hash = generatorContentHash(content);
|
||||
|
||||
const publintResult: Result =
|
||||
cache?.[file]?.hash === hash
|
||||
? (cache?.[file]?.result ?? [])
|
||||
: await publint({
|
||||
level: 'suggestion',
|
||||
pkgDir: dirname(file),
|
||||
strict: true,
|
||||
});
|
||||
|
||||
cache[file] = {
|
||||
hash,
|
||||
result: publintResult,
|
||||
};
|
||||
|
||||
return { pkgJson, pkgPath: file, publintResult };
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}),
|
||||
);
|
||||
|
||||
await outputJSON(cacheFile, cache);
|
||||
printResult(results, check);
|
||||
}
|
||||
|
||||
function printResult(
|
||||
results: Array<{
|
||||
pkgJson: Record<string, number | string>;
|
||||
pkgPath: string;
|
||||
publintResult: Result;
|
||||
} | null>,
|
||||
check?: boolean,
|
||||
) {
|
||||
let errorCount = 0;
|
||||
let warningCount = 0;
|
||||
let suggestionsCount = 0;
|
||||
|
||||
for (const result of results) {
|
||||
if (!result) {
|
||||
continue;
|
||||
}
|
||||
const { pkgJson, pkgPath, publintResult } = result;
|
||||
const messages = publintResult?.messages ?? [];
|
||||
if (messages?.length < 1) {
|
||||
continue;
|
||||
}
|
||||
|
||||
consola.log('');
|
||||
consola.log(pkgPath);
|
||||
for (const message of messages) {
|
||||
switch (message.type) {
|
||||
case 'error': {
|
||||
errorCount++;
|
||||
|
||||
break;
|
||||
}
|
||||
case 'suggestion': {
|
||||
suggestionsCount++;
|
||||
break;
|
||||
}
|
||||
case 'warning': {
|
||||
warningCount++;
|
||||
|
||||
break;
|
||||
}
|
||||
// No default
|
||||
}
|
||||
const ruleUrl = `https://publint.dev/rules#${message.code.toLocaleLowerCase()}`;
|
||||
consola.log(
|
||||
` ${formatMessage(message, pkgJson)}${colors.dim(` ${ruleUrl}`)}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
const totalCount = warningCount + errorCount + suggestionsCount;
|
||||
if (totalCount > 0) {
|
||||
consola.error(
|
||||
colors.red(
|
||||
`${UNICODE.FAILURE} ${totalCount} problem (${errorCount} errors, ${warningCount} warnings, ${suggestionsCount} suggestions)`,
|
||||
),
|
||||
);
|
||||
!check && process.exit(1);
|
||||
} else {
|
||||
consola.log(colors.green(`${UNICODE.SUCCESS} No problem`));
|
||||
}
|
||||
}
|
||||
|
||||
function definePubLintCommand(cac: CAC) {
|
||||
cac
|
||||
.command('publint [...files]')
|
||||
.usage('Check if the monorepo package conforms to the publint standard.')
|
||||
.option('--check', 'Only errors are checked, no program exit is performed.')
|
||||
.action(runPublint);
|
||||
}
|
||||
|
||||
export { definePubLintCommand };
|
Reference in New Issue
Block a user