Files
jiapuapp/tests/compile-audit.ps1
T

67 lines
2.4 KiB
PowerShell

$ErrorActionPreference = 'Stop'
$root = Split-Path -Parent $PSScriptRoot
$errors = [System.Collections.Generic.List[string]]::new()
function Test-LocalImport {
param(
[string]$SourceFile,
[string]$Target,
[string[]]$Extensions
)
if ($Target -match '^(https?:|sass:)') { return $true }
if ($Target.StartsWith('@/')) {
$candidate = Join-Path $root $Target.Substring(2)
} elseif ($Target.StartsWith('./') -or $Target.StartsWith('../')) {
$candidate = Join-Path (Split-Path -Parent $SourceFile) $Target
} else {
return $true
}
if (Test-Path -LiteralPath $candidate) { return $true }
foreach ($extension in $Extensions) {
if (Test-Path -LiteralPath ($candidate + $extension)) { return $true }
}
return $false
}
$pages = Get-Content -Raw -Encoding UTF8 (Join-Path $root 'pages.json') | ConvertFrom-Json
foreach ($page in $pages.pages) {
$pageFile = Join-Path $root ($page.path + '.vue')
if (-not (Test-Path -LiteralPath $pageFile)) {
$errors.Add("Route has no page file: $($page.path)")
}
}
$manifest = Get-Content -Raw -Encoding UTF8 (Join-Path $root 'manifest.json') | ConvertFrom-Json
if ($manifest.vueVersion -eq '3' -and -not (Test-Path -LiteralPath (Join-Path $root 'index.html'))) {
$errors.Add('Vue 3 project is missing index.html.')
}
$sourceFiles = Get-ChildItem -Path $root -Recurse -File | Where-Object {
$_.FullName -notmatch '\\unpackage\\' -and $_.Extension -in '.vue', '.js', '.scss'
}
foreach ($source in $sourceFiles) {
$content = Get-Content -Raw -Encoding UTF8 $source.FullName
foreach ($match in [regex]::Matches($content, '@(?:import|use|forward)\s+["''](?<target>[^"'']+)["'']')) {
$target = $match.Groups['target'].Value
if (-not (Test-LocalImport -SourceFile $source.FullName -Target $target -Extensions @('.scss'))) {
$errors.Add("Missing Sass import in $($source.FullName.Substring($root.Length + 1)): $target")
}
}
foreach ($match in [regex]::Matches($content, '(?:from|import)\s+["''](?<target>[^"'']+)["'']')) {
$target = $match.Groups['target'].Value
if (-not (Test-LocalImport -SourceFile $source.FullName -Target $target -Extensions @('.js', '.vue', '.json'))) {
$errors.Add("Missing module import in $($source.FullName.Substring($root.Length + 1)): $target")
}
}
}
if ($errors.Count -gt 0) {
throw "Compile audit failed:`n$($errors -join "`n")"
}
Write-Output 'PASS compile audit'