t-comm
Version:
专业、稳定、纯粹的工具库
73 lines (66 loc) • 2.35 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
var fs = require('fs');
var fs_fs = require('../fs/fs.js');
var syncRepo_helper = require('./helper.js');
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
var fs__default = /*#__PURE__*/_interopDefaultLegacy(fs);
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) {
syncRepo_helper.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["default"].statSync(file).isDirectory()) {
continue;
}
var content = fs_fs.readFileSync(file);
var newContent = content;
if (typeof from === 'string') {
newContent = content.replaceAll(from, to);
} else {
newContent = content.replace(from, to);
}
fs_fs.writeFileSync(file, newContent);
console.log("[Replace Content]: \u6587\u4EF6: ".concat(file, ", from: ").concat(from, ", to: ").concat(to));
}
}
exports.replaceContentSimple = replaceContentSimple;