How to automatically push tag to git after npm version command?

Gaj*_*jus 5 shell npm

npm version:

Run this in a package directory to bump the version and write the new data back to package.json [..] If run in a git repo, it will also create a version commit and tag.

How do I configure npm/ wrap npm version command to automatically push tag to git?, i.e. an equivalent of:

npm version patch
+ foo@3.0.1
git push origin v3.0.1
Run Code Online (Sandbox Code Playgroud)

NPM documentation recommends adding a postversion script to the prackage.json, e.g.

"scripts": {
    "postversion": "git push && git push --tags && rm -rf build/temp"
}
Run Code Online (Sandbox Code Playgroud)

However, this suggestion applies to a single package only and it is bad because it syncs all the tags, not just the last created tag.

Eta*_*ner 2

假设新标签是当前修订版上的唯一标签,这样的操作可能会起作用。

$ npm version patch
$ tag=$(git tag --points-at HEAD)
$ git push origin "$tag"
Run Code Online (Sandbox Code Playgroud)

否则,您可以尝试捕获(并解析)这样的输出npm version patch(假设输出总是+ foo@3.0.1并且标签总是v<part after @)。

$ tag=$(npm version patch 2>&1)
$ tag=v${tag#*@}
$ git push origin "$tag"
Run Code Online (Sandbox Code Playgroud)

您还可以尝试在调用之前获取可用标签npm version patch,然后将该列表与可用标签进行比较,之后应该找到新标签并可以推送它。