-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_version1.ps1
50 lines (43 loc) · 1.97 KB
/
update_version1.ps1
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
# update_version.ps1
# Usage: ./update_version.ps1 <new_version>
# FORMAT IS <0.0.0>
param (
[string]$new_version
)
if ($new_version -match '^[0-9]+\.[0-9]+\.[0-9]+$') {
# Define directories to exclude
$excludeDirs = @('node_modules', '.git', 'dist', 'out', 'build')
# Get all package.json files recursively excluding specific directories
$packageJsonFiles = Get-ChildItem -Recurse -Filter "package.json" | Where-Object {
$dir = $_.DirectoryName
-not ($excludeDirs | ForEach-Object { $dir -like "*$_*" })
}
foreach ($packageJsonFile in $packageJsonFiles) {
Write-Host "Processing $($packageJsonFile.FullName)"
# Read the content of the current package.json file
$packageJsonContent = Get-Content -Raw -Path $packageJsonFile.FullName
try {
$packageJsonParsed = $packageJsonContent | ConvertFrom-Json
} catch {
Write-Host "Error parsing $($packageJsonFile.FullName)"
continue
}
# Check if the current package.json has a version field
if ($packageJsonParsed.version) {
$current_version = $packageJsonParsed.version
if ($current_version -ne $new_version) {
Write-Host "Updating version from $current_version to $new_version in $($packageJsonFile.FullName)"
# Update the version
$updatedPackageJson = $packageJsonContent -replace '"version": "' + $current_version + '"', '"version": "' + $new_version + '"'
# Write the updated content back to the package.json file
$updatedPackageJson | Set-Content -Path $packageJsonFile.FullName
} else {
Write-Host "Version is already $new_version in $($packageJsonFile.FullName)"
}
} else {
Write-Host "No version field found in $($packageJsonFile.FullName)"
}
}
} else {
Write-Host "Version format $new_version isn't correct, proper format is <0.0.0>"
}