1/* 2 * Copyright (c) 2022-2025 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-2025 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`; 32 33 const label = `/** 34 * @file Defines all permissions. 35 * @kit AbilityKit 36 */ 37 38 /** 39 * Indicates permissions. 40 * 41 * @typedef Permissions 42 * @syscap SystemCapability.Security.AccessToken 43 * @since 9 44 */ 45 /** 46 * Indicates permissions. 47 * 48 * @typedef Permissions 49 * @syscap SystemCapability.Security.AccessToken 50 * @atomicservice 51 * @since 11 52 */\n`; 53 54 const label_1_2 = `/** 55 * @file Defines all permissions. 56 * @kit AbilityKit 57 */ 58 59 /** 60 * Indicates permissions. 61 * 62 * @typedef Permissions 63 * @syscap SystemCapability.Security.AccessToken 64 * @atomicservice 65 * @since 20 66 */\n`; 67 68const typeHead = 'export type Permissions =\n'; 69const typeTail = ';'; 70const tab = ' '; 71const tabs = tab.repeat(2); 72const tabzz = tab.repeat(3); 73const commentHead = `${tabs}/**\n`; 74const commentBody = `${tabzz}* `; 75const commentTail = `${tabzz}*/\n`; 76const sinceTag = '@since '; 77const arkTS20Version = 20; 78const deprecatedTag = '@deprecated '; 79const orOperator = `${tabs}|${tab}`; 80 81const getPermissions = downloadPath => { 82 try { 83 const content = fs.readFileSync(downloadPath, { encoding: 'utf8' }); 84 const configMap = JSON.parse(decodeURIComponent(content)); 85 return configMap.definePermissions.filter(permission => { 86 if (permission.availableType) { 87 return permission.availableType !== 'SERVICE'; 88 } 89 }); 90 } catch (error) { 91 console.error('Convert json file to object failed'); 92 } 93 return undefined; 94}; 95 96const getArkTsVersion = (permission, fileType) => { 97 if (fileType === 'ts') { 98 return permission.since; 99 } 100 if (permission.since <= 20) { 101 if (permission.deprecated && permission.deprecated !== '') { 102 const deprecatedVersion = permission.deprecated.replace(/[^\d]/g, ''); 103 if (parseInt(deprecatedVersion) <= 20) { 104 return undefined; 105 } 106 return arkTS20Version; 107 } 108 return arkTS20Version; 109 } else { 110 return permission.since; 111 } 112} 113 114const convertJsonToDTS = (permissions, outputFilePath) => { 115 if (fs.existsSync(outputFilePath)) { 116 fs.unlinkSync(outputFilePath); 117 } 118 const fileType = outputFilePath.substring(outputFilePath.lastIndexOf(".") + 1); 119 fs.appendFileSync(outputFilePath, copyRight, 'utf8'); 120 if (fileType === 'ts') { 121 fs.appendFileSync(outputFilePath, label, 'utf8'); 122 } else { 123 fs.appendFileSync(outputFilePath, label_1_2, 'utf8'); 124 } 125 fs.appendFileSync(outputFilePath, typeHead, 'utf8'); 126 127 permissions.forEach((permission, index) => { 128 const arkTsSinceVersion = getArkTsVersion(permission, fileType); 129 if (arkTsSinceVersion === undefined) { 130 return; 131 } 132 fs.appendFileSync(outputFilePath, commentHead, 'utf8'); 133 const since = `${commentBody}${sinceTag}${arkTsSinceVersion}\n`; 134 fs.appendFileSync(outputFilePath, since, 'utf8'); 135 if (permission.deprecated) { 136 const deprecated = `${commentBody}${deprecatedTag}${permission.deprecated}\n`; 137 fs.appendFileSync(outputFilePath, deprecated, 'utf8'); 138 } 139 fs.appendFileSync(outputFilePath, commentTail, 'utf8'); 140 const permissionName = `${index === 0 ? tabs : orOperator}'${permission.name}'${ 141 index === permissions.length - 1 ? '' : '\n' 142 }`; 143 fs.appendFileSync(outputFilePath, permissionName, 'utf8'); 144 }); 145 fs.appendFileSync(outputFilePath, typeTail, 'utf8'); 146 console.log('Convert module.json definePermissions to permissions successfully, filename = ' + outputFilePath); 147}; 148 149/** 150 * Read config.json file and convert it to permission.d.ts 151 * 152 * @param { 153 * filePath: name of downloaded config.json file 154 * outputFileName: name of converted d.ts file name 155 * } options 156 */ 157const convert = options => { 158 const permissions = getPermissions(options.filePath); 159 if (permissions) { 160 convertJsonToDTS(permissions, options.outputFilePath); 161 } else { 162 console.error('Config file does not have definePermissions property'); 163 } 164}; 165 166const arguments = process.argv; 167const options = { 168 filePath: arguments[2], 169 outputFilePath: arguments[3], 170}; 171try { 172 convert(options); 173} catch (error) { 174 console.error(`ERROR FOR CONVERTING PERMISSION: ${error}`); 175} 176