|
1 | 1 | #!/usr/bin/env pwsh
|
2 | 2 |
|
3 |
| -Write-Host "`nStarting project setup..." -ForegroundColor Cyan |
4 |
| - |
5 |
| -function Run-Command($cmd, $errorMsg) { |
6 |
| - try { |
7 |
| - Write-Host "Running: $cmd" -ForegroundColor Yellow |
8 |
| - Invoke-Expression $cmd |
9 |
| - } catch { |
10 |
| - Write-Host "$errorMsg" -ForegroundColor Red |
11 |
| - exit 1 |
| 3 | +Write-Host "`nStarting init..." -ForegroundColor Cyan |
| 4 | + |
| 5 | +# Cross-platform command execution with error checking |
| 6 | +function Run-Command($command, $arguments, $errorMsg) { |
| 7 | + Write-Host "Running: $command $($arguments -join ' ')" -ForegroundColor Yellow |
| 8 | + |
| 9 | + # Check if command exists first |
| 10 | + if (-not (Get-Command $command -ErrorAction SilentlyContinue)) { |
| 11 | + throw "$command is not available on this system. Please ensure you have the command installed globally and it's available in your PATH." |
12 | 12 | }
|
13 |
| -} |
14 | 13 |
|
15 |
| -# 1. Install vsts-npm-auth globally |
16 |
| -Run-Command "npm install -g vsts-npm-auth" "Failed to install vsts-npm-auth." |
| 14 | + & $command @arguments |
17 | 15 |
|
18 |
| -# 2. Authenticate with vsts-npm-auth |
19 |
| -if (Test-Path ".npmrc") { |
20 |
| - try { |
21 |
| - Run-Command "vsts-npm-auth -config .npmrc" "Initial authentication failed." |
22 |
| - } catch { |
23 |
| - Write-Host "Trying with force (-f) flag..." -ForegroundColor DarkYellow |
24 |
| - Run-Command "vsts-npm-auth -config .npmrc -f" "Forced authentication failed." |
| 16 | + if ($LASTEXITCODE -ne 0) { |
| 17 | + throw "$errorMsg (Exit code: $LASTEXITCODE)" |
25 | 18 | }
|
26 |
| -} else { |
27 |
| - Write-Host ".npmrc file not found in the current directory." -ForegroundColor Red |
28 |
| - exit 1 |
29 | 19 | }
|
30 | 20 |
|
31 |
| -# 3. Install project dependencies |
32 |
| -Run-Command "npm install" "Failed to install project dependencies." |
33 | 21 |
|
34 |
| -# 4. Install gulp globally |
35 |
| -Run-Command "npm install -g gulp" "Failed to install Gulp globally." |
| 22 | +Push-Location $PSScriptRoot |
| 23 | + |
| 24 | +try { |
| 25 | + Write-Host "`n[1/5] Installing vsts-npm-auth globally..." -ForegroundColor Cyan |
| 26 | + Run-Command "npm" @("install", "-g", "vsts-npm-auth") "Failed to install vsts-npm-auth." |
| 27 | + |
| 28 | + Write-Host "`n[2/5] Authenticating with Azure DevOps..." -ForegroundColor Cyan |
| 29 | + if (Test-Path ".npmrc") { |
| 30 | + try { |
| 31 | + Run-Command "vsts-npm-auth" @("-config", ".npmrc") "Initial authentication failed." |
| 32 | + } |
| 33 | + catch { |
| 34 | + Write-Host "Initial authentication failed. Trying with force (-f) flag..." -ForegroundColor DarkYellow |
| 35 | + Run-Command "vsts-npm-auth" @("-config", ".npmrc", "-f") "Forced authentication failed." |
| 36 | + } |
| 37 | + } else { |
| 38 | + Write-Host ".npmrc file not found in the current directory." -ForegroundColor Red |
| 39 | + throw ".npmrc file not found in the current directory." |
| 40 | + } |
| 41 | + |
| 42 | + Write-Host "`n[3/5] Installing project dependencies..." -ForegroundColor Cyan |
| 43 | + Run-Command "npm" @("install") "Failed to install project dependencies." |
| 44 | + |
| 45 | + Write-Host "`n[4/5] Installing Gulp globally..." -ForegroundColor Cyan |
| 46 | + Run-Command "npm" @("install", "-g", "gulp") "Failed to install Gulp globally." |
| 47 | + |
| 48 | + Write-Host "`n[5/5] Running gulp installDependencies..." -ForegroundColor Cyan |
| 49 | + Run-Command "gulp" @("installDependencies") "Failed to run 'gulp installDependencies'." |
| 50 | + |
| 51 | + Write-Host "`n✅ Setup complete." -ForegroundColor Green |
| 52 | +} |
| 53 | +catch { |
| 54 | + Write-Host "`n❌ Setup failed: $($_.Exception.Message)" -ForegroundColor Red |
| 55 | + exit 1 |
| 56 | +} |
| 57 | +finally { |
| 58 | + Pop-Location |
| 59 | +} |
36 | 60 |
|
37 |
| -# 5. Run gulp task |
38 |
| -Run-Command "gulp installDependencies" "Failed to run 'gulp installDependencies'." |
39 | 61 |
|
40 |
| -Write-Host "`n✅ Setup complete." -ForegroundColor Green |
|
0 commit comments