Skip to content

Instantly share code, notes, and snippets.

@vbjay
Created December 31, 2024 23:37
Show Gist options
  • Save vbjay/0b5880b84089204e8abaf7f2c46675d0 to your computer and use it in GitHub Desktop.
Save vbjay/0b5880b84089204e8abaf7f2c46675d0 to your computer and use it in GitHub Desktop.
Yarn Detailed Peer Dependency Details
<#
.SYNOPSIS
Generates detailed yarn peer dependency information.
.DESCRIPTION
This script uses the `yarn explain peer-requirements` command to gather information about peer dependencies.
It processes the output to determine if the dependencies are satisfied (Good) or not (Bad), and generates
detailed commands for further inspection.
.PARAMETER IncludeGood
If specified, includes results marked as "Good" in the output.
.PARAMETER Run
If specified, executes the detailed yarn commands and includes their results in the output.
.EXAMPLE
.\Generate-YarnPeerDetails.ps1
Runs the script and outputs the peer dependency information, excluding "Good" results.
.EXAMPLE
.\Generate-YarnPeerDetails.ps1 -IncludeGood
Runs the script and outputs all peer dependency information, including "Good" results.
.EXAMPLE
.\Generate-YarnPeerDetails.ps1 -Run
Runs the script, executes the detailed yarn commands, and includes their results in the output.
.NOTES
The script sets the console output encoding to UTF-8 and uses debug and verbose messages for detailed logging.
#>
[CmdletBinding()]
param (
[switch]$IncludeGood,
[switch] $Run
)
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
$DebugPreference = "Continue"
# Initialize an array to store the results
$results = @()
# Use yarn command output directly
yarn explain peer-requirements | ForEach-Object {
# Match lines containing the ✓ or ✘ character
Write-Debug "Matching $_"
if ($_ -match "^(p[^ ]+)") {
$problemID = $matches[1]
$line = $_
# Determine if the result is good or bad
if ($_ -match "\u2713") {
# Unicode for ✓
$result = "Good"
}
elseif ($_ -match "\u2718") {
# Unicode for ✘
$result = "Bad"
}
else {
$result = "unknown"
}
# Generate the detailed yarn command
$command = "yarn explain peer-requirements $problemID"
# Create a PSObject with the result and command
$obj = [PSCustomObject]@{
ProblemID = $problemID
Line = $line
Result = $result
Command = $command
}
# Add the object to the results array
$results += $obj
}
}
# Sort the results by the Result property
$sortedResults = $results | Sort-Object -Property Result
# Don't Filter results if IncludeGood switch is specified
if (-not $IncludeGood) {
$sortedResults = $sortedResults | Where-Object { $_.Result -ne "Good" }
}
# Output the sorted results
if (-not $Run) {
$sortedResults
}
else {
$cmdResults = @()
$sortedResults | ForEach-Object {
Write-Verbose "$($_.Command) - Result: $($_.Result)"
$cmdResult = & yarn explain peer-requirements $_.ProblemID
$cmdResult | ForEach-Object {
$_ | ForEach-Object {
$cmdResults += $_
}
}
$finalResult = $cmdResults -join "`r`n"
$_ | Add-Member -Name 'CommandResult' -Type NoteProperty -Value $finalResult
Write-Verbose $_.Line
$_
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment