import fs from "fs"; import path from "path"; import {FILE_INFO, PARSE_TYPE} from "../@types/types"; import {logger} from "../logger/Logger"; import ObjectUtil from "./ObjectUtil"; import convert, {ElementCompact} from "xml-js"; export default class FileUtil{ public static getFileInfo(_path:string):FILE_INFO { if(!_path) return {name:"", extension:""}; try { let fileArr = _path.split("/"); let fileNameStr = fileArr[fileArr.length-1]; let fileDesc = fileNameStr.split('.'); return { name: fileDesc[0], extension:fileDesc[1] } } catch (e) { logger.debug(" File info analyze from path falied "); return {name:"", extension:""}; } } public static getExt(file:string):string{ let ret:string = ""; if (file.lastIndexOf(".") > -1) { ret = file.substring(file.lastIndexOf(".")+1, file.length).toLowerCase(); } return ret; } public static getFileName(file:string):string{ let ret:string = file; if (file.lastIndexOf("/") > -1) { ret = file.substring(file.lastIndexOf("/")+1, file.length); } if (ret.lastIndexOf(".") > -1) { ret = ret.substring(0, ret.lastIndexOf(".")); } return ret; } public static getParentPath(file:string):string{ const name:string = this.getFileName(file); return file.substring(0, file.lastIndexOf(name)); } public static getFileStat(file:string){ try{ return fs.statSync(file); }catch(e){} } public static isExist(_path:string){ // try{ // fs.readFileSync(_path, "utf-8"); // }catch(e){ // return false; // } // return true; try{ // PathLike = string | Buffer | URL; return fs.existsSync(_path); }catch(e){ return false; } // return true; } // 같은 함수 제거 // public static isExistDir(_path:string){ // // try{ // // fs.readdirSync(_path, "utf-8"); // // }catch(e){ // // return false; // // } // // return true; // // try{ // // PathLike = string | Buffer | URL; // return fs.existsSync(_path); // }catch(e){ // return false; // } // // return true; // } public static readString(_path:string){ try{ return fs.readFileSync(_path, "utf-8") }catch(e) { return ""; } } // 같은 함수 제거 // public static readToString(_path:string){ // if (path == null) { // _path = path; // } // try{ // return fs.readFileSync(_path, "utf-8"); // }catch(e){ // // console.log(e); // } // return ""; // } public static readJson(file:string):any{ // let json = {}; // try{ // json = eval(fs.readFileSync(file, "utf-8")); // }catch(e) {} // return json; const str = this.readString(file); return ObjectUtil.parse(str, PARSE_TYPE.JSON) } public static readXmlToJson(file:string):ElementCompact | undefined{ try { let content = fs.readFileSync(file, "utf-8") return convert.xml2js(content, {compact: true}); }catch (e) { return undefined; } } public static readFiles(_path:string) { if (path == null) { _path = path; } let filenames = []; try{ if (this.isExist(_path)) { let readFiles = fs.readdirSync(_path); for (let i = 0; i < readFiles.length; i++) { const abPath = path + "/" + readFiles[i]; let stat = fs.lstatSync(abPath); if(stat.isFile()){ let info = { name:readFiles[i] ,absolutePath:abPath ,parentPath:path ,size:stat } filenames.push(info); } } } }catch(e){ // console.log(e); } return filenames; } public static writeString(file: string, content: string) { try{ fs.mkdirSync(this.getParentPath(file), { recursive: true }); fs.writeFileSync(file, content, {encoding:"utf-8"}); }catch(e){} } // public static format(...arguments:any){ // let text = arguments[0]; // for (let i = 1; i < arguments.length; i++) { // text = String(text).replace('%s', arguments[i]); // } // return text; // } }