front/scripts/copy-login-map-assets.js
2026-03-23 10:00:44 +08:00

63 lines
1.9 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.

/**
* 构建前复制登录页地图静态资源到 public:
* - echarts.min.js(自托管,避免 CDN)
* - world.json(世界地图 GeoJSON)
*/
import { mkdir, copyFile, access, writeFile } from 'fs/promises';
import { join, dirname } from 'path';
import { fileURLToPath } from 'url';
const VERBOSE_SCRIPT_LOGS = process.env.DEBUG_SCRIPT_LOGS === 'true';
if (!VERBOSE_SCRIPT_LOGS) {
console.log = () => {};
console.warn = () => {};
}
const __dirname = dirname(fileURLToPath(import.meta.url));
const root = join(__dirname, '..');
const publicDir = join(root, 'public');
const assetsEcharts = join(publicDir, 'assets', 'echarts');
const assetsGeo = join(publicDir, 'assets', 'geo');
const echartsSrc = join(root, 'node_modules', 'echarts', 'dist', 'echarts.min.js');
const worldUrl = 'https://echarts.apache.org/examples/data/asset/geo/world.json';
async function ensureDir(p) {
await mkdir(p, { recursive: true });
}
async function copyEcharts() {
await ensureDir(assetsEcharts);
const dest = join(assetsEcharts, 'echarts.min.js');
try {
await access(echartsSrc);
await copyFile(echartsSrc, dest);
console.log('[copy-login-map-assets] echarts.min.js copied to public/assets/echarts/');
} catch (e) {
console.warn('[copy-login-map-assets] echarts.min.js not found, skip:', e.message);
}
}
async function downloadWorld() {
await ensureDir(assetsGeo);
const dest = join(assetsGeo, 'world.json');
const res = await fetch(worldUrl);
if (!res.ok) throw new Error(res.statusText);
const buf = await res.arrayBuffer();
await writeFile(dest, Buffer.from(buf));
console.log('[copy-login-map-assets] world.json downloaded to public/assets/geo/');
}
async function main() {
await copyEcharts();
try {
await downloadWorld();
} catch (e) {
console.warn('[copy-login-map-assets] world.json download failed:', e.message);
}
}
main().catch((e) => {
console.error(e);
process.exit(1);
});