1/* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import ExcelJS from 'exceljs'; 17import path from 'path'; 18import fs from 'fs'; 19import { LogUtil } from '../utils/logUtil'; 20import { ToolNameExcelCallback, joinNewMessage, joinOldMessage, ToolReturnData } from './config'; 21import { BasicDiffInfo, diffTypeMap } from '../typedef/diff/ApiInfoDiff'; 22import { FunctionUtils } from '../utils/FunctionUtils'; 23import { SyscapProcessorHelper } from '../coreImpl/diff/syscapFieldProcessor'; 24 25export namespace WriterHelper { 26 export function JSONReporter(data: string, dest: string, fileName: string) { 27 const outputFile = path.resolve(dest, fileName); 28 fs.writeFileSync(outputFile, data); 29 LogUtil.i('JSONReporter', `report is in ${outputFile}`); 30 } 31 32 export async function ExcelReporter( 33 data: ToolReturnData, 34 dest: string, 35 fileName: string, 36 callback: ToolNameExcelCallback | undefined 37 ) { 38 const workbook: ExcelJS.Workbook = new ExcelJS.Workbook(); 39 const sheet: ExcelJS.Worksheet = workbook.addWorksheet(); 40 41 if (typeof callback === 'function') { 42 //callback是一个函数,才能当回调函数使用 43 callback(data, sheet, dest); 44 } 45 46 const buffer: NodeJS.ArrayBufferView = (await workbook.xlsx.writeBuffer()) as NodeJS.ArrayBufferView; 47 const outputFile: string = path.resolve(dest, fileName); 48 fs.writeFileSync(outputFile, buffer); 49 LogUtil.i('ExcelReporter', `report is in ${outputFile}`); 50 } 51 52 export class MarkdownReporter { 53 static writeInMarkdown(data: BasicDiffInfo[], dest: string | undefined): void { 54 const fileNameMap: Map<string, string> = FunctionUtils.readSubsystemFile().fileNameMap; 55 fileNameMap.forEach((fileName: string, syscap: string) => { 56 let diffsInSameSystem: BasicDiffInfo[] = []; 57 data.forEach((diffInfo: BasicDiffInfo) => { 58 if (SyscapProcessorHelper.getSyscapField(diffInfo) === syscap) { 59 diffsInSameSystem.push(diffInfo); 60 } 61 }); 62 if (diffsInSameSystem.length === 0) { 63 return; 64 } 65 66 MarkdownReporter.sortDiffInfoByStatus(diffsInSameSystem, fileName, dest); 67 }); 68 } 69 70 static sortDiffInfoByStatus(diffsInSameSystem: BasicDiffInfo[], fileName: string, dest: string | undefined): void { 71 const sortDiffInfos: BasicDiffInfo[] = []; 72 for (const type of diffTypeMap.keys()) { 73 diffsInSameSystem.forEach((diffInfo) => { 74 if (diffInfo.getDiffType() === type) { 75 sortDiffInfos.push(diffInfo); 76 } 77 }); 78 } 79 MarkdownReporter.exportDiffMd(fileName, sortDiffInfos, dest); 80 } 81 82 static exportDiffMd(fileName: string, diffInfos: BasicDiffInfo[], dest: string | undefined): void { 83 let markDownContent: string = 84 '| 操作 | 旧版本 | 新版本 | d.ts文件 |\n' + '| ---- | ------ | ------ | -------- |\n'; 85 for (let i = 0; i < diffInfos.length; i++) { 86 let diffInfo: BasicDiffInfo = diffInfos[i]; 87 const dtsName = diffInfo.getNewDtsName() ? diffInfo.getNewDtsName() : diffInfo.getOldDtsName(); 88 markDownContent += 89 `|${diffTypeMap.get(diffInfo.getDiffType())}|${MarkdownReporter.formatDiffMessage( 90 joinOldMessage(diffInfo) 91 )}` + `|${MarkdownReporter.formatDiffMessage(joinNewMessage(diffInfo))}|${dtsName.replace(/\\/g, '/')}|\n`; 92 } 93 const mdFilesDir = `${dest}\\diff合集`; 94 if (!fs.existsSync(mdFilesDir)) { 95 fs.mkdirSync(mdFilesDir); 96 } 97 98 fs.writeFileSync(`${dest}\\diff合集\\js-apidiff-${fileName}.md`, markDownContent); 99 } 100 101 static formatDiffMessage(diffMessage: string): string { 102 const message = diffMessage 103 .replace(/\r|\n/g, '<br>') 104 .replace(/\|/g, '\\|') 105 .replace(/\<(?!br>)/g, '\\<'); 106 return message; 107 } 108 } 109} 110