const fs = require ( "fs" ) ; const path = require ( "path" ) ; const _ = require ( "lodash" ) ; const os = require ( "os" ) ; const SEP = /\/|\\/ ; export const PLUGIN_CACHE_FOLDER = "sfpowerkit" ; export default class FileUtils { param deletedComponents public static deleteComponents ( deletedComponents ) { deletedComponents . forEach ( ( component ) => { if ( fs . existsSync ( component ) ) { if ( fs . lstatSync ( component ) . isDirectory ( ) ) { FileUtils . deleteFolderRecursive ( component ) ; } else { fs . unlinkSync ( component ) ; } } } ) ; } param folder param extension public static getAllFilesSync ( folder , extension = ".xml" ) { let result = [ ] ; let pathExists = fs . existsSync ( folder ) ; let folderName = path . basename ( folder ) ; if ( ! pathExists ) { console . log ( "Folder does not exist:" , folderName ) ; return result ; } let content = fs . readdirSync ( folder ) ; content . forEach ( ( file ) => { let curFile = path . join ( folder , file ) ; let stats = fs . statSync ( curFile ) ; if ( stats . isFile ( ) ) { if ( extension . indexOf ( path . extname ( curFile ) ) != - 1 || extension === "" ) { result . push ( curFile ) ; } } else if ( stats . isDirectory ( ) ) { let files = this . getAllFilesSync ( curFile , extension ) ; result = _ . concat ( result , files ) ; } } ) ; return result ; } public static getGlobalCacheDir ( ) { let homedir = os . homedir ( ) ; let configDir = homedir + path . sep + PLUGIN_CACHE_FOLDER ; if ( ! fs . existsSync ( configDir ) ) { console . log ( "Config folder does not exists, Creating Folder" ) ; fs . mkdirSync ( configDir ) ; } return configDir ; } param fileName public static getGlobalCachePath ( fileName ) { let homedir = os . homedir ( ) ; let configDir = homedir + path . sep + PLUGIN_CACHE_FOLDER ; if ( ! fs . existsSync ( configDir ) ) { console . log ( "Config folder does not exists, Creating Folder" ) ; fs . mkdirSync ( configDir ) ; } return configDir + path . sep + fileName ; } param targetDir param param1 public static mkDirByPathSync ( targetDir , { isRelativeToScript = false } = { } ) { const sep = path . sep ; const initDir = path . isAbsolute ( targetDir ) ? sep : "" ; const baseDir = isRelativeToScript ? __dirname : "." ; targetDir . split ( sep ) . reduce ( ( parentDir , childDir ) => { const curDir = path . resolve ( baseDir , parentDir , childDir ) ; try { fs . mkdirSync ( curDir ) ; } catch ( err ) { if ( err . code !== "EEXIST" && err . code !== "EPERM" && err . code !== "EISDIR" ) { throw err ; } } return curDir ; } , initDir ) ; } param filePath param extension public static getFileNameWithoutExtension ( filePath , extension ? ) { let fileParts = filePath . split ( SEP ) ; let fileName = fileParts [ fileParts . length - 1 ] ; if ( extension ) { fileName = fileName . substr ( 0 , fileName . lastIndexOf ( extension ) ) ; } else { fileName = fileName . substr ( 0 , fileName . indexOf ( "." ) ) ; } return fileName ; } param src param dest public static copyRecursiveSync ( src , dest ) { let exists = fs . existsSync ( src ) ; if ( exists ) { let stats = fs . statSync ( src ) ; let isDirectory = stats . isDirectory ( ) ; if ( isDirectory ) { exists = fs . existsSync ( dest ) ; if ( ! exists ) { fs . mkdirSync ( dest ) ; } fs . readdirSync ( src ) . forEach ( function ( childItemName ) { FileUtils . copyRecursiveSync ( path . join ( src , childItemName ) , path . join ( dest , childItemName ) ) ; } ) ; } else { fs . copyFileSync ( src , dest ) ; } } } param src param foldername public static getFolderPath ( src , foldername ) { let exists = fs . existsSync ( src ) ; let toReturn = "" ; if ( exists ) { let stats = fs . statSync ( src ) ; let isDirectory = stats . isDirectory ( ) ; if ( isDirectory ) { let childs = fs . readdirSync ( src ) ; for ( let i = 0 ; i < childs . length ; i ++ ) { let childItemName = childs [ i ] ; if ( childItemName === foldername ) { toReturn = path . join ( src , childItemName ) ; } else { let childStat = fs . statSync ( path . join ( src , childItemName ) ) ; if ( childStat . isDirectory ( ) ) { toReturn = FileUtils . getFolderPath ( path . join ( src , childItemName ) , foldername ) ; } } if ( toReturn !== "" ) { break ; } } } } return toReturn ; } param folder public static deleteFolderRecursive ( folder ) { if ( fs . existsSync ( folder ) ) { fs . readdirSync ( folder ) . forEach ( function ( file , index ) { let curPath = path . join ( folder , file ) ; if ( fs . lstatSync ( curPath ) . isDirectory ( ) ) { FileUtils . deleteFolderRecursive ( curPath ) ; } else { fs . unlinkSync ( curPath ) ; } } ) ; fs . rmdirSync ( folder ) ; } } public static makefolderid ( length ) { var result = "" ; var characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" ; var charactersLength = characters . length ; for ( var i = 0 ; i < length ; i ++ ) { result += characters . charAt ( Math . floor ( Math . random ( ) * charactersLength ) ) ; } return result ; } }