Private/Get-GraphModuleShard.ps1

function Get-GraphModuleNameIndex {
    <#
    .SYNOPSIS
        Loads and caches the small list of known module/moduleName pairs and their shard file.
    .DESCRIPTION
        data/graph-command-by-module-names.json lets Get-GraphMapping -Module resolve a
        wildcard pattern (e.g. "Users") to the shard file(s) that must be loaded, without
        scanning the by-module folder's file names or loading the full catalog.
    #>

    [CmdletBinding()]
    [OutputType([pscustomobject])]
    param(
        [switch]$Force
    )

    if (-not $Force -and $script:GraphModuleNameCache) {
        return $script:GraphModuleNameCache
    }

    $paths = Get-GraphCatalogPath
    $modules = @()
    if ($paths.ModuleNamesPath) {
        Write-Verbose "Loading GraphShell module name index from $($paths.ModuleNamesPath)"
        $raw = Get-Content -LiteralPath $paths.ModuleNamesPath -Raw
        $parsed = $raw | ConvertFrom-Json
        $modules = @($parsed.modules)
    }
    else {
        Write-Verbose 'GraphShell module name index (graph-command-by-module-names.json) not found.'
    }

    $script:GraphModuleNameCache = [pscustomobject]@{ Modules = $modules }
    return $script:GraphModuleNameCache
}

function Get-GraphModuleShard {
    <#
    .SYNOPSIS
        Loads and caches a single by-module lite shard.
    #>

    [CmdletBinding()]
    [OutputType([pscustomobject])]
    param(
        [Parameter(Mandatory)]
        [string]$FileName,

        [switch]$Force
    )

    if (-not $script:GraphModuleShardCache) {
        $script:GraphModuleShardCache = [System.Collections.Generic.Dictionary[string, object]]::new([System.StringComparer]::OrdinalIgnoreCase)
    }

    if (-not $Force -and $script:GraphModuleShardCache.ContainsKey($FileName)) {
        return $script:GraphModuleShardCache[$FileName]
    }

    $paths = Get-GraphCatalogPath
    if (-not $paths.ByModulePath) {
        Write-Verbose 'GraphShell could not locate the graph-command-by-module folder.'
        return $null
    }
    $shardPath = Join-Path $paths.ByModulePath $FileName
    if (-not (Test-Path -LiteralPath $shardPath)) {
        Write-Verbose "GraphShell module shard not found: $shardPath"
        return $null
    }

    Write-Verbose "Loading GraphShell module shard '$FileName' from $shardPath"
    $raw = Get-Content -LiteralPath $shardPath -Raw
    $parsed = $raw | ConvertFrom-Json

    $script:GraphModuleShardCache[$FileName] = $parsed
    return $parsed
}