From 22983bcdd3add05799abdd11a6c6619fea0ad203 Mon Sep 17 00:00:00 2001 From: Pete Davison Date: Fri, 31 Mar 2023 01:10:11 +0100 Subject: [PATCH] feat: release tool improvements (#1096) --- CHANGELOG.md | 4 ++++ Taskfile.yml | 8 -------- cmd/release/main.go | 29 ++++++++++++++++++++++++----- 3 files changed, 28 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 58e9466b..5830dac0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## Unreleased + +- More improvements to the release tool (#1096 by @pd93) + ## v3.23.0 - 2023-03-26 Task now has an [official extension for Visual Studio Code](https://marketplace.visualstudio.com/items?itemName=task.vscode-task) contributed by @pd93! :tada: The extension is maintained in a [new repository](https://github.com/go-task/vscode-task) under the `go-task` organization. We're looking to gather feedback from the community so please give it a go and let us know what you think via a [discussion](https://github.com/go-task/vscode-task/discussions), [issue](https://github.com/go-task/vscode-task/issues) or on our [Discord](https://discord.gg/6TY36E39UK)! diff --git a/Taskfile.yml b/Taskfile.yml index e89fffee..14bef3c5 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -107,14 +107,6 @@ tasks: cmds: - go run ./cmd/release {{.CLI_ARGS}} - npm:bump: - desc: Bump version in package.json. Requires `jq`. - vars: - VERSION: '{{coalesce .CLI_ARGS .VERSION}}' - cmds: - - cat package.json | jq '.version = "{{.VERSION}}"' > temp.json; mv temp.json package.json - - cat package-lock.json | jq '.version = "{{.VERSION}}" | .packages."".version = "{{.VERSION}}"' > temp.json; mv temp.json package-lock.json - npm:publish: desc: Publish release to npm cmds: diff --git a/cmd/release/main.go b/cmd/release/main.go index 3bb13ce9..4b2a0a03 100644 --- a/cmd/release/main.go +++ b/cmd/release/main.go @@ -26,6 +26,7 @@ var ( changelogReleaseRegex = regexp.MustCompile(`## Unreleased`) changelogUserRegex = regexp.MustCompile(`@(\w+)`) changelogIssueRegex = regexp.MustCompile(`#(\d+)`) + versionRegex = regexp.MustCompile(`(?m)^ "version": "\d+\.\d+\.\d+",$`) ) func main() { @@ -55,6 +56,14 @@ func release() error { return err } + if err := setJSONVersion("package.json", version); err != nil { + return err + } + + if err := setJSONVersion("package-lock.json", version); err != nil { + return err + } + return nil } @@ -86,7 +95,7 @@ func changelog(version *semver.Version) error { // Open changelog source file b, err := os.ReadFile(changelogSource) if err != nil { - panic(err) + return err } changelog := string(b) date := time.Now().Format("2006-01-02") @@ -96,7 +105,7 @@ func changelog(version *semver.Version) error { // Write the changelog to the source file if err := os.WriteFile(changelogSource, []byte(changelog), 0644); err != nil { - panic(err) + return err } // Add the frontmatter to the changelog @@ -107,9 +116,19 @@ func changelog(version *semver.Version) error { changelog = changelogIssueRegex.ReplaceAllString(changelog, "[#$1](https://github.com/go-task/task/issues/$1)") // Write the changelog to the target file - if err := os.WriteFile(changelogTarget, []byte(changelog), 0644); err != nil { - panic(err) + return os.WriteFile(changelogTarget, []byte(changelog), 0644) +} + +func setJSONVersion(fileName string, version *semver.Version) error { + // Read the JSON file + b, err := os.ReadFile(fileName) + if err != nil { + return err } - return nil + // Replace the version + new := versionRegex.ReplaceAllString(string(b), fmt.Sprintf(` "version": "%s",`, version.String())) + + // Write the JSON file + return os.WriteFile(fileName, []byte(new), 0644) }