1/* 2 * Copyright (c) 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 fs = require('fs'); 17 18const copyRight = `/* 19 * Copyright (c) 2022 Huawei Device Co., Ltd. 20 * Licensed under the Apache License, Version 2.0 (the "License"); 21 * you may not use this file except in compliance with the License. 22 * You may obtain a copy of the License at 23 * 24 * http://www.apache.org/licenses/LICENSE-2.0 25 * 26 * Unless required by applicable law or agreed to in writing, software 27 * distributed under the License is distributed on an "AS IS" BASIS, 28 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 29 * See the License for the specific language governing permissions and 30 * limitations under the License. 31 */\n\n`; 32const typeHead = 'export type Permissions =\n'; 33const typeTail = ';'; 34const tab = ' '; 35const tabs = tab.repeat(2); 36const tabzz = tab.repeat(3); 37const commentHead = `${tabs}/**\n`; 38const commentBody = `${tabzz}* `; 39const commentTail = `${tabzz}*/\n`; 40const sinceTag = '@since '; 41const deprecatedTag = '@deprecated '; 42const orOperator = `${tabs}|${tab}`; 43 44const getPermissions = downloadPath => { 45 try { 46 const content = fs.readFileSync(downloadPath, { encoding: 'utf8' }); 47 const configMap = JSON.parse(decodeURIComponent(content)); 48 if (configMap.module.definePermissions) { 49 return configMap.module.definePermissions; 50 } 51 } catch (error) { 52 console.error('Convert json file to object failed'); 53 } 54 return undefined; 55}; 56 57const convertJsonToDTS = (permissions, outputFilePath) => { 58 if (fs.existsSync(outputFilePath)) { 59 fs.unlinkSync(outputFilePath); 60 } 61 fs.appendFileSync(outputFilePath, copyRight, 'utf8'); 62 fs.appendFileSync(outputFilePath, typeHead, 'utf8'); 63 permissions.forEach((permission, index) => { 64 if (permission.since || permission.deprecated) { 65 fs.appendFileSync(outputFilePath, commentHead, 'utf8'); 66 if (permission.since) { 67 const since = `${commentBody}${sinceTag}${permission.since}\n`; 68 fs.appendFileSync(outputFilePath, since, 'utf8'); 69 } 70 if (permission.deprecated) { 71 const deprecated = `${commentBody}${deprecatedTag}${permission.deprecated}\n`; 72 fs.appendFileSync(outputFilePath, deprecated, 'utf8'); 73 } 74 fs.appendFileSync(outputFilePath, commentTail, 'utf8'); 75 } 76 const permissionName = `${index === 0 ? tabs : orOperator}'${permission.name}'${ 77 index === permissions.length - 1 ? '' : '\n' 78 }`; 79 fs.appendFileSync(outputFilePath, permissionName, 'utf8'); 80 }); 81 fs.appendFileSync(outputFilePath, typeTail, 'utf8'); 82 console.log('Convert config.json definePermissions to permissions.d.ts successfully'); 83}; 84 85/** 86 * Read config.json file and convert it to permission.d.ts 87 * 88 * @param { 89 * filePath: name of downloaded config.json file 90 * outputFileName: name of converted d.ts file name 91 * } options 92 */ 93const convert = options => { 94 const permissions = getPermissions(options.filePath); 95 if (permissions) { 96 convertJsonToDTS(permissions, options.outputFilePath); 97 } else { 98 console.error('Config file does not have definePermissions property'); 99 } 100}; 101 102const arguments = process.argv; 103const options = { 104 filePath: arguments[2], 105 outputFilePath: arguments[3], 106}; 107try { 108 convert(options); 109} catch (error) { 110 console.error(`ERROR FOR CONVERTING PERMISSION: ${error}`); 111} 112