javascript – 如何在npm版本期间运行脚本

有没有办法在npm版本中运行脚本,即在版本号增加之后但在创建和推送git标签之前运行? 最佳答案 您可以创建一个版本脚本,该版本脚本将在包版本增加后但在提交和标记之前调用.

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

根据npm version文档检查执行顺序.您可以在下面看到有趣的摘录,特别是第4点:

  1. Check to make sure the git working directory is clean before we get
    started. Your scripts may add files to the commit in future steps.
    This step is skipped if the –force flag is set.
  2. Run the preversion script. These scripts have access to the old version in 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.
  3. Bump version in package.json as requested (patch, minor, major, etc).
  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). Again, scripts should explicitly add generated files to the commit using git add.
  5. Commit and tag.
  6. Run the postversion script. Use it to clean up the file system or automatically push the commit and/or tag.

此功能在npm v2.13.0中引入.有关详细信息,请参阅version: allow scripts to add files to the commit.

点赞