diff --git a/examples/New-NetworkDiagram.ps1 b/examples/New-NetworkDiagram.ps1 index e20d7d2..a527ee5 100644 --- a/examples/New-NetworkDiagram.ps1 +++ b/examples/New-NetworkDiagram.ps1 @@ -31,6 +31,26 @@ param( [string]$OutputPath = '.\my-network.drawio' ) +function Get-WizardInventoryPath { + param([Parameter(Mandatory)][string]$OutputPath) + + $outputDirectory = [System.IO.Path]::GetDirectoryName($OutputPath) + $outputBaseName = [System.IO.Path]::GetFileNameWithoutExtension($OutputPath) + + if ([string]::IsNullOrEmpty($outputBaseName)) { + $outputBaseName = [System.IO.Path]::GetFileName($OutputPath) + } + + $inventoryFileName = "$outputBaseName-inventory.json" + if ([string]::IsNullOrEmpty($outputDirectory)) { + return $inventoryFileName + } + + return (Join-Path $outputDirectory $inventoryFileName) +} + +$inventoryPath = Get-WizardInventoryPath -OutputPath $OutputPath + # ── IPv4 CIDR helpers ──────────────────────────────────────────────────────── # Real prefix-aware host enumeration (replaces the old /24-only "first 3 octets" # assumption). Works for any prefix; the caller caps how many hosts get scanned. @@ -491,7 +511,6 @@ if (Test-Path $OutputPath) { } # Save inventory for future use -$inventoryPath = $OutputPath -replace '\.drawio$', '-inventory.json' $inventoryData = @{ knownDevices = @($nodes | ForEach-Object { @{ diff --git a/tests/NetDiagram.Tests.ps1 b/tests/NetDiagram.Tests.ps1 index e321402..49e9ae3 100644 --- a/tests/NetDiagram.Tests.ps1 +++ b/tests/NetDiagram.Tests.ps1 @@ -636,4 +636,20 @@ Describe 'Quick-start wizard CIDR math (#4 regression)' { $targets = @(Get-SubnetScanTarget -NetworkValue $net -BroadcastValue $bc -ScanDepth 'Full') $targets.Count | Should -Be 0 } + + It 'Builds a sibling inventory path for a .xml output path' { + $outputPath = Join-Path $script:TestDataPath 'office.xml' + $inventoryPath = Get-WizardInventoryPath -OutputPath $outputPath + + $inventoryPath | Should -Be (Join-Path $script:TestDataPath 'office-inventory.json') + $inventoryPath | Should -Not -Be $outputPath + } + + It 'Builds a sibling inventory path for an extensionless output path' { + $outputPath = Join-Path $script:TestDataPath 'office' + $inventoryPath = Get-WizardInventoryPath -OutputPath $outputPath + + $inventoryPath | Should -Be (Join-Path $script:TestDataPath 'office-inventory.json') + $inventoryPath | Should -Not -Be $outputPath + } }