f07220b0c6
- Add internal/node package: reads/writes package.json version (single or multi-path via node.package_json / node.package_jsons) - Add maven.pom_paths support: update multiple pom.xml files in one release commit; pom_paths overrides pom_path; --pom flag clears pom_paths - Add git.bump_rules config: per-type control of which version component bumps (breaking/feat/fix accept "patch" or "minor"); wired through version.Next() as a new sixth parameter - Extract injectable function vars (absPath, gitAllCommits, gitCommitsSince, gitCommitFiles) to enable error-path testing without interfaces - Achieve 100% per-package statement coverage across all 12 packages Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
43 lines
1.1 KiB
Go
43 lines
1.1 KiB
Go
package node
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
)
|
|
|
|
// ReadVersion returns the version field from a package.json file.
|
|
func ReadVersion(path string) (string, error) {
|
|
data, err := os.ReadFile(path)
|
|
if err != nil {
|
|
return "", fmt.Errorf("read %s: %w", path, err)
|
|
}
|
|
var pkg struct {
|
|
Version string `json:"version"`
|
|
}
|
|
if err := json.Unmarshal(data, &pkg); err != nil {
|
|
return "", fmt.Errorf("parse %s: %w", path, err)
|
|
}
|
|
if pkg.Version == "" {
|
|
return "", fmt.Errorf("no version field in %s", path)
|
|
}
|
|
return pkg.Version, nil
|
|
}
|
|
|
|
// WriteVersion replaces the version field in a package.json file in-place.
|
|
// oldVersion must match what ReadVersion returned. Formatting is preserved.
|
|
func WriteVersion(path, oldVersion, newVersion string) error {
|
|
data, err := os.ReadFile(path)
|
|
if err != nil {
|
|
return fmt.Errorf("read %s: %w", path, err)
|
|
}
|
|
old := `"version": "` + oldVersion + `"`
|
|
repl := `"version": "` + newVersion + `"`
|
|
if !strings.Contains(string(data), old) {
|
|
return fmt.Errorf("version %q not found in %s", oldVersion, path)
|
|
}
|
|
updated := strings.Replace(string(data), old, repl, 1)
|
|
return os.WriteFile(path, []byte(updated), 0644)
|
|
}
|