forked from perses/shared
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnpm-bump.go
More file actions
85 lines (69 loc) · 2.83 KB
/
npm-bump.go
File metadata and controls
85 lines (69 loc) · 2.83 KB
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
// Copyright The Perses Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"flag"
"fmt"
"os"
"path/filepath"
"regexp"
"github.com/perses/perses/scripts/pkg/npm"
"github.com/sirupsen/logrus"
)
var versionPattern = regexp.MustCompile(`^\d+\.\d+\.\d+(?:-[\w\d.]+)?$`)
func updatePackageVersion(workspaces []string, workspacePath string, newVersion string) error {
pkgPath := filepath.Join(workspacePath, "package.json")
data, err := os.ReadFile(pkgPath)
if err != nil {
logrus.WithError(err).Fatalf("unable to read the file %s", pkgPath)
}
// First, update the package version in the package.json
bumpVersion := regexp.MustCompile(`"version":\s*"[0-9]+\.[0-9]+\.[0-9]+(-(alpha|beta|rc)\.[0-9]+)?"`)
data = bumpVersion.ReplaceAll(data, []byte(fmt.Sprintf(`"version": "%s"`, newVersion)))
// Then, update all @perses-dev/* dependencies to the new version
for _, workspace := range workspaces {
bumpNPMDeps := regexp.MustCompile(fmt.Sprintf(`"@perses-dev/%s":\s*"(\^)?[0-9]+\.[0-9]+\.[0-9]+(-(alpha|beta|rc)\.[0-9]+)?"`, workspace))
data = bumpNPMDeps.ReplaceAll(data, []byte(fmt.Sprintf(`"@perses-dev/%s": "%s"`, workspace, newVersion)))
}
if writeErr := os.WriteFile(pkgPath, data, 0644); writeErr != nil {
logrus.WithError(writeErr).Fatalf("unable to write the file %s", pkgPath)
}
return nil
}
func main() {
flag.Parse()
if len(flag.Args()) == 0 {
logrus.Fatal("version argument is required. Usage: npm-bump <version>")
}
version := flag.Args()[0]
if !versionPattern.MatchString(version) {
logrus.Fatalf("Invalid semantic version format: %s. Expected format: X.Y.Z or X.Y.Z-prerelease", version)
}
workspaces := npm.MustGetWorkspaces(".")
if len(workspaces) == 0 {
logrus.Info("No workspaces found")
return
}
// First, update the root package.json
if err := updatePackageVersion(workspaces, ".", version); err != nil {
logrus.WithError(err).Fatal("failed to update root package.json")
}
logrus.Infof("Updating %d workspace(s) to version %s", len(workspaces), version)
for _, workspace := range workspaces {
if err := updatePackageVersion(workspaces, workspace, version); err != nil {
logrus.WithError(err).Fatalf("failed to update workspace: %s", workspace)
}
logrus.Infof("✓ Updated %s to version %s", workspace, version)
}
logrus.Info("All workspace packages updated successfully")
}