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 {existsSync, readFileSync, writeFileSync} from 'fs'; 17import {readJsonSync} from 'fs-extra'; 18import type {IOptions} from '../configs/IOptions'; 19 20export class FileUtils { 21 /** 22 * Read file and return content 23 * 24 * @param filePath file path 25 */ 26 public static readFile(filePath: string): string | undefined { 27 if (!existsSync(filePath)) { 28 console.error(`File <${this.getFileName(filePath)} is not found.>`); 29 return undefined; 30 } 31 return readFileSync(filePath, 'utf-8'); 32 } 33 34 /** 35 * Read file and convert to json object. 36 * 37 * @param filePath file path 38 */ 39 public static readFileAsJson(filePath: string): IOptions | undefined { 40 if (!existsSync(filePath)) { 41 console.error(`File <${this.getFileName(filePath)} is not found.>`); 42 return undefined; 43 } 44 45 try { 46 return readJsonSync(filePath); 47 } catch (e) { 48 console.error('json file read error: ' + filePath); 49 return undefined; 50 } 51 } 52 53 /** 54 * Get File Name 55 * 56 * @param filePath file path 57 */ 58 public static getFileName(filePath: string): string | undefined { 59 if (!filePath) { 60 return undefined; 61 } 62 63 const lastSepIndex: number = filePath.lastIndexOf('/'); 64 if (lastSepIndex >= 0) { 65 return filePath.slice(lastSepIndex + 1); 66 } 67 68 return filePath.slice(filePath.lastIndexOf('\\') + 1); 69 } 70 71 /** 72 * Get suffix of a file. 73 * 74 * @param filePath file path 75 */ 76 public static getFileExtension(filePath: string): string | undefined { 77 if (!filePath || !filePath.includes('.')) { 78 return undefined; 79 } 80 81 // get file name 82 let fileName: string = this.getFileName(filePath); 83 if (!fileName.includes('.')) { 84 return undefined; 85 } 86 87 return fileName.slice(fileName.lastIndexOf('.') + 1); 88 } 89 90 public static writeFile(filePath: string, content: string): void { 91 writeFileSync(filePath, content); 92 } 93 94 /** 95 * get prefix of directory 96 * @param dirPath 97 */ 98 public static getPrefix(dirPath: string): string | undefined { 99 if (!dirPath || (!dirPath.includes('/') && !dirPath.includes('\\'))) { 100 return undefined; 101 } 102 103 const sepIndex: number = dirPath.lastIndexOf('/'); 104 if (sepIndex >= 0) { 105 return dirPath.slice(0, sepIndex + 1); 106 } 107 108 return dirPath.slice(0, dirPath.lastIndexOf('\\') + 1); 109 } 110 111 public static getPathWithoutPrefix(filePath: string, prefix: string): string | undefined { 112 if (!filePath.startsWith(prefix)) { 113 return filePath; 114 } 115 116 return filePath.slice(prefix.length); 117 } 118} 119