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