Files
cindy/scripts/hardcoded-color-audit.mjs
Lizi bb3f71d084 chore: Cindy 首次开源提交 / initial open-source release
Cindy 是心动网络 (X.D. Network Inc.) 的跨端 AI Agent 客户端。
本次提交包含 Desktop(Electron)、Mobile(Expo)及全部共享 packages
的完整源码,以 Apache-2.0 协议开源。

This is the first public release of Cindy, X.D. Network's cross-platform
AI agent client: the Electron desktop app, the Expo mobile app, and all
shared workspace packages, licensed under Apache-2.0.

- 许可与合规:根 LICENSE(Apache-2.0)+ NOTICE;第三方组件归属声明与
  SPDX SBOM 收口在 docs/legal/notices/,由 pnpm licenses:generate 生成。
- 社区文件:README / CONTRIBUTING / CODE_OF_CONDUCT / SECURITY / SUPPORT
  中英双语;issue / PR 模板与 CI workflow 在 .github/。
- 协议层以公开 submodule 引入:github.com/makecindy/cindy-protocol。
- 提交前已通过仓库门禁:全量 pnpm test:unit 与涉及 package 的 typecheck。

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-24 08:30:44 +08:00

78 lines
2.6 KiB
JavaScript

#!/usr/bin/env node
// 零残留扫描(计划 §2 P 节五要素合同):
// ① BASE_REF = PR target 分支(默认 main,可经 env 覆盖)
// ② 扫描范围 = 仅 diff **新增行**(+ 行,不含 context)
// ③ 固定 regex:#hex / rgb() / rgba() / hsl() / hsla()
// ④ exemption 版本化(scripts/hardcoded-color-exemptions.json,每条含 glob/理由/owner)
// ⑤ 输出 raw / allowed / unexpected 三计数,**仅 unexpected=0 通过**
//
// 用法: node scripts/hardcoded-color-audit.mjs [--base-ref <ref>]
// 可证伪:同一 BASE_REF 两台机器三计数一致;人为加一条未登记 hex → unexpected 0→1。
import { execSync } from "node:child_process";
import { readFileSync } from "node:fs";
const args = process.argv.slice(2);
let baseRef = "main";
for (let i = 0; i < args.length; i++) {
if (args[i] === "--base-ref" && args[i + 1]) baseRef = args[i + 1];
}
const HEX_RE = /#[0-9a-fA-F]{3,8}\b/g;
const RGB_RE = /rgba?\([^)]+\)/g;
const HSL_RE = /hsla?\([^)]+\)/g;
let exemptions = [];
try {
exemptions = JSON.parse(readFileSync("scripts/hardcoded-color-exemptions.json", "utf8"));
} catch {
exemptions = [];
}
function matchGlob(file, glob) {
const re = "^" + glob.replace(/[.+^${}()|[\]\\]/g, "\\$&").replace(/\*/g, ".*") + "$";
return new RegExp(re).test(file);
}
function isExempt(file) {
return exemptions.some((e) => matchGlob(file, e.glob));
}
function diffAddedHits(ref) {
const out = execSync(`git diff ${ref}...HEAD --unified=0`, {
encoding: "utf8",
maxBuffer: 64 * 1024 * 1024,
});
const hits = [];
let file = "";
for (const line of out.split("\n")) {
if (line.startsWith("+++ ")) { file = line.slice(4).replace(/^b\//, ""); }
else if (line.startsWith("+") && !line.startsWith("+++")) {
if (file.endsWith("hardcoded-color-exemptions.json")) continue;
const text = line.slice(1);
const all = [
...text.matchAll(HEX_RE),
...text.matchAll(RGB_RE),
...text.matchAll(HSL_RE),
].map((m) => m[0]);
for (const v of all) hits.push({ file, value: v, text: text.trim().slice(0, 80) });
}
}
return hits;
}
const hits = diffAddedHits(baseRef);
const allowed = hits.filter((h) => isExempt(h.file));
const unexpected = hits.filter((h) => !isExempt(h.file));
console.log(`BASE_REF: ${baseRef}`);
console.log(`raw: ${hits.length}`);
console.log(`allowed: ${allowed.length}`);
console.log(`unexpected: ${unexpected.length}`);
if (unexpected.length) {
console.log("--- unexpected(未登记的硬编码色) ---");
for (const h of unexpected) console.log(` ${h.file}: ${h.value} // ${h.text}`);
process.exit(1);
}
console.log("PASS: unexpected=0");