161847f8eSopenharmony_ci/* 261847f8eSopenharmony_ci * Copyright (c) 2021-2022 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 path = require('path'); 1761847f8eSopenharmony_ciconst fs = require('fs'); 1861847f8eSopenharmony_ciconst SECOND_PARAM = 2; 1961847f8eSopenharmony_ci 2061847f8eSopenharmony_cifunction checkEntry(prId) { 2161847f8eSopenharmony_ci let newToolResult = []; 2261847f8eSopenharmony_ci const sourceDirname = __dirname; 2361847f8eSopenharmony_ci __dirname = 'interface/sdk-js/build-tools/api_check_plugin'; 2461847f8eSopenharmony_ci const mdFilesPath = path.resolve(sourceDirname, '../../../../', 'all_files.txt'); 2561847f8eSopenharmony_ci const MAX_TIMES = 3; 2661847f8eSopenharmony_ci let buffer = new Buffer.from(''); 2761847f8eSopenharmony_ci let i = 0; 2861847f8eSopenharmony_ci let execute = false; 2961847f8eSopenharmony_ci try { 3061847f8eSopenharmony_ci const execSync = require('child_process').execSync; 3161847f8eSopenharmony_ci do { 3261847f8eSopenharmony_ci try { 3361847f8eSopenharmony_ci buffer = execSync('cd interface/sdk-js/build-tools/api_diff && npm install && cd ../api_check_plugin && npm install', { 3461847f8eSopenharmony_ci timeout: 120000, 3561847f8eSopenharmony_ci }); 3661847f8eSopenharmony_ci execute = true; 3761847f8eSopenharmony_ci } catch (error) { } 3861847f8eSopenharmony_ci } while (++i < MAX_TIMES && !execute); 3961847f8eSopenharmony_ci if (!execute) { 4061847f8eSopenharmony_ci throw 'npm install timeout'; 4161847f8eSopenharmony_ci } 4261847f8eSopenharmony_ci const { reqGitApi, getMdFiles } = require(path.resolve(__dirname, './src/api_check_plugin')); 4361847f8eSopenharmony_ci const { ruleArr } = require(path.resolve(__dirname, './src/utils')); 4461847f8eSopenharmony_ci 4561847f8eSopenharmony_ci const filePathArr = getMdFiles(mdFilesPath, false); 4661847f8eSopenharmony_ci const filePath = filePathArr.join(','); 4761847f8eSopenharmony_ci const resultPath = path.resolve(__dirname, './newResult.json'); 4861847f8eSopenharmony_ci const ruleInfo = ruleArr.join(','); 4961847f8eSopenharmony_ci let ApiCheckResult = true; 5061847f8eSopenharmony_ci buffer = execSync(`cd interface/sdk-js/build-tools/dts_parser/package && node ./JS_API_CHECK.js -N checkOnline --path ${filePath} --checker ${ruleInfo} --prId ${prId} --output ${resultPath} --excel false`, { 5161847f8eSopenharmony_ci timeout: 120000, 5261847f8eSopenharmony_ci }); 5361847f8eSopenharmony_ci if (fs.existsSync(path.resolve(__dirname, resultPath))) { 5461847f8eSopenharmony_ci const newToolResultArr = require(resultPath); 5561847f8eSopenharmony_ci if (newToolResultArr.length === 0) { 5661847f8eSopenharmony_ci newToolResult.push('api_check: true'); 5761847f8eSopenharmony_ci return; 5861847f8eSopenharmony_ci } 5961847f8eSopenharmony_ci newToolResultArr.forEach(newToolResultInfo => { 6061847f8eSopenharmony_ci const filePath = newToolResultInfo.buggyFilePath; 6161847f8eSopenharmony_ci const apiIndex = filePath.indexOf('api'); 6261847f8eSopenharmony_ci const arktsIndex = filePath.indexOf('arkts'); 6361847f8eSopenharmony_ci newToolResultInfo.buggyFilePath = filePath.slice(apiIndex !== -1 ? apiIndex : arktsIndex !== -1 ? arktsIndex : 0, filePath.length); 6461847f8eSopenharmony_ci newToolResult.push(newToolResultInfo); 6561847f8eSopenharmony_ci }); 6661847f8eSopenharmony_ci newToolResult.push('api_check: false'); 6761847f8eSopenharmony_ci ApiCheckResult = false; 6861847f8eSopenharmony_ci } 6961847f8eSopenharmony_ci newToolResult = reqGitApi(newToolResult, prId, ApiCheckResult); 7061847f8eSopenharmony_ci removeDir(path.resolve(__dirname, '../api_diff/node_modules')); 7161847f8eSopenharmony_ci removeDir(path.resolve(__dirname, 'node_modules')); 7261847f8eSopenharmony_ci } catch (error) { 7361847f8eSopenharmony_ci // catch error 7461847f8eSopenharmony_ci newToolResult.push(`API_CHECK_ERROR : ${error}`); 7561847f8eSopenharmony_ci newToolResult.push(`buffer : ${buffer.toString()}`); 7661847f8eSopenharmony_ci } finally { 7761847f8eSopenharmony_ci writeResultFile(newToolResult, path.resolve(__dirname, './Result.txt'), {}); 7861847f8eSopenharmony_ci } 7961847f8eSopenharmony_ci} 8061847f8eSopenharmony_ci 8161847f8eSopenharmony_cifunction removeDir(url) { 8261847f8eSopenharmony_ci const statObj = fs.statSync(url); 8361847f8eSopenharmony_ci if (statObj.isDirectory()) { 8461847f8eSopenharmony_ci let dirs = fs.readdirSync(url); 8561847f8eSopenharmony_ci dirs = dirs.map((dir) => path.join(url, dir)); 8661847f8eSopenharmony_ci for (let i = 0; i < dirs.length; i++) { 8761847f8eSopenharmony_ci removeDir(dirs[i]); 8861847f8eSopenharmony_ci } 8961847f8eSopenharmony_ci fs.rmdirSync(url); 9061847f8eSopenharmony_ci } else { 9161847f8eSopenharmony_ci fs.unlinkSync(url); 9261847f8eSopenharmony_ci } 9361847f8eSopenharmony_ci} 9461847f8eSopenharmony_ci 9561847f8eSopenharmony_cifunction writeResultFile(resultData, outputPath, option) { 9661847f8eSopenharmony_ci const STANDARD_INDENT = 2; 9761847f8eSopenharmony_ci fs.writeFile(path.resolve(__dirname, outputPath), JSON.stringify(resultData, null, STANDARD_INDENT), option, (err) => { 9861847f8eSopenharmony_ci if (err) { 9961847f8eSopenharmony_ci console.error(`ERROR FOR CREATE FILE:${err}`); 10061847f8eSopenharmony_ci } else { 10161847f8eSopenharmony_ci console.log('API CHECK FINISH!'); 10261847f8eSopenharmony_ci } 10361847f8eSopenharmony_ci }); 10461847f8eSopenharmony_ci} 10561847f8eSopenharmony_ci 10661847f8eSopenharmony_cicheckEntry(process.argv[SECOND_PARAM]); 107