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