2015-11-23 67 views
0

我有一個基於yeoman的設計的節點應用程序,我希望它使用npm將其分發給用戶。使用節點創建命令行應用程序

事情是在使用節點安裝應用程序之後。我希望用戶提供幾個全局命令,通過這些命令他可以輕鬆地使用我的應用程序,就像使用gruntboweryeoman時提供的命令一樣。

$ bower install <package-name> 
$ grunt serve 

同樣,我想我的應用程序提供一些命令,以便用戶可以輕鬆地處理它。讓命令是xyz

$ xyz init #will initialize the application and create the scaffolding. 

我已經在應用設計上node我只是希望它提供 使用全局命令行應用程序的安裝。

回答

2

只需創建一個腳本斌:

的package.json:

"bin": { 
    "xyz": "index.js" 
    } 

index.js:

console.log(process.argv); 

用法:

npm install xyz 
xyz foo bar 

輸出:

[ 'node', '/path/to/your/script/file', 'foo', 'bar'] 

欲瞭解更多信息,可以參考這篇博客文章: Building a simple command line tool with npm

相關問題