toeic-vocab-tw / scripts /update_metadata.ps1
LEE-WHITE's picture
Duplicate from kknono668/toeic-vocab-tw
8850458 verified
Param(
[string]$Version
)
$ErrorActionPreference = 'Stop'
$root = Split-Path $PSScriptRoot -Parent
$jsonPath = Join-Path $root 'data' 'toeic_vocabulary.json'
if (!(Test-Path $jsonPath)) { throw "找不到資料檔:$jsonPath" }
$data = Get-Content -Path $jsonPath -Raw -Encoding UTF8 | ConvertFrom-Json
if ($data -is [System.Array]) {
# 新結構:頂層陣列,metadata 存在 data/metadata.json
$total = @($data).Count
$metaPath = Join-Path $root 'data' 'metadata.json'
if (Test-Path $metaPath) {
$meta = Get-Content -Path $metaPath -Raw -Encoding UTF8 | ConvertFrom-Json
} else {
$meta = [ordered]@{}
}
$meta.total_words = $total
$meta.last_updated = (Get-Date).ToString('o')
if ($Version) { $meta.version = $Version }
($meta | ConvertTo-Json -Depth 100) | Set-Content -Path $metaPath -Encoding UTF8
Write-Host "已更新 metadata.json:total_words=$total; version=$($meta.version); last_updated=$($meta.last_updated)"
} else {
# 舊結構:含 metadata 與 vocabulary_by_importance
$total = 0
foreach ($prop in $data.vocabulary_by_importance.PSObject.Properties) {
$items = $prop.Value
if ($items -is [System.Collections.IEnumerable]) {
$total += @($items).Count
}
}
$data.metadata.total_words = $total
$data.metadata.last_updated = (Get-Date).ToString('o')
if ($Version) { $data.metadata.version = $Version }
$jsonOut = $data | ConvertTo-Json -Depth 100
Set-Content -Path $jsonPath -Value $jsonOut -Encoding UTF8
Write-Host "已更新 metadata:total_words=$total; version=$($data.metadata.version); last_updated=$($data.metadata.last_updated)"
}