-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup_js.ps1
More file actions
77 lines (64 loc) · 2.44 KB
/
setup_js.ps1
File metadata and controls
77 lines (64 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<#
.SYNOPSIS
Setup script for installing/updating JavaScript (Node) dependencies.
.DESCRIPTION
Ensures Node.js & npm exist, installs dependencies from package.json.
Skips install if node_modules present unless -Force used.
Outputs jest version after successful install for verification.
.PARAMETER Force
Force reinstall (always run `npm install`).
.PARAMETER CI
Reduce output noise for CI environments (adds --no-audit --no-fund, minimal logs).
.PARAMETER NoJestCheck
Skip printing jest version after install.
.EXAMPLES
./setup_js.ps1
./setup_js.ps1 -Force
./setup_js.ps1 -CI
./setup_js.ps1 -Force -CI
#>
param(
[switch]$Force,
[switch]$CI,
[switch]$NoJestCheck
)
$ErrorActionPreference = 'Stop'
if (-not (Test-Path 'package.json')) {
Write-Error 'package.json not found in current directory; run from project root.'
exit 1
}
if (-not (Get-Command node -ErrorAction SilentlyContinue)) { Write-Error 'Node.js is required but was not found. Install from https://nodejs.org/' ; exit 1 }
if (-not (Get-Command npm -ErrorAction SilentlyContinue)) { Write-Error 'npm is required but was not found (it ships with Node.js).' ; exit 1 }
$nodeVersion = (node --version) 2>$null
$npmVersion = (npm --version) 2>$null
Write-Host ("Node.js {0} / npm {1}" -f $nodeVersion,$npmVersion)
$skip = $false
if (-not $Force -and (Test-Path 'node_modules')) {
Write-Host 'node_modules already present; skipping install (use -Force to override).'
$skip = $true
}
if (-not $skip) {
Write-Host 'Installing JavaScript dependencies with npm...'
$npmArgs = @('install')
if ($CI) { $npmArgs += @('--no-fund','--no-audit') }
npm @npmArgs
Write-Host 'npm install complete.'
} else {
Write-Host 'Skipped install.'
}
if (-not $NoJestCheck) {
try {
# Use npm exec to run locally installed jest (avoids npx downloading wrong version)
$jestVersion = (npm exec jest -- --version) 2>$null
if ($LASTEXITCODE -eq 0 -and $jestVersion) {
Write-Host ("jest version: {0}" -f $jestVersion)
} else {
Write-Host 'jest not found after install (unexpected)' -ForegroundColor Yellow
# Don't fail setup just because jest version check failed
}
} catch {
Write-Host 'jest invocation failed (non-critical)' -ForegroundColor Yellow
# Don't fail setup just because jest version check failed
}
}
Write-Host '=== JavaScript setup done ==='