简体   繁体   English

如何在npm版本中运行脚本

[英]How to run script during npm version

有没有办法在npm version运行脚本,即在版本号增加之后但在创建和推送git标签之前运行?

You can create a version script that will be called once the package version is increased but before the commit and tag. 您可以创建一个version脚本,该version脚本将在包版本增加后但在提交和标记之前调用。

"scripts": {
  "version": "./your_script"
}

Check the order of execution according to the npm version documentation. 根据npm版本文档检查执行顺序。 Below you can see the interesting excerpt, in particular point 4: 您可以在下面看到有趣的摘录,特别是第4点:

  1. Check to make sure the git working directory is clean before we get started. 在开始之前检查以确保git工作目录是干净的。 Your scripts may add files to the commit in future steps. 您的脚本可能会在以后的步骤中将文件添加到提交中。 This step is skipped if the --force flag is set. 如果设置了--force标志,则跳过此步骤。
  2. Run the preversion script. 运行preversion脚本。 These scripts have access to the old version in package.json. 这些脚本可以访问package.json中的旧版本。 A typical use would be running your full test suite before deploying. 典型用途是在部署之前运行完整的测试套件。 Any files you want added to the commit should be explicitly added using git add. 应该使用git add显式添加要添加到提交的任何文件。
  3. Bump version in package.json as requested (patch, minor, major, etc). 在package.json中根据请求进行凹凸版本(补丁,次要,主要等)。
  4. Run the version script. 运行版本脚本。 These scripts have access to the new version in package.json (so they can incorporate it into file headers in generated files for example). 这些脚本可以访问package.json中的新版本(例如,它们可以将它合并到生成的文件中的文件头中)。 Again, scripts should explicitly add generated files to the commit using git add. 同样,脚本应该使用git add将生成的文件显式添加到提交中。
  5. Commit and tag. 提交和标记。
  6. Run the postversion script. 运行postversion脚本。 Use it to clean up the file system or automatically push the commit and/or tag. 使用它来清理文件系统或自动推送提交和/或标记。

This functionality was introduced in npm v2.13.0 . 此功能是在npm v2.13.0中引入的。 See version: allow scripts to add files to the commit for more info. 请参阅version:允许脚本将文件添加到提交中以获取更多信息。

In this page https://docs.npmjs.com/misc/scripts there are some scripts that you can use: 在这个页面https://docs.npmjs.com/misc/scripts中有一些你可以使用的脚本:

  • preversion, version: Run BEFORE bump the package version. preversion,version:运行BEFORE以获取包版本。
  • postversion: Run AFTER bump the package version. postversion:运行AFTER碰撞包版本。

With this you can add one of these scripts in your package.json: 有了这个,您可以在package.json中添加以下脚本之一:

"scripts": { "postversion" : "./your_script" }

Also, you can take a look in the hooks: https://docs.npmjs.com/misc/scripts#hook-scripts 此外,您可以查看钩子: https//docs.npmjs.com/misc/scripts#hook-scripts

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

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