curtis
9小時前 0756bf12d10cf1b7f78c571de0a9ad69cbaeb7ca
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/**
 * 需求 0.3.1 生成處理額外分類腳本
 * * 執行方式:
 * node scripts/scanimpl_annalysis_addon_json.js
 */
 
const fs = require('fs');
const path = require('path');
 
// 尋找目標檔案的遞迴函數
function findTargetJsonFiles(dir, fileList = []) {
  if (!fs.existsSync(dir)) return fileList;
 
  const files = fs.readdirSync(dir);
  for (const file of files) {
    const filePath = path.join(dir, file);
    const stat = fs.statSync(filePath);
 
    // 略過不需要掃描的系統或依賴目錄
    if (stat.isDirectory() && !['node_modules', '.git', 'scripts'].includes(file)) {
      findTargetJsonFiles(filePath, fileList);
    } else if (file.startsWith('scanimpl_annalysis.') && file.endsWith('.json')) {
      fileList.push(filePath);
    }
  }
  return fileList;
}
 
function main() {
  // 設定路徑參數 (以執行時的目錄為基準)
  const baseDir = path.join(process.cwd(), 'doc/curtis/prompt/scanimpl_analysis');
  const classificationFilePath = path.join(baseDir, '02', 'team_tasks_classification.json');
 
  console.log('=============================================');
  console.log('開始執行額外分類整合...');
  console.log(`讀取額外分類檔: ${classificationFilePath}`);
 
  if (!fs.existsSync(classificationFilePath)) {
    console.error(`[錯誤] 找不到額外分類檔: ${classificationFilePath}`);
    console.log('請確認是否在專案根目錄下執行此腳本。');
    return;
  }
 
  // 1. 讀取並解析 team_tasks_classification.json
  const classificationData = JSON.parse(fs.readFileSync(classificationFilePath, 'utf8'));
 
  // 建立 lineno 到 tags 的 Mapping 字典
  // 這麼做可以處理同一個 lineno 同時要新增多個不同 tag 的情況
  const addonMap = new Map();
  classificationData.forEach(item => {
    if (!item.lineno || !item.tag) return;
 
    // 將 lineno 轉為字串以確保後續比對型別一致
    const linenoStr = String(item.lineno);
    if (!addonMap.has(linenoStr)) {
      addonMap.set(linenoStr, new Set());
    }
    addonMap.get(linenoStr).add(item.tag);
  });
 
  console.log(`成功載入 ${addonMap.size} 筆需要更新的行號設定。`);
 
  // 2. 找出專案內所有的 scanimpl_annalysis.*.json 檔案
  const targetFiles = findTargetJsonFiles(baseDir);
 
  if (targetFiles.length === 0) {
    console.warn(`[警告] 在專案中找不到任何 scanimpl_annalysis.*.json 檔案`);
    return;
  }
 
  let totalUpdatedFiles = 0;
  let totalAddedTags = 0;
 
  // 3. 處理每一個目標 JSON 檔
  targetFiles.forEach(filePath => {
    const fileName = path.basename(filePath);
    let isModified = false;
    let fileContent;
 
    try {
      fileContent = JSON.parse(fs.readFileSync(filePath, 'utf8'));
    } catch (e) {
      console.error(`[錯誤] 解析檔案 ${fileName} 失敗:`, e.message);
      return;
    }
 
    // 0.3.1.1 步驟1 變更資料
    if (Array.isArray(fileContent)) {
      fileContent.forEach(methodDef => {
        const currentLIndex = String(methodDef.lIndex);
 
        // 檢查該方法的 lIndex 是否存在於我們要添加的對映表中
        if (addonMap.has(currentLIndex)) {
          const newTags = Array.from(addonMap.get(currentLIndex));
 
          if (!methodDef.tags) {
            methodDef.tags = [];
          }
 
          newTags.forEach(newTag => {
            // 避免重複添加相同的 tag
            if (!methodDef.tags.includes(newTag)) {
              methodDef.tags.push(newTag);
              isModified = true;
              totalAddedTags++;
              console.log(`  [新增] 檔案: ${fileName} | 行號: ${currentLIndex} | 新增 Tag: "${newTag}"`);
            }
          });
        }
      });
    }
 
    // 0.3.1.2 步驟2 寫入資料
    if (isModified) {
      fs.writeFileSync(filePath, JSON.stringify(fileContent, null, 2), 'utf8');
      totalUpdatedFiles++;
    }
  });
 
  console.log('=============================================');
  console.log(`執行完畢!`);
  console.log(`共更新了 ${totalUpdatedFiles} 個檔案,累計新增了 ${totalAddedTags} 個標籤。`);
}
 
main();