t-comm
Version:
专业、稳定、纯粹的工具库
65 lines (62 loc) • 2.08 kB
JavaScript
import fs__default from 'fs';
import { readFileSync, writeFileSync } from '../fs/fs.mjs';
import { checkValidReplaceList } from './helper.mjs';
function replaceContentSimple(_a) {
var replaceList = _a.replaceList;
var _loop_1 = function _loop_1(item) {
var dirList = item.dirList,
to = item.to,
from = item.from,
list = item.list;
var newDir = Array.isArray(dirList) ? dirList : [dirList];
if (!(newDir === null || newDir === void 0 ? void 0 : newDir.length)) return "continue";
if (list === null || list === void 0 ? void 0 : list.length) {
checkValidReplaceList(list);
list.forEach(function (inner) {
replaceEachContent({
dirList: newDir,
to: (inner === null || inner === void 0 ? void 0 : inner[1]) || '',
from: (inner === null || inner === void 0 ? void 0 : inner[0]) || ''
});
});
return "continue";
}
if (!from || !to) return "continue";
replaceEachContent({
dirList: newDir,
to: to,
from: from
});
};
for (var _i = 0, replaceList_1 = replaceList; _i < replaceList_1.length; _i++) {
var item = replaceList_1[_i];
_loop_1(item);
}
}
function replaceEachContent(_a) {
var dirList = _a.dirList,
to = _a.to,
from = _a.from;
// eslint-disable-next-line @typescript-eslint/no-require-imports
var glob = require('glob');
var list = glob.sync(dirList);
if (!from) {
throw new Error('[replaceEachContent] from 不能为空!');
}
for (var _i = 0, list_1 = list; _i < list_1.length; _i++) {
var file = list_1[_i];
if (fs__default.statSync(file).isDirectory()) {
continue;
}
var content = readFileSync(file);
var newContent = content;
if (typeof from === 'string') {
newContent = content.replaceAll(from, to);
} else {
newContent = content.replace(from, to);
}
writeFileSync(file, newContent);
console.log("[Replace Content]: \u6587\u4EF6: ".concat(file, ", from: ").concat(from, ", to: ").concat(to));
}
}
export { replaceContentSimple };