Last active
December 19, 2015 14:58
-
-
Save DanSearle/b7ce83fceaf8a1d63774 to your computer and use it in GitHub Desktop.
Powershell script to create seperate a node and npm environment for a project without the need to have it installed on the machine. This aids development with node and npm based tools for web development on windows. This script will launch a powershell enviroment with node and npm setup for the project.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function download_node { | |
param([string]$node_directory, [string]$node_version) | |
$node_exe = (Join-Path $node_directory "node.exe") | |
if (Test-Path $node_exe) { | |
Write-Host "Doing nothing as $node_exe exists" | |
return | |
} | |
if (!$node_version) { | |
$versions = (Invoke-WebRequest https://nodejs.org/download/release/index.json | ConvertFrom-Json) | |
$node_version=($versions | Where-Object { $_.lts -ne $false } | Sort-Object version | Select-Object -Last 1).version | |
} | |
$dir = "win-x86" | |
if ($ENV:PROCESSOR_ARCHITECTURE -contains '64') | |
{ | |
$dir = "win-x64" | |
} | |
$download_url="https://nodejs.org/download/release/$node_version/$dir/node.exe" | |
Write-Host "Ensuring $node_directory exists" | |
New-Item -ItemType Directory -Force -Path "$node_directory" >$null | |
Write-Host "Downloading $download_url to $node_directory" | |
Invoke-WebRequest $download_url -OutFile $node_exe | |
} | |
function download_npm { | |
param([string]$node_directory, [string]$npm_url) | |
if (Test-Path (Join-Path $node_directory "npm.cmd")) { | |
Write-Host "Doing nothing as npm.cmd exists within $node_directory" | |
return | |
} | |
if (!$npm_url) { | |
$npm_url=(Invoke-WebRequest https://api.github.com/repos/npm/npm/releases/latest | ConvertFrom-Json).zipball_url | |
} | |
$node_modules_dir = (Join-Path $node_directory "node_modules") | |
$new_dir = (Join-Path $node_modules_dir "npm") | |
if (Test-Path $new_dir) { | |
Remove-Item -Recurse -Force -Path $new_dir >$null | |
} | |
$npm_zip_file = (Join-Path $node_directory "npm.zip") | |
Write-Host "Downloading $npm_url to $node_directory" | |
Invoke-WebRequest $npm_url -OutFile $npm_zip_file | |
[Reflection.Assembly]::LoadWithPartialName('System.IO.Compression.FileSystem') | |
$zip_file = [IO.Compression.ZipFile]::OpenRead($npm_zip_file) | |
$unzip_directory = ($zip_file.Entries | Select -First 1).FullName | |
[System.IO.Compression.ZipFile]::ExtractToDirectory($npm_zip_file, $node_directory); | |
$zip_file.Dispose(); | |
New-Item -ItemType Directory -Force -Path $node_modules_dir >$null | |
$original_dir = (Join-Path $node_directory $unzip_directory) | |
$new_dir = (Join-Path $node_modules_dir "npm") | |
Write-Host "Moving npm from $original_dir to $new_dir" | |
Move-Item $original_dir $new_dir | |
Write-Host "Copying npm.cmd to $node_directory" | |
Copy-Item (Join-Path (Join-Path $new_dir "bin") "npm.cmd") $node_directory | |
Write-Host "Removing npm zip file" | |
Remove-Item $npm_zip_file | |
} | |
if (!$node_directory) { | |
$node_directory = (Join-Path $PSScriptRoot "bin") | |
} | |
download_node $node_directory | |
download_npm $node_directory | |
$original_path = ($env:PATH.Split(";") | Where-Object { $_ -notmatch 'node' -and $_ -ne ''}) | |
Write-Host "Munging Path to ensure npm and node are in the correct place" | |
$env:PATH = (@($node_directory) + $original_path) -join ";" | |
Write-Host "Startup Complete! The directory $node_directory can be checked in to source control to ensure consistent versions" | |
Write-Host "Enjoy the node and npm commands" | |
$host.enternestedprompt() | |
Write-Host "Exiting environment" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment