Files
hometown/build.js
2026-03-09 09:56:43 +08:00

69 lines
1.9 KiB
JavaScript
Raw Permalink 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.
#!/usr/bin/env node
/**
* 将静态资源复制到 dist/,便于直接部署到任意静态主机。
* 产出: dist/index.html, dist/config.json, dist/lib/, dist/image/
*/
const fs = require('fs');
const path = require('path');
const ROOT = path.resolve(__dirname);
const DIST = path.join(ROOT, 'dist');
function copyFile(src, dest) {
fs.mkdirSync(path.dirname(dest), { recursive: true });
fs.copyFileSync(src, dest);
}
function copyDir(srcDir, destDir) {
if (!fs.existsSync(srcDir)) return;
fs.mkdirSync(destDir, { recursive: true });
for (const name of fs.readdirSync(srcDir)) {
const s = path.join(srcDir, name);
const d = path.join(destDir, name);
if (fs.statSync(s).isDirectory()) copyDir(s, d);
else copyFile(s, d);
}
}
/** 清空目录内容(不删目录本身),避免 EPERM如宝塔 wwwroot 下 dist 无法 rmSync */
function emptyDir(dir) {
if (!fs.existsSync(dir)) return;
for (const name of fs.readdirSync(dir)) {
const p = path.join(dir, name);
try {
if (fs.statSync(p).isDirectory()) {
fs.rmSync(p, { recursive: true });
} else {
fs.unlinkSync(p);
}
} catch (e) {
if (e.code !== 'EPERM' && e.code !== 'EACCES') throw e;
}
}
}
function main() {
if (fs.existsSync(DIST)) {
try {
fs.rmSync(DIST, { recursive: true });
} catch (e) {
if (e.code === 'EPERM' || e.code === 'EACCES') {
emptyDir(DIST);
} else {
throw e;
}
}
}
fs.mkdirSync(DIST, { recursive: true });
copyFile(path.join(ROOT, 'index.html'), path.join(DIST, 'index.html'));
copyFile(path.join(ROOT, 'config.json'), path.join(DIST, 'config.json'));
copyFile(path.join(ROOT, 'api.config.json'), path.join(DIST, 'api.config.json'));
copyDir(path.join(ROOT, 'lib'), path.join(DIST, 'lib'));
copyDir(path.join(ROOT, 'image'), path.join(DIST, 'image'));
console.log('已构建到 dist/(前端静态),可直接部署。');
}
main();