• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2025 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
16import * as ts from 'typescript';
17import fs from 'fs';
18import path from 'path';
19
20export const WHITELIST: string = 'whiteList';
21export const extConfig: string = 'externalconfig';
22
23export let WHITELISTNEW: Array<string> = [];
24
25const COMPONENT_PATH = './build-tools/ets-loader/components';
26
27export function readExternalComponents(): object {
28    const EXT_COMPONENT_MAP: object = {};
29    const componentPaths: string[] | undefined = getExternalComponentPaths();
30    if (!componentPaths) {
31        return EXT_COMPONENT_MAP;
32    }
33    for (const componentPath of componentPaths) {
34        if (!fs.existsSync(componentPath)) {
35            continue;
36        }
37        const files: string[] = fs.readdirSync(componentPath);
38        files.forEach(function (item) {
39            const fPath: string = path.join(componentPath, item);
40            const json: any = require(fPath);
41            const stat: any = fs.statSync(fPath);
42            if (stat.isFile()) {
43                if (json.name) {
44                    const compName: string = json.name;
45                    delete json.name;
46                    EXT_COMPONENT_MAP[compName] = json;
47                } else {
48                    WHITELISTNEW = Array.from(new Set(json.extWhiteList ? json.extWhiteList : []));
49                }
50            }
51        });
52    }
53    return EXT_COMPONENT_MAP;
54}
55
56export function getExternalComponentPaths(): string[] | undefined {
57    const externalComponentsPath: string[] = [];
58    if (process.env.externalApiPaths) {
59        const externalApiPaths = process.env.externalApiPaths.split(path.delimiter);
60        externalApiPaths.forEach(sdkPath => {
61            externalComponentsPath.push(path.resolve(sdkPath, COMPONENT_PATH));
62        });
63        return externalComponentsPath;
64    }
65    return undefined;
66}
67
68export function concatenateEtsOptions(outOptions: ts.CompilerOptions, extOptions: ts.CompilerOptions): void {
69    const tempComponents: Array<string> = Array.from(new Set([...outOptions.ets.components,
70    ...extOptions.ets.components]));
71    const tempExtendComponents: Array<{ name: string; type: string; instance: string; }> =
72        Array.from(new Set([...outOptions.ets.extend.components,
73        ...extOptions.ets.extend.components]));
74    outOptions.ets.components = tempComponents;
75    outOptions.ets.extend.components = tempExtendComponents;
76}