• 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 ts = require('typescript');
17const path = require('path');
18const fs = require('fs');
19
20const addTSInterfaceSet = ['ForEach', 'LazyForEach', 'TapGesture', 'LongPressGesture', 'LongPressGesture',
21  'PanGesture', 'SwipeGesture', 'PinchGesture', 'RotationGesture', 'GestureGroup','PageTransitionEnter',
22  'PageTransitionExit'];
23const addTSAttributeSet = ['AlphabetIndexer', 'Animator', 'Badge', 'Blank', 'Button', 'Calendar', 'CalendarPicker',
24  'Canvas', 'Checkbox', 'CheckboxGroup', 'Circle', 'Column', 'ColumnSplit', 'Counter', 'DataPanel', 'DatePicker',
25  'Divider', 'Ellipse', 'Flex', 'FormComponent', 'Gauge', 'Grid', 'GridItem', 'GridContainer', 'Image',
26  'ImageAnimator', 'IndicatorComponent', 'Line', 'List', 'ListItem', 'ListItemGroup', 'LoadingProgress', 'Marquee', 'Navigation', 'Navigator',
27  'Panel', 'Path', 'PatternLock', 'Piece', 'PluginComponent', 'Polygon', 'Polyline', 'Progress',
28  'QRCode', 'Radio', 'Rating', 'Rect', 'Refresh', 'Row', 'RowSplit', 'Scroll', 'ScrollBar', 'Search',
29  'Select', 'Shape', 'Sheet', 'Slider', 'Span', 'Stack', 'Stepper', 'StepperItem', 'Swiper',
30  'TabContent', 'Tabs', 'Text', 'TextArea', 'TextClock', 'TextInput', 'TextPicker', 'TextTimer',
31  'Toggle', 'Video', 'Web', 'XComponent', 'RichText', 'RemoteWindow', 'WaterFlow', 'FlowItem', 'ImageSpan',
32  'RootScene', 'Screen', 'WindowScene', 'EffectComponent', 'RichEditor'];
33
34generateTargetFile(process.argv[2], process.argv[3]);
35function generateTargetFile(filePath, output) {
36  const files = [];
37  const globalTsFile = path.resolve(filePath, '../../ets/global.d.ts');
38  const featureAbilityPath = path.resolve(filePath, '../../../common/full/featureability.d.ts');
39  const middleTsFile = path.resolve(filePath, 'middle_class.d.ts');
40  if (fs.existsSync(globalTsFile)) {
41    files.push(globalTsFile);
42  }
43  if (fs.existsSync(featureAbilityPath)) {
44    files.push(featureAbilityPath);
45  }
46  readFile(filePath, files);
47  if (!fs.existsSync(output)) {
48    mkDir(output);
49  }
50  const license = `/*
51  * Copyright (c) 2021 Huawei Device Co., Ltd.
52  * Licensed under the Apache License, Version 2.0 (the "License");
53  * you may not use this file except in compliance with the License.
54  * You may obtain a copy of the License at
55  *
56  *     http://www.apache.org/licenses/LICENSE-2.0
57  *
58  * Unless required by applicable law or agreed to in writing, software
59  * distributed under the License is distributed on an "AS IS" BASIS,
60  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
61  * See the License for the specific language governing permissions and
62  * limitations under the License.
63  */`;
64  files.forEach((item) => {
65    let content = fs.readFileSync(item, 'utf8');
66    const fileName = path.resolve(output, path.basename(item));
67    if (item === featureAbilityPath) {
68      content = processsFile(content, fileName, true);
69    } else if (item === globalTsFile) {
70      content = license + '\n\n' + processsFile(content, fileName, true);
71    } else {
72      content = processImportType(content);
73    }
74    fs.writeFile(fileName, content, err => {
75      if (err) {
76        console.error(err);
77        return;
78      }
79    });
80  });
81}
82
83function processImportType(content) {
84  return content.replace(/(import\s*\(("|'))(\.\.\/api\/[^("|')]*("|')\)\.)/g, (item, item1, item2, item3) => {
85    return item1 + '../../' + item3;
86  });
87}
88
89function readFile(dir, fileDir) {
90  const files = fs.readdirSync(dir);
91  files.forEach((element) => {
92    const filePath = path.join(dir, element);
93    const status = fs.statSync(filePath);
94    if (status.isDirectory()) {
95      readFile(filePath, fileDir);
96    } else {
97      fileDir.push(filePath);
98    }
99  });
100}
101
102function mkDir(filePath) {
103  const parent = path.join(filePath, '..');
104  if (!(fs.existsSync(parent) && !fs.statSync(parent).isFile())) {
105    mkDir(parent);
106  }
107  fs.mkdirSync(filePath);
108}
109
110function processsFile(content, fileName, isGlobal) {
111  let sourceFile = ts.createSourceFile(fileName, content, ts.ScriptTarget.Latest, true, ts.ScriptKind.TS);
112  const newStatements = [];
113  if (sourceFile.statements && sourceFile.statements.length) {
114    if (isGlobal) {
115      sourceFile.statements.forEach((node) => {
116        if (!ts.isImportDeclaration(node)) {
117          let modifiers = ts.canHaveModifiers(node) ? ts.getModifiers(node) : undefined;
118          if (modifiers && modifiers.length && modifiers[0].kind === ts.SyntaxKind.ExportKeyword) {
119            modifiers.splice(0, 1);
120          }
121          if (isVariable(node)) {
122            const name = node.declarationList.declarations[0].name.getText();
123            const type = node.declarationList.declarations[0].type.getText();
124            if (name.indexOf(type) !== -1) {
125              const declarationNode = ts.factory.updateVariableDeclaration(node.declarationList.declarations[0],
126                ts.factory.createIdentifier(type), node.declarationList.declarations[0].exclamationToken,
127                node.declarationList.declarations[0].type, node.declarationList.declarations[0].initializer);
128              node.declarationList = ts.factory.updateVariableDeclarationList(node.declarationList, [declarationNode]);
129            }
130          }
131          newStatements.push(node);
132        }
133      });
134    } else {
135      sourceFile.statements.forEach((node) => {
136        processComponent(node, newStatements);
137      });
138    }
139  }
140  sourceFile = ts.factory.updateSourceFile(sourceFile, newStatements);
141  const printer = ts.createPrinter({ removeComments: false, newLine: ts.NewLineKind.LineFeed });
142  const result = printer.printNode(ts.EmitHint.Unspecified, sourceFile, sourceFile);
143  return result;
144}
145
146function processComponent(node, newStatements) {
147  let extendNode = null;
148  if (isInterface(node)) {
149    const componentName = node.name.getText().replace(/Interface$/, '');
150    const result = validateComponentMembers(node, componentName);
151    const modifiers = ts.canHaveModifiers(node) ? ts.getModifiers(node) : undefined;
152    if (result.isComponentName) {
153      const heritageClause = ts.factory.createHeritageClause(ts.SyntaxKind.ExtendsKeyword,
154        [ts.factory.createExpressionWithTypeArguments(result.extendNode, undefined)]);
155      extendNode = null;
156      node = ts.factory.updateInterfaceDeclaration(node, modifiers,
157        node.name, node.typeParameters, [heritageClause], node.members);
158    }
159    if (addTSInterfaceSet.includes(componentName)) {
160      node = ts.factory.updateInterfaceDeclaration(node, modifiers, node.name,
161        node.typeParameters, [ts.factory.createHeritageClause(ts.SyntaxKind.ExtendsKeyword,
162          [ts.factory.createExpressionWithTypeArguments(ts.factory.createIdentifier('TS' + componentName + 'Interface'),
163          undefined)])], node.members);
164    }
165  }
166  if (isClass(node) && addTSAttributeSet.includes(node.name.getText().replace(/Attribute$/, ''))) {
167    const modifiers = ts.canHaveModifiers(node) ? ts.getModifiers(node) : undefined;
168    node = ts.factory.updateClassDeclaration(node, modifiers, node.name,
169      node.typeParameters, [ts.factory.createHeritageClause(ts.SyntaxKind.ExtendsKeyword,
170        [ts.factory.createExpressionWithTypeArguments(ts.factory.createIdentifier('TS' + node.name.getText()),
171          undefined)])], node.members);
172  }
173  newStatements.push(node);
174}
175
176function validateComponentMembers(node, componentName) {
177  let extendNode = null;
178  let isComponentName = false;
179  if (node.members) {
180    for (let i = 0; i < node.members.length; i++) {
181      const callSignNode = node.members[i];
182      if (isSignNode(callSignNode)) {
183        const callSignName = callSignNode.type.typeName.getText().replace(/Attribute$/, '');
184        if (componentName === callSignName) {
185          extendNode = callSignNode.type.typeName;
186          isComponentName = true;
187          break;
188        }
189      }
190    }
191  }
192  return { isComponentName, extendNode };
193}
194
195function isVariable(node) {
196  if (ts.isVariableStatement(node) && node.declarationList && node.declarationList.declarations &&
197    node.declarationList.declarations.length && ts.isVariableDeclaration(node.declarationList.declarations[0]) &&
198    node.declarationList.declarations[0].name && node.declarationList.declarations[0].type) {
199    return true;
200  }
201  return false;
202}
203
204function isInterface(node) {
205  return ts.isInterfaceDeclaration(node) && node.name && ts.isIdentifier(node.name) &&
206    /Interface$/.test(node.name.getText());
207}
208
209function isClass(node) {
210  return ts.isClassDeclaration(node) && node.name && ts.isIdentifier(node.name) &&
211    /Attribute$/.test(node.name.getText());
212}
213
214function isSignNode(node) {
215  return (ts.isCallSignatureDeclaration(node) || ts.isConstructSignatureDeclaration(node)) &&
216    node.type && ts.isTypeReferenceNode(node.type) && node.type.typeName && ts.isIdentifier(node.type.typeName) &&
217    /Attribute$/.test(node.type.typeName.getText());
218}
219
220generateComponentConfig(process.argv[4], process.argv[5]);
221function generateComponentConfig(dir, buildPublicSDK) {
222  const configFile = path.resolve(dir, 'component_map.js');
223  if (fs.existsSync(configFile)) {
224    const { COMPONENT_MAP, FORM_MAP, COMMON_ATTRS, forbiddenUseStateType } = require(configFile);
225    const buildConfig = {
226      forbiddenUseStateTypeForDecorators: [
227        '@State', '@Prop', '@Link', '@Provide', '@Consume', '@ObjectLink', '@BuilderParam',
228        '@LocalStorageLink', '@LocalStorageProp', '@StorageLink', '@StorageProp'
229      ],
230      forbiddenUseStateType: [...forbiddenUseStateType]
231    };
232    try {
233      removeSystemApiComp(buildPublicSDK, COMPONENT_MAP, FORM_MAP);
234      let commonMap = new Map();
235      commonMap.attrs = [...COMMON_ATTRS];
236      COMPONENT_MAP.Common = commonMap;
237      fs.writeFileSync(path.resolve(dir, '../component_config.json'), JSON.stringify(COMPONENT_MAP));
238      fs.writeFileSync(path.resolve(dir, '../form_config.json'), JSON.stringify(FORM_MAP));
239      fs.writeFileSync(path.resolve(dir, '../build_config.json'), JSON.stringify(buildConfig));
240    } catch (error) {
241      console.error(error);
242    }
243  }
244}
245
246function removeSystemApiComp(buildPublicSDK, COMPONENT_MAP, FORM_MAP) {
247  const systemApiComp = [];
248  if (buildPublicSDK === 'true') {
249    for (const comp in COMPONENT_MAP) {
250      if (COMPONENT_MAP[comp] && COMPONENT_MAP[comp].systemApi) {
251        systemApiComp.push(comp);
252      }
253    }
254  }
255  systemApiComp.forEach(comp => {
256    if (COMPONENT_MAP[comp]) {
257      delete COMPONENT_MAP[comp];
258    }
259    if (FORM_MAP[comp]) {
260      delete FORM_MAP[comp];
261    }
262  });
263}
264