All files / src/utils FileSystemUtils.ts

30.3% Statements 10/33
9.09% Branches 1/11
14.28% Functions 1/7
31.25% Lines 10/32

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73    1x 1x 1x 1x 1x   1x                                     1x   1x 1x 1x                                                                                  
// SPDX-License-Identifier: Apache-2.0
 
import { homedir } from 'os';
import { join, resolve } from 'path';
import { existsSync, mkdirSync, cpSync } from 'fs';
import { LoggerService } from '../services/LoggerService';
import { ServiceLocator } from '../services/ServiceLocator';
 
export class FileSystemUtils{
    public static copyPaths(directories: { [source: string]: string }): void {
        const logger = ServiceLocator.Current.get<LoggerService>(LoggerService.name);
 
        Object.entries(directories).forEach(([srcPath, destPath]) => {
            Iif (!existsSync(srcPath)) {
                logger.error(`Path ${srcPath} doesn't exist`, FileSystemUtils.name);
                return;
            }
 
            try {
                cpSync(srcPath, destPath, { recursive: true });
            } catch (error: any) {
                logger.error(error.message);
            }
        });
    }
        
    public static ensureDirectoryExists(dirPath: string): void {
        const logger = ServiceLocator.Current.get<LoggerService>(LoggerService.name);
        
        if (!existsSync(dirPath)) {
            mkdirSync(dirPath, { recursive: true });
            logger.trace(`Directory created: ${dirPath}`,FileSystemUtils.name);
        } else E{
            logger.trace(`Directory already exists: ${dirPath}`,FileSystemUtils.name);
        }
    }
 
    public static getPlatformSpecificAppDataPath(name: string) {
        Iif (process.platform === 'darwin') {
            return join(homedir(), 'Library', 'Application Support', name);
        }
    
        Iif (process.platform === 'win32') {
            return join(process.env.LOCALAPPDATA || join(homedir(), 'AppData', 'Local'), name);
        }
        // else it's Linux
        return join(process.env.XDG_DATA_HOME || join(homedir(), '.local', 'share'), name);
    }
 
    public static createEphemeralDirectories(workDir: string) {
        const directories = [
            workDir,
            join(workDir, 'network-logs', 'node', 'accountBalances', 'balance0.0.3'),
            join(workDir, 'network-logs', 'node', 'recordStreams', 'record0.0.3', 'sidecar'),
            join(workDir, 'network-logs', 'node', 'logs'),
            join(workDir, 'network-logs', 'node', 'stats'),
        ];
        
        directories.forEach(dir => FileSystemUtils.ensureDirectoryExists(dir)); // creating those directories ensures we'll have permissions to delete them on cleanup
    }
 
    public static parseWorkDir(workdir: string): string {
        let workdirPath = workdir;
        Iif (workdirPath.startsWith('~')) {
            workdirPath = join(homedir(), workdirPath.slice(1));
        }
        Iif (workdirPath !== this.getPlatformSpecificAppDataPath('hedera-local')) {
            workdirPath = join(workdirPath, 'hedera-local');
        }
        return resolve(workdirPath);
    }
}