161847f8eSopenharmony_ci/* 261847f8eSopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd. 361847f8eSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 461847f8eSopenharmony_ci * you may not use this file except in compliance with the License. 561847f8eSopenharmony_ci * You may obtain a copy of the License at 661847f8eSopenharmony_ci * 761847f8eSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 861847f8eSopenharmony_ci * 961847f8eSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1061847f8eSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1161847f8eSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1261847f8eSopenharmony_ci * See the License for the specific language governing permissions and 1361847f8eSopenharmony_ci * limitations under the License. 1461847f8eSopenharmony_ci */ 1561847f8eSopenharmony_ci 1661847f8eSopenharmony_ciconst fs = require('fs'); 1761847f8eSopenharmony_ciconst ts = require('typescript'); 1861847f8eSopenharmony_ciconst ExcelJS = require('exceljs'); 1961847f8eSopenharmony_ciconst path = require('path'); 2061847f8eSopenharmony_ciconst { sys } = require('typescript'); 2161847f8eSopenharmony_ci// 是否统计type类型API的开关,true表示统计 2261847f8eSopenharmony_ciconst typeCollection = true; 2361847f8eSopenharmony_ci// 是否不合并同名函数的开关,true不合并 2461847f8eSopenharmony_ciconst isNotMerge = true; 2561847f8eSopenharmony_ci 2661847f8eSopenharmony_ci// 解析文件 文本内容并将结果push到列表中并去重 2761847f8eSopenharmony_cifunction parse(files) { 2861847f8eSopenharmony_ci const api = []; 2961847f8eSopenharmony_ci const exportApi = []; 3061847f8eSopenharmony_ci const returnDeclarationArr = new Set([]); 3161847f8eSopenharmony_ci const hash = new Set([]); 3261847f8eSopenharmony_ci const fileContentList = []; 3361847f8eSopenharmony_ci files.forEach(file => { 3461847f8eSopenharmony_ci let fileContent = fs.readFileSync(file, 'utf-8'); 3561847f8eSopenharmony_ci fileContentList.push({ 3661847f8eSopenharmony_ci fileName: path.basename(file).replace(/.d.ts$/g, '.ts'), 3761847f8eSopenharmony_ci fileContent: fileContent, 3861847f8eSopenharmony_ci fileRoot: file, 3961847f8eSopenharmony_ci }); 4061847f8eSopenharmony_ci }); 4161847f8eSopenharmony_ci fileContentList.forEach(item => { 4261847f8eSopenharmony_ci const fileName = item.fileName.replace(/\.d.ts$/g, '.ts'); 4361847f8eSopenharmony_ci let packageName = item.fileRoot.indexOf('component\\ets\\') >= 0 || 4461847f8eSopenharmony_ci item.fileRoot.indexOf('component/ets/') >= 0 ? 'ArkUI' : fileName.replace((/\@|.ts$/g), '').replace(/D:\\/g, ''); 4561847f8eSopenharmony_ci ts.transpileModule(item.fileContent, { 4661847f8eSopenharmony_ci compilerOptions: { 4761847f8eSopenharmony_ci 'target': ts.ScriptTarget.ES2017, 4861847f8eSopenharmony_ci }, 4961847f8eSopenharmony_ci fileName: fileName, 5061847f8eSopenharmony_ci transformers: { before: [getReturnDeclarationArr(packageName, exportApi, returnDeclarationArr)] }, 5161847f8eSopenharmony_ci }); 5261847f8eSopenharmony_ci }); 5361847f8eSopenharmony_ci 5461847f8eSopenharmony_ci fileContentList.forEach(item => { 5561847f8eSopenharmony_ci const fileName = item.fileName.replace(/\.d.ts$/g, '.ts'); 5661847f8eSopenharmony_ci let packageName = item.fileRoot.indexOf('component\\ets\\') >= 0 || 5761847f8eSopenharmony_ci item.fileRoot.indexOf('component/ets/') >= 0 ? 'ArkUI' : fileName.replace(/\@|.ts$/g, '').replace(/D:\\/g, ''); 5861847f8eSopenharmony_ci ts.transpileModule(item.fileContent, { 5961847f8eSopenharmony_ci compilerOptions: { 6061847f8eSopenharmony_ci 'target': ts.ScriptTarget.ES2017, 6161847f8eSopenharmony_ci }, 6261847f8eSopenharmony_ci fileName: fileName, 6361847f8eSopenharmony_ci transformers: { before: [processDeclarationSourceFile(packageName, api, exportApi, returnDeclarationArr, hash, item.fileRoot)] }, 6461847f8eSopenharmony_ci }); 6561847f8eSopenharmony_ci }); 6661847f8eSopenharmony_ci return api; 6761847f8eSopenharmony_ci} 6861847f8eSopenharmony_ci 6961847f8eSopenharmony_ci// 获取返回值类型 7061847f8eSopenharmony_cifunction visitAllNode(node, returnDeclarationArr) { 7161847f8eSopenharmony_ci if ((ts.isMethodDeclaration(node) || ts.isFunctionDeclaration(node)) && node && node.type && 7261847f8eSopenharmony_ci ts.isTypeReferenceNode(node.type)) { 7361847f8eSopenharmony_ci returnDeclarationArr.add(node.type.typeName.getText()); 7461847f8eSopenharmony_ci } 7561847f8eSopenharmony_ci node.getChildren().forEach(item => { 7661847f8eSopenharmony_ci visitAllNode(item, returnDeclarationArr); 7761847f8eSopenharmony_ci }); 7861847f8eSopenharmony_ci} 7961847f8eSopenharmony_ci 8061847f8eSopenharmony_ci// 获取导入Api的数组 8161847f8eSopenharmony_cifunction getExportApi(node, packageName, exportApi) { 8261847f8eSopenharmony_ci node.statements.forEach(stat => { 8361847f8eSopenharmony_ci if (ts.isModuleDeclaration(stat)) { 8461847f8eSopenharmony_ci if (stat.getText().indexOf('namespace') > 0) { 8561847f8eSopenharmony_ci let apiInfo = { 8661847f8eSopenharmony_ci isSystemApi: '公开API', 8761847f8eSopenharmony_ci version: '', 8861847f8eSopenharmony_ci deprecated: '', 8961847f8eSopenharmony_ci permission: 'N/A', 9061847f8eSopenharmony_ci sysCap: 'N/A', 9161847f8eSopenharmony_ci model: '', 9261847f8eSopenharmony_ci }; 9361847f8eSopenharmony_ci exportApi.push({ 9461847f8eSopenharmony_ci packageName: packageName, 9561847f8eSopenharmony_ci className: stat.name.escapedText.toString(), 9661847f8eSopenharmony_ci methodName: '', 9761847f8eSopenharmony_ci apiInfo: getApiInfo(stat, apiInfo), 9861847f8eSopenharmony_ci }); 9961847f8eSopenharmony_ci } 10061847f8eSopenharmony_ci } 10161847f8eSopenharmony_ci }); 10261847f8eSopenharmony_ci} 10361847f8eSopenharmony_ci 10461847f8eSopenharmony_ci// 获取返回值类型和命名空间 10561847f8eSopenharmony_ciconst getReturnDeclarationArr = (packageName, exportApi, returnDeclarationArr) => { 10661847f8eSopenharmony_ci return (context) => { 10761847f8eSopenharmony_ci return (node) => { 10861847f8eSopenharmony_ci visitAllNode(node, returnDeclarationArr); 10961847f8eSopenharmony_ci getExportApi(node, packageName, exportApi); 11061847f8eSopenharmony_ci return node; 11161847f8eSopenharmony_ci }; 11261847f8eSopenharmony_ci }; 11361847f8eSopenharmony_ci}; 11461847f8eSopenharmony_ci 11561847f8eSopenharmony_ci 11661847f8eSopenharmony_ci// 搜集API接口并去重 11761847f8eSopenharmony_cifunction processDeclarationSourceFile(packageName, api, exportApi, returnDeclarationArr, hash, dtsPath) { 11861847f8eSopenharmony_ci return (context) => { 11961847f8eSopenharmony_ci return (node) => { 12061847f8eSopenharmony_ci const statements = node.statements; 12161847f8eSopenharmony_ci const currentClassFuncSet = new Set([]); 12261847f8eSopenharmony_ci const currentTypeList = new Array(); 12361847f8eSopenharmony_ci getCurrentTypeList(statements, currentTypeList); 12461847f8eSopenharmony_ci 12561847f8eSopenharmony_ci statements.forEach(stat => { 12661847f8eSopenharmony_ci let apiInfo = { 12761847f8eSopenharmony_ci isSystemApi: '公开API', 12861847f8eSopenharmony_ci version: '', 12961847f8eSopenharmony_ci deprecated: '', 13061847f8eSopenharmony_ci permission: 'N/A', 13161847f8eSopenharmony_ci sysCap: 'N/A', 13261847f8eSopenharmony_ci model: '', 13361847f8eSopenharmony_ci headimport: 'N/A', 13461847f8eSopenharmony_ci endexport: 'N/A', 13561847f8eSopenharmony_ci }; 13661847f8eSopenharmony_ci collectApi(packageName, api, stat, apiInfo, exportApi, returnDeclarationArr, hash, dtsPath, 13761847f8eSopenharmony_ci currentTypeList, currentClassFuncSet); 13861847f8eSopenharmony_ci }); 13961847f8eSopenharmony_ci return node; 14061847f8eSopenharmony_ci }; 14161847f8eSopenharmony_ci }; 14261847f8eSopenharmony_ci} 14361847f8eSopenharmony_ci 14461847f8eSopenharmony_cifunction getCurrentTypeList(statements, currentTypeList) { 14561847f8eSopenharmony_ci statements.forEach(stat => { 14661847f8eSopenharmony_ci if (ts.isTypeAliasDeclaration(stat)) { 14761847f8eSopenharmony_ci if (stat.type.types) { 14861847f8eSopenharmony_ci let typeObj = { 14961847f8eSopenharmony_ci name: stat.name.escapedText, 15061847f8eSopenharmony_ci value: [], 15161847f8eSopenharmony_ci }; 15261847f8eSopenharmony_ci stat.type.types.forEach(type => { 15361847f8eSopenharmony_ci if (type.literal && type.literal.text) { 15461847f8eSopenharmony_ci typeObj.value.push(type.literal.text); 15561847f8eSopenharmony_ci } 15661847f8eSopenharmony_ci }); 15761847f8eSopenharmony_ci if (typeObj.value.length > 0) { 15861847f8eSopenharmony_ci currentTypeList.push(typeObj); 15961847f8eSopenharmony_ci } 16061847f8eSopenharmony_ci } 16161847f8eSopenharmony_ci } 16261847f8eSopenharmony_ci }); 16361847f8eSopenharmony_ci} 16461847f8eSopenharmony_ci 16561847f8eSopenharmony_cifunction collectApi(packageName, api, stat, apiInfo, exportApi, returnDeclarationArr, hash, dtsPath, currentTypeList, 16661847f8eSopenharmony_ci currentClassFuncSet) { 16761847f8eSopenharmony_ci if (ts.isInterfaceDeclaration(stat)) { 16861847f8eSopenharmony_ci collectInterfaceDeclaration(stat, packageName, api, exportApi, returnDeclarationArr, hash, apiInfo, 16961847f8eSopenharmony_ci currentTypeList, dtsPath); 17061847f8eSopenharmony_ci } else if (ts.isModuleDeclaration(stat)) { 17161847f8eSopenharmony_ci collectModuleDeclaration(stat, packageName, api, exportApi, returnDeclarationArr, hash, apiInfo, 17261847f8eSopenharmony_ci currentTypeList, dtsPath); 17361847f8eSopenharmony_ci } else if (ts.isClassDeclaration(stat)) { 17461847f8eSopenharmony_ci collectClassDeclaration(stat, packageName, api, exportApi, returnDeclarationArr, hash, apiInfo, 17561847f8eSopenharmony_ci currentTypeList, dtsPath); 17661847f8eSopenharmony_ci } else if (ts.isEnumDeclaration(stat)) { 17761847f8eSopenharmony_ci collectEnumDeclaration(stat, packageName, api, exportApi, returnDeclarationArr, hash, apiInfo, 17861847f8eSopenharmony_ci currentTypeList, dtsPath); 17961847f8eSopenharmony_ci } else if (ts.isVariableStatement(stat)) { 18061847f8eSopenharmony_ci const apiName = stat.declarationList.declarations[0].name.escapedText; 18161847f8eSopenharmony_ci addApi(packageName, 'global', apiName, stat.getText().trim(), getApiInfo(stat, apiInfo), 18261847f8eSopenharmony_ci 'Decorator', api, hash, dtsPath, 8); 18361847f8eSopenharmony_ci } else { 18461847f8eSopenharmony_ci collectSpecialApi(stat, packageName, api, hash, currentClassFuncSet, dtsPath, exportApi, apiInfo); 18561847f8eSopenharmony_ci } 18661847f8eSopenharmony_ci} 18761847f8eSopenharmony_ci 18861847f8eSopenharmony_cifunction collectSpecialApi(stat, packageName, api, hash, currentClassFuncSet, dtsPath, exportApi, apiInfo) { 18961847f8eSopenharmony_ci if (ts.isMethodDeclaration(stat) || ts.isMethodSignature(stat) || ts.isFunctionDeclaration(stat)) { 19061847f8eSopenharmony_ci const methodName = stat.name.escapedText ? stat.name.escapedText.toString() : stat.name.text.toString(); 19161847f8eSopenharmony_ci let className = ''; 19261847f8eSopenharmony_ci exportApi.forEach(item => { 19361847f8eSopenharmony_ci if (item.methodName === methodName && item.packageName === packageName) { 19461847f8eSopenharmony_ci className = item.className; 19561847f8eSopenharmony_ci if (item.apiInfo) { 19661847f8eSopenharmony_ci apiInfo = item.apiInfo; 19761847f8eSopenharmony_ci } 19861847f8eSopenharmony_ci } 19961847f8eSopenharmony_ci }); 20061847f8eSopenharmony_ci addFunctionOnOffApi(packageName, className, methodName, getApiInfo(stat, apiInfo), 'Method', api, 20161847f8eSopenharmony_ci hash, currentClassFuncSet, stat, dtsPath); 20261847f8eSopenharmony_ci } 20361847f8eSopenharmony_ci} 20461847f8eSopenharmony_ci 20561847f8eSopenharmony_cifunction collectInterfaceDeclaration(stat, packageName, api, exportApi, returnDeclarationArr, hash, apiInfo, currentTypeList, dtsPath) { 20661847f8eSopenharmony_ci const className = stat.name.escapedText.toString(); 20761847f8eSopenharmony_ci const interfaceChildren = stat.members; 20861847f8eSopenharmony_ci let tmpApiInfo = getApiInfo(stat, apiInfo); 20961847f8eSopenharmony_ci collectEachChildNode(interfaceChildren, packageName, className, 'Field', api, exportApi, returnDeclarationArr, hash, 21061847f8eSopenharmony_ci tmpApiInfo, currentTypeList, dtsPath); 21161847f8eSopenharmony_ci} 21261847f8eSopenharmony_ci 21361847f8eSopenharmony_cifunction collectClassDeclaration(stat, packageName, api, exportApi, returnDeclarationArr, hash, apiInfo, currentTypeList, dtsPath) { 21461847f8eSopenharmony_ci const className = stat.name.escapedText.toString(); 21561847f8eSopenharmony_ci const classChildren = stat.members; 21661847f8eSopenharmony_ci let tmpApiInfo = getApiInfo(stat, apiInfo); 21761847f8eSopenharmony_ci collectEachChildNode(classChildren, packageName, className, 'Field', api, exportApi, returnDeclarationArr, hash, 21861847f8eSopenharmony_ci tmpApiInfo, currentTypeList, dtsPath); 21961847f8eSopenharmony_ci} 22061847f8eSopenharmony_ci 22161847f8eSopenharmony_cifunction collectEnumDeclaration(stat, packageName, api, exportApi, returnDeclarationArr, hash, apiInfo, currentTypeList, dtsPath) { 22261847f8eSopenharmony_ci const className = stat.name.escapedText.toString(); 22361847f8eSopenharmony_ci const enumChildren = stat.members; 22461847f8eSopenharmony_ci let tmpApiInfo = getApiInfo(stat, apiInfo); 22561847f8eSopenharmony_ci collectEachChildNode(enumChildren, packageName, className, 'Enum', api, exportApi, returnDeclarationArr, hash, 22661847f8eSopenharmony_ci tmpApiInfo, currentTypeList, dtsPath); 22761847f8eSopenharmony_ci} 22861847f8eSopenharmony_ci 22961847f8eSopenharmony_cifunction collectModuleDeclaration(stat, packageName, api, exportApi, returnDeclarationArr, hash, apiInfo, currentTypeList, dtsPath) { 23061847f8eSopenharmony_ci const className = stat.name.escapedText ? stat.name.escapedText.toString() : stat.name.text.toString(); 23161847f8eSopenharmony_ci const moduleChildren = stat.body.statements; 23261847f8eSopenharmony_ci let tmpApiInfo = getApiInfo(stat, apiInfo); 23361847f8eSopenharmony_ci collectEachChildNode(moduleChildren, packageName, className, 'Field', api, exportApi, returnDeclarationArr, hash, 23461847f8eSopenharmony_ci tmpApiInfo, currentTypeList, dtsPath); 23561847f8eSopenharmony_ci} 23661847f8eSopenharmony_ci 23761847f8eSopenharmony_cifunction collectTypeApi(child, packageName, className, api, hash, apiInfo, dtsPath) { 23861847f8eSopenharmony_ci let typeObj = { 23961847f8eSopenharmony_ci name: child.name.escapedText, 24061847f8eSopenharmony_ci value: [], 24161847f8eSopenharmony_ci }; 24261847f8eSopenharmony_ci if (child.type.types) { 24361847f8eSopenharmony_ci child.type.types?.forEach(type => { 24461847f8eSopenharmony_ci collectTypeApiInTypes(type, apiInfo, child, api, hash, dtsPath, typeObj, packageName); 24561847f8eSopenharmony_ci }); 24661847f8eSopenharmony_ci } else if (child.type.members) { 24761847f8eSopenharmony_ci child.type.members?.forEach(member => { 24861847f8eSopenharmony_ci collectTypeApiInMembers(member, apiInfo, child, api, hash, dtsPath, typeObj, packageName, className); 24961847f8eSopenharmony_ci }); 25061847f8eSopenharmony_ci } 25161847f8eSopenharmony_ci return typeObj; 25261847f8eSopenharmony_ci} 25361847f8eSopenharmony_ci 25461847f8eSopenharmony_cifunction collectTypeApiInTypes(type, apiInfo, child, api, hash, dtsPath, typeObj, packageName) { 25561847f8eSopenharmony_ci if (type.literal && type.literal.text) { 25661847f8eSopenharmony_ci typeObj.value.push(type.literal.text); 25761847f8eSopenharmony_ci if (typeCollection && type.literal) { 25861847f8eSopenharmony_ci let faterApiInfo = JSON.parse(JSON.stringify(apiInfo)); 25961847f8eSopenharmony_ci addApi(packageName, child.name.escapedText, type.literal.text, child.getText(), 26061847f8eSopenharmony_ci getApiInfo(child, faterApiInfo), 'Type', api, hash, dtsPath, 1); 26161847f8eSopenharmony_ci } 26261847f8eSopenharmony_ci } else { 26361847f8eSopenharmony_ci if (type.getText() !== '') { 26461847f8eSopenharmony_ci typeObj.value.push(type.getText()); 26561847f8eSopenharmony_ci if (typeCollection && type.literal) { 26661847f8eSopenharmony_ci let faterApiInfo = JSON.parse(JSON.stringify(apiInfo)); 26761847f8eSopenharmony_ci addApi(packageName, child.name.escapedText, type.getText(), child.getText(), 26861847f8eSopenharmony_ci getApiInfo(child, faterApiInfo), 'Type', api, hash, dtsPath, 2); 26961847f8eSopenharmony_ci } 27061847f8eSopenharmony_ci } 27161847f8eSopenharmony_ci } 27261847f8eSopenharmony_ci} 27361847f8eSopenharmony_ci 27461847f8eSopenharmony_cifunction collectTypeApiInMembers(member, apiInfo, child, api, hash, dtsPath, typeObj, packageName, className) { 27561847f8eSopenharmony_ci member.type.types?.forEach(type => { 27661847f8eSopenharmony_ci if (type.literal && type.literal.text) { 27761847f8eSopenharmony_ci typeObj.value.push(type.literal.text); 27861847f8eSopenharmony_ci if (typeCollection) { 27961847f8eSopenharmony_ci let faterApiInfo = JSON.parse(JSON.stringify(apiInfo)); 28061847f8eSopenharmony_ci addApi(packageName, child.name.escapedText, type.literal.text, child.getText(), 28161847f8eSopenharmony_ci getApiInfo(child, faterApiInfo), 'Type', api, hash, dtsPath, 3); 28261847f8eSopenharmony_ci } 28361847f8eSopenharmony_ci } else { 28461847f8eSopenharmony_ci if (type.getText() !== '') { 28561847f8eSopenharmony_ci typeObj.value.push(type.getText()); 28661847f8eSopenharmony_ci if (typeCollection) { 28761847f8eSopenharmony_ci let faterApiInfo = JSON.parse(JSON.stringify(apiInfo)); 28861847f8eSopenharmony_ci addApi(packageName, className, child.name.escapedText, child.getText(), 28961847f8eSopenharmony_ci getApiInfo(child, faterApiInfo), 'Type', api, hash, dtsPath, 4); 29061847f8eSopenharmony_ci } 29161847f8eSopenharmony_ci } 29261847f8eSopenharmony_ci } 29361847f8eSopenharmony_ci }); 29461847f8eSopenharmony_ci} 29561847f8eSopenharmony_ci 29661847f8eSopenharmony_cifunction collectEachChildNode(children, packageName, className, faterApiType, api, exportApi, returnDeclarationArr, 29761847f8eSopenharmony_ci hash, apiInfo, currentTypeList, dtsPath) { 29861847f8eSopenharmony_ci const currentClassFunSet = new Set([]); 29961847f8eSopenharmony_ci children.forEach(child => { 30061847f8eSopenharmony_ci if (ts.isTypeAliasDeclaration(child)) { 30161847f8eSopenharmony_ci if (child.type) { 30261847f8eSopenharmony_ci let typeObj = collectTypeApi(child, packageName, className, api, hash, apiInfo, dtsPath); 30361847f8eSopenharmony_ci if (typeObj.value.length > 0) { 30461847f8eSopenharmony_ci currentTypeList.push(typeObj); 30561847f8eSopenharmony_ci } 30661847f8eSopenharmony_ci } 30761847f8eSopenharmony_ci } 30861847f8eSopenharmony_ci }); 30961847f8eSopenharmony_ci children.forEach(child => { 31061847f8eSopenharmony_ci let faterApiInfo = JSON.parse(JSON.stringify(apiInfo)); 31161847f8eSopenharmony_ci let apiType = new String(faterApiType); 31261847f8eSopenharmony_ci if (/export.*\{.*\}/g.test(child.getText())) { 31361847f8eSopenharmony_ci exportApi.push({ 31461847f8eSopenharmony_ci packageName: packageName, 31561847f8eSopenharmony_ci className: className, 31661847f8eSopenharmony_ci methodName: child.getText().replace('export', '').replace('{', '').replace('}', '').replace(';', '').trim(), 31761847f8eSopenharmony_ci apiInfo: faterApiInfo, 31861847f8eSopenharmony_ci }); 31961847f8eSopenharmony_ci return; 32061847f8eSopenharmony_ci } 32161847f8eSopenharmony_ci if (ts.isInterfaceDeclaration(child)) { 32261847f8eSopenharmony_ci collectInterfaceDeclaration(child, packageName, api, exportApi, returnDeclarationArr, hash, faterApiInfo, currentTypeList, dtsPath); 32361847f8eSopenharmony_ci } else if (ts.isModuleDeclaration(child)) { 32461847f8eSopenharmony_ci collectModuleDeclaration(child, packageName, api, exportApi, returnDeclarationArr, hash, faterApiInfo, currentTypeList, dtsPath); 32561847f8eSopenharmony_ci } else if (ts.isClassDeclaration(child)) { 32661847f8eSopenharmony_ci collectClassDeclaration(child, packageName, api, exportApi, returnDeclarationArr, hash, faterApiInfo, currentTypeList, dtsPath); 32761847f8eSopenharmony_ci } else if (ts.isEnumDeclaration(child)) { 32861847f8eSopenharmony_ci collectEnumDeclaration(child, packageName, api, exportApi, returnDeclarationArr, hash, faterApiInfo, currentTypeList, dtsPath); 32961847f8eSopenharmony_ci } else { 33061847f8eSopenharmony_ci if ((ts.isMethodDeclaration(child) || ts.isMethodSignature(child) || ts.isFunctionDeclaration(child)) && 33161847f8eSopenharmony_ci (child.name.escapedText === 'on' || child.name.escapedText === 'off') && child.parameters && child.parameters.length > 0) { 33261847f8eSopenharmony_ci apiType = 'Method'; 33361847f8eSopenharmony_ci collectSubscriptionTypeApi(child, apiType, packageName, className, faterApiType, api, currentTypeList, 33461847f8eSopenharmony_ci hash, currentClassFunSet, dtsPath); 33561847f8eSopenharmony_ci } else { 33661847f8eSopenharmony_ci collectOtherApi(child, packageName, className, faterApiInfo, apiType, api, 33761847f8eSopenharmony_ci hash, currentClassFunSet, child, dtsPath, returnDeclarationArr); 33861847f8eSopenharmony_ci } 33961847f8eSopenharmony_ci } 34061847f8eSopenharmony_ci }); 34161847f8eSopenharmony_ci} 34261847f8eSopenharmony_ci 34361847f8eSopenharmony_cifunction collectOtherApi(child, packageName, className, faterApiInfo, apiType, api, 34461847f8eSopenharmony_ci hash, currentClassFunSet, child, dtsPath, returnDeclarationArr) { 34561847f8eSopenharmony_ci let methodName = ''; 34661847f8eSopenharmony_ci if (isSpecialMethod(child)) { 34761847f8eSopenharmony_ci if (child.name) { 34861847f8eSopenharmony_ci methodName = child.name.getText(); 34961847f8eSopenharmony_ci } else { 35061847f8eSopenharmony_ci methodName = className; 35161847f8eSopenharmony_ci } 35261847f8eSopenharmony_ci apiType = 'Method'; 35361847f8eSopenharmony_ci } else if (ts.isPropertyDeclaration(child) || ts.isPropertySignature(child)) { 35461847f8eSopenharmony_ci if (child.type && child.type.parameters) { 35561847f8eSopenharmony_ci methodName = child.name.escapedText; 35661847f8eSopenharmony_ci apiType = 'Method'; 35761847f8eSopenharmony_ci } else { 35861847f8eSopenharmony_ci methodName = child.name.escapedText; 35961847f8eSopenharmony_ci apiType = 'Field'; 36061847f8eSopenharmony_ci } 36161847f8eSopenharmony_ci } else { 36261847f8eSopenharmony_ci if (child.name) { 36361847f8eSopenharmony_ci methodName = child.name.getText(); 36461847f8eSopenharmony_ci } 36561847f8eSopenharmony_ci } 36661847f8eSopenharmony_ci if (methodName !== '') { 36761847f8eSopenharmony_ci addFunctionOnOffApi(packageName, className, methodName, faterApiInfo, apiType, api, 36861847f8eSopenharmony_ci hash, currentClassFunSet, child, dtsPath); 36961847f8eSopenharmony_ci } else { 37061847f8eSopenharmony_ci if (child.getText().indexOf('constructor') === 0) { 37161847f8eSopenharmony_ci methodName = 'constructor'; 37261847f8eSopenharmony_ci apiType = 'Method'; 37361847f8eSopenharmony_ci } else if (child.getText().indexOf('const') === 0) { 37461847f8eSopenharmony_ci const infoObj = collectFieleOrConstant(apiType, methodName, child, returnDeclarationArr); 37561847f8eSopenharmony_ci methodName = infoObj.methodName; 37661847f8eSopenharmony_ci apiType = infoObj.apiType; 37761847f8eSopenharmony_ci } else if (/\w+:\s*\w+/g.test(child.getText())) { 37861847f8eSopenharmony_ci apiType = 'Field'; 37961847f8eSopenharmony_ci methodName = child.getText().split(':')[0].trim(); 38061847f8eSopenharmony_ci } 38161847f8eSopenharmony_ci if (methodName !== '') { 38261847f8eSopenharmony_ci addApi(packageName, className, methodName, child.getText(), 38361847f8eSopenharmony_ci getApiInfo(child, faterApiInfo), apiType, api, hash, dtsPath, 5); 38461847f8eSopenharmony_ci } 38561847f8eSopenharmony_ci } 38661847f8eSopenharmony_ci} 38761847f8eSopenharmony_ci 38861847f8eSopenharmony_cifunction isSpecialMethod(child){ 38961847f8eSopenharmony_ci return ts.isMethodDeclaration(child) || ts.isMethodSignature(child) || ts.isFunctionDeclaration(child) || 39061847f8eSopenharmony_ci ts.isCallSignatureDeclaration(child) || ts.isConstructSignatureDeclaration(child) || 39161847f8eSopenharmony_ci ts.isIndexSignatureDeclaration(child); 39261847f8eSopenharmony_ci} 39361847f8eSopenharmony_ci 39461847f8eSopenharmony_cifunction collectFieleOrConstant(apiType, methodName, child, returnDeclarationArr) { 39561847f8eSopenharmony_ci if (child.getText().replace('const', '').indexOf(':') > 0) { 39661847f8eSopenharmony_ci if (returnDeclarationArr.has(child.getText().replace('const', '').split(':')[1].trim())) { 39761847f8eSopenharmony_ci apiType = 'Field'; 39861847f8eSopenharmony_ci } else { 39961847f8eSopenharmony_ci apiType = 'Constant'; 40061847f8eSopenharmony_ci } 40161847f8eSopenharmony_ci methodName = child.getText().replace('const', '').split(':')[0].trim(); 40261847f8eSopenharmony_ci } else if (child.getText().replace('const', '').indexOf('=') > 0) { 40361847f8eSopenharmony_ci if (returnDeclarationArr.has(child.getText().replace('const', '').split('=')[1].trim())) { 40461847f8eSopenharmony_ci apiType = 'Field'; 40561847f8eSopenharmony_ci } else { 40661847f8eSopenharmony_ci apiType = 'Constant'; 40761847f8eSopenharmony_ci } 40861847f8eSopenharmony_ci methodName = child.getText().replace('const', '').split('=')[0].trim(); 40961847f8eSopenharmony_ci } 41061847f8eSopenharmony_ci return { apiType, methodName }; 41161847f8eSopenharmony_ci} 41261847f8eSopenharmony_ci 41361847f8eSopenharmony_cifunction collectSubscriptionTypeApi(child, apiType, packageName, className, faterApiInfo, api, currentTypeList, 41461847f8eSopenharmony_ci hash, currentClassFunSet, dtsPath) { 41561847f8eSopenharmony_ci for (let i = 0; i < child.parameters.length; i++) { 41661847f8eSopenharmony_ci const param = child.parameters[i]; 41761847f8eSopenharmony_ci if (isCommonSubscriptionType(param)) { 41861847f8eSopenharmony_ci if (param.type && param.type.literal && param.type.literal.text) { 41961847f8eSopenharmony_ci collectTypeOrEventApi(packageName, className, faterApiInfo, apiType, api, 42061847f8eSopenharmony_ci hash, currentClassFunSet, child, dtsPath, param); 42161847f8eSopenharmony_ci } else if (param.type && param.type.types && param.type.types.length > 0) { 42261847f8eSopenharmony_ci collectSpecialSubscriptionTypeApi(param, packageName, className, faterApiInfo, apiType, api, 42361847f8eSopenharmony_ci hash, currentClassFunSet, child, dtsPath); 42461847f8eSopenharmony_ci } else if (param.type && param.type.typeName && param.type.typeName.escapedText) { 42561847f8eSopenharmony_ci inCurrentTypeListApi(packageName, className, faterApiInfo, apiType, api, 42661847f8eSopenharmony_ci hash, currentClassFunSet, child, dtsPath, currentTypeList, param); 42761847f8eSopenharmony_ci } else if (param.type && param.type.typeName && param.type.typeName.left && 42861847f8eSopenharmony_ci param.type.typeName.right) { 42961847f8eSopenharmony_ci let methodName = child.name.escapedText + '_' + param.type.typeName.left.escapedText + '_' + 43061847f8eSopenharmony_ci param.type.typeName.right.escapedText; 43161847f8eSopenharmony_ci addFunctionOnOffApi(packageName, className, methodName, faterApiInfo, apiType, api, 43261847f8eSopenharmony_ci hash, currentClassFunSet, child, dtsPath); 43361847f8eSopenharmony_ci } else { 43461847f8eSopenharmony_ci let methodName = child.name.escapedText; 43561847f8eSopenharmony_ci addFunctionOnOffApi(packageName, className, methodName, faterApiInfo, apiType, api, 43661847f8eSopenharmony_ci hash, currentClassFunSet, child, dtsPath); 43761847f8eSopenharmony_ci } 43861847f8eSopenharmony_ci break; 43961847f8eSopenharmony_ci } else { 44061847f8eSopenharmony_ci let methodName = child.name.escapedText; 44161847f8eSopenharmony_ci addFunctionOnOffApi(packageName, className, methodName, faterApiInfo, apiType, api, 44261847f8eSopenharmony_ci hash, currentClassFunSet, child, dtsPath); 44361847f8eSopenharmony_ci } 44461847f8eSopenharmony_ci } 44561847f8eSopenharmony_ci} 44661847f8eSopenharmony_ci 44761847f8eSopenharmony_cifunction isCommonSubscriptionType(param) { 44861847f8eSopenharmony_ci return param.name.escapedText === 'type' || param.name.escapedText === 'event' || 44961847f8eSopenharmony_ci param.name.escapedText === 'eventType'; 45061847f8eSopenharmony_ci} 45161847f8eSopenharmony_ci 45261847f8eSopenharmony_cifunction collectSpecialSubscriptionTypeApi(param, packageName, className, faterApiInfo, apiType, api, 45361847f8eSopenharmony_ci hash, currentClassFunSet, child, dtsPath) { 45461847f8eSopenharmony_ci param.type.types.forEach(type => { 45561847f8eSopenharmony_ci if (type.literal && type.literal.text) { 45661847f8eSopenharmony_ci const methodName = child.name.escapedText + '_' + type.literal.text; 45761847f8eSopenharmony_ci addFunctionOnOffApi(packageName, className, methodName, faterApiInfo, apiType, api, 45861847f8eSopenharmony_ci hash, currentClassFunSet, child, dtsPath); 45961847f8eSopenharmony_ci } 46061847f8eSopenharmony_ci }); 46161847f8eSopenharmony_ci} 46261847f8eSopenharmony_ci 46361847f8eSopenharmony_cifunction inCurrentTypeListApi(packageName, className, faterApiInfo, apiType, api, hash, currentClassFunSet, child, 46461847f8eSopenharmony_ci dtsPath, currentTypeList, param) { 46561847f8eSopenharmony_ci if (currentTypeList && currentTypeList.length > 0) { 46661847f8eSopenharmony_ci currentTypeList.forEach(type => { 46761847f8eSopenharmony_ci if (type.name === param.type.typeName.escapedText) { 46861847f8eSopenharmony_ci type.value.forEach(typeString => { 46961847f8eSopenharmony_ci let methodName = child.name.escapedText + '_' + typeString; 47061847f8eSopenharmony_ci addFunctionOnOffApi(packageName, className, methodName, faterApiInfo, apiType, api, 47161847f8eSopenharmony_ci hash, currentClassFunSet, child, dtsPath); 47261847f8eSopenharmony_ci }); 47361847f8eSopenharmony_ci } 47461847f8eSopenharmony_ci }); 47561847f8eSopenharmony_ci } else { 47661847f8eSopenharmony_ci let methodName = child.name.escapedText; 47761847f8eSopenharmony_ci addFunctionOnOffApi(packageName, className, methodName, faterApiInfo, apiType, api, 47861847f8eSopenharmony_ci hash, currentClassFunSet, child, dtsPath); 47961847f8eSopenharmony_ci } 48061847f8eSopenharmony_ci} 48161847f8eSopenharmony_ci 48261847f8eSopenharmony_cifunction collectTypeOrEventApi(packageName, className, faterApiInfo, apiType, api, 48361847f8eSopenharmony_ci hash, currentClassFunSet, child, dtsPath, param) { 48461847f8eSopenharmony_ci const typeTextArr = param.getText().replace(/\s*/g, '').split(':'); 48561847f8eSopenharmony_ci if (typeTextArr[0] === 'type' || typeTextArr[0] === 'event') { 48661847f8eSopenharmony_ci let methodName = child.name.escapedText + '_' + param.type.literal.text; 48761847f8eSopenharmony_ci addFunctionOnOffApi(packageName, className, methodName, faterApiInfo, apiType, api, 48861847f8eSopenharmony_ci hash, currentClassFunSet, child, dtsPath); 48961847f8eSopenharmony_ci } else { 49061847f8eSopenharmony_ci let methodName = child.name.escapedText + '_' + param.type.literal.text; 49161847f8eSopenharmony_ci addFunctionOnOffApi(packageName, className, methodName, faterApiInfo, apiType, api, 49261847f8eSopenharmony_ci hash, currentClassFunSet, child, dtsPath); 49361847f8eSopenharmony_ci } 49461847f8eSopenharmony_ci} 49561847f8eSopenharmony_ci 49661847f8eSopenharmony_cifunction addFunctionOnOffApi(packageName, className, methodName, apiInfo, apiType, api, 49761847f8eSopenharmony_ci hash, currentClassFunSet, childNode, dtsPath) { 49861847f8eSopenharmony_ci // 合并同名函数 49961847f8eSopenharmony_ci if (currentClassFunSet.has(methodName) && !isNotMerge) { 50061847f8eSopenharmony_ci collectSameNameApiText(api, packageName, className, methodName, childNode); 50161847f8eSopenharmony_ci } else { 50261847f8eSopenharmony_ci notMergeSameNameFun(packageName, className, methodName, apiInfo, apiType, api, 50361847f8eSopenharmony_ci hash, currentClassFunSet, childNode, dtsPath); 50461847f8eSopenharmony_ci } 50561847f8eSopenharmony_ci} 50661847f8eSopenharmony_ci 50761847f8eSopenharmony_cifunction collectSameNameApiText(api, packageName, className, methodName, childNode) { 50861847f8eSopenharmony_ci for (let i = 0; i < api.length; i++) { 50961847f8eSopenharmony_ci const curApi = api[i]; 51061847f8eSopenharmony_ci if (curApi.packageName === packageName && curApi.className === className && 51161847f8eSopenharmony_ci curApi.methodName === methodName) { 51261847f8eSopenharmony_ci if (curApi.methodText.indexOf(`${childNode.getText().replace('declare', '').trim()}`) < 0) { 51361847f8eSopenharmony_ci curApi.methodText += `\n${childNode.getText().replace('declare', '').replace(/\r|\n/ig, '').trim()}`; 51461847f8eSopenharmony_ci break; 51561847f8eSopenharmony_ci } 51661847f8eSopenharmony_ci } 51761847f8eSopenharmony_ci } 51861847f8eSopenharmony_ci} 51961847f8eSopenharmony_ci 52061847f8eSopenharmony_cifunction notMergeSameNameFun(packageName, className, methodName, apiInfo, apiType, api, 52161847f8eSopenharmony_ci hash, currentClassFunSet, childNode, dtsPath) { 52261847f8eSopenharmony_ci if (!currentClassFunSet.has(methodName)) { 52361847f8eSopenharmony_ci currentClassFunSet.add(methodName); 52461847f8eSopenharmony_ci addApi(packageName, className, methodName, childNode.getText().replace('declare', '').trim(), 52561847f8eSopenharmony_ci getApiInfo(childNode, apiInfo), apiType, api, hash, dtsPath, 6); 52661847f8eSopenharmony_ci } else { 52761847f8eSopenharmony_ci if (childNode.getFullText().indexOf('\/**') >= 0) { 52861847f8eSopenharmony_ci addApi(packageName, className, methodName, childNode.getText().replace('declare', '').trim(), 52961847f8eSopenharmony_ci getApiInfo(childNode, apiInfo), apiType, api, hash, dtsPath, 7); 53061847f8eSopenharmony_ci } else { 53161847f8eSopenharmony_ci let firstApiInfo = {}; 53261847f8eSopenharmony_ci handleSameNameApiJsDoc(api, firstApiInfo, packageName, className, methodName); 53361847f8eSopenharmony_ci addApi(packageName, className, methodName, childNode.getText().replace('declare', '').trim(), 53461847f8eSopenharmony_ci firstApiInfo, apiType, api, hash, dtsPath, 8); 53561847f8eSopenharmony_ci } 53661847f8eSopenharmony_ci } 53761847f8eSopenharmony_ci} 53861847f8eSopenharmony_ci 53961847f8eSopenharmony_cifunction handleSameNameApiJsDoc(api, firstApiInfo, packageName, className, methodName) { 54061847f8eSopenharmony_ci for (let i = 0; i < api.length; i++) { 54161847f8eSopenharmony_ci const curApi = api[i]; 54261847f8eSopenharmony_ci if (curApi.packageName === packageName && curApi.className === className && 54361847f8eSopenharmony_ci curApi.methodName === methodName) { 54461847f8eSopenharmony_ci firstApiInfo.isSystemApi = curApi.isSystemApi; 54561847f8eSopenharmony_ci firstApiInfo.version = curApi.version; 54661847f8eSopenharmony_ci firstApiInfo.sysCap = curApi.sysCap; 54761847f8eSopenharmony_ci firstApiInfo.permission = curApi.permission; 54861847f8eSopenharmony_ci firstApiInfo.model = curApi.model; 54961847f8eSopenharmony_ci firstApiInfo.deprecated = curApi.deprecated; 55061847f8eSopenharmony_ci } 55161847f8eSopenharmony_ci } 55261847f8eSopenharmony_ci} 55361847f8eSopenharmony_ci 55461847f8eSopenharmony_cifunction getApiInfo(node, apiInfo) { 55561847f8eSopenharmony_ci const notesStr = node.getFullText().replace(node.getText(), ''); 55661847f8eSopenharmony_ci apiInfo.model = getModelInfo(notesStr); 55761847f8eSopenharmony_ci apiInfo.errorCode = getErrorCode(notesStr); 55861847f8eSopenharmony_ci apiInfo.deprecated = getDeprecatedInfo(notesStr); 55961847f8eSopenharmony_ci apiInfo.permission = getPermissionInfo(notesStr); 56061847f8eSopenharmony_ci apiInfo.isSystemApi = getSystemApi(notesStr); 56161847f8eSopenharmony_ci apiInfo.version = getSinceVersion(notesStr); 56261847f8eSopenharmony_ci apiInfo.sysCap = getSyscap(notesStr); 56361847f8eSopenharmony_ci apiInfo.formInfo = isForm(notesStr); 56461847f8eSopenharmony_ci apiInfo.isCrossPlatform = isCrossPlatform(notesStr); 56561847f8eSopenharmony_ci return apiInfo; 56661847f8eSopenharmony_ci} 56761847f8eSopenharmony_ci 56861847f8eSopenharmony_cifunction getSystemApi(notesStr) { 56961847f8eSopenharmony_ci let isSystemApi = ''; 57061847f8eSopenharmony_ci if (/\@[S|s][Y|y][S|s][T|t][E|e][M|m][A|a][P|p][I|i]/g.test(notesStr)) { 57161847f8eSopenharmony_ci isSystemApi = '系统API'; 57261847f8eSopenharmony_ci } else { 57361847f8eSopenharmony_ci isSystemApi = '公开API'; 57461847f8eSopenharmony_ci } 57561847f8eSopenharmony_ci} 57661847f8eSopenharmony_ci 57761847f8eSopenharmony_cifunction getSinceVersion(notesStr) { 57861847f8eSopenharmony_ci let version; 57961847f8eSopenharmony_ci if (/\@[S|s][I|i][N|n][C|c][E|e]\s*(\d+)/g.test(notesStr)) { 58061847f8eSopenharmony_ci notesStr.replace(/\@[S|s][I|i][N|n][C|c][E|e]\s*(\d+)/g, (versionInfo) => { 58161847f8eSopenharmony_ci version = versionInfo.replace(/\@[S|s][I|i][N|n][C|c][E|e]/g, '').trim(); 58261847f8eSopenharmony_ci }); 58361847f8eSopenharmony_ci } else { 58461847f8eSopenharmony_ci version = 'N/A'; 58561847f8eSopenharmony_ci } 58661847f8eSopenharmony_ci return version; 58761847f8eSopenharmony_ci} 58861847f8eSopenharmony_ci 58961847f8eSopenharmony_cifunction getSyscap(notesStr) { 59061847f8eSopenharmony_ci let syscap = ''; 59161847f8eSopenharmony_ci if (/\@[S|s][Y|y][S|s][C|c][A|a][P|p]\s*((\w|\.|\/|\{|\@|\}|\s)+)/g.test(notesStr)) { 59261847f8eSopenharmony_ci notesStr.replace(/\@[S|s][Y|y][S|s][C|c][A|a][P|p]\s*((\w|\.|\/|\{|\@|\}|\s)+)/g, sysCapInfo => { 59361847f8eSopenharmony_ci syscap = sysCapInfo.replace(/\@[S|s][Y|y][S|s][C|c][A|a][P|p]/g, '').trim(); 59461847f8eSopenharmony_ci }); 59561847f8eSopenharmony_ci } 59661847f8eSopenharmony_ci return syscap; 59761847f8eSopenharmony_ci} 59861847f8eSopenharmony_ci 59961847f8eSopenharmony_cifunction isForm(notesStr) { 60061847f8eSopenharmony_ci let formInfo = ''; 60161847f8eSopenharmony_ci if (/\@form/g.test(notesStr)) { 60261847f8eSopenharmony_ci formInfo = '是'; 60361847f8eSopenharmony_ci } else { 60461847f8eSopenharmony_ci formInfo = '否'; 60561847f8eSopenharmony_ci } 60661847f8eSopenharmony_ci return formInfo; 60761847f8eSopenharmony_ci} 60861847f8eSopenharmony_ci 60961847f8eSopenharmony_cifunction getPermissionInfo(notesStr) { 61061847f8eSopenharmony_ci let permission = ''; 61161847f8eSopenharmony_ci if (/\@[P|p][E|e][R|r][M|m][I|i][S|s][S|s][I|i][O|o][N|n]\s*((\w|\.|\/|\{|\@|\}|\s)+)/g.test(notesStr)) { 61261847f8eSopenharmony_ci notesStr.replace(/\@[P|p][E|e][R|r][M|m][I|i][S|s][S|s][I|i][O|o][N|n]\s*((\w|\.|\/|\{|\@|\}|\s)+)/g, 61361847f8eSopenharmony_ci permissionInfo => { 61461847f8eSopenharmony_ci permission = permissionInfo.replace( 61561847f8eSopenharmony_ci /\@[P|p][E|e][R|r][M|m][I|i][S|s][S|s][I|i][O|o][N|n]/g, '').trim(); 61661847f8eSopenharmony_ci return permission; 61761847f8eSopenharmony_ci }); 61861847f8eSopenharmony_ci } else { 61961847f8eSopenharmony_ci permission = 'N/A'; 62061847f8eSopenharmony_ci } 62161847f8eSopenharmony_ci return permission; 62261847f8eSopenharmony_ci} 62361847f8eSopenharmony_ci 62461847f8eSopenharmony_cifunction getDeprecatedInfo(notesStr) { 62561847f8eSopenharmony_ci let deprecated = ''; 62661847f8eSopenharmony_ci if (/\@[D|d][E|e][P|p][R|r][E|e][C|c][A|a][T|t][E|e][D|d].*[S|s][I|i][N|n][C|c][E|e]\s*(\d+)/g.test(notesStr)) { 62761847f8eSopenharmony_ci notesStr.replace(/\@[D|d][E|e][P|p][R|r][E|e][C|c][A|a][T|t][E|e][D|d].*[S|s][I|i][N|n][C|c][E|e]\s*(\d+)/g, 62861847f8eSopenharmony_ci deprecatedInfo => { 62961847f8eSopenharmony_ci deprecated = deprecatedInfo.replace( 63061847f8eSopenharmony_ci /\@[D|d][E|e][P|p][R|r][E|e][C|c][A|a][T|t][E|e][D|d].*[S|s][I|i][N|n][C|c][E|e]\s*/g, '').trim(); 63161847f8eSopenharmony_ci 63261847f8eSopenharmony_ci }); 63361847f8eSopenharmony_ci } else { 63461847f8eSopenharmony_ci deprecated = 'N/A'; 63561847f8eSopenharmony_ci } 63661847f8eSopenharmony_ci return deprecated; 63761847f8eSopenharmony_ci} 63861847f8eSopenharmony_ci 63961847f8eSopenharmony_cifunction getErrorCode(notesStr) { 64061847f8eSopenharmony_ci let errorCode = ''; 64161847f8eSopenharmony_ci if (/\@throws { BusinessError } \d{2,18}/g.test(notesStr)) { 64261847f8eSopenharmony_ci notesStr.replace(/\@throws { BusinessError } \d{2,18}/g, (code) => { 64361847f8eSopenharmony_ci if (errorCode === '') { 64461847f8eSopenharmony_ci errorCode += `${code.replace(/\@throws { BusinessError } /, '')}`; 64561847f8eSopenharmony_ci } else { 64661847f8eSopenharmony_ci errorCode += `,${code.replace(/\@throws { BusinessError } /, '')}`; 64761847f8eSopenharmony_ci } 64861847f8eSopenharmony_ci }); 64961847f8eSopenharmony_ci } else { 65061847f8eSopenharmony_ci errorCode = 'N/A'; 65161847f8eSopenharmony_ci } 65261847f8eSopenharmony_ci return errorCode; 65361847f8eSopenharmony_ci} 65461847f8eSopenharmony_ci 65561847f8eSopenharmony_cifunction getModelInfo(notesStr) { 65661847f8eSopenharmony_ci let model = ''; 65761847f8eSopenharmony_ci if (/\@[F|f][A|a][M|m][O|o][D|d][E|e][L|l][O|o][N|n][L|l][Y|y]/g.test(notesStr)) { 65861847f8eSopenharmony_ci notesStr.replace(/\@[F|f][A|a][M|m][O|o][D|d][E|e][L|l][O|o][N|n][L|l][Y|y]/g, modelInfo => { 65961847f8eSopenharmony_ci model = modelInfo; 66061847f8eSopenharmony_ci }); 66161847f8eSopenharmony_ci } else if (/\@[S|s][T|t][A|a][G|g][E|e][M|m][O|o][D|d][E|e][L|l][O|o][N|n][L|l][Y|y]/g.test(notesStr)) { 66261847f8eSopenharmony_ci notesStr.replace(/\@[S|s][T|t][A|a][G|g][E|e][M|m][O|o][D|d][E|e][L|l][O|o][N|n][L|l][Y|y]/g, modelInfo => { 66361847f8eSopenharmony_ci model = modelInfo; 66461847f8eSopenharmony_ci }); 66561847f8eSopenharmony_ci } else { 66661847f8eSopenharmony_ci model = 'N/A'; 66761847f8eSopenharmony_ci } 66861847f8eSopenharmony_ci return model; 66961847f8eSopenharmony_ci} 67061847f8eSopenharmony_ci 67161847f8eSopenharmony_cifunction isCrossPlatform(notesStr) { 67261847f8eSopenharmony_ci let isCrossPlatform = ''; 67361847f8eSopenharmony_ci if (/\@crossplatform/g.test(notesStr)) { 67461847f8eSopenharmony_ci isCrossPlatform = '是'; 67561847f8eSopenharmony_ci } else { 67661847f8eSopenharmony_ci isCrossPlatform = '否'; 67761847f8eSopenharmony_ci } 67861847f8eSopenharmony_ci return isCrossPlatform; 67961847f8eSopenharmony_ci} 68061847f8eSopenharmony_ci 68161847f8eSopenharmony_cifunction addApi(packageName, className, methodName, methodText, apiInfo, apiType, api, hash, dtsPath, type) { 68261847f8eSopenharmony_ci let recard = isNotMerge ? `${packageName}.${className}/${methodName}/${methodText}` : 68361847f8eSopenharmony_ci `${packageName}.${className}/${methodName}`; 68461847f8eSopenharmony_ci if (!hash.has(recard)) { 68561847f8eSopenharmony_ci hash.add(recard); 68661847f8eSopenharmony_ci api.push({ 68761847f8eSopenharmony_ci packageName: packageName, 68861847f8eSopenharmony_ci className: className, 68961847f8eSopenharmony_ci methodName: methodName, 69061847f8eSopenharmony_ci methodText: methodText.replace(/export\s/g, ''), 69161847f8eSopenharmony_ci isSystemApi: apiInfo.isSystemApi, 69261847f8eSopenharmony_ci version: apiInfo.version, 69361847f8eSopenharmony_ci deprecated: apiInfo.deprecated, 69461847f8eSopenharmony_ci apiType: apiType, 69561847f8eSopenharmony_ci sysCap: apiInfo.sysCap, 69661847f8eSopenharmony_ci permission: apiInfo.permission, 69761847f8eSopenharmony_ci model: apiInfo.model, 69861847f8eSopenharmony_ci dtsPath: dtsPath, 69961847f8eSopenharmony_ci errorCode: apiInfo.errorCode, 70061847f8eSopenharmony_ci formInfo: apiInfo.formInfo, 70161847f8eSopenharmony_ci isCrossPlatform: apiInfo.isCrossPlatform, 70261847f8eSopenharmony_ci }); 70361847f8eSopenharmony_ci } 70461847f8eSopenharmony_ci} 70561847f8eSopenharmony_ci 70661847f8eSopenharmony_cifunction readFile(dir, utFiles) { 70761847f8eSopenharmony_ci try { 70861847f8eSopenharmony_ci const files = fs.readdirSync(dir); 70961847f8eSopenharmony_ci files.forEach((element) => { 71061847f8eSopenharmony_ci const filePath = path.join(dir, element); 71161847f8eSopenharmony_ci const status = fs.statSync(filePath); 71261847f8eSopenharmony_ci if (status.isDirectory()) { 71361847f8eSopenharmony_ci readFile(filePath, utFiles); 71461847f8eSopenharmony_ci } else { 71561847f8eSopenharmony_ci if (/\.d\.ts/.test(filePath)) { 71661847f8eSopenharmony_ci utFiles.push(filePath); 71761847f8eSopenharmony_ci } 71861847f8eSopenharmony_ci } 71961847f8eSopenharmony_ci }); 72061847f8eSopenharmony_ci } catch (e) { 72161847f8eSopenharmony_ci console.error('ETS ERROR: ' + e); 72261847f8eSopenharmony_ci } 72361847f8eSopenharmony_ci} 72461847f8eSopenharmony_ci 72561847f8eSopenharmony_cifunction exportData() { 72661847f8eSopenharmony_ci const dir = path.resolve(__dirname, './API'); 72761847f8eSopenharmony_ci let fileList = []; 72861847f8eSopenharmony_ci readFile(dir, fileList); 72961847f8eSopenharmony_ci let api = parse(fileList); 73061847f8eSopenharmony_ci excel(api); 73161847f8eSopenharmony_ci} 73261847f8eSopenharmony_ci 73361847f8eSopenharmony_ciasync function excel(api) { 73461847f8eSopenharmony_ci let buffer = await getExcelBuffer(api); 73561847f8eSopenharmony_ci fs.writeFile('Js_Api.xlsx', buffer, function (err) { 73661847f8eSopenharmony_ci if (err) { 73761847f8eSopenharmony_ci console.error(err); 73861847f8eSopenharmony_ci return; 73961847f8eSopenharmony_ci } 74061847f8eSopenharmony_ci }); 74161847f8eSopenharmony_ci} 74261847f8eSopenharmony_ci 74361847f8eSopenharmony_ciasync function getExcelBuffer(api) { 74461847f8eSopenharmony_ci const workbook = new ExcelJS.Workbook(); 74561847f8eSopenharmony_ci const sheet = workbook.addWorksheet('Js Api', { views: [{ xSplit: 1 }] }); 74661847f8eSopenharmony_ci sheet.getRow(1).values = ['模块名', '类名', '方法名', '函数', '类型', 'SysCap', 74761847f8eSopenharmony_ci '权限', '支持起始版本', '访问级别', '是否为卡片', '是否支持跨平台']; 74861847f8eSopenharmony_ci for (let i = 1; i <= api.length; i++) { 74961847f8eSopenharmony_ci const apiData = api[i - 1]; 75061847f8eSopenharmony_ci sheet.getRow(i + 1).values = [apiData.packageName, apiData.className, apiData.methodName, 75161847f8eSopenharmony_ci apiData.methodText, apiData.apiType, apiData.sysCap, apiData.permission, 75261847f8eSopenharmony_ci apiData.version, apiData.isSystemApi, apiData.formInfo, apiData.isCrossPlatform]; 75361847f8eSopenharmony_ci } 75461847f8eSopenharmony_ci const buffer = await workbook.xlsx.writeBuffer(); 75561847f8eSopenharmony_ci return buffer; 75661847f8eSopenharmony_ci} 75761847f8eSopenharmony_ci 75861847f8eSopenharmony_ciexportData();