diff --git a/resources/NpmDsc/NpmDsc.psm1 b/resources/NpmDsc/NpmDsc.psm1 index 64f7b4bc..1225dd0f 100644 --- a/resources/NpmDsc/NpmDsc.psm1 +++ b/resources/NpmDsc/NpmDsc.psm1 @@ -340,15 +340,31 @@ class NpmPackage { } [void] Set() { - $inDesiredState = $this.Test() + if ($this.Test()) { + return + } + + $installParams = @{ + PackageName = $this.Name + Arguments = $this.Arguments + Global = $this.Global + } + if ($this.Ensure -eq [Ensure]::Present) { - if (-not $inDesiredState) { - Install-NpmPackage -PackageName $this.Name -Arguments $this.Arguments -Global $this.Global - } + [NpmPackage]::EnsureValidPackageName($this.Name) + Install-NpmPackage @installParams } else { - if (-not $inDesiredState) { - Uninstall-NpmPackage -PackageName $this.Name -Arguments $this.Arguments -Global $this.Global - } + Uninstall-NpmPackage @installParams + } + } + + hidden static [void] EnsureValidPackageName($name) { + # TODO: Handling owner/repo package references pointing to GH repositories requires accounting + # for git errors (missing git, failed authentication, etc.). + # + # See: https://docs.npmjs.com/cli/v11/commands/npm-install#description + if (($name -notmatch '^git(?:\+(?:ssh|https?|file))?://') -and $name.Contains('/') -and -not $name.StartsWith('@')) { + throw "The Set operation currently only supports packages specified as [<@scope>/]. The given package looks like a GitHub repository: $($name)." } }