1/* 2 * Copyright (c) 2021-2022 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 { readFile, excel, parse, getExcelBuffer, applicationModules, } = require('./collectApi'); 17const path = require('path'); 18const ts = require('typescript') 19const { importFiles, applicationApis, finalClassName } = require('./collect_import_name'); 20function getApiData(fileData) { 21 const SDK_API_FILES = []; 22 getAllApiFiles(SDK_API_FILES); 23 const APP_API_FILES = []; 24 fileData.forEach((value, key) => { 25 for (let i = 0; i < SDK_API_FILES.length; i++) { 26 const filePath = SDK_API_FILES[i]; 27 if (path.basename(filePath).replace(/\.d\.ts/, '') === key) { 28 APP_API_FILES.push(filePath); 29 break; 30 } 31 } 32 }); 33 const apis = parse(APP_API_FILES); 34 const newApis = filterData(apis, fileData); 35 const finalApis = []; 36 for (let i = 0; i < applicationApis.length; i++) { 37 for (let j = 0; j < newApis.length; j++) { 38 if (!applicationApis[i].value) { 39 if (applicationApis[i].moduleName.match(new RegExp(newApis[j].className, 'i')) && 40 applicationApis[i].apiName == newApis[j].methodName) { 41 finalApis.push(newApis[j]); 42 } else if (applicationApis[i].apiName == newApis[j].className) { 43 finalApis.push(newApis[j]); 44 } else if (applicationApis[i].apiName == newApis[j].methodName) { 45 finalApis.push(newApis[j]); 46 } 47 } else { 48 if (applicationApis[i].apiName == newApis[j].className && 49 applicationApis[i].value == newApis[j].methodName) { 50 finalApis.push(newApis[j]); 51 } 52 } 53 } 54 } 55 for (let i = 0; i < applicationModules.length; i++) { 56 for (let j = 0; j < newApis.length; j++) { 57 if (applicationModules[i].packageName == newApis[j].packageName && 58 applicationModules[i].methodName == newApis[j].className) { 59 newApis[j].namespace = applicationModules[i].className; 60 } 61 } 62 } 63 let noRepeatApis = [...new Set(finalApis)]; 64 count(finalApis, noRepeatApis); 65 excel(noRepeatApis); 66} 67 68function getAllApiFiles(files) { 69 readFile(path.resolve(__dirname, '../sdk'), files); 70} 71 72function filterData(apis, rulers) { 73 const appApis = []; 74 rulers.forEach((value, key) => { 75 const modules = rulers.get(key); 76 modules.forEach(module => { 77 apis.forEach(api => { 78 if (key.replace(/^\@/, '') === api.packageName && module.match(new RegExp(api.className, 'i'))) { 79 appApis.push(api); 80 } else if (key.replace(/^\@/, '') === api.packageName) { 81 appApis.push(api); 82 } 83 }); 84 }); 85 }); 86 return appApis; 87} 88 89function count(finalApis, noRepeatApis) { 90 let newArr = new Array(noRepeatApis.length); 91 for (let j = 0; j < noRepeatApis.length; j++) { 92 let number = 0; 93 for (let k = 0; k < finalApis.length; k++) { 94 if (noRepeatApis[j] == finalApis[k]) { 95 number++ 96 } 97 } 98 noRepeatApis[j].count = number; 99 } 100} 101 102try { 103 getApiData(importFiles); 104} catch (error) { 105 console.error("FORMAT API ERROR: ", error); 106}