2017-09-23 81 views
0

我正在嘗試創建一個git鉤子來評估package.json的更改並自動運行shrinkwrapper並將更改後的文件提交到存儲庫。我嘗試了很多,但無法獲得工作解決方案。有誰知道這是怎麼回事?添加git鉤子來監視文件的更改

而且,只要我添加了一個NPM模塊,並嘗試運行NPM拆封,我得到這個錯誤

npm ERR! Darwin 16.7.0 
npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "shrinkwrap" 
npm ERR! node v6.11.3 
npm ERR! npm v3.10.10 

npm ERR! Problems were encountered 
npm ERR! Please correct and try again. 
npm ERR! missing: [email protected]^4.2.2, required by [email protected] 
npm ERR! 
npm ERR! If you need help, you may report this error at: 
npm ERR!  <https://github.com/npm/npm/issues> 

npm ERR! Please include the following file with any support request: 
npm ERR!  /Users/maxDoung/Desktop/Redux-React/npm-debug.log 

出於某種原因NPM收縮不工作,如果我手動更新的package.json或安裝模塊使用npm。另外,我不確定節點或npm版本是否重要。我使用NPM 3.10.10和節點v6.11.3

這裏是我的依賴

"dependencies": { 
    "apn": "^1.7.7", 
    "bluebird": "^3.4.1", 
    "body-parser": "^1.15.0", 
    "busboy": "^0.2.13", 
    "connect-redis": "^3.1.0", 
    "cookie-parser": "^1.4.1", 
    "cors": "^2.7.1", 
    "debug": "^2.2.0", 
    "destroy": "^1.0.4", 
    "express": "^4.13.4", 
    "git-rev": "^0.2.1", 
    "glob": "^7.0.3", 
    "helmet": "^1.3.0", 
    "hiredis": "^0.4.1", 
    "humps": "^1.1.0", 
    "lodash": "^4.14.1", 
    "methods": "^1.1.2", 
    "mysql": "^2.11.1", 
    "node-gcm": "^0.14.4", 
    "node-inspector": "^0.12.8", 
    "node-resource": "^1.2.0", 
    "on-finished": "^2.3.0", 
    "on-headers": "^1.0.1", 
    "parseurl": "^1.3.1", 
    "path-to-regexp": "^1.5.3", 
    "redis": "^2.6.0-0", 
    "request": "^2.69.0", 
    "sequelize": "^3.23.6", 
    "serve-favicon": "^2.3.0", 
    "socket.io": "^1.4.6", 
    "through2": "^2.0.1" 
    }, 
+0

你能否顯示你的倉庫的文件夾結構,主要是'npm shrinkwrap'的相關部分? –

+0

您是否想要對推送中的每個提交進行評估,或僅對每次推送進行評估? – Kannaiyan

回答

1

也許這是你想要的,或者至少它會指向你到正確的方向。我已經在Linux上測試過了。

請按照下列步驟操作(適應他們爲您的特定需求):

1 - 將以下文件在的.git /掛鉤/預提交

#!/bin/bash 

is_package_json_at_git_index() { 
    echo "> Getting list of files at git index." 

    # Get the list of files added to the git index and verify that package.json is there. 
    git diff --cached --name-only | grep -x package.json 
} 

update_shrinkwrap_and_at_to_git_index() { 
    echo "> Updating npm-shrinkwrap.json and adding to this commit." 

    # Run shrinkwrap in the case that it was never ran 
    npm shrinkwrap 

    # Install dependencies that could be added to package.json and update npm-shrinkwrap.json 
    npm install 

    # Add the updated- npm-shrinkwrap.json to the git index so we include it in the current commit 
    git add npm-shrinkwrap.json 
} 

if is_package_json_at_git_index; then 
    update_shrinkwrap_and_at_to_git_index 
fi 

2 - 向預先提交添加運行權限 chmod +x .git/hooks/pre-commit

現在,如果您執行以下操作:

  1. 手動更新package.json可能更新依賴項的版本。
  2. 您運行git add package.json
  3. 您運行git commit -m "Your commit message"

將發生以下情況:

  1. NPM-shrinkwrap.json將被更新
  2. NPM-shrinkwrap.json會自動添加到提交

注意:你可以使用其他的鉤子,請看看他們的詳細說明here

+0

感謝加百利的回答。如果您手動更新package.json,則不起作用;例如,如果將「request-promise」:「^ 4.2.2」添加到package.json中,則嘗試提交。你會得到一個錯誤,說明請求保證缺失。如果您使用npm install,但不手動,您的解決方案可以正常工作。有沒有可以手動更新package.json的解決方案? –

+0

@MaxDoung,我已經在Ubuntu 16.04上用npm 5.3.0進行了測試,並且可以很好地執行以下操作:1.編輯package.json並在依賴部分添加「request-promise」:「^ 4.2.2」。 2. git add package.json,3. git commit -m「添加請求 - 承諾依賴關係」。之後,npm-shrinkwrap。json將所有更改與更新的package.json一起提交到存儲庫。 –

+0

@MaxDoung你可以試着用npm 5.3.0來運行我的例子嗎? –