• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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        fs.appendFileSync(outputFilePath, permissionName, "utf8");
79    });
80    fs.appendFileSync(outputFilePath, typeTail, "utf8");
81    console.log(
82        "Convert config.json definePermissions to permissions.d.ts successfully"
83    );
84};
85
86/**
87 * Read config.json file and convert it to permission.d.ts
88 *
89 * @param {
90 *   filePath: name of downloaded config.json file
91 *   outputFileName: name of converted d.ts file name
92 * } options
93 */
94const convert = (options) => {
95    const permissions = getPermissions(options.filePath);
96    if (permissions) {
97        convertJsonToDTS(permissions, options.outputFilePath);
98    } else {
99        console.error("Config file does not have definePermissions property");
100    }
101};
102
103const arguments = process.argv;
104const options = {
105    filePath: arguments[2],
106    outputFilePath: arguments[3],
107};
108try {
109    convert(options);
110} catch (error) {
111    console.error(`ERROR FOR CONVERTING PERMISSION: ${error}`);
112}
113