mirror of
https://github.com/makecindy/cindy.git
synced 2026-12-02 02:41:23 +08:00
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>
132 lines
3.9 KiB
JavaScript
132 lines
3.9 KiB
JavaScript
/**
|
|
* One-shot migration: rewrites console.log/info/warn/error/debug/trace in
|
|
* apps/desktop/src/main → log.info/warn/error/debug/trace via the new
|
|
* createLogger() module. Idempotent — safe to run twice.
|
|
*
|
|
* Run from repo root:
|
|
* node scripts/migrate-logger.cjs
|
|
*
|
|
* Skips:
|
|
* - __tests__/** (tests keep using console for dev convenience)
|
|
* - third_party/** (vendored, has its own logger conventions)
|
|
* - logger.ts (the new logger itself)
|
|
* - *.d.ts
|
|
*/
|
|
|
|
const fs = require('node:fs');
|
|
const path = require('node:path');
|
|
|
|
const REPO_ROOT = path.resolve(__dirname, '..');
|
|
const MAIN_ROOT = path.join(REPO_ROOT, 'apps', 'desktop', 'src', 'main');
|
|
|
|
const SKIP_DIR = new Set(['__tests__', 'third_party']);
|
|
const SKIP_FILE = new Set(['logger.ts']);
|
|
|
|
const LEVEL_MAP = {
|
|
log: 'info',
|
|
info: 'info',
|
|
warn: 'warn',
|
|
error: 'error',
|
|
debug: 'debug',
|
|
trace: 'trace',
|
|
};
|
|
|
|
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()) {
|
|
if (SKIP_FILE.has(ent.name)) continue;
|
|
if (!ent.name.endsWith('.ts') || ent.name.endsWith('.d.ts')) continue;
|
|
files.push(path.join(dir, ent.name));
|
|
}
|
|
}
|
|
return files;
|
|
}
|
|
|
|
function relImportPath(filePath) {
|
|
const fromDir = path.dirname(filePath);
|
|
const targetFile = path.join(MAIN_ROOT, 'logger');
|
|
let rel = path.relative(fromDir, targetFile).replace(/\\/g, '/');
|
|
if (!rel.startsWith('.')) rel = './' + rel;
|
|
return rel;
|
|
}
|
|
|
|
function scopeFromPath(filePath) {
|
|
const base = path.basename(filePath, '.ts');
|
|
if (base === 'index') {
|
|
return path.basename(path.dirname(filePath));
|
|
}
|
|
return base;
|
|
}
|
|
|
|
function migrate(filePath) {
|
|
const original = fs.readFileSync(filePath, 'utf-8');
|
|
let src = original;
|
|
|
|
// Replace console.X( → log.Y( only when preceded by whitespace, ;, {, (
|
|
// or comma — guards against matches inside string literals (lead is then
|
|
// a quote or backtick, which we don't include).
|
|
const callRx = /(^|[\s;{(,])console\.(log|info|warn|error|debug|trace)\(/g;
|
|
let count = 0;
|
|
src = src.replace(callRx, (_match, lead, fn) => {
|
|
count++;
|
|
return `${lead}log.${LEVEL_MAP[fn]}(`;
|
|
});
|
|
|
|
if (count === 0) return { changed: false, count: 0 };
|
|
|
|
// Inject the import + log instance if missing.
|
|
const importPath = relImportPath(filePath);
|
|
const scope = scopeFromPath(filePath);
|
|
|
|
const hasImport = /createLogger.*from\s+['"][^'"]*\/logger['"]/.test(src)
|
|
|| /from\s+['"][^'"]*\/logger['"][^;]*createLogger/.test(src);
|
|
const hasInstance = /\bconst\s+log\s*=\s*createLogger\b/.test(src);
|
|
|
|
if (!hasImport || !hasInstance) {
|
|
// Find the position after the last top-level import statement.
|
|
const importRx = /^import\s[^\n]+;\s*$/gm;
|
|
let lastImportEnd = 0;
|
|
let m;
|
|
while ((m = importRx.exec(src)) !== null) {
|
|
lastImportEnd = m.index + m[0].length;
|
|
}
|
|
|
|
let inject = '';
|
|
if (!hasImport) inject += `\nimport { createLogger } from '${importPath}';`;
|
|
if (!hasInstance) inject += `\n\nconst log = createLogger('${scope}');`;
|
|
inject += '\n';
|
|
|
|
if (lastImportEnd > 0) {
|
|
src = src.slice(0, lastImportEnd) + inject + src.slice(lastImportEnd);
|
|
} else {
|
|
src = inject.trimStart() + src;
|
|
}
|
|
}
|
|
|
|
if (src !== original) {
|
|
fs.writeFileSync(filePath, src);
|
|
return { changed: true, count };
|
|
}
|
|
return { changed: false, count: 0 };
|
|
}
|
|
|
|
const files = walk(MAIN_ROOT);
|
|
const summary = [];
|
|
let total = 0;
|
|
for (const f of files) {
|
|
const r = migrate(f);
|
|
if (r.changed) {
|
|
summary.push({ file: path.relative(MAIN_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(4)} ${s.file}\n`);
|
|
}
|