• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 rollupObject 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 {
17  ProjectConfig,
18  IArkProjectConfig
19} from "./project_config";
20import {
21  OH_MODULES_OHPM_HYPIUM,
22  OH_MODULES_OHOS_HYPIUM,
23  MOCK_CONFIG_PATH
24} from "./path_config";
25
26class Logger {
27  private prefix: string;
28  private messsage: string;
29  static instances = [];
30
31  constructor(prefix: string) {
32    this.prefix = prefix;
33  }
34
35  public debug(color: string, msg: string, reset: string) {
36    console.debug(`${color}${this.prefix}: ${JSON.stringify(msg)}${reset}`);
37  }
38
39  public warn(color: string, msg: string, reset: string) {
40    console.warn(`${color}${this.prefix}: ${JSON.stringify(msg)}${reset}`);
41  }
42
43  public error(color: string, errormsg: string, reset: string) {
44    this.messsage = errormsg.toString();
45  }
46
47  public warn(color: string, msg: string) {
48    this.messsage = color.toString();
49  }
50
51  public getPrefix() {
52    return this.prefix;
53  }
54
55  public static getLogger(prefix): object {
56    for (const instance of Logger.instances) {
57      if (instance.getPrefix() == prefix) {
58        return instance;
59      }
60    }
61  }
62
63  public static createLogger(prefix) {
64    const logger = new Logger(prefix);
65    Logger.instances.push(logger);
66    return logger;
67  }
68}
69
70class HvigorConsoleLogger {
71
72  private prefix: string;
73  private messsage: string;
74  static instances = [];
75
76  constructor(prefix: string) {
77    this.prefix = prefix;
78  }
79
80  public printError(error: object): void {
81    this.messsage = error.code.toString();
82  }
83
84  public printErrorAndExit(error: object): void {
85    this.messsage = error.code.toString();
86  }
87
88  public getPrefix(): string {
89    return this.prefix;
90  }
91
92  public static getLogger(prefix): Object {
93    for (const instance of HvigorConsoleLogger.instances) {
94      if (instance.getPrefix() == prefix) {
95        return instance;
96      }
97    }
98  }
99
100  public static createLogger(prefix): HvigorConsoleLogger {
101    const logger = new HvigorConsoleLogger(prefix);
102    HvigorConsoleLogger.instances.push(logger);
103    return logger;
104  }
105}
106
107class CacheInCacheStoreManager {
108  cache: Map<string, object>;
109
110  constructor() {
111    this.cache = new Map<string, object>();
112  }
113
114  public getCache(key: string): object {
115    return this.cache.get(key);
116  }
117
118  public setCache(key: string, value: object): void {
119    this.cache.set(key, value);
120  }
121}
122
123class CacheStoreManager {
124  cacheInCacheStoreManager: Map<string, CacheInCacheStoreManager>;
125
126  constructor() {
127    this.cacheInCacheStoreManager = new Map<string, CacheInCacheStoreManager>();
128  }
129
130  public mount(cacheStoreManagerKey: string): CacheInCacheStoreManager {
131    let cacheInCacheStoreManagerValue: CacheInCacheStoreManager | undefined =
132      this.cacheInCacheStoreManager.get(cacheStoreManagerKey);
133
134    if (!cacheInCacheStoreManagerValue) {
135      cacheInCacheStoreManagerValue = new CacheInCacheStoreManager();
136      this.cacheInCacheStoreManager.set(cacheStoreManagerKey, cacheInCacheStoreManagerValue);
137    }
138
139    return cacheInCacheStoreManagerValue;
140  }
141
142  public unmount(cacheStoreManagerKey: string): void {
143    this.cacheInCacheStoreManager?.delete(cacheStoreManagerKey);
144  }
145
146  public keys(): IterableIterator<string> {
147    return this.cacheInCacheStoreManager?.keys();
148  }
149}
150
151class Share {
152  projectConfig: ProjectConfig;
153  arkProjectConfig: IArkProjectConfig;
154  symlinkMap = {};
155  currentModuleMetaMap = {};
156
157  allComponents?: Map<string, Array<string>>;
158  allFiles?: Set<string>;
159  cache?: Map<string, object>;
160  cacheStoreManager?: CacheStoreManager;
161
162  constructor(buildMode: string) {
163    this.projectConfig = new ProjectConfig(buildMode);
164  }
165
166  public throwArkTsCompilerError(error: object) {
167    console.error(JSON.stringify(error));
168  }
169
170  public getLogger(prefix: string): Logger {
171    const logger = Logger.getLogger(prefix);
172    if (!logger || logger == undefined) {
173      return Logger.createLogger(prefix);
174    }
175    return logger;
176  }
177
178  public getHvigorConsoleLogger(prefix: string): HvigorConsoleLogger {
179    const logger = HvigorConsoleLogger.getLogger(prefix);
180    if (!logger || logger == undefined) {
181      return HvigorConsoleLogger.createLogger(prefix);
182    }
183    return logger;
184  }
185
186  public importResolver (moduleRequest: string): Object | undefined {
187    if (moduleRequest === "@ohos/library////\\\\/") {
188      return {
189        normalizedPath: "@ohos/library/Index",
190        packageName: "@ohos/library"
191      }
192    }
193    if (moduleRequest === "@ohos/library/src/main/ets////") {
194      return {
195        normalizedPath: "@ohos/library/src/main/ets/Index",
196        packageName: "@ohos/library"
197      }
198    }
199    if (moduleRequest === "bytecode_alias_oh///\\\/") {
200      return {
201        normalizedPath: "bytecode_alias_oh/Index",
202        packageName: "bytecode_alias_oh"
203      }
204    }
205    if (moduleRequest === "@ohos/library/src/main/ets/pages") {
206      return {
207        normalizedPath: "@ohos/library/src/main/ets/pages/index",
208        packageName: "@ohos/library"
209      }
210    }
211    return undefined;
212  }
213  public scan(testcase: string) {
214    if (!testcase) {
215      return;
216    }
217    this.projectConfig.scan(testcase);
218    this.symlinkMap[`${this.projectConfig.projectTopDir}/${OH_MODULES_OHPM_HYPIUM}`] = [
219      `${this.projectConfig.projectTopDir}/${OH_MODULES_OHOS_HYPIUM}`
220    ];
221  }
222
223  public setMockParams() {
224    this.projectConfig.setMockParams({ mockConfigPath: MOCK_CONFIG_PATH });
225  }
226
227  public initWithCache(): void {
228    this.cache = new Map<string, object>();
229    this.cacheStoreManager = undefined;
230  }
231
232  public initWithCacheStoreManager(): void {
233    this.cache = undefined;
234    this.cacheStoreManager = new CacheStoreManager();
235  }
236
237  public initWithoutCache(): void {
238    this.cache = undefined;
239    this.cacheStoreManager = undefined;
240  }
241}
242
243export default Share;
244