Initial commit.
This commit is contained in:
59
Modules/posh-nodejs/posh-nodejs.psm1
Normal file
59
Modules/posh-nodejs/posh-nodejs.psm1
Normal file
@@ -0,0 +1,59 @@
|
||||
if (Get-Module posh-nodejs) { return }
|
||||
|
||||
$NodeJsHome = "$HOME\Apps\nodejs"
|
||||
|
||||
$InitialNodePath = $env:Path.Split(";") | Where-Object { $_ -like "$NodeJsHome*" }
|
||||
|
||||
function CompleteNodeJsVersion {
|
||||
param($commandName, $parameterName, $wordToComplete,
|
||||
$commandAst, $fakeBoundParameters)
|
||||
|
||||
Get-ChildItem -Directory $NodeJsHome
|
||||
| ForEach-Object { $_.Name.Split("-")[1].Substring(1) }
|
||||
| Where-Object { $_ -Like "$wordToComplete*" }
|
||||
| Sort-Object
|
||||
}
|
||||
|
||||
|
||||
function nvm {
|
||||
[CmdletBinding()]
|
||||
param(
|
||||
[Parameter(Mandatory = $true)]
|
||||
[ValidateSet("list", "activate", "deactivate")]
|
||||
$command,
|
||||
|
||||
[Parameter(Mandatory = $false)]
|
||||
[ArgumentCompleter({ CompleteNodeJsVersion @args })]
|
||||
$version)
|
||||
switch ($command) {
|
||||
"list" {
|
||||
Get-ChildItem -Directory $NodeJsHome
|
||||
| ForEach-Object {
|
||||
$version = $_.Name.Split("-")[1].Substring(1)
|
||||
Write-Output "$version"
|
||||
}
|
||||
}
|
||||
|
||||
"activate" {
|
||||
$path = "$NodeJsHome\node-v$version-win-x64"
|
||||
if (Test-Path "$path") {
|
||||
$paths = $env:Path.Split(";") | Where-Object { $_ -NotLike "$NodeJsHome*" }
|
||||
$paths += $path
|
||||
$env:Path = $paths | Join-String -Separator ";"
|
||||
}
|
||||
else {
|
||||
Write-Error "NodeJS version '${version}' not found."
|
||||
}
|
||||
}
|
||||
|
||||
"deactivate" {
|
||||
$paths = $env:Path.Split(";") | Where-Object { $_ -NotLike "$NodeJsHome*" }
|
||||
$paths += $InitialNodePath
|
||||
$env:Path = $paths | Join-String -Separator ";"
|
||||
}
|
||||
|
||||
default {
|
||||
Write-Error "Unknown nvm command ${args[0]}."
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user