add permissions converter

Signed-off-by: herunlei <herunlei@huawei.com>
This commit is contained in:
herunlei 2022-10-17 21:01:22 +08:00
parent 9d97dbd28c
commit 8a305b252b
4 changed files with 125 additions and 0 deletions

5
build-tools/.gitignore vendored Normal file
View File

@ -0,0 +1,5 @@
**/node_modules/**
**/package-lock.json
**/.editorconfig
**/.prettierrc
api/

View File

@ -0,0 +1,3 @@
{
"editor.suggest.snippetsPreventQuickSuggestions": false
}

View File

@ -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}`);
}

View File

@ -0,0 +1,6 @@
{
"name": "permissions",
"version": "1.0.0",
"description": "",
"main": "./convert.js"
}