All files / src/rules export-service.ts

45% Statements 27/60
100% Branches 0/0
0% Functions 0/1
45% Lines 27/60

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 611x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                                                                    
import * as XLSX from 'xlsx';
import * as vscode from 'vscode';
import { t } from '../i18n/messages';
 
const RULES_HEADER = ['id', 'severity', 'description', 'message', 'languages', 'excludeLanguages'];
const RULES_EXAMPLE = [
  'no-todo', 'warning', '禁止提交 TODO 注释',
  '发现 TODO 注释,请清理后提交', 'javascript,typescript', '',
];
 
const GUIDE_AOA: string[][] = [
  ['字段', '含义', '取值/格式', '示例'],
  ['id', '规则唯一标识', '小写字母、数字、连字符,全局唯一', 'no-todo'],
  ['severity', '严重级别', 'error / warning / info', 'warning'],
  ['description', '规则简述(给人看)', '自由文本', '禁止提交 TODO 注释'],
  ['message', '命中时展示给开发者的提示语', '自由文本', '发现 TODO 注释,请清理后提交'],
  ['languages', '生效的语言', '逗号分隔,留空表示对所有语言生效', 'javascript,typescript'],
  ['excludeLanguages', '排除的语言', '逗号分隔,可留空', ''],
  ['', '', '', ''],
  ['填写说明', '', '', ''],
  ['1. severity 仅接受 error / warning / info 三个值', '', '', ''],
  ['2. languages / excludeLanguages 多值用英文逗号分隔', '', '', ''],
  ['3. 示例行可删除,仅作填写参考', '', '', ''],
  ['4. 该模板可直接用于「使用模板文件导入」功能回环校验', '', '', ''],
];
 
export async function exportTemplate(): Promise<void> {
  const uri = await vscode.window.showSaveDialog({
    defaultUri: vscode.Uri.file('code-review-rules-template.xlsx'),
    filters: { 'Excel': ['xlsx'] },
    saveLabel: t('exportTemplate.saveLabel'),
  });
  if (!uri) { return; }

  const wb = XLSX.utils.book_new();

  const wsRules = XLSX.utils.aoa_to_sheet([RULES_HEADER, RULES_EXAMPLE]);
  wsRules['!cols'] = [
    { wch: 16 }, { wch: 10 }, { wch: 32 }, { wch: 40 }, { wch: 24 }, { wch: 20 },
  ];
  XLSX.utils.book_append_sheet(wb, wsRules, '规则');

  const wsGuide = XLSX.utils.aoa_to_sheet(GUIDE_AOA);
  wsGuide['!cols'] = [{ wch: 22 }, { wch: 28 }, { wch: 42 }, { wch: 36 }];
  XLSX.utils.book_append_sheet(wb, wsGuide, '说明');

  try {
    XLSX.writeFile(wb, uri.fsPath);
    const openFolder = t('exportTemplate.openFolder');
    const choice = await vscode.window.showInformationMessage(
      t('exportTemplate.success'), openFolder,
    );
    if (choice === openFolder) {
      vscode.commands.executeCommand('revealFileInOS', uri);
    }
  } catch (err) {
    const msg = err instanceof Error ? err.message : String(err);
    vscode.window.showErrorMessage(t('exportTemplate.fail', { 0: msg }));
  }
}