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 scan(testcase: string) { 187 if (!testcase) { 188 return; 189 } 190 this.projectConfig.scan(testcase); 191 this.symlinkMap[`${this.projectConfig.projectTopDir}/${OH_MODULES_OHPM_HYPIUM}`] = [ 192 `${this.projectConfig.projectTopDir}/${OH_MODULES_OHOS_HYPIUM}` 193 ]; 194 } 195 196 public setMockParams() { 197 this.projectConfig.setMockParams({ mockConfigPath: MOCK_CONFIG_PATH }); 198 } 199 200 public initWithCache(): void { 201 this.cache = new Map<string, object>(); 202 this.cacheStoreManager = undefined; 203 } 204 205 public initWithCacheStoreManager(): void { 206 this.cache = undefined; 207 this.cacheStoreManager = new CacheStoreManager(); 208 } 209 210 public initWithoutCache(): void { 211 this.cache = undefined; 212 this.cacheStoreManager = undefined; 213 } 214} 215 216export default Share; 217