【问题标题】:How check consistency npm-shrinkwrap.json and package.json如何检查一致性 npm-shrinkwrap.json 和 package.json
【发布时间】:2016-11-01 06:44:43
【问题描述】:

有时我的团队成员在更新 package.json 后忘记更新 npm-shrinkwrap.json。我从 uber 知道 this package,但它不能与 npm v3 一起使用。所以现在不是解决方案。

我是否可以自动检查 npm-shrinkwrap.json 和 package.json 的一致性?我想在 git-hook 或/和连续中做到这一点。

【问题讨论】:

    标签: node.js githooks npm-shrinkwrap package-lock.json


    【解决方案1】:

    您可以测试npm package git-hooks,它允许安装pre-commit or pre-push hooks(即client-side hooks

    此类钩子(如pre-commit one)可用于检查源文件的一致性,如npm-shrinkwrap.json

    另见turadg/npm-shrinkwrap-git-hooks

    一组脚本可根据需要自动 npm shrinkwrapnpm install

    如果您对package.json 进行更改,则pre-commit 挂钩将run npm shrinkwrap 更新npm-shrinkwrap.json

    #!/usr/bin/env bash
    
    # This ensures that dependencies are installed locally whenever merging a commit
    # that changed the shrinkwrap.
    
    function package_changes_staged {
      ! git diff --cached  --quiet -- package.json
    }
    
    # update shrinkwrap when spec changes
    if package_changes_staged; then
      echo "Running 'npm shrinkwrap' to match new package spec..." >&2
      npm shrinkwrap
      git add npm-shrinkwrap.json
    fi
    

    更新 by galk-in

    我选择 pre-commit 并在 package.json 中进行此更新

    ...
    "scripts": {
      "check-shrinkwrap": "if (! git diff --cached  --quiet -- package.json); then echo 'Running `npm shrinkwrap` to match new package spec...' >&2; npm shrinkwrap; git add npm-shrinkwrap.json; fi"
    },
    ...
    "pre-commit": [
      "check-shrinkwrap",
      "test"
    ]
    ...
    

    【讨论】:

    【解决方案2】:

    2017 年 6 月 24 日更新 现代答案是使用 npm 5 和 package-lock.json

    【讨论】:

      猜你喜欢
      • 2022-12-13
      • 2022-10-09
      • 2020-01-19
      • 2018-02-21
      • 2012-06-28
      • 2017-10-30
      • 1970-01-01
      • 2019-07-13
      相关资源
      最近更新 更多