From e69aed5183303946ebc95db1430cf6483581def3 Mon Sep 17 00:00:00 2001 From: Patrick Hallisey Date: Wed, 14 Feb 2024 11:29:39 -0800 Subject: [PATCH] Move emitter-package.json scripts to eng/common (#7655) --- .../typespec/New-EmitterPackageJson.ps1 | 73 ++++++++++++++++++ .../typespec/New-EmitterPackageLock.ps1 | 57 ++++++++++++++ .../autorest/New-EmitterPackageJson.ps1 | 75 +------------------ .../autorest/New-EmitterPackageLock.ps1 | 59 +-------------- 4 files changed, 134 insertions(+), 130 deletions(-) create mode 100644 eng/common/scripts/typespec/New-EmitterPackageJson.ps1 create mode 100644 eng/common/scripts/typespec/New-EmitterPackageLock.ps1 diff --git a/eng/common/scripts/typespec/New-EmitterPackageJson.ps1 b/eng/common/scripts/typespec/New-EmitterPackageJson.ps1 new file mode 100644 index 00000000000..01e883d068c --- /dev/null +++ b/eng/common/scripts/typespec/New-EmitterPackageJson.ps1 @@ -0,0 +1,73 @@ +[CmdletBinding()] +param ( + [parameter(Mandatory = $true)] + [ValidateScript({ Test-Path $_ })] + [string]$PackageJsonPath, + + [parameter(Mandatory = $false)] + [ValidateScript({ Test-Path $_ })] + [string]$OverridesPath, + + [parameter(Mandatory = $true)] + [string]$OutputDirectory, + + [parameter(Mandatory = $false)] + [string]$PackageJsonFileName = "emitter-package.json" +) + +$packageJson = Get-Content $PackageJsonPath | ConvertFrom-Json -AsHashtable + +# If we provide OverridesPath, use that to load a hashtable of version overrides +$overrides = @{} + +if ($OverridesPath) { + Write-Host "Using overrides from $OverridesPath`:`n" + $overrides = Get-Content $OverridesPath | ConvertFrom-Json -AsHashtable + Write-Host ($overrides | ConvertTo-Json) + Write-Host "" +} + + +# If there's a peer dependency and a dev dependency for the same package, carry the +# dev dependency forward into emitter-package.json + +$devDependencies = @{} + +foreach ($package in $packageJson.peerDependencies.Keys) { + $pinnedVersion = $packageJson.devDependencies[$package] + if ($pinnedVersion -and -not $overrides[$package]) { + Write-Host "Pinning $package to $pinnedVersion" + $devDependencies[$package] = $pinnedVersion + } +} + +$emitterPackageJson = [ordered]@{ + "main" = "dist/src/index.js" + "dependencies" = @{ + $packageJson.name = $overrides[$packageJson.name] ?? $packageJson.version + } +} + +# you shouldn't specify the same package in both dependencies and overrides +$overrides.Remove($packageJson.name) + +# Avoid adding an empty devDependencies section +if($devDependencies.Keys.Count -gt 0) { + $emitterPackageJson["devDependencies"] = $devDependencies +} + +# Avoid adding an empty overrides section +if($overrides.Keys.Count -gt 0) { + $emitterPackageJson["overrides"] = $overrides +} + +New-Item $OutputDirectory -ItemType Directory -ErrorAction SilentlyContinue | Out-Null +$OutputDirectory = Resolve-Path $OutputDirectory + +$dest = Join-Path $OutputDirectory $PackageJsonFileName +$destJson = $emitterPackageJson | ConvertTo-Json -Depth 100 + +Write-Host "Generating $dest" +$destJson | Out-File $dest + +Write-Host $destJson diff --git a/eng/common/scripts/typespec/New-EmitterPackageLock.ps1 b/eng/common/scripts/typespec/New-EmitterPackageLock.ps1 new file mode 100644 index 00000000000..1872e104e4d --- /dev/null +++ b/eng/common/scripts/typespec/New-EmitterPackageLock.ps1 @@ -0,0 +1,57 @@ +[CmdletBinding()] +param ( + [parameter(Mandatory = $true)] + [string]$EmitterPackageJsonPath, + + [parameter(Mandatory = $true)] + [string]$OutputDirectory, + + [parameter(Mandatory = $false)] + [string]$NpmrcPath, + + [parameter(Mandatory = $false)] + [string]$LockFileName = "emitter-package-lock.json" +) + +New-Item $OutputDirectory -ItemType Directory -ErrorAction SilentlyContinue | Out-Null +$OutputDirectory = Resolve-Path $OutputDirectory + +$tempFile = New-TemporaryFile +Remove-Item $tempFile + +# use a consistent folder name to avoid random package name in package-lock.json +Write-Host "Creating temporary folder $tempFile/emitter-consumer" +$tempFolder = New-Item "$tempFile/emitter-consumer" -ItemType Directory + +if ($NpmrcPath) { + Write-Host "Copy npmrc from $NpmrcPath to $tempFolder/.npmrc" + Copy-Item $NpmrcPath "$tempFolder/.npmrc" +} + +Push-Location $tempFolder + +try { + Write-Host "Copy $EmitterPackageJsonPath to $tempFolder/package.json" + Copy-Item $EmitterPackageJsonPath "$tempFolder/package.json" + + Write-Host 'npm install --legacy-peer-deps' + npm install --legacy-peer-deps + + if ($LASTEXITCODE) { + Write-Error "npm install failed with exit code $LASTEXITCODE" + exit $LASTEXITCODE + } + + Write-Host '##[group]npm list --all' + npm list --all + Write-Host '##[endgroup]' + + $dest = Join-Path $OutputDirectory $LockFileName + Write-Host "Copy package-lock.json to $dest" + Copy-Item 'package-lock.json' $dest +} +finally { + Pop-Location +} + +Remove-Item $tempFolder -Recurse -Force diff --git a/eng/scripts/autorest/New-EmitterPackageJson.ps1 b/eng/scripts/autorest/New-EmitterPackageJson.ps1 index 01e883d068c..2c004bc10c3 100644 --- a/eng/scripts/autorest/New-EmitterPackageJson.ps1 +++ b/eng/scripts/autorest/New-EmitterPackageJson.ps1 @@ -1,73 +1,2 @@ -[CmdletBinding()] -param ( - [parameter(Mandatory = $true)] - [ValidateScript({ Test-Path $_ })] - [string]$PackageJsonPath, - - [parameter(Mandatory = $false)] - [ValidateScript({ Test-Path $_ })] - [string]$OverridesPath, - - [parameter(Mandatory = $true)] - [string]$OutputDirectory, - - [parameter(Mandatory = $false)] - [string]$PackageJsonFileName = "emitter-package.json" -) - -$packageJson = Get-Content $PackageJsonPath | ConvertFrom-Json -AsHashtable - -# If we provide OverridesPath, use that to load a hashtable of version overrides -$overrides = @{} - -if ($OverridesPath) { - Write-Host "Using overrides from $OverridesPath`:`n" - $overrides = Get-Content $OverridesPath | ConvertFrom-Json -AsHashtable - Write-Host ($overrides | ConvertTo-Json) - Write-Host "" -} - - -# If there's a peer dependency and a dev dependency for the same package, carry the -# dev dependency forward into emitter-package.json - -$devDependencies = @{} - -foreach ($package in $packageJson.peerDependencies.Keys) { - $pinnedVersion = $packageJson.devDependencies[$package] - if ($pinnedVersion -and -not $overrides[$package]) { - Write-Host "Pinning $package to $pinnedVersion" - $devDependencies[$package] = $pinnedVersion - } -} - -$emitterPackageJson = [ordered]@{ - "main" = "dist/src/index.js" - "dependencies" = @{ - $packageJson.name = $overrides[$packageJson.name] ?? $packageJson.version - } -} - -# you shouldn't specify the same package in both dependencies and overrides -$overrides.Remove($packageJson.name) - -# Avoid adding an empty devDependencies section -if($devDependencies.Keys.Count -gt 0) { - $emitterPackageJson["devDependencies"] = $devDependencies -} - -# Avoid adding an empty overrides section -if($overrides.Keys.Count -gt 0) { - $emitterPackageJson["overrides"] = $overrides -} - -New-Item $OutputDirectory -ItemType Directory -ErrorAction SilentlyContinue | Out-Null -$OutputDirectory = Resolve-Path $OutputDirectory - -$dest = Join-Path $OutputDirectory $PackageJsonFileName -$destJson = $emitterPackageJson | ConvertTo-Json -Depth 100 - -Write-Host "Generating $dest" -$destJson | Out-File $dest - -Write-Host $destJson +Write-Warning "This script is deprecated. Please use the 'eng/common/scripts/typespec/New-EmitterPackageJson.ps1' script instead." +. (Join-Path $PSScriptRoot "..\..\common\scripts\typespec\New-EmitterPackageJson.ps1") @args diff --git a/eng/scripts/autorest/New-EmitterPackageLock.ps1 b/eng/scripts/autorest/New-EmitterPackageLock.ps1 index 1872e104e4d..7f22ede3764 100644 --- a/eng/scripts/autorest/New-EmitterPackageLock.ps1 +++ b/eng/scripts/autorest/New-EmitterPackageLock.ps1 @@ -1,57 +1,2 @@ -[CmdletBinding()] -param ( - [parameter(Mandatory = $true)] - [string]$EmitterPackageJsonPath, - - [parameter(Mandatory = $true)] - [string]$OutputDirectory, - - [parameter(Mandatory = $false)] - [string]$NpmrcPath, - - [parameter(Mandatory = $false)] - [string]$LockFileName = "emitter-package-lock.json" -) - -New-Item $OutputDirectory -ItemType Directory -ErrorAction SilentlyContinue | Out-Null -$OutputDirectory = Resolve-Path $OutputDirectory - -$tempFile = New-TemporaryFile -Remove-Item $tempFile - -# use a consistent folder name to avoid random package name in package-lock.json -Write-Host "Creating temporary folder $tempFile/emitter-consumer" -$tempFolder = New-Item "$tempFile/emitter-consumer" -ItemType Directory - -if ($NpmrcPath) { - Write-Host "Copy npmrc from $NpmrcPath to $tempFolder/.npmrc" - Copy-Item $NpmrcPath "$tempFolder/.npmrc" -} - -Push-Location $tempFolder - -try { - Write-Host "Copy $EmitterPackageJsonPath to $tempFolder/package.json" - Copy-Item $EmitterPackageJsonPath "$tempFolder/package.json" - - Write-Host 'npm install --legacy-peer-deps' - npm install --legacy-peer-deps - - if ($LASTEXITCODE) { - Write-Error "npm install failed with exit code $LASTEXITCODE" - exit $LASTEXITCODE - } - - Write-Host '##[group]npm list --all' - npm list --all - Write-Host '##[endgroup]' - - $dest = Join-Path $OutputDirectory $LockFileName - Write-Host "Copy package-lock.json to $dest" - Copy-Item 'package-lock.json' $dest -} -finally { - Pop-Location -} - -Remove-Item $tempFolder -Recurse -Force +Write-Warning "This script is deprecated. Please use the 'eng/common/scripts/typespec/New-EmitterPackageLock.ps1' script instead." +. (Join-Path $PSScriptRoot "..\..\common\scripts\typespec\New-EmitterPackageLock.ps1") @args