Files
PC-Light-Forum/js/save.js
DESKTOP-RQ919RC\Pc 40d06c180f feat: 添加项目库组件并优化搜索页面样式和功能
新增项目库组件item-project,包含学校、专业、学费等信息的展示
优化搜索页面样式,调整分类项宽度和间距
修复搜索页面滚动定位问题,完善分页和URL参数处理
添加本地开发服务器脚本serve.ps1
更新public.css和public.less样式文件
2025-11-20 19:11:48 +08:00

99 lines
4.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const fs = require("fs");
const path = require("path");
// 配置需要监听的文件列表:键为 txt 文件路径,值为对应的 js 文件路径
const watchList = {
// 监听 item-bottom.txt同步到 item-bottom.js
"../component/item-bottom/item-bottom.txt": "../component/item-bottom/item-bottom.js",
// 监听 item-forum.txt同步到 item-forum.js
"../component/item-forum/item-forum.txt": "../component/item-forum/item-forum.js",
// 监听 item-head.txt同步到 item-head.js
"../component/item-head/item-head.txt": "../component/item-head/item-head.js",
// 监听 item-offer.txt同步到 item-offer.js
"../component/item-offer/item-offer.txt": "../component/item-offer/item-offer.js",
// 监听 item-summary.txt同步到 item-summary.js
"../component/item-summary/item-summary.txt": "../component/item-summary/item-summary.js",
// 监听 item-vote.txt同步到 item-vote.js
"../component/item-vote/item-vote.txt": "../component/item-vote/item-vote.js",
// 监听 item-mj.txt同步到 item-mj.js
"../component/item-mj/item-mj.txt": "../component/item-mj/item-mj.js",
// 监听 item-tenement.txt同步到 item-tenement.js
"../component/item-tenement/item-tenement.txt": "../component/item-tenement/item-tenement.js",
// 监听 latest-list.txt同步到 latest-list.js
"../component/latest-list/latest-list.txt": "../component/latest-list/latest-list.js",
// 监听 slideshow-box.txt同步到 slideshow-box.js
"../component/slideshow-box/slideshow-box.txt": "../component/slideshow-box/slideshow-box.js",
// 监听 head-top.txt同步到 head-top.js
"../component/head-top/head-top.txt": "../component/head-top/head-top.js",
// 监听 hot-tag.txt同步到 hot-tag.js
"../component/hot-tag/hot-tag.txt": "../component/hot-tag/hot-tag.js",
// 监听 hot-search.txt同步到 hot-search.js
"../component/hot-search/hot-search.txt": "../component/hot-search/hot-search.js",
// 监听 item-project.txt同步到 item-project.js
"../component/item-project/item-project.txt": "../component/item-project/item-project.js",
// 监听 bi.txt同步到 bi.js
"../component/bi/bi.txt": "../component/bi/bi.js",
// 可添加更多文件(格式:'txt路径': 'js路径'
// './component/other/other.txt': './component/other/other.js',
};
// 检查所有文件是否存在
Object.entries(watchList).forEach(([txtPath, jsPath]) => {
const resolvedTxt = path.resolve(__dirname, txtPath);
const resolvedJs = path.resolve(__dirname, jsPath);
if (!fs.existsSync(resolvedTxt)) {
console.error(`错误:文件不存在 - ${resolvedTxt}`);
process.exit(1);
}
if (!fs.existsSync(resolvedJs)) {
console.error(`错误:文件不存在 - ${resolvedJs}`);
process.exit(1);
}
});
// 同步逻辑:将 txtContent 替换到对应 js 的 template 中
function syncContent(txtPath, jsPath) {
try {
const txtContent = fs.readFileSync(txtPath, "utf8")?.replace(/[\n\r]/g, '');
let jsContent = fs.readFileSync(jsPath, "utf8");
// 匹配 template: `...` 结构,替换反引号内的内容
const templateRegex = /(template:\s*)(`[^`]*`)/;
// 匹配 template.innerHTML = `...` 结构
const innerHTMLRegex = /(template\.innerHTML\s*=\s*)(`[^`]*`)/;
if (templateRegex.test(jsContent)) {
jsContent = jsContent.replace(templateRegex, `$1\`${txtContent}\``);
fs.writeFileSync(jsPath, jsContent, "utf8");
console.log(`✅ 同步成功:${path.basename(txtPath)}${path.basename(jsPath)} (template格式)`);
} else if (innerHTMLRegex.test(jsContent)) {
jsContent = jsContent.replace(innerHTMLRegex, `$1\`${txtContent}\``);
fs.writeFileSync(jsPath, jsContent, "utf8");
console.log(`✅ 同步成功:${path.basename(txtPath)}${path.basename(jsPath)} (innerHTML格式)`);
} else {
console.error(`❌ 格式错误:${path.basename(jsPath)} 中未找到 template: \`...\` 或 template.innerHTML = \`...\``);
}
} catch (err) {
console.error(`❌ 同步失败:${err.message}`);
}
}
// 为每个文件添加监听
Object.entries(watchList).forEach(([txtPath, jsPath]) => {
const resolvedTxt = path.resolve(__dirname, txtPath);
const resolvedJs = path.resolve(__dirname, jsPath);
fs.watch(resolvedTxt, (eventType) => {
if (eventType === "change") {
console.log(`\n检测到 ${path.basename(resolvedTxt)} 保存,开始同步...`);
syncContent(resolvedTxt, resolvedJs);
}
});
console.log(`开始监听:${resolvedTxt}`);
});
console.log("\n服务启动成功所有文件修改后将自动同步 🚀");