|
| 1 | +import { existsSync, mkdirSync, readFileSync, readdirSync, rmSync, writeFileSync } from "node:fs"; |
| 2 | +import { cp } from "node:fs/promises"; |
| 3 | +import { join, resolve } from "node:path"; |
| 4 | + |
| 5 | +const BASE_DIR = resolve("../boot/src/main/resources"); |
| 6 | +const STATIC_DIR = join(BASE_DIR, "static"); |
| 7 | +const ASSETS_DIR = join(STATIC_DIR, "assets"); |
| 8 | +const TEMPLATES_DIR = join(BASE_DIR, "templates"); |
| 9 | +const SRC_DIR = resolve("dist"); |
| 10 | + |
| 11 | +async function main() { |
| 12 | + console.log("Copy assets into SpringBoot resources"); |
| 13 | + |
| 14 | + if (!existsSync(SRC_DIR)) { |
| 15 | + console.error(`Error: ${SRC_DIR} does not exist`); |
| 16 | + process.exit(1); |
| 17 | + } |
| 18 | + |
| 19 | + mkdirSync(ASSETS_DIR, { recursive: true }); |
| 20 | + mkdirSync(TEMPLATES_DIR, { recursive: true }); |
| 21 | + |
| 22 | + if (existsSync(ASSETS_DIR)) { |
| 23 | + const files = readdirSync(ASSETS_DIR); |
| 24 | + for (const file of files) { |
| 25 | + rmSync(join(ASSETS_DIR, file), { recursive: true, force: true }); |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + try { |
| 30 | + if (existsSync(join(SRC_DIR, "vite.svg"))) { |
| 31 | + await cp(join(SRC_DIR, "vite.svg"), join(STATIC_DIR, "vite.svg")); |
| 32 | + } |
| 33 | + |
| 34 | + const assetsSourceDir = join(SRC_DIR, "assets"); |
| 35 | + if (existsSync(assetsSourceDir)) { |
| 36 | + await cp(assetsSourceDir, ASSETS_DIR, { recursive: true }); |
| 37 | + } |
| 38 | + } catch (err) { |
| 39 | + console.error("Error copying assets:", err); |
| 40 | + process.exit(1); |
| 41 | + } |
| 42 | + |
| 43 | + const INDEX_SRC = join(SRC_DIR, "index.html"); |
| 44 | + const INDEX_DEST = join(TEMPLATES_DIR, "index.html"); |
| 45 | + |
| 46 | + if (!existsSync(INDEX_SRC)) { |
| 47 | + console.error(`Error: ${INDEX_SRC} does not exist. Make sure you built the frontend project first.`); |
| 48 | + process.exit(1); |
| 49 | + } |
| 50 | + |
| 51 | + const htmlContent = readFileSync(INDEX_SRC, "utf8"); |
| 52 | + const updatedHtml = htmlContent |
| 53 | + .replace(/href="([^"]*)"/g, 'th:href="@{$1}"') |
| 54 | + .replace(/src="([^"]*)"/g, 'th:src="@{$1}"'); |
| 55 | + |
| 56 | + writeFileSync(INDEX_DEST, updatedHtml); |
| 57 | + console.log("SpringBoot resources updated successfully"); |
| 58 | +} |
| 59 | + |
| 60 | +main().catch((err) => { |
| 61 | + console.error("Error:", err); |
| 62 | + process.exit(1); |
| 63 | +}); |
0 commit comments