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 fs = require('fs'); 17const path = require('path'); 18const urlPrefix = 'https://gitee.com/openharmony/utils_system_resources/raw/'; 19const urlSuffix = '/systemres/main/config.json'; 20 21exports.checkJSDoc = function (node, sourceFile, fileName) { 22 const checkLegality = require('./src/check_legality'); 23 return checkLegality.checkJsDocOfCurrentNode(node, sourceFile, fileName, false, false); 24}; 25 26exports.initEnv = function (version) { 27 const { checkOption } = require('./src/utils'); 28 return new Promise((resolve, reject) => { 29 const permissionName = getPermissionName(version); 30 const permissionConfig = getLocalPermissionConfig(permissionName); 31 if (permissionConfig) { 32 checkOption.permissionContent = permissionConfig; 33 resolve(); 34 return; 35 } 36 const workingBranch = version || 'mas' + 'ter'; 37 const url = `${urlPrefix}${workingBranch}${urlSuffix}`; 38 updatePermissionConfig(url, (content) => { 39 if (content) { 40 checkOption.permissionContent = content; 41 savePermissionConfig(content, permissionName); 42 } 43 resolve(); 44 }); 45 }); 46}; 47 48function updatePermissionConfig(url, callback) { 49 let requestText; 50 const https = require('https'); 51 const request = https.get(url, { timeout: 2000 }, (res) => { 52 res.on('data', (chunk) => { 53 if (typeof chunk === 'string') { 54 requestText = chunk; 55 } else { 56 const dataStr = new TextDecoder().decode(chunk); 57 requestText = requestText ? (requestText + dataStr) : dataStr; 58 } 59 }); 60 }).on('error', () => { 61 console.warn('use the default permission list.'); 62 }).on('close', () => { 63 callback(requestText); 64 }).on('timeout', () => { 65 request.destroy(); 66 }); 67} 68 69function getLocalPermissionConfig(name) { 70 const localPermissionFile = path.resolve(__dirname, name); 71 if (fs.existsSync(localPermissionFile)) { 72 const content = fs.readFileSync(localPermissionFile); 73 try { 74 JSON.parse(content); 75 return content; 76 } catch (err) { 77 console.warn(`parse error ${localPermissionFile}`); 78 } 79 } 80 return undefined; 81} 82 83function savePermissionConfig(content, name) { 84 const localPermissionFile = path.resolve(__dirname, name); 85 fs.writeFileSync(localPermissionFile, content); 86 console.log(`update permission configuration to ${localPermissionFile}`); 87} 88 89function getPermissionName(version) { 90 return `permissions_${version}.config.json`; 91} 92