Skip to content

Instantly share code, notes, and snippets.

@andria-dev
Created November 5, 2020 20:22
Show Gist options
  • Save andria-dev/c5fbdb7dc616d468245c7ed035c4527b to your computer and use it in GitHub Desktop.
Save andria-dev/c5fbdb7dc616d468245c7ed035c4527b to your computer and use it in GitHub Desktop.
A Powershell function to get all of the node dependencies defined in a JSON file that match a pattern.
# Usage: yarn remove $(Get-NodePackages -Pattern 'gatsby')
function Get-NodePackages {
param (
$Pattern = '.*',
$PackagePath = '.\package.json'
)
$package = ConvertFrom-Json $(gc $PackagePath | Out-String)
$dependencies = $package.dependencies ?? @()
$devDependencies = $package.devDependencies ?? @()
$dependencyMembers = $(Get-Member -InputObject $dependencies) + $(Get-Member -InputObject $devDependencies)
return $(
$dependencyMembers | Where-Object {
$_.MemberType -eq 'NoteProperty' -and $_.Name -match $Pattern
}
).Name
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment