2017-04-26 110 views
0

Npm提供了一種在安裝每個軟件包後執行自定義可執行文件或腳本的方法(請參閱Hook Scripts)。如何從npm「postinstall」掛鉤獲取包名?

這裏是一個小鉤子腳本,我寫道:

鉤測試NPM/node_modules/.hooks /安裝後

#!/usr/bin/env node 
console.log("postinstall... " + process.argv.join(" ")); 

我再以通常的方式安裝包:

$ npm install --save some-package 

然而,結果並不像我所希望的:

> [email protected] postinstall /Users/macuser/Desktop/hook-test-npm/node_modules/some-package 
> /Users/macuser/Desktop/hook-test-npm/node_modules/.hooks/postinstall 
postinstall... /usr/local/bin/node /Users/macuser/Desktop/hook-test-npm/node_modules/.hooks/postinstall 

剛剛安裝的軟件包名稱(「some-package」)似乎沒有作爲我的可執行程序鉤子的參數提供。

有沒有辦法從鉤子內部訪問這些信息?

回答

0

經過進一步實驗,我發現以下兩個環境變量似乎包含我正在尋找的信息。我不知道這些是否應該直接使用;但他們一定會爲我暫時解決問題:

#!/usr/bin/env node 

console.log("postinstall..."); 

// Print out the name of the package that was just installed. 
console.log(" " + process.env.npm_package_name); 

// Print out the directory of the package that was just installed. 
console.log(" " + process.env.PWD);