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 16import {FileUtils} from './FileUtils'; 17 18export const NAME_CACHE_SUFFIX: string = '.cache.json'; 19export const PROPERTY_CACHE_FILE: string = 'property.cache.json'; 20 21export function writeCache(cache: Map<string, string>, destFileName: string): void { 22 // convert map to json string 23 if (!cache) { 24 return; 25 } 26 27 const cacheString: string = JSON.stringify(Object.fromEntries(cache)); 28 FileUtils.writeFile(destFileName, cacheString); 29} 30 31export function readCache(filePath: string): Object | undefined { 32 // read json string from file 33 const cacheString: string = FileUtils.readFile(filePath); 34 if (cacheString === undefined) { 35 return undefined; 36 } 37 38 // get map from json string 39 return JSON.parse(cacheString); 40} 41 42export function getMapFromJson(jsonObj: Object): Map<string, string> { 43 if (jsonObj === undefined) { 44 return new Map<string, string>(); 45 } 46 47 return new Map<string, string>(Object.entries(jsonObj)); 48} 49