简体   繁体   English

自动更新package.json版本并在代码中读取

[英]Automatically update package.json version and read it in code

I have logger which is displaying some stuff in production and i want to add version to this logger so i got better clue if/when deployment is finished.我有一个记录器,它在生产中显示一些东西,我想向这个记录器添加版本,所以如果/何时部署完成,我会得到更好的线索。 Is it possible to:是否有可能:

  1. Update package.json version on merge合并时更新 package.json 版本
  2. Read this version in code and add it to the logger在代码中读取此版本并将其添加到记录器

That will be really helpful这将非常有帮助

for the second question if you have access to process you can read the version property of package.json like this:对于第二个问题,如果您有权访问process ,您可以像这样阅读package.jsonversion属性:

process.env.npm_package_version

for the first question, that is a little tricky, you can use some packages such as husky to run some npm scripts which you already add to package.json .对于第一个问题,这有点棘手,您可以使用一些包(例如husky )来运行一些npm scripts ,这些脚本已经添加到package.json中。

so you would listen to git hooks such as commit or merge and using pre- or post- scripts to run a command before or after a hook is fired.所以你会听 git 钩子,例如commitmerge ,并使用pre-post-置脚本在触发钩子之前或之后运行命令。

so I guess if you can combine these, you would get your desired result所以我想如果你能把这些结合起来,你会得到你想要的结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM