2025-11-12 18:02:15 +10:00
|
|
|
$content = Get-Content 'coreDefinitions.json' | ConvertFrom-Json
|
2025-08-12 11:31:41 +10:00
|
|
|
|
|
|
|
|
$jsDocMatch = '/** @type {'
|
|
|
|
|
|
|
|
|
|
$content.events | ForEach-Object {
|
|
|
|
|
# Let's construct our JSDoc and regex match
|
|
|
|
|
# "name": "inRange",
|
|
|
|
|
# "cppName": "InRange",
|
|
|
|
|
# "params": {
|
|
|
|
|
# "num": 2,
|
|
|
|
|
# "list": [
|
|
|
|
|
# { "name": "srcObj", "type": "BaseObject", "required": true },
|
|
|
|
|
# { "name": "objInRange", "type": "BaseObject", "required": true }
|
|
|
|
|
# ]
|
|
|
|
|
# },
|
|
|
|
|
# "returns": "void"
|
2025-11-12 18:02:15 +10:00
|
|
|
$parmCountMatch = $false
|
2025-08-12 11:31:41 +10:00
|
|
|
$newRegex = "function $($_.name)\("
|
2025-11-12 18:02:15 +10:00
|
|
|
if( $_.name -eq 'onCallback' ) {
|
2025-08-12 11:31:41 +10:00
|
|
|
$newRegex = "function $($_.name)\d+\("
|
|
|
|
|
}
|
2025-11-12 18:02:15 +10:00
|
|
|
elseif( $_.name -eq 'onSpelLCast' ) {
|
|
|
|
|
$parmCountMatch = $true
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-12 11:31:41 +10:00
|
|
|
# /** @type { ( srcObj: BaseObject, objInRange: BaseObject ) => void } */
|
|
|
|
|
$jsDoc = '/** @type { ('
|
|
|
|
|
$pLen = $_.params.list.Length
|
|
|
|
|
if( $pLen -gt 0 ) {
|
|
|
|
|
for( $i = 0; $i -lt $pLen; $i++ ) {
|
|
|
|
|
$p = $_.params.list[$i]
|
|
|
|
|
$jsDoc += " $($p.name): $($p.type)"
|
|
|
|
|
if( $i -ne ($pLen - 1) ) {
|
|
|
|
|
$jsDoc += ','
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$jsDoc += " ) => $($_.returns) } */"
|
2025-11-12 18:02:15 +10:00
|
|
|
$_ | Add-Member -MemberType NoteProperty -Name 'Regex' -Value $newRegex
|
|
|
|
|
$_ | Add-Member -MemberType NoteProperty -Name 'JSDoc' -Value $jsDoc
|
|
|
|
|
$_ | Add-Member -MemberType NoteProperty -Name 'MatchParmCount' -Value $parmCountMatch
|
2025-08-12 11:31:41 +10:00
|
|
|
}
|
|
|
|
|
|
2025-08-13 14:49:57 +10:00
|
|
|
$possibles = $content.events | Select-Object -Property name, Regex, JSDoc
|
|
|
|
|
|
|
|
|
|
# Now add the extra e.g. command registration
|
2025-11-12 18:02:15 +10:00
|
|
|
$possibles += @( [PSCustomObject]@{ name = 'Command Registration'; Regex = 'function command_.+\('; JSDoc = '/** @type { ( socket: Socket, cmdString: string ) => void } */' } )
|
2025-08-12 11:31:41 +10:00
|
|
|
|
|
|
|
|
$jsFiles = Get-ChildItem -Path $PSScriptRoot -Recurse -Include *.js
|
|
|
|
|
foreach( $js in $jsFiles ) {
|
|
|
|
|
Write-Output "Processing $($js.FullName)"
|
|
|
|
|
$lines = Get-Content $js
|
|
|
|
|
Write-Output " .. Found $($lines.Length) lines"
|
|
|
|
|
$anyChange = $false
|
|
|
|
|
$inserts = @()
|
|
|
|
|
for( $line = 0 ; $line -lt $lines.Length; $line++ ) {
|
2025-08-13 14:49:57 +10:00
|
|
|
foreach( $possible in $possibles ) {
|
2025-08-12 11:31:41 +10:00
|
|
|
#Write-Output "Checking $($possible.name)"
|
|
|
|
|
if( $lines[$line] -match $possible.Regex ) {
|
|
|
|
|
# Got a match ... did the previous line look like JSDoc?
|
2025-11-12 18:02:15 +10:00
|
|
|
$doIt = $true
|
|
|
|
|
if( $possible.MatchParmCount ) {
|
|
|
|
|
$parmRegex = '.+\(([^)]*)\)'
|
|
|
|
|
if( $lines[$line] -match $parmRegex ) {
|
|
|
|
|
$params = $matches[1].Trim()
|
|
|
|
|
if( $params -eq '' ) {
|
|
|
|
|
$count = 0
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
$count = ($params -split '\s*,\s*').Count
|
|
|
|
|
}
|
|
|
|
|
Write-Output "Number of parameters: $count"
|
|
|
|
|
}
|
|
|
|
|
$doIt = ($count -eq $possible.params.list.length)
|
|
|
|
|
}
|
2025-08-12 11:31:41 +10:00
|
|
|
# Because if so, we may have to replace it
|
2025-11-12 18:02:15 +10:00
|
|
|
if( $doIt ) {
|
|
|
|
|
if( $line -gt 0 -and $lines[$line - 1].StartsWith( $jsDocMatch ) ) {
|
|
|
|
|
# OK, Javadoc to replace
|
|
|
|
|
Write-Output "Updating JSDoc for $($possible.name) at $($line - 1)"
|
|
|
|
|
$lines[$line - 1] = $possible.JSDoc
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
# We have to insert a line, now ...
|
|
|
|
|
$inserts += [PSCustomObject]@{ Index = $line - 1; Value = $possible.JSDoc }
|
|
|
|
|
Write-Output "Inserting JSDoc for $($possible.name) at $($line - 1)"
|
|
|
|
|
}
|
2025-08-12 11:31:41 +10:00
|
|
|
}
|
|
|
|
|
$anyChange = $true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if( $anyChange -eq $true ) {
|
|
|
|
|
if( $inserts.Length -eq 0 ) {
|
2025-11-12 18:02:15 +10:00
|
|
|
Write-Output 'Replacing contents with JSDoc updates'
|
2025-08-12 11:31:41 +10:00
|
|
|
# Only replacements of existing JSDoc, not additions
|
|
|
|
|
$lines | Set-Content $js
|
2025-11-12 18:02:15 +10:00
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
Write-Output 'Adding new content now'
|
2025-08-12 11:31:41 +10:00
|
|
|
$newContent = @()
|
|
|
|
|
$idx = 0
|
|
|
|
|
for( $line = 0 ; $line -lt $lines.Length; $line++ ) {
|
|
|
|
|
$newContent += $lines[$line]
|
|
|
|
|
if( $line -eq $inserts[$idx].Index ) {
|
|
|
|
|
$newContent += @( $inserts[$idx].Value )
|
|
|
|
|
$idx++
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$newContent | Set-Content $js
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|