Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
209 lines (178 sloc) 6.53 KB
#!powershell
# init
$wnaveRoot = Join-Path $Env:UserProfile '.wnave'
if (!(Test-Path $wnaveRoot)) {
New-Item -ItemType directory -Path $wnaveRoot
}
################################################################################
## node stuff
################################################################################
function getNodeVersionList() {
$distList = (New-Object System.Net.WebClient).DownloadString('http://nodejs.org/dist/')
$versionPattern = New-Object System.Text.RegularExpressions.Regex('"v(?<version>\d+\.\d+\.\d+)/\"')
$nodeVersions = $versionPattern.Matches($distList) | %{New-Object System.Version($_.Groups[1].Value) }
return $nodeVersions | Sort-Object -Descending
}
function getNodeVersion($version) {
# determine version to load
if ($version -eq 'latest') {
$version = getNodeVersionList | select -index 0
}
elseif ($version -eq 'stable') {
$version = getNodeVersionList | where { ($_.Minor % 2) -eq 0 } | select -index 0
}
else {
# todo: ex: 0.8 should install hightest 0.8 version
try {
$version = New-Object System.Version($version)
} catch {
Write-Host $version is not a valid version string
exit
}
}
return $version
}
function installNode($version) {
$version = getNodeVersion($version)
$urls = @(
'http://nodejs.org/dist/v' + $version + '/node.exe'
)
if ($env:PROCESSOR_ARCHITECTURE -eq 'AMD64') {
$url = 'http://nodejs.org/dist/v' + $version + '/x64/node.exe'
$urls = ,$url + $urls
}
# create directory
$destDir = Join-Path $wnaveRoot $version;
if (!(Test-Path $destDir)) {
Write-Host directory $destDir created
$noDisplay = New-Item -ItemType directory -Path $destDir
} else {
Write-Host directory $destDir already exists
}
#download node
$node = Join-Path $destDir 'node.exe'
if (Test-Path $node -PathType Leaf) {
Write-Host already installed
return
}
$success = $false
foreach ($url in $urls) {
try {
Write-Host trying to download $url to $node
(New-Object System.Net.WebClient).DownLoadFile($url, $node);
$success = $true
break;
} catch {
Write-Host download of $url failed
}
}
# download failed
if (!($success)) {
Write-Host could not install node $version
Remove-Item $destDir -Recurse
exit
}
Write-Host successfully installed node from $url
# write PowerShell script
$init = Join-Path $destDir 'init.ps1'
'# modify path' > $init
'$env:Path = "' + $destDir + ';" + $env:Path' >> $init
'# set environment variables' >> $init
'$env:NAVEVERSION = "' + $version + '"' >> $init
'Write-Host using ' + $version >> $init
}
function uninstallNode($version) {
$target = Join-Path $wnaveRoot $version
Remove-Item $target -Recurse
}
function useNode($version) {
$version = getNodeVersion($version);
$nodeDir = Join-Path $wnaveRoot $version
if (!(Test-Path $nodeDir -PathType Container)) {
installNode($version)
}
$init = Join-Path $nodeDir 'init.ps1'
powershell -noexit $init
}
function listInstalled {
# TODO: better listing?
Get-ChildItem $wnaveRoot
}
function listRemote {
# TODO: better listing?
getNodeVersionList
}
################################################################################
## npm stuff
################################################################################
function getNpmVersionList() {
$npmList = (New-Object System.Net.WebClient).DownloadString('http://nodejs.org/dist/npm/')
$npmVPattern = New-Object System.Text.RegularExpressions.Regex('"npm-(?<version>\d+\.\d+\.\d+)\.zip"')
$npmVersions = $npmVPattern.Matches($npmList) | %{New-Object System.Version($_.Groups[1].Value) }
return $npmVersions | Sort-Object -Descending
}
function getNpmVersion($version) {
# determine version to load
if ($version -eq 'latest') {
$version = getNpmVersionList | select -index 0
}
else {
try {
$version = New-Object System.Version($version)
} catch {
Write-Host $version is not a valid version string
exit
}
}
return $version
}
function installNpm($version) {
$version = getNpmVersion($version)
$node = (Get-Command node).Definition
$node_dir = (get-item $node).DirectoryName
# download npm
$filename = 'npm-' + $version + '.zip'
$downloaded = [System.Environment]::ExpandEnvironmentVariables('%temp%\' + $filename)
$url = 'http://nodejs.org/dist/npm/' + $filename;
(New-Object System.Net.WebClient).DownLoadFile($url, $downloaded);
# extract files
$zip = new-object -com shell.application
$source = $zip.namespace($downloaded);
$target = $zip.namespace($node_dir);
$target.CopyHere($source.items())
Write-Host installed npm $version
}
################################################################################
## CLI
################################################################################
switch ($args[0]) {
<#
'addToPath' {
$workingDir = (Split-Path -Path $MyInvocation.MyCommand.Definition -Parent);
$env:Path = "$env:Path;$workingDir"
Write-Host added wnave to path
}
#>
'install' { installNode($args[1]) }
'uninstall' { uninstallNode($args[1]) }
'use' { useNode($args[1]) }
'ls' { listInstalled }
'ls-remote' { listRemote }
'npm-install' { installNpm($args[1]) }
'npm-ls' { getNpmVersionList }
default {
Write-Host '== commands =='
Write-Host ' install <version> Install the version passed (ex: 0.6.0)'
Write-Host ' uninstall <version> Delete the install for <version>'
Write-Host ' use <version> nter a subshell where <version> is being used'
Write-Host ' ls list local installed versions'
Write-Host ' ls-remote remote available versions'
Write-Host
Write-Host ' npm-install <version> Install npm the version passed (ex 1.1.60)'
Write-Host ' npm-ls list remote available npm versions'
Write-Host
Write-Host '<version> can be the string "latest" to get the latest distribution.'
Write-Host '<version> can be the string "stable" to get the latest stable version.'
Write-Host ' stable is not available for install-npm'
}
}