161847f8eSopenharmony_ci/*
261847f8eSopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
361847f8eSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
461847f8eSopenharmony_ci * you may not use this file except in compliance with the License.
561847f8eSopenharmony_ci * You may obtain a copy of the License at
661847f8eSopenharmony_ci *
761847f8eSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
861847f8eSopenharmony_ci *
961847f8eSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1061847f8eSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1161847f8eSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1261847f8eSopenharmony_ci * See the License for the specific language governing permissions and
1361847f8eSopenharmony_ci * limitations under the License.
1461847f8eSopenharmony_ci */
1561847f8eSopenharmony_ci
1661847f8eSopenharmony_ciimport fs, { constants } from 'fs';
1761847f8eSopenharmony_ciimport path from 'path';
1861847f8eSopenharmony_ciimport { LogUtil } from './logUtil';
1961847f8eSopenharmony_ciimport type { Stats } from 'fs';
2061847f8eSopenharmony_ci
2161847f8eSopenharmony_ciexport class FileUtils {
2261847f8eSopenharmony_ci  static readFileContent(file: string): string | undefined {
2361847f8eSopenharmony_ci    return fs.readFileSync(file, 'utf-8');
2461847f8eSopenharmony_ci  }
2561847f8eSopenharmony_ci
2661847f8eSopenharmony_ci  static readFilesInDir(dirName: string, filter?: (name: string) => boolean): Array<string> {
2761847f8eSopenharmony_ci    const files: Array<string> = [];
2861847f8eSopenharmony_ci    fs.readdirSync(dirName, { withFileTypes: true }).forEach((dir) => {
2961847f8eSopenharmony_ci      const filePath = path.join(dirName, dir.name);
3061847f8eSopenharmony_ci      if (dir.isFile()) {
3161847f8eSopenharmony_ci        if (!filter) {
3261847f8eSopenharmony_ci          files.push(filePath);
3361847f8eSopenharmony_ci          return;
3461847f8eSopenharmony_ci        }
3561847f8eSopenharmony_ci        if (filter(dir.name)) {
3661847f8eSopenharmony_ci          files.push(filePath);
3761847f8eSopenharmony_ci        }
3861847f8eSopenharmony_ci        return;
3961847f8eSopenharmony_ci      }
4061847f8eSopenharmony_ci      files.push(...FileUtils.readFilesInDir(filePath, filter));
4161847f8eSopenharmony_ci    });
4261847f8eSopenharmony_ci    return files;
4361847f8eSopenharmony_ci  }
4461847f8eSopenharmony_ci
4561847f8eSopenharmony_ci  static writeStringToFile(str: string, filePath: string): void {
4661847f8eSopenharmony_ci    const parentDir = path.dirname(filePath);
4761847f8eSopenharmony_ci    if (!FileUtils.isExists(parentDir)) {
4861847f8eSopenharmony_ci      fs.mkdirSync(parentDir, { recursive: true });
4961847f8eSopenharmony_ci    }
5061847f8eSopenharmony_ci    fs.writeFileSync(filePath, str);
5161847f8eSopenharmony_ci  }
5261847f8eSopenharmony_ci
5361847f8eSopenharmony_ci  static isDirectory(path: string): boolean {
5461847f8eSopenharmony_ci    const stats: Stats = fs.lstatSync(path);
5561847f8eSopenharmony_ci    return stats.isDirectory();
5661847f8eSopenharmony_ci  }
5761847f8eSopenharmony_ci
5861847f8eSopenharmony_ci  static isExists(path: string | undefined): boolean {
5961847f8eSopenharmony_ci    if (!path) {
6061847f8eSopenharmony_ci      return false;
6161847f8eSopenharmony_ci    }
6261847f8eSopenharmony_ci    try {
6361847f8eSopenharmony_ci      fs.accessSync(path, constants.R_OK);
6461847f8eSopenharmony_ci      return true;
6561847f8eSopenharmony_ci    } catch (exception) {
6661847f8eSopenharmony_ci      return false;
6761847f8eSopenharmony_ci    }
6861847f8eSopenharmony_ci  }
6961847f8eSopenharmony_ci
7061847f8eSopenharmony_ci  static getFileTimeStamp(): string {
7161847f8eSopenharmony_ci    const now = new Date();
7261847f8eSopenharmony_ci    return `${now.getFullYear()}_${now.getMonth() + 1}_${now.getDate()}_${now.getHours()}_${now.getMinutes()}_${now.getSeconds()}`;
7361847f8eSopenharmony_ci  }
7461847f8eSopenharmony_ci}