diff --git a/eng/common/scripts/TypeSpec-Project-Generate.ps1 b/eng/common/scripts/TypeSpec-Project-Generate.ps1 index 418221982a3..eacdf1f2e4f 100644 --- a/eng/common/scripts/TypeSpec-Project-Generate.ps1 +++ b/eng/common/scripts/TypeSpec-Project-Generate.ps1 @@ -14,6 +14,87 @@ $ErrorActionPreference = "Stop" . $PSScriptRoot/common.ps1 Install-ModuleIfNotInstalled "powershell-yaml" "0.4.1" | Import-Module +function GetNpmPackageVersion([string]$packagePath) { + $packagePath = Resolve-Path $packagePath + $packageFolder = Split-Path -Parent $packagePath + $packageName = Split-Path -Leaf $packagePath + Push-Location $packageFolder + try { + $packageVersion = node -p -e "require('./$packageName').version" + return $packageVersion + } + finally { + Pop-Location + } +} + +function GetEmitterDependencyVersion([string]$packagePath) { + $matchString = "`"$emitterName`": `"(.*?)`"" + If ((Get-Content -Raw $packagePath) -match $matchString) { + return $Matches[1] + } +} + +function NpmInstallAtRoot() { + $root = Resolve-Path "$PSScriptRoot/../../.." + Push-Location $root + try { + #default to root/eng/emitter-package.json but you can override by writing + #Get-${Language}-EmitterPackageJsonPath in your Language-Settings.ps1 + $replacementPackageJson = "$PSScriptRoot/../../emitter-package.json" + if (Test-Path "Function:$GetEmitterPackageJsonPathFn") { + $replacementPackageJson = &$GetEmitterPackageJsonPathFn + } + + $replacementVersion = GetEmitterDependencyVersion $replacementPackageJson + Write-Host "Replacement emitter version: $replacementVersion" + + $installedPath = Join-Path $root "node_modules" $emitterName "package.json" + Write-Host "Checking installed emitter at $installedPath" + if (Test-Path $installedPath) { + $installedVersion = GetNpmPackageVersion $installedPath + Write-Host "Installed emitter version: $installedVersion" + + if ($installedVersion -eq $replacementVersion) { + Write-Host "Emitter already installed. Skip installing." + return + } + } + + Write-Host "Installing package at"(Get-Location) + + if (Test-Path "node_modules") { + Write-Host "Remove existing node_modules at"(Get-Location) + Remove-Item -Path "node_modules" -Force -Recurse + } + + $useAlphaNpmRegistry = (Get-Content $replacementPackageJson -Raw).Contains("-alpha.") + if($useAlphaNpmRegistry) { + Write-Host "Package.json contains '-alpha.' in the version, Creating .npmrc using public/azure-sdk-for-js-test-autorest feed." + "registry=https://pkgs.dev.azure.com/azure-sdk/public/_packaging/azure-sdk-for-js-test-autorest/npm/registry/ `n`nalways-auth=true" | Out-File '.npmrc' + } + + $hasPackageFile = Test-Path "package.json" + + $npmInstallCommand = "npm install --prefix $root $emitterName@$replacementVersion --no-package-lock --omit=dev" + Write-Host($npmInstallCommand) + Invoke-Expression $npmInstallCommand + if ($LASTEXITCODE) { exit $LASTEXITCODE } + + if ($hasPackageFile) { + git restore package.json + } + else { + rm package.json + } + + if ($LASTEXITCODE) { exit $LASTEXITCODE } + } + finally { + Pop-Location + } +} + function NpmInstallForProject([string]$workingDirectory) { Push-Location $workingDirectory try { @@ -36,25 +117,7 @@ function NpmInstallForProject([string]$workingDirectory) { Remove-Item -Path "package-lock.json" -Force } - #default to root/eng/emitter-package.json but you can override by writing - #Get-${Language}-EmitterPackageJsonPath in your Language-Settings.ps1 - $replacementPackageJson = Join-Path $PSScriptRoot "../../emitter-package.json" - if (Test-Path "Function:$GetEmitterPackageJsonPathFn") { - $replacementPackageJson = &$GetEmitterPackageJsonPathFn - } - - Write-Host("Copying package.json from $replacementPackageJson") - Copy-Item -Path $replacementPackageJson -Destination "package.json" -Force - - $useAlphaNpmRegistry = (Get-Content $replacementPackageJson -Raw).Contains("-alpha.") - - if($useAlphaNpmRegistry) { - Write-Host "Package.json contains '-alpha.' in the version, Creating .npmrc using public/azure-sdk-for-js-test-autorest feed." - "registry=https://pkgs.dev.azure.com/azure-sdk/public/_packaging/azure-sdk-for-js-test-autorest/npm/registry/ `n`nalways-auth=true" | Out-File '.npmrc' - } - - npm install --no-lock-file - if ($LASTEXITCODE) { exit $LASTEXITCODE } + NpmInstallAtRoot } finally { Pop-Location