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 error(color: string, errormsg: string, reset: string) { 40 this.messsage = color.toString(); 41 } 42 43 public getPrefix() { 44 return this.prefix; 45 } 46 47 48 public static getLogger(prefix): object { 49 for (const instance of Logger.instances) { 50 if (instance.getPrefix() == prefix) { 51 return instance; 52 } 53 } 54 } 55 public static createLogger(prefix) { 56 const logger = new Logger(prefix); 57 Logger.instances.push(logger); 58 return logger; 59 } 60} 61 62class CacheInCacheStoreManager { 63 cache: Map<string, object>; 64 65 constructor() { 66 this.cache = new Map<string, object>(); 67 } 68 69 public getCache(key: string): object { 70 return this.cache.get(key); 71 } 72 73 public setCache(key: string, value: object): void { 74 this.cache.set(key, value); 75 } 76} 77 78class CacheStoreManager { 79 cacheInCacheStoreManager: Map<string, CacheInCacheStoreManager>; 80 81 constructor() { 82 this.cacheInCacheStoreManager = new Map<string, CacheInCacheStoreManager>(); 83 } 84 85 public mount(cacheStoreManagerKey: string): CacheInCacheStoreManager { 86 let cacheInCacheStoreManagerValue: CacheInCacheStoreManager | undefined = 87 this.cacheInCacheStoreManager.get(cacheStoreManagerKey); 88 89 if (!cacheInCacheStoreManagerValue) { 90 cacheInCacheStoreManagerValue = new CacheInCacheStoreManager(); 91 this.cacheInCacheStoreManager.set(cacheStoreManagerKey, cacheInCacheStoreManagerValue); 92 } 93 94 return cacheInCacheStoreManagerValue; 95 } 96 97 public unmount(cacheStoreManagerKey: string): void { 98 this.cacheInCacheStoreManager?.delete(cacheStoreManagerKey); 99 } 100 101 public keys(): IterableIterator<string> { 102 return this.cacheInCacheStoreManager?.keys(); 103 } 104} 105 106class Share { 107 projectConfig: ProjectConfig; 108 arkProjectConfig: IArkProjectConfig; 109 symlinkMap = {}; 110 currentModuleMetaMap = {}; 111 112 allComponents?: Map<string, Array<string>>; 113 allFiles?: Set<string>; 114 cache?: Map<string, object>; 115 cacheStoreManager?: CacheStoreManager; 116 117 constructor(buildMode: string) { 118 this.projectConfig = new ProjectConfig(buildMode); 119 } 120 121 public throwArkTsCompilerError(error: object) { 122 console.error(JSON.stringify(error)); 123 } 124 125 public getLogger(prefix: string): Logger { 126 const logger = Logger.getLogger(prefix); 127 if (!logger || logger == undefined) { 128 return Logger.createLogger(prefix); 129 } 130 return logger; 131 } 132 133 public scan(testcase: string) { 134 if (!testcase) { 135 return; 136 } 137 this.projectConfig.scan(testcase); 138 this.symlinkMap[`${this.projectConfig.projectTopDir}/${OH_MODULES_OHPM_HYPIUM}`] = [ 139 `${this.projectConfig.projectTopDir}/${OH_MODULES_OHOS_HYPIUM}` 140 ]; 141 } 142 143 public setMockParams() { 144 this.projectConfig.setMockParams({ mockConfigPath: MOCK_CONFIG_PATH }); 145 } 146 147 public initWithCache(): void { 148 this.cache = new Map<string, object>(); 149 this.cacheStoreManager = undefined; 150 } 151 152 public initWithCacheStoreManager(): void { 153 this.cache = undefined; 154 this.cacheStoreManager = new CacheStoreManager(); 155 } 156 157 public initWithoutCache(): void { 158 this.cache = undefined; 159 this.cacheStoreManager = undefined; 160 } 161} 162 163export default Share; 164