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