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 16const { ReporterFormat } = require('./configs'); 17const { Logger } = require('./utils'); 18const exceljs = require('exceljs'); 19const fs = require('fs'); 20const path = require('path'); 21 22class ApiJsonWriter { 23 constructor(outputPath) { 24 this.outputPath = outputPath; 25 this.apiInfos = []; 26 this.noSerializeKeys = new Set(['apiSourceFile', 'apiNode']); 27 } 28 29 add(apiInfos) { 30 this.apiInfos.push(...apiInfos); 31 } 32 33 flush() { 34 const output = path.resolve(this.outputPath, 'collectedApi.json'); 35 fs.writeFileSync(output, JSON.stringify(this.apiInfos, (key, value) => { 36 if (this.noSerializeKeys.has(key)) { 37 return undefined; 38 } 39 return value; 40 })); 41 Logger.info('ApiJsonWriter', `report is in ${output}`); 42 } 43} 44 45class ApiExcelWriter { 46 constructor(outputDir) { 47 this.outputDir = outputDir; 48 this.apiInfos = []; 49 this.enable = true; 50 } 51 52 close() { 53 this.enable = false; 54 } 55 56 open() { 57 this.enable = true; 58 } 59 60 add(apiInfos) { 61 this.apiInfos.push(...apiInfos.filter((value) => { 62 return !(value.packageName === 'ArkUI' && value.qualifiedTypeName === ''); 63 })); 64 } 65 66 async flush() { 67 if (!this.enable) { 68 return; 69 } 70 const apiInfoSet = new Set(); 71 const subscribeWorkbook = new exceljs.Workbook(); 72 const subscribeSheet = subscribeWorkbook.addWorksheet('Js Api', { views: [{ xSplit: 1 }] }); 73 subscribeSheet.getRow(1).values = ['类名', '接口名', '接口类型', '方法声明', '接口路径']; 74 let lineNumber = 0; 75 this.apiInfos.forEach((apiInfo, index) => { 76 const typeName = apiInfo.qualifiedTypeName ? apiInfo.qualifiedTypeName : (apiInfo.typeName ? apiInfo.typeName : 'unnamed'); 77 if (!apiInfoSet.has(formatInfo(apiInfo, typeName))) { 78 subscribeSheet.getRow(lineNumber + 2).values = [ 79 typeName, 80 apiInfo.propertyName, 81 apiInfo.apiType, 82 apiInfo.apiText.replace(/\;$/g, ''), 83 apiInfo.dtsPath 84 ]; 85 lineNumber++; 86 apiInfoSet.add(formatInfo(apiInfo, typeName)); 87 } 88 }); 89 const subscribeBuffer = await subscribeWorkbook.xlsx.writeBuffer(); 90 const subscribeOutputFile = path.resolve(this.outputDir, 'subscribe_api.xlsx'); 91 fs.writeFileSync(subscribeOutputFile, subscribeBuffer); 92 const workbook = new exceljs.Workbook(); 93 const sheet = workbook.addWorksheet('Js Api', { views: [{ xSplit: 1 }] }); 94 sheet.getRow(1).values = ['模块名', '类名', '方法名', '函数', '文件位置']; 95 this.apiInfos.forEach((apiInfo, index) => { 96 const typeName = apiInfo.componentName ? apiInfo.componentName : 97 (apiInfo.typeName ? apiInfo.typeName : apiInfo.qualifiedTypeName); 98 sheet.getRow(index + 2).values = [ 99 path.basename(apiInfo.packageName, '.d.ts').replace('@', ''), 100 typeName, 101 apiInfo.propertyName, 102 apiInfo.apiRawText, 103 `${apiInfo.sourceFileName}(${apiInfo.pos})` 104 ]; 105 }); 106 const buffer = await workbook.xlsx.writeBuffer(); 107 const outputFile = path.resolve(this.outputDir, 'app_api.xlsx'); 108 fs.writeFileSync(outputFile, buffer); 109 Logger.info('ApiExcelWriter', `report is in ${outputFile}`); 110 } 111} 112 113class ApiWriter { 114 constructor(outputPath, formatFlag) { 115 this.outputPath = outputPath; 116 this.formatFlag = formatFlag; 117 this.apiInfos = []; 118 } 119 120 add(apiInfos) { 121 this.apiInfos.push(...apiInfos); 122 } 123 124 async flush() { 125 if (this.formatFlag === ReporterFormat.FLAG_JSON) { 126 this.writeJson(this.apiInfos); 127 } else if (this.formatFlag === ReporterFormat.FLAG_EXCEL) { 128 await this.writeExcel(this.apiInfos); 129 } else if (this.formatFlag === ReporterFormat.FLAG_DEBUG) { 130 this.writeJson(this.apiInfos); 131 await this.writeExcel(this.apiInfos); 132 } else { 133 this.writeJson(this.apiInfos); 134 } 135 } 136 137 writeJson(apiInfos) { 138 const apiJsonWriter = new ApiJsonWriter(this.outputPath); 139 apiJsonWriter.add(apiInfos); 140 apiJsonWriter.flush(); 141 } 142 143 async writeExcel(apiInfos) { 144 const apiExcelWriter = new ApiExcelWriter(this.outputPath); 145 apiExcelWriter.add(apiInfos); 146 await apiExcelWriter.flush(); 147 } 148} 149 150function formatInfo(apiInfo, typeName) { 151 return `${typeName}_${apiInfo.propertyName}_${apiInfo.apiText}_ ${apiInfo.dtsPath}`; 152} 153 154exports.ApiJsonWriter = ApiJsonWriter; 155exports.ApiExcelWriter = ApiExcelWriter; 156exports.ApiWriter = ApiWriter;