1/* 2* Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development 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 16const vscode = require("vscode"); 17import * as fs from 'fs'; 18import { getGenerateConf, GEN_TYPE } from './conf'; 19import { getCurrentTimeString } from './tool'; 20import * as logs from './log'; 21 22export class Filer { 23 genType: number = GEN_TYPE.GEN_NEW; 24 private static instance: Filer; 25 26 constructor() { 27 this.genType = getGenerateConf(); 28 } 29 30 static getInstance(): Filer { 31 if (!Filer.instance) { 32 Filer.instance = new Filer(); 33 } 34 return Filer.instance; 35 } 36 37 setGenType(value: number) { 38 this.genType = value; 39 } 40 41 getGenType(): number { 42 return this.genType; 43 } 44 45 getNewFileName(filePath: string): string { 46 let pathList = filePath.split('.'); 47 let prevFile = ''; 48 for (let i=0;i<pathList.length-1;i++) { 49 prevFile = pathList[i]; 50 } 51 let postFile = pathList[pathList.length-1]; 52 let ctstr = getCurrentTimeString(); 53 let newFileName = prevFile + ctstr + postFile; 54 logs.Logger.getInstance().info('getNewFileName: ' + newFileName); 55 return newFileName; 56 } 57 58 getNewDirName(dirPath: string): string { 59 let ctstr = getCurrentTimeString(); 60 let newDirName = dirPath + ctstr; 61 logs.Logger.getInstance().info('getNewDirName: ' + newDirName); 62 return newDirName; 63 } 64 65 saveFileSync(filePath: string, fileContent: string): string { 66 if (fs.existsSync(filePath)) { 67 // TODO: if file exist, replace, append or new file 68 switch (this.genType) { 69 case GEN_TYPE.GEN_APPEND: 70 fs.appendFileSync(filePath, fileContent); 71 break; 72 case GEN_TYPE.GEN_REPLACE: 73 fs.writeFileSync(filePath, fileContent); 74 break; 75 case GEN_TYPE.GEN_NEW: 76 let newFileName = this.getNewFileName(filePath); 77 fs.writeFileSync(newFileName, fileContent); 78 return newFileName; 79 break; 80 default: 81 logs.Logger.getInstance().error('Unknown gen type!'); 82 break; 83 } 84 } else { 85 fs.writeFileSync(filePath, fileContent); 86 } 87 return filePath; 88 } 89 90 mkdirSync(dirPath: string): string { 91 if (fs.existsSync(dirPath)) { 92 // TODO: if file exist, replace, append or new file 93 switch (this.genType) { 94 case GEN_TYPE.GEN_APPEND: 95 logs.Logger.getInstance().warn('Dir ' + dirPath + ' exist'); 96 break; 97 case GEN_TYPE.GEN_REPLACE: 98 logs.Logger.getInstance().warn('Dir ' + dirPath + ' exist'); 99 break; 100 case GEN_TYPE.GEN_NEW: 101 let newDirName = this.getNewDirName(dirPath); 102 fs.mkdirSync(newDirName, { recursive: true }); 103 return newDirName; 104 break; 105 default: 106 logs.Logger.getInstance().error('Unknown gen type!'); 107 break; 108 } 109 } else { 110 fs.mkdirSync(dirPath, { recursive: true }); 111 } 112 return dirPath; 113 } 114} 115 116 117