diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 1315dbd..11f718b 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -55,6 +55,11 @@ jobs: env: VITE_BASE: ${{ env.VITE_BASE || format('{0}/', steps.pages.outputs.base_path) }} + - name: Verify links and asset paths + run: npm test + env: + VITE_BASE: ${{ env.VITE_BASE || format('{0}/', steps.pages.outputs.base_path) }} + - name: Collect PRs to deploy uses: actions/github-script@v9 with: diff --git a/.github/workflows/pr-preview.yml b/.github/workflows/pr-preview.yml index 6f6d45d..42a333e 100644 --- a/.github/workflows/pr-preview.yml +++ b/.github/workflows/pr-preview.yml @@ -26,7 +26,9 @@ jobs: - name: Determine Base URL Prefix id: base_url run: | - if [ "${{ github.event.repository.name }}" = "${{ github.repository_owner }}.github.io" ]; then + REPO_NAME=$(echo "${{ github.event.repository.name }}" | tr '[:upper:]' '[:lower:]') + OWNER_NAME=$(echo "${{ github.repository_owner }}" | tr '[:upper:]' '[:lower:]') + if [ "$REPO_NAME" = "${OWNER_NAME}.github.io" ]; then echo "prefix=/" >> $GITHUB_OUTPUT else echo "prefix=/${{ github.event.repository.name }}/" >> $GITHUB_OUTPUT @@ -44,6 +46,11 @@ jobs: env: VITE_BASE: ${{ steps.base_url.outputs.prefix }}pr-${{ github.event.number }}/ + - name: Verify links and asset paths + run: npm test + env: + VITE_BASE: ${{ steps.base_url.outputs.prefix }}pr-${{ github.event.number }}/ + - name: Package build run: | cd dist diff --git a/Dockerfile b/Dockerfile index b64a232..68081fd 100644 --- a/Dockerfile +++ b/Dockerfile @@ -13,6 +13,7 @@ ARG VITE_BASE=/ ENV VITE_BASE=${VITE_BASE} RUN npm run build +RUN npm test FROM nginx:alpine diff --git a/docs/news/index.md b/docs/news/index.md index 43b92be..ab5500f 100644 --- a/docs/news/index.md +++ b/docs/news/index.md @@ -16,7 +16,7 @@ description: Latest developments, changes, war updates, and traffic plots for Mu
Game News
Find out about the latest news from the game!
-
Web News
+
Web News
The news from this Web-page. Note these do not really get used, usually everything important gets put up as Game News.
Frequentation Plots
diff --git a/package.json b/package.json index 5039516..dacae02 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,8 @@ "preview": "vitepress preview docs", "docs:dev": "vitepress dev docs", "docs:build": "vitepress build docs", - "docs:preview": "vitepress preview docs" + "docs:preview": "vitepress preview docs", + "test": "node scripts/test-link-and-asset-paths.js" }, "dependencies": { "vue": "^3.5.42" diff --git a/scripts/ci-env.sh b/scripts/ci-env.sh index 55e9544..13b3d03 100755 --- a/scripts/ci-env.sh +++ b/scripts/ci-env.sh @@ -28,8 +28,10 @@ else fi echo "VITE_HOSTNAME=$VITE_HOSTNAME" >> $GITHUB_ENV -# Explicitly set VITE_BASE=/ for the official production environment -if [ "$OWNER" = "mume" ] && [ "$REPO" = "mume.github.io" ]; then +# Explicitly set VITE_BASE=/ for the official production environment if not already set +OWNER_LOWER=$(echo "$OWNER" | tr '[:upper:]' '[:lower:]') +REPO_LOWER=$(echo "$REPO" | tr '[:upper:]' '[:lower:]') +if [ -z "$VITE_BASE" ] && [ "$OWNER_LOWER" = "mume" ] && [ "$REPO_LOWER" = "mume.github.io" ]; then export VITE_BASE="/" echo "VITE_BASE=$VITE_BASE" >> $GITHUB_ENV fi diff --git a/scripts/test-link-and-asset-paths.js b/scripts/test-link-and-asset-paths.js new file mode 100644 index 0000000..401251b --- /dev/null +++ b/scripts/test-link-and-asset-paths.js @@ -0,0 +1,163 @@ +import fs from 'fs' +import path from 'path' +import { fileURLToPath } from 'url' + +const __filename = fileURLToPath(import.meta.url) +const __dirname = path.dirname(__filename) +const distDir = path.resolve(__dirname, '../dist') + +if (!fs.existsSync(distDir)) { + console.error(`[TEST ERROR] dist directory does not exist at ${distDir}. Run 'npm run build' first.`) + process.exit(1) +} + +function getAllHtmlFiles(dir, fileList = []) { + const files = fs.readdirSync(dir) + for (const file of files) { + const filePath = path.join(dir, file) + if (fs.statSync(filePath).isDirectory()) { + getAllHtmlFiles(filePath, fileList) + } else if (file.endsWith('.html')) { + fileList.push(filePath) + } + } + return fileList +} + +const htmlFiles = getAllHtmlFiles(distDir) +if (htmlFiles.length === 0) { + console.error('[TEST ERROR] No HTML files found in dist directory.') + process.exit(1) +} + +let errors = [] +let totalLinksChecked = 0 +let totalAssetsChecked = 0 + +let baseEnv = process.env.VITE_BASE || '/' +if (!baseEnv.startsWith('/')) baseEnv = '/' + baseEnv +if (!baseEnv.endsWith('/')) baseEnv = baseEnv + '/' +const expectedBase = baseEnv + +console.log(`[TEST] Verifying ${htmlFiles.length} HTML files in dist/ against expected base: "${expectedBase}"...`) + +for (const htmlFile of htmlFiles) { + const content = fs.readFileSync(htmlFile, 'utf8') + const relPath = path.relative(distDir, htmlFile) + + // 1. Check for invalid prefix '/mume.github.io/' if expectedBase does not contain it + if (!expectedBase.includes('/mume.github.io/') && content.includes('/mume.github.io/')) { + errors.push(`${relPath}: Contains invalid hardcoded path prefix '/mume.github.io/'`) + } + + // 2. Extract and check src/href attributes (,