diff --git a/build-tools/.gitignore b/build-tools/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..788432e1970568d4104eed1ed8026e0a962cdb98 --- /dev/null +++ b/build-tools/.gitignore @@ -0,0 +1,5 @@ +**/node_modules/** +**/package-lock.json +**/.editorconfig +**/.prettierrc +api/ diff --git a/build-tools/permissions_converter/.vscode/settings.json b/build-tools/permissions_converter/.vscode/settings.json new file mode 100644 index 0000000000000000000000000000000000000000..51dc9242112d062632635868b503ec3dcfa674c5 --- /dev/null +++ b/build-tools/permissions_converter/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "editor.suggest.snippetsPreventQuickSuggestions": false +} diff --git a/build-tools/permissions_converter/convert.js b/build-tools/permissions_converter/convert.js new file mode 100644 index 0000000000000000000000000000000000000000..31af90f3d54fcc2828f853aa9d5c5fb81f1a5baa --- /dev/null +++ b/build-tools/permissions_converter/convert.js @@ -0,0 +1,111 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +const fs = require('fs'); + +const copyRight = `/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */\n\n`; +const typeHead = 'export type Permissions =\n'; +const typeTail = ';'; +const tab = ' '; +const tabs = tab.repeat(2); +const tabzz = tab.repeat(3); +const commentHead = `${tabs}/**\n`; +const commentBody = `${tabzz}* `; +const commentTail = `${tabzz}*/\n`; +const sinceTag = '@since '; +const deprecatedTag = '@deprecated '; +const orOperator = `${tabs}|${tab}`; + +const getPermissions = downloadPath => { + try { + const content = fs.readFileSync(downloadPath, { encoding: 'utf8' }); + const configMap = JSON.parse(decodeURIComponent(content)); + if (configMap.module.definePermissions) { + return configMap.module.definePermissions; + } + } catch (error) { + console.error('Convert json file to object failed'); + } + return undefined; +}; + +const convertJsonToDTS = (permissions, outputFilePath) => { + if (fs.existsSync(outputFilePath)) { + fs.rmSync(outputFilePath, { recursive: true, force: true }); + } + fs.appendFileSync(outputFilePath, copyRight, 'utf8'); + fs.appendFileSync(outputFilePath, typeHead, 'utf8'); + permissions.forEach((permission, index) => { + if (permission.since || permission.deprecated) { + fs.appendFileSync(outputFilePath, commentHead, 'utf8'); + if (permission.since) { + const since = `${commentBody}${sinceTag}${permission.since}\n`; + fs.appendFileSync(outputFilePath, since, 'utf8'); + } + if (permission.deprecated) { + const deprecated = `${commentBody}${deprecatedTag}${permission.deprecated}\n`; + fs.appendFileSync(outputFilePath, deprecated, 'utf8'); + } + fs.appendFileSync(outputFilePath, commentTail, 'utf8'); + } + const permissionName = `${index === 0 ? tabs : orOperator}'${permission.name}'${ + index === permissions.length - 1 ? '' : '\n' + }`; + fs.appendFileSync(outputFilePath, permissionName, 'utf8'); + }); + fs.appendFileSync(outputFilePath, typeTail, 'utf8'); + console.log('Convert config.json definePermissions to permissions.d.ts successfully'); +}; + +/** + * Read config.json file and convert it to permission.d.ts + * + * @param { + * filePath: name of downloaded config.json file + * outputFileName: name of converted d.ts file name + * } options + */ +const convert = options => { + const permissions = getPermissions(options.filePath); + if (permissions) { + convertJsonToDTS(permissions, options.outputFilePath); + } else { + console.error('Config file does not have definePermissions property'); + } +}; + +const arguments = process.argv; +const options = { + filePath: arguments[2], + outputFilePath: arguments[3], +}; +try { + convert(options); +} catch (error) { + console.error(`ERROR FOR CONVERTING PERMISSION: ${error}`); +} diff --git a/build-tools/permissions_converter/package.json b/build-tools/permissions_converter/package.json new file mode 100644 index 0000000000000000000000000000000000000000..a0dec26bd57461d1ace84f44986273b599cae801 --- /dev/null +++ b/build-tools/permissions_converter/package.json @@ -0,0 +1,6 @@ +{ + "name": "permissions", + "version": "1.0.0", + "description": "", + "main": "./convert.js" +}