File size: 2,940 Bytes
dcedb25 | 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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | [CmdletBinding()]
param(
[string]$ProjectRoot = (Split-Path -Parent $PSScriptRoot)
)
$ErrorActionPreference = 'Stop'
function Get-CommandVersion {
param(
[string]$Command,
[string]$Argument
)
$version = (& $Command $Argument).Trim().TrimStart('v')
if ($LASTEXITCODE -ne 0 -or [string]::IsNullOrWhiteSpace($version)) {
throw "Unable to determine $Command version."
}
return [Version]$version
}
function Get-LockHash {
param([string]$Path)
return (Get-FileHash -LiteralPath $Path -Algorithm SHA256).Hash.ToLowerInvariant()
}
$ProjectRoot = (Resolve-Path -LiteralPath $ProjectRoot).Path
$packageJson = Join-Path $ProjectRoot 'package.json'
$lockfile = Join-Path $ProjectRoot 'package-lock.json'
$nodeModules = Join-Path $ProjectRoot 'node_modules'
$lockStamp = Join-Path $nodeModules '.freellmapi-bootstrap-lock'
$envFile = Join-Path $ProjectRoot '.env'
if (-not (Test-Path -LiteralPath $packageJson) -or -not (Test-Path -LiteralPath $lockfile)) {
throw "Project root must contain package.json and package-lock.json: $ProjectRoot"
}
$nodeVersion = Get-CommandVersion -Command 'node' -Argument '--version'
if ($nodeVersion -lt [Version]'20.18.0' -or $nodeVersion -ge [Version]'25.0.0') {
throw "Node.js >=20.18.0 and <25.0.0 is required; found $nodeVersion."
}
$npmVersion = Get-CommandVersion -Command 'npm' -Argument '--version'
if ($npmVersion -lt [Version]'10.0.0') {
throw "npm >=10.0.0 is required; found $npmVersion."
}
$lockHash = Get-LockHash -Path $lockfile
$installedLockHash = if (Test-Path -LiteralPath $lockStamp) {
(Get-Content -LiteralPath $lockStamp -Raw).Trim().ToLowerInvariant()
} else {
''
}
Push-Location $ProjectRoot
try {
if (-not (Test-Path -LiteralPath $nodeModules) -or $installedLockHash -ne $lockHash) {
Write-Host 'Installing dependencies...'
& npm install
if ($LASTEXITCODE -ne 0) {
throw "npm install failed with exit code $LASTEXITCODE."
}
$lockHash = Get-LockHash -Path $lockfile
Set-Content -LiteralPath $lockStamp -Value $lockHash -NoNewline -Encoding ascii
} else {
Write-Host 'Dependencies already match package-lock.json.'
}
if (-not (Test-Path -LiteralPath $envFile)) {
$encryptionKey = (& node -e "process.stdout.write(require('node:crypto').randomBytes(32).toString('hex'))").Trim()
if ($LASTEXITCODE -ne 0 -or $encryptionKey -notmatch '^[0-9a-f]{64}$') {
throw 'Unable to generate ENCRYPTION_KEY.'
}
@(
"ENCRYPTION_KEY=$encryptionKey"
'PORT=3001'
) | Set-Content -LiteralPath $envFile -Encoding ascii
Write-Host 'Created .env with a new ENCRYPTION_KEY.'
} else {
Write-Host '.env already exists; leaving it unchanged.'
}
} finally {
Pop-Location
}
Write-Host 'Bootstrap complete. Start development with: npm run dev'
|