1/*
2 * Copyright (c) 2023 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 systemIndexArray: Array<SystemIndexEntity> = [];
17const systemNoMockArray = [
18  'system.app',
19  'system.configuration',
20  'system.device',
21  'system.mediaquery',
22  'system.prompt',
23  'system.router'
24];
25
26export function addToSystemIndexArray(systemIndexEntity: SystemIndexEntity): void {
27  systemIndexArray.push(systemIndexEntity);
28}
29
30export function getSystemIndexArray(): Array<SystemIndexEntity> {
31  return systemIndexArray;
32}
33
34/**
35 * generate startswith 'system_'
36 * @returns
37 */
38export function generateSystemIndex(): string {
39  let systemIndex = 'import regeneratorRuntime from \'babel-runtime/regenerator\'\n';
40  let exportFunction = '';
41  systemIndexArray.forEach(value => {
42    if (!systemNoMockArray.includes(value.filename.replace('_', '.'))) {
43      systemIndex += `import { ${value.mockFunctionName} } from './${value.filename}'\n`;
44      exportFunction += `${value.mockFunctionName}();\n`;
45    }
46  });
47  systemIndex += 'import {mockRequireNapiFun} from \'./napi/index\';\n';
48  systemIndex += `;(function mockSystemPlugin() {
49    global.regeneratorRuntime = regeneratorRuntime
50    global.systemplugin = {}
51    global.ohosplugin = {}\n`;
52  systemIndex += exportFunction;
53  systemIndex += 'mockRequireNapiFun();\n';
54  systemIndex += '}());';
55  return systemIndex;
56}
57
58export interface SystemIndexEntity {
59  filename: string;
60  mockFunctionName: string;
61}
62