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 path = require('path');
17const fs = require('fs');
18const SECOND_PARAM = 2;
19
20function checkEntry(prId) {
21  let newToolResult = [];
22  const sourceDirname = __dirname;
23  __dirname = 'interface/sdk-js/build-tools/api_check_plugin';
24  const mdFilesPath = path.resolve(sourceDirname, '../../../../', 'all_files.txt');
25  const MAX_TIMES = 3;
26  let buffer = new Buffer.from('');
27  let i = 0;
28  let execute = false;
29  try {
30    const execSync = require('child_process').execSync;
31    do {
32      try {
33        buffer = execSync('cd interface/sdk-js/build-tools/api_diff && npm install && cd ../api_check_plugin && npm install', {
34          timeout: 120000,
35        });
36        execute = true;
37      } catch (error) { }
38    } while (++i < MAX_TIMES && !execute);
39    if (!execute) {
40      throw 'npm install timeout';
41    }
42    const { reqGitApi, getMdFiles } = require(path.resolve(__dirname, './src/api_check_plugin'));
43    const { ruleArr } = require(path.resolve(__dirname, './src/utils'));
44
45    const filePathArr = getMdFiles(mdFilesPath, false);
46    const filePath = filePathArr.join(',');
47    const resultPath = path.resolve(__dirname, './newResult.json');
48    const ruleInfo = ruleArr.join(',');
49    let ApiCheckResult = true;
50    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`, {
51      timeout: 120000,
52    });
53    if (fs.existsSync(path.resolve(__dirname, resultPath))) {
54      const newToolResultArr = require(resultPath);
55      if (newToolResultArr.length === 0) {
56        newToolResult.push('api_check: true');
57        return;
58      }
59      newToolResultArr.forEach(newToolResultInfo => {
60        const filePath = newToolResultInfo.buggyFilePath;
61        const apiIndex = filePath.indexOf('api');
62        const arktsIndex = filePath.indexOf('arkts');
63        newToolResultInfo.buggyFilePath = filePath.slice(apiIndex !== -1 ? apiIndex : arktsIndex !== -1 ? arktsIndex : 0, filePath.length);
64        newToolResult.push(newToolResultInfo);
65      });
66      newToolResult.push('api_check: false');
67      ApiCheckResult = false;
68    }
69    newToolResult = reqGitApi(newToolResult, prId, ApiCheckResult);
70    removeDir(path.resolve(__dirname, '../api_diff/node_modules'));
71    removeDir(path.resolve(__dirname, 'node_modules'));
72  } catch (error) {
73    // catch error
74    newToolResult.push(`API_CHECK_ERROR : ${error}`);
75    newToolResult.push(`buffer : ${buffer.toString()}`);
76  } finally {
77    writeResultFile(newToolResult, path.resolve(__dirname, './Result.txt'), {});
78  }
79}
80
81function removeDir(url) {
82  const statObj = fs.statSync(url);
83  if (statObj.isDirectory()) {
84    let dirs = fs.readdirSync(url);
85    dirs = dirs.map((dir) => path.join(url, dir));
86    for (let i = 0; i < dirs.length; i++) {
87      removeDir(dirs[i]);
88    }
89    fs.rmdirSync(url);
90  } else {
91    fs.unlinkSync(url);
92  }
93}
94
95function writeResultFile(resultData, outputPath, option) {
96  const STANDARD_INDENT = 2;
97  fs.writeFile(path.resolve(__dirname, outputPath), JSON.stringify(resultData, null, STANDARD_INDENT), option, (err) => {
98    if (err) {
99      console.error(`ERROR FOR CREATE FILE:${err}`);
100    } else {
101      console.log('API CHECK FINISH!');
102    }
103  });
104}
105
106checkEntry(process.argv[SECOND_PARAM]);
107