Skip to content

Commit 7b946b4

Browse files
authored
[Infrastructure] Add a script to update NPM dependencies and create PRs (#54701)
* Add a script to update NPM dependencies and create PRs
1 parent c1048a8 commit 7b946b4

File tree

1 file changed

+104
-0
lines changed

1 file changed

+104
-0
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
param (
2+
[switch]$WhatIf,
3+
[switch]$SkipPullRequestCreation
4+
)
5+
6+
$ErrorActionPreference = "Stop"
7+
8+
$env:npm_config_cache = "$PWD/src/submodules/Node-Externals/cache"
9+
10+
Write-Host "Ensuring the repository is clean"
11+
if (-not $WhatIf) {
12+
git clean -xdff
13+
}
14+
15+
Write-Host "Removing package-lock.json"
16+
if (-not $WhatIf) {
17+
Remove-Item .\package-lock.json
18+
}
19+
20+
Write-Host "Running npm install"
21+
if (-not $WhatIf) {
22+
npm install --prefer-online --include optional
23+
}
24+
25+
Write-Host "Adding optional dependencies to the cache"
26+
$rollupOptionalDependencies = (Get-Content .\package-lock.json | ConvertFrom-Json -AsHashtable).packages['node_modules/rollup'].optionalDependencies |
27+
Select-Object '@rollup/rollup-*';
28+
$commonOptionalDependencyVersion = $null;
29+
30+
foreach ($optionalDependency in ($rollupOptionalDependencies | Get-Member -MemberType NoteProperty)) {
31+
$optionalDependencyName = $optionalDependency.Name
32+
$optionalDependencyVersion = $rollupOptionalDependencies.$optionalDependencyName
33+
34+
if ($null -eq $commonOptionalDependencyVersion) {
35+
$commonOptionalDependencyVersion = $optionalDependencyVersion
36+
}
37+
38+
Write-Host "Adding $optionalDependencyName@$optionalDependencyVersion to the cache"
39+
if (-not $WhatIf) {
40+
npm cache add $optionalDependencyName@$optionalDependencyVersion
41+
}
42+
}
43+
44+
if ($null -ne $commonOptionalDependencyVersion) {
45+
Write-Host "Adding @rollup/wasm-node@$commonOptionalDependencyVersion to the cache"
46+
if (-not $WhatIf) {
47+
npm cache add "@rollup/wasm-node@$commonOptionalDependencyVersion"
48+
}
49+
}
50+
51+
Write-Host "Verifying the cache"
52+
if(-not $WhatIf) {
53+
npm cache verify
54+
}
55+
56+
# Navigate to the Node-Externals submodule
57+
# Checkout a branch named infrastructure/update-npm-package-cache-<date>
58+
# Stage the changes in the folder
59+
# Commit the changes with the message "Updated npm package cache <date>"
60+
# Use the GH CLI to create a PR for the branch in the origin remote
61+
62+
Push-Location src/submodules/Node-Externals
63+
$branchName = "infrastructure/update-npm-package-cache-$(Get-Date -Format 'yyyy-MM-dd')"
64+
if (-not $WhatIf) {
65+
git branch -D $branchName 2>$null
66+
git checkout -b $branchName
67+
git add .
68+
git commit -m "Updated npm package cache $(Get-Date -Format 'yyyy-MM-dd')"
69+
}
70+
71+
if ($WhatIf -or $SkipPullRequestCreation) {
72+
Write-Host "Skipping pull request creation for Node-Externals submodule"
73+
}
74+
else {
75+
Write-Host "Creating pull request for Node-Externals submodule"
76+
git branch --set-upstream-to=origin/main
77+
git push origin $branchName`:$branchName --force;
78+
gh repo set-default dotnet/Node-Externals
79+
gh pr create --base main --head "infrastructure/update-npm-package-cache-$(Get-Date -Format 'yyyy-MM-dd')" --title "[Infrastructure] Updated npm package cache $(Get-Date -Format 'yyyy-MM-dd')" --body ""
80+
}
81+
82+
## Navigate to the root of the repository
83+
## Checkout a branch named infrastructure/update-npm-package-cache-<date>
84+
## Stage the changes in the folder
85+
## Commit the changes with the message "Updated npm package cache <date>"
86+
## Use the GH CLI to create a PR for the branch in the origin remote
87+
Pop-Location
88+
if(-not $WhatIf) {
89+
git branch -D $branchName 2>$null
90+
git checkout -b $branchName
91+
git add .
92+
git commit -m "Updated npm package cache $(Get-Date -Format 'yyyy-MM-dd')"
93+
}
94+
95+
if ($WhatIf -or $SkipPullRequestCreation) {
96+
Write-Host "Skipping pull request creation for the root of the repository"
97+
}
98+
else {
99+
Write-Host "Creating pull request for the root of the repository"
100+
git branch --set-upstream-to=origin/main
101+
git push origin $branchName`:$branchName --force;
102+
gh repo set-default dotnet/aspnetcore
103+
gh pr create --base main --head $branchName --title "[Infrastructure] Updated npm package cache $(Get-Date -Format 'yyyy-MM-dd')" --body ""
104+
}

0 commit comments

Comments
 (0)