如何使用Grunt腳手架快速建立Node.js項目,gruntnode.js
作者:zhanhailiang 日期:2014-11-17
1. 安裝node.js,npm工具(略)。
2. 安裝grunt, grunt-init
npm install -g grunt-clinpm install -g grunt-init
3. 下載腳手架:grunt-init-node - Create a Node.js module, including Nodeunit unit tests.
git clone git@github.com:billfeller/grunt-init-node.git ~/.grunt-init/node
注意,其預設路徑為~/.grunt-init/xxxx,如果下載到預設路徑,後續只需要通過grunt-init xxxx就可直接初始化;若下載到私人指定路徑時,則grunt-init /path/to/template使用絕對路徑來初始化。
4. 初始化:
[root@~/wade/nodejs/grunt-init-node-demo]# grunt-init nodeRunning "init:node" (init) taskThis task will create one or more files in the current directory, based on theenvironment and the answers to a few questions. Note that answering "?" to anyquestion will show question-specific help and answering "none" to most questionswill leave its value blank. "node" template notes:Project name shouldn't contain "node" or "js" and should be a unique ID notalready in use at search.npmjs.org. Please answer the following:[?] Project name (grunt-init-node-demo) [?] Description (The best project ever.) [?] Version (0.1.0) [?] Project git repository (git://github.com/billfeller/grunt-init-node-demo.git) [?] Project homepage (https://github.com/billfeller/grunt-init-node-demo) [?] Project issues tracker (https://github.com/billfeller/grunt-init-node-demo/issues) [?] Licenses (MIT) [?] Author name (billfeller) [?] Author email (billfeller@gmail.com) [?] Author url (none) http://blog.csdn.net/billfeller[?] What versions of node does it run on? (>= 0.8.0) [?] Main module/entry point (lib/grunt-init-node-demo) [?] Npm test command (grunt nodeunit) [?] Will this project be tested with Travis CI? (Y/n) Y[?] Do you need to make any changes to the above before continuing? (y/N) N Writing .gitignore...OKWriting .jshintrc...OKWriting .travis.yml...OKWriting Gruntfile.js...OKWriting README.md...OKWriting lib/grunt-init-node-demo.js...OKWriting test/grunt-init-node-demo_test.js...OKWriting LICENSE-MIT...OKWriting package.json...OK Initialized from template "node".You should now install project dependencies with npm install. After that, youmay execute project tasks with grunt. For more information about installingand configuring Grunt, please see the Getting Started guide: http://gruntjs.com/getting-started Done, without errors.
當看到“Done, without errors.”,就表示初始化成功,之後就可以開始專註於業務開發了。
註:如果在初始化時遇到如下提示:
[root@~/wade/nodejs]# grunt-init nodeRunning "init:node" (init) taskThis task will create one or more files in the current directory, based on theenvironment and the answers to a few questions. Note that answering "?" to anyquestion will show question-specific help and answering "none" to most questionswill leave its value blank.Warning: Existing files may be overwritten! Use --force to continue.Aborted due to warnings.
那是因為目前的目錄已經存在同名的相關檔案,此時可以通過清除相關檔案或者重建一個空目錄來初始化。
5. grunt官方維護幾個模板:
- grunt-init-commonjs - Create a commonjs module, including Nodeunit unit tests.
- grunt-init-gruntfile - Create a basic Gruntfile.
- grunt-init-gruntplugin - Create a Grunt plugin, including Nodeunit unit tests.
- grunt-init-jquery - Create a jQuery plugin, including QUnit unit tests.
- grunt-init-node - Create a Node.js module, including Nodeunit unit tests.
可以參考以上做法初始化相關的項目。
6. 參考:
- Getting started
- Project Scaffolding
- 利用 Grunt (幾乎)無痛地做前端開發 (一)