83 lines
2.0 KiB
TypeScript
Raw Normal View History

2021-12-06 23:26:38 +08:00
import RuleInterface from "./RuleInterface";
import FileObj from "../FileObj";
import path from 'path';
2022-04-28 15:06:59 +08:00
let pattern = new RegExp(/s(eason)?(\d+)/);
2021-12-06 23:26:38 +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;
2022-04-28 15:06:59 +08:00
/**
*/
autoSeason: boolean;
2021-12-06 23:26:38 +08:00
constructor(data: any) {
this.insertContent = data.insertContent;
this.type = data.type;
this.atInput = data.atInput;
this.atIsRightToleft = data.atIsRightToleft;
this.ignorePostfix = data.ignorePostfix;
2022-04-28 15:06:59 +08:00
this.autoSeason = data.autoSeason;
2021-12-06 23:26:38 +08:00
}
deal(file: FileObj): void {
let str = this.ignorePostfix ? file.realName : file.name;
2022-06-12 13:24:47 +08:00
let season = '';
if (this.autoSeason) {
let patternRes = path.basename(file.path).replace(/[ ]+/, "").toLocaleLowerCase().match(pattern);
if (patternRes && patternRes[2]) {
season = patternRes[2];
}
}
2021-12-06 23:26:38 +08:00
switch (this.type) {
case "front":
2022-06-12 13:24:47 +08:00
str = this.insertContent + season + str;
2021-12-06 23:26:38 +08:00
break;
case "backend":
2022-06-12 13:24:47 +08:00
str = str + this.insertContent + season;
2021-12-06 23:26:38 +08:00
break;
case "at":
let index = this.atIsRightToleft ? str.length - this.atInput + 1 : this.atInput - 1;
2022-06-12 13:24:47 +08:00
str = str.substring(0, index) + this.insertContent + season + str.substring(index);
2021-12-06 23:26:38 +08:00
break;
case "replace":
2022-06-12 13:24:47 +08:00
str = this.insertContent + season;
2021-12-06 23:26:38 +08:00
break;
}
2022-06-12 13:24:47 +08:00
2022-04-28 15:06:59 +08:00
2021-12-06 23:26:38 +08:00
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;
}
}
2022-04-28 15:06:59 +08:00
2021-12-06 23:26:38 +08:00
file.name = file.realName + file.expandName;
}
2021-06-27 22:06:11 +08:00
}