calude code 2.188 根据cli.map还原

得益于 cli.map的sourcesContent

然后还原脚本:

const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');

const MAP_FILE = './cli.js.map';
const OUTPUT_DIR = './restored_source';

function safeWriteFile(filePath, content) {
  fs.mkdirSync(path.dirname(filePath), { recursive: true });
  fs.writeFileSync(filePath, content, 'utf8');
}

function normalizeSourcePath(source, index) {
  if (!source || typeof source !== 'string') {
    return `unknown_${index}.js`;
  }

  let normalized = source
    .replace(/^webpack:\/\//, '')
    .replace(/^file:\/\//, '')
    .replace(/^\//, '')
    .replace(/[<>:"|?*]/g, '_');

  if (!normalized.trim()) {
    normalized = `unknown_${index}.js`;
  }

  return normalized;
}

function restoreMap() {
  if (!fs.existsSync(MAP_FILE)) {
    throw new Error(`找不到文件: ${MAP_FILE}`);
  }

  const map = JSON.parse(fs.readFileSync(MAP_FILE, 'utf8'));
  const sources = Array.isArray(map.sources) ? map.sources : [];
  const sourcesContent = Array.isArray(map.sourcesContent) ? map.sourcesContent : null;

  console.log(`sources 数量: ${sources.length}`);
  console.log(`包含 sourcesContent: ${!!sourcesContent}`);

  if (!sources.length) {
    throw new Error('map 文件里没有 sources');
  }

  if (!sourcesContent) {
    throw new Error('map 文件里没有 sourcesContent,无法直接还原完整源码');
  }

  let restoredCount = 0;

  sources.forEach((source, index) => {
    const content = sourcesContent[index];

    if (content == null) {
      console.warn(`跳过 ${source},因为没有对应内容`);
      return;
    }

    const relativePath = normalizeSourcePath(source, index);
    const outputPath = path.join(OUTPUT_DIR, relativePath);

    safeWriteFile(outputPath, content);
    restoredCount++;
    console.log(`已还原: ${outputPath}`);
  });

  console.log(`完成,共还原 ${restoredCount} 个文件`);
}

function formatFiles() {
  try {
    execSync(`npx prettier --write "${OUTPUT_DIR}/**/*.{js,ts,jsx,tsx,json,css,scss,vue}"`, {
      stdio: 'inherit'
    });
  } catch (err) {
    console.warn('格式化失败');
  }
}

function main() {
  try {
    restoreMap();
    formatFiles();
  } catch (err) {
    console.error('执行失败:', err.message);
    process.exit(1);
  }
}

main();

已还原: src\utils\zodToJsonSchema.ts
已还原: src\utils\toolSearch.ts
已还原: src\services\vcr.ts
已还原: src\services\tokenEstimation.ts
已还原: src\utils\pdf.ts
已还原: src\tools\FileReadTool\limits.ts
已还原: src\tools\FileReadTool\UI.tsx
已还原: src\tools\FileReadTool\FileReadTool.ts
已还原: src\types\textInputTypes.ts
已还原: src\utils\mcpInstructionsDelta.ts
已还原: src\utils\claudeInChrome\prompt.ts
已还原: src\utils\hooks\hookEvents.ts
已还原: src\utils\hooks\AsyncHookRegistry.ts
已还原: src\utils\messagePredicates.ts
已还原: src\memdir\findRelevantMemories.ts
已还原: src\utils\attachments.ts

posted @ 2026-04-02 09:20  尘梦  阅读(31)  评论(0)    收藏  举报