Files
cindy/scripts/migrate-renderer-logger.cjs
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

105 lines
3.2 KiB
JavaScript

/**
* One-shot migration: rewrites `import { logger } from '@/lib/logger'` +
* `logger.log/warn/error` calls in apps/desktop/src/renderer to the new
* `createLogger`-based API. Idempotent.
*
* Run from repo root:
* node scripts/migrate-renderer-logger.cjs
*
* Skips:
* - __tests__/**
* - lib/logger.ts (the module itself)
* - *.d.ts
*/
const fs = require('node:fs');
const path = require('node:path');
const REPO_ROOT = path.resolve(__dirname, '..');
const ROOT = path.join(REPO_ROOT, 'apps', 'desktop', 'src', 'renderer');
const SKIP_DIR = new Set(['__tests__']);
function walk(dir, files = []) {
for (const ent of fs.readdirSync(dir, { withFileTypes: true })) {
if (ent.isDirectory()) {
if (SKIP_DIR.has(ent.name)) continue;
walk(path.join(dir, ent.name), files);
} else if (ent.isFile()) {
const p = path.join(dir, ent.name);
if (p === path.join(ROOT, 'lib', 'logger.ts')) continue;
if (ent.name.endsWith('.d.ts')) continue;
if (!ent.name.endsWith('.ts') && !ent.name.endsWith('.tsx')) continue;
files.push(p);
}
}
return files;
}
function scopeFromPath(filePath) {
let base = path.basename(filePath).replace(/\.(ts|tsx)$/, '');
if (base === 'index') {
base = path.basename(path.dirname(filePath));
}
// PascalCase the scope so renderer scopes are visually distinct in main.log.
return base.charAt(0).toUpperCase() + base.slice(1);
}
function migrate(filePath) {
const original = fs.readFileSync(filePath, 'utf-8');
let src = original;
// Quick check: does this file use the old `logger` import?
if (!/import\s*\{\s*logger\s*\}\s*from\s*['"]@\/lib\/logger['"]/.test(src)) {
return { changed: false, count: 0 };
}
let count = 0;
// logger.X( → log.Y( (only when preceded by safe lead chars)
src = src.replace(
/(^|[\s;{(,])logger\.(log|info|warn|error|debug|trace)\(/g,
(_m, lead, fn) => {
count++;
const map = { log: 'info', info: 'info', warn: 'warn', error: 'error', debug: 'debug', trace: 'trace' };
return `${lead}log.${map[fn]}(`;
},
);
// Swap the import.
src = src.replace(
/import\s*\{\s*logger\s*\}\s*from\s*(['"])@\/lib\/logger\1\s*;?/,
`import { createLogger } from '@/lib/logger';`,
);
// Inject `const log = createLogger(...)` after that import line if missing.
if (!/\bconst\s+log\s*=\s*createLogger\b/.test(src)) {
const scope = scopeFromPath(filePath);
src = src.replace(
/(import\s*\{\s*createLogger\s*\}\s*from\s*['"]@\/lib\/logger['"]\s*;?)/,
`$1\n\nconst log = createLogger('${scope}');`,
);
}
if (src !== original) {
fs.writeFileSync(filePath, src);
return { changed: true, count };
}
return { changed: false, count: 0 };
}
const files = walk(ROOT);
const summary = [];
let total = 0;
for (const f of files) {
const r = migrate(f);
if (r.changed) {
summary.push({ file: path.relative(ROOT, f).replace(/\\/g, '/'), count: r.count });
total += r.count;
}
}
process.stdout.write(`Files changed: ${summary.length}\n`);
process.stdout.write(`Total replacements: ${total}\n`);
for (const s of summary) {
process.stdout.write(` ${String(s.count).padStart(3)} ${s.file}\n`);
}