12

有没有办法使用 git 的 HEAD 哈希自动更新 package.json 的版本号( https://npmjs.org/doc/json.html )?我想version: 1.0.0+rev82e4b91cfe42cd86e9453b4987b9cc446566de6在项目的 package.json 文件中有类似的东西。加号之前的所有内容都是手动设置的,并且每次我提交某些内容时都会更新哈希值。

这可能吗?我在这个话题上找不到任何东西:-/

4

4 回答 4

4

看来这可以在没有插件的情况下直接完成。npm version $(git describe)将从git describepackage.json 中获取值并更新版本的值。

(我目前使用的是 npm 3.10 版。)

于 2017-06-09T19:54:00.920 回答
1

其中一部分可以解决,git-describe实际上有一个 grunt 插件可以为您包装它(https://github.com/mikaelkaron/grunt-git-describe/)。

第二部分你必须手动完成(现在),但我现在实际上有一个类似的问题,所以我可以尝试为你(和我自己)开发一个 grunt 插件。

完全公开,我是grunt-git-describe上面的作者。

于 2013-04-10T04:13:58.610 回答
1

git rev-parse HEAD将写出对当前头分支的最后一次提交,然后您可以在运行构建时将其附加到包中的版本号。

有许多git rev-xxx命令可能对您可能想要记录的任何其他内容有用。

于 2013-11-22T23:07:29.993 回答
-2

Automatically at what point? Possibilities are:

  1. Update the version field every time you run a build from a machine environment
  2. Update the version field every time you run any build.
  3. Update the version field in a git hook to keep in sync. Personally, I'd be nervous about a hook that performs a change when something changes. This strategy is highest risk, highest reward.

If you go with the first or second approach, it depends of course on what build tool you use. If you use grunt, see about a grunt plugin. I can't find any grunt plugins that do what you're asking for, but you can create one fairly easily.

http://gruntjs.com/plugins

If you do end up creating one, let me know as I am also in need of a similar process :)

In my case, I am using SVN, but want the same pattern. I want to put the SVN revision number as my build number.

My recommendation is to leave the build number blank in the file that is checked in and have your build environment do a git clone for a new build and update the build number. Then built packages always have something about them indicating the git commit they came from.

I think that for development, you don't really need it because you can always ask git which version you have checked out. There is a grunt-git plugin which you could maybe use to figure out the version in dev. (maybe git show?)

于 2013-04-08T14:38:49.527 回答