66 lines
1.6 KiB
TypeScript
Raw Normal View History

2021-06-27 22:06:11 +08:00
import RuleInterface from "./RuleInterface";
import FileObj from "../FileObj";
2021-06-28 18:07:02 +08:00
import path from 'path';
2021-06-27 22:06:11 +08:00
export default class InsertRule implements RuleInterface {
/**
*
*/
insertContent: string;
/**
* frontbackendat:位置replace:替换当前文件名
*/
type: string;
/**
* type为at,,1
*/
atInput: number;
/**
* type为at,true:false:
*/
atIsRightToleft: boolean;
/**
* true:false
*/
ignorePostfix: boolean;
constructor(data: any) {
this.insertContent = data.insertContent;
this.type = data.type;
this.atInput = data.atInput;
this.atIsRightToleft = data.atIsRightToleft;
this.ignorePostfix = data.ignorePostfix;
}
2021-06-28 18:07:02 +08:00
deal(file: FileObj): void {
let str = this.ignorePostfix ? file.realName : file.name;
switch (this.type) {
case "front":
str = this.insertContent + str;
break;
case "backend":
str = str + this.insertContent;
break;
case "at":
let index = this.atIsRightToleft ? str.length - this.atInput + 1 : this.atInput - 1;
str = str.substring(0, index) + this.insertContent + str.substring(index);
break;
case "replace":
str = this.insertContent;
break;
}
if (this.ignorePostfix) {
file.realName = str;
} else {
file.expandName = path.extname(str);
if (file.expandName.length > 0) {
file.realName = str.substring(0, str.lastIndexOf("."));
} else {
file.realName = str;
}
}
file.name = file.realName + file.expandName;
2021-06-27 22:06:11 +08:00
}
}