57 lines
1.7 KiB
JavaScript
57 lines
1.7 KiB
JavaScript
#!/usr/bin/env node
|
|||
|
|
// 组件 WXSS 选择器检查 CLI
|
||
|
|
//
|
||
|
|
// node scripts/check-wxss-component-selectors.mjs miniapp/components
|
||
|
|
// node scripts/check-wxss-component-selectors.mjs <某个.wxss>
|
||
|
|
//
|
||
|
|
// ⚠️ 只对**自定义组件**目录有意义(page 的 wxss 允许标签选择器)。
|
||
|
|
//
|
||
|
|
// 由来:2026-09-19 开发者工具警告
|
||
|
|
// Some selectors are not allowed in component wxss ... (components/recommend-card/index.wxss:1:1092)
|
||
|
|
// 实测违规 1 处:`.compact-like image`(标签选择器)。
|
||
|
|
//
|
||
|
|
// 退出码:0 = 通过;1 = 存在违规;2 = 用法/路径错误。
|
||
|
|
|
||
|
|
import { relative } from 'node:path';
|
||
|
|
import { statSync } from 'node:fs';
|
||
|
|
import { checkWxss, readFileSync, walkWxss } from './lib/wxss-selectors.mjs';
|
||
|
|
|
||
|
|
const roots = process.argv.slice(2);
|
||
|
|
if (roots.length === 0) {
|
||
|
|
console.error('用法:node scripts/check-wxss-component-selectors.mjs <组件目录或文件> [...]');
|
||
|
|
process.exit(2);
|
||
|
|
}
|
||
|
|
|
||
|
|
const files = [];
|
||
|
|
for (const r of roots) {
|
||
|
|
let isFile = false;
|
||
|
|
try {
|
||
|
|
isFile = statSync(r).isFile();
|
||
|
|
} catch {
|
||
|
|
isFile = false;
|
||
|
|
}
|
||
|
|
if (isFile) {
|
||
|
|
files.push(r);
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
const found = walkWxss(r);
|
||
|
|
if (found.length === 0) {
|
||
|
|
console.error(`未找到 .wxss(路径不存在或目录为空):${r}`);
|
||
|
|
process.exit(2);
|
||
|
|
}
|
||
|
|
files.push(...found);
|
||
|
|
}
|
||
|
|
|
||
|
|
let failed = 0;
|
||
|
|
for (const f of files) {
|
||
|
|
const problems = checkWxss(readFileSync(f, 'utf8'));
|
||
|
|
if (problems.length) {
|
||
|
|
failed++;
|
||
|
|
console.log(`✗ ${relative(process.cwd(), f)}`);
|
||
|
|
for (const p of problems) console.log(` 第 ${p.line} 行:${p.message}`);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
console.log(`\n检查 ${files.length} 个组件 WXSS 文件,含违规选择器 ${failed} 个`);
|
||
|
|
process.exit(failed ? 1 : 0);
|