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-2024 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`;
53const typeHead = 'export type Permissions =\n';
54const typeTail = ';';
55const tab = ' ';
56const tabs = tab.repeat(2);
57const tabzz = tab.repeat(3);
58const commentHead = `${tabs}/**\n`;
59const commentBody = `${tabzz}* `;
60const commentTail = `${tabzz}*/\n`;
61const sinceTag = '@since ';
62const deprecatedTag = '@deprecated ';
63const orOperator = `${tabs}|${tab}`;
64
65const getPermissions = downloadPath => {
66  try {
67    const content = fs.readFileSync(downloadPath, { encoding: 'utf8' });
68    const configMap = JSON.parse(decodeURIComponent(content));
69    if (configMap.module.definePermissions) {
70      return configMap.module.definePermissions;
71    }
72  } catch (error) {
73    console.error('Convert json file to object failed');
74  }
75  return undefined;
76};
77
78const convertJsonToDTS = (permissions, outputFilePath) => {
79  if (fs.existsSync(outputFilePath)) {
80    fs.unlinkSync(outputFilePath);
81  }
82  fs.appendFileSync(outputFilePath, copyRight, 'utf8');
83  fs.appendFileSync(outputFilePath, label, 'utf8');
84  fs.appendFileSync(outputFilePath, typeHead, 'utf8');
85  permissions.forEach((permission, index) => {
86    if (permission.since || permission.deprecated) {
87      fs.appendFileSync(outputFilePath, commentHead, 'utf8');
88      if (permission.since) {
89        const since = `${commentBody}${sinceTag}${permission.since}\n`;
90        fs.appendFileSync(outputFilePath, since, 'utf8');
91      }
92      if (permission.deprecated) {
93        const deprecated = `${commentBody}${deprecatedTag}${permission.deprecated}\n`;
94        fs.appendFileSync(outputFilePath, deprecated, 'utf8');
95      }
96      fs.appendFileSync(outputFilePath, commentTail, 'utf8');
97    }
98    const permissionName = `${index === 0 ? tabs : orOperator}'${permission.name}'${
99      index === permissions.length - 1 ? '' : '\n'
100    }`;
101    fs.appendFileSync(outputFilePath, permissionName, 'utf8');
102  });
103  fs.appendFileSync(outputFilePath, typeTail, 'utf8');
104  console.log('Convert config.json definePermissions to permissions.d.ts successfully');
105};
106
107/**
108 * Read config.json file and convert it to permission.d.ts
109 *
110 * @param {
111 *   filePath: name of downloaded config.json file
112 *   outputFileName: name of converted d.ts file name
113 * } options
114 */
115const convert = options => {
116  const permissions = getPermissions(options.filePath);
117  if (permissions) {
118    convertJsonToDTS(permissions, options.outputFilePath);
119  } else {
120    console.error('Config file does not have definePermissions property');
121  }
122};
123
124const arguments = process.argv;
125const options = {
126  filePath: arguments[2],
127  outputFilePath: arguments[3],
128};
129try {
130  convert(options);
131} catch (error) {
132  console.error(`ERROR FOR CONVERTING PERMISSION: ${error}`);
133}
134