Files
hometown/copy-public.js
2026-03-08 00:19:29 +08:00

35 lines
1.2 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.
#!/usr/bin/env node
/**
* 将根目录的 config.json、api.config.json、lib/、image/ 同步到 public/
* 供 Vite 在 dev 与 build 时使用。run.sh 写入 api.config.json 后执行 build 即可生效。
*/
const fs = require('fs');
const path = require('path');
const ROOT = path.resolve(__dirname);
const PUBLIC = path.join(ROOT, 'public');
function copyFile(src, dest) {
if (!fs.existsSync(src)) return;
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 fs.copyFileSync(s, d);
}
}
if (!fs.existsSync(PUBLIC)) fs.mkdirSync(PUBLIC, { recursive: true });
copyFile(path.join(ROOT, 'config.json'), path.join(PUBLIC, 'config.json'));
copyFile(path.join(ROOT, 'api.config.json'), path.join(PUBLIC, 'api.config.json'));
copyDir(path.join(ROOT, 'lib'), path.join(PUBLIC, 'lib'));
copyDir(path.join(ROOT, 'image'), path.join(PUBLIC, 'image'));
console.log('已同步 config/api.config.json、lib、image 到 public/');