Skip to content

Commit d9a8d92

Browse files
authored
Add PowerShell script for private npm package installation
This script installs a specified package from a custom npm registry using Azure DevOps PAT for authentication.
1 parent 2c9444c commit d9a8d92

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

deepstudio/private-install.ps1

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
$ErrorActionPreference = "Stop"
2+
3+
$PackageName = if ($env:DEEPSTUDIO_PKG) { $env:DEEPSTUDIO_PKG } else { "deepstudio-server" }
4+
5+
function Require-Npm {
6+
if (-not (Get-Command npm -ErrorAction SilentlyContinue)) {
7+
Write-Host ""
8+
Write-Host "❌ Node.js / npm not found."
9+
Write-Host "Please install Node.js first:"
10+
Write-Host "https://nodejs.org/en/download"
11+
Write-Host ""
12+
exit 1
13+
}
14+
}
15+
16+
function Read-Secure([string]$msg) {
17+
$secure = Read-Host $msg -AsSecureString
18+
$bstr = [Runtime.InteropServices.Marshal]::SecureStringToBSTR($secure)
19+
try { [Runtime.InteropServices.Marshal]::PtrToStringBSTR($bstr) }
20+
finally { [Runtime.InteropServices.Marshal]::ZeroFreeBSTR($bstr) }
21+
}
22+
23+
function SetCfg($k, $v) { npm config set --global $k $v | Out-Null }
24+
function DelCfg($k) { try { npm config delete --global $k | Out-Null } catch {} }
25+
26+
Require-Npm
27+
28+
$registry = Read-Host "Enter npm registry URL (Azure Artifacts)"
29+
if ([string]::IsNullOrWhiteSpace($registry)) {
30+
throw "Registry URL is required."
31+
}
32+
33+
# normalize registry
34+
$registry = $registry.TrimEnd("/")
35+
36+
# derive auth prefix
37+
$uri = [Uri]$registry
38+
$authPrefix = "//" + $uri.Host + $uri.AbsolutePath + "/"
39+
40+
$pat = Read-Secure "Enter Azure DevOps PAT (Packaging:Read)"
41+
if ([string]::IsNullOrWhiteSpace($pat)) {
42+
throw "PAT is empty."
43+
}
44+
45+
$patB64 = [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes($pat))
46+
$pat = $null
47+
48+
try {
49+
Write-Host "Installing $PackageName@latest"
50+
51+
SetCfg "registry" "$registry/"
52+
SetCfg "${authPrefix}:username" "microsoft"
53+
SetCfg "${authPrefix}:_password" $patB64
54+
SetCfg "${authPrefix}:email" "npm@example.com"
55+
56+
npm install -g "$PackageName@latest" --registry "$registry/"
57+
}
58+
finally {
59+
DelCfg "registry"
60+
DelCfg "${authPrefix}:username"
61+
DelCfg "${authPrefix}:_password"
62+
DelCfg "${authPrefix}:email"
63+
}

0 commit comments

Comments
 (0)