• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2021-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');
17const path = require('path');
18const ts = require('typescript');
19const SECOND_PARAM = 2;
20const THIRD_PARAM = 3;
21const FOURTH_PARAM = 4;
22const systemModules = [];
23
24function generateKitConfig(kitFilePath, output, apiFilePath) {
25  readSystemApis(apiFilePath, systemModules);
26  const kitFiles = [];
27  readFile(kitFilePath, kitFiles);
28  if (fs.existsSync(output)) {
29    removeDir(output);
30  }
31  mkDir(output);
32  kitFiles.forEach((item) => {
33    let content = fs.readFileSync(item, 'utf8');
34    const outputPath = path.resolve(output, path.basename(item).replace('.d.ts', '.json'));
35    const symbol = {};
36    const kitSourceFile = ts.createSourceFile(item, content, ts.ScriptTarget.Latest, true, ts.ScriptKind.TS);
37    if (kitSourceFile.statements && kitSourceFile.statements.length > 0) {
38      kitSourceFile.statements.forEach(statement => {
39        getImportDeclarationInfo(statement, symbol);
40      });
41    }
42    const result = {
43      'symbols': symbol
44    };
45    const STANDARD_INDENT = 2;
46    createKitConfigs(outputPath, JSON.stringify(result, null, STANDARD_INDENT));
47  });
48}
49
50function getImportDeclarationInfo(statement, symbol) {
51  if (!ts.isImportDeclaration(statement)) {
52    return;
53  }
54  let source = '';
55  if (statement.moduleSpecifier && ts.isStringLiteral(statement.moduleSpecifier)) {
56    source = statement.moduleSpecifier.getText().replace(/('|")*/g, '');
57    for (let i = 0; i < systemModules.length; i++) {
58      const moduleName = systemModules[i];
59      if (moduleName.replace(/(\.d\.ts|\.d\.ets)$/, '') === source) {
60        source = moduleName;
61        break;
62      }
63    }
64  }
65  if (statement.importClause) {
66    const clause = statement.importClause;
67    if (clause.name) {
68      addSymbol(symbol, clause.name.getText(), source, 'default');
69    }
70    if (clause.namedBindings) {
71      const binding = clause.namedBindings;
72      if (ts.isNamespaceImport(binding) && binding.name) {
73        addSymbol(symbol, binding.name.getText(), source, 'default');
74      } else if (ts.isNamedImports(binding) && binding.elements && binding.elements.length > 0) {
75        processNamedImports(binding.elements, symbol, source);
76      }
77    }
78  }
79}
80
81function processNamedImports(elements, symbol, source) {
82  elements.forEach(element => {
83    if (ts.isImportSpecifier(element)) {
84      const name = element.name.getText();
85      const bindingsName = element.propertyName ? element.propertyName.getText() : name;
86      addSymbol(symbol, name, source, bindingsName);
87    }
88  });
89}
90
91function addSymbol(symbol, name, source, bindings) {
92  symbol[name] = {
93    source: source,
94    bindings: bindings
95  };
96}
97
98function readFile(dir, fileDir) {
99  const files = fs.readdirSync(dir);
100  files.forEach((element) => {
101    const filePath = path.join(dir, element);
102    const status = fs.statSync(filePath);
103    if (status.isDirectory()) {
104      readFile(filePath, fileDir);
105    } else {
106      fileDir.push(filePath);
107    }
108  });
109}
110
111function readSystemApis(dir, fileDir) {
112  const files = fs.readdirSync(dir);
113  files.forEach(file => {
114    const filePath = path.join(dir, file);
115    const status = fs.statSync(filePath);
116    if (!status.isDirectory()) {
117      fileDir.push(file);
118    }
119  });
120}
121
122function mkDir(filePath) {
123  const parent = path.join(filePath, '..');
124  if (!(fs.existsSync(parent) && !fs.statSync(parent).isFile())) {
125    mkDir(parent);
126  }
127  fs.mkdirSync(filePath);
128}
129
130function removeDir(path) {
131  if (fs.existsSync(path)) {
132    fs.rmdirSync(path, {recursive: true});
133  }
134}
135
136function createKitConfigs(fileName, content) {
137  fs.writeFile(fileName, content, err => {
138    if (err) {
139      console.error(err);
140      return;
141    }
142  });
143}
144
145generateKitConfig(process.argv[SECOND_PARAM], process.argv[THIRD_PARAM], process.argv[FOURTH_PARAM]);
146