前言
有一种技术可以提高我们的工作效率,可以让我们专心做我们擅长的事,可以屏蔽复杂性,可以规范我们的架构和我们的代码,可以让我们的享受编程的乐趣。Yeoman可以做到。

很多年以前,rails刚刚出世,伴随着一个新的名词“脚手架(scaffolding)”出现了。脚手架是一种提高开发效率的工具的代名词。随后,各种编程语言都开始实现自己的“脚手架”工具。Maven重新构造了Java的世界,改变了几千万的开发者对于Java项目构建的认识。Yeoman在Javascript领域正做着同样的事情。

看着各种工具,各种语言,都在飞跃式的进化。做为一个开发者,我每天都在为变化而惊喜!

目录

  1. Yeoman介绍
  2. Yeoman工具包yo命令
  3. Yeoman工具包yo – 快速构建一个web项目
  4. Yeoman工具包bower,grunt的组合用法

1. Yeoman介绍

Yeoman是Google的团队和外部贡献者团队合作开发的,他的目标是通过Grunt(一个用于开发任务自动化的命令行工具)和Bower(一个HTML、CSS、Javascript和图片等前端资源的包管理器)的包装为开发者创建一个易用的工作流。

Yeoman主要有三部分组成:yo(脚手架工具)、grunt(构建工具)、bower(包管理器)。这三个工具是分别独立开发的,但是需要配合使用,来实现我们高效的工作流模式。

2. Yeoman工具包yo命令

yo插件都是通过npm, Node.js包管理器安装和管理的.

我的系统环境

  • win7 64bit
  • Nodejs:v0.10.5
  • Npm:1.2.19
  1. ~ D:\workspace\javascript>node -v
  2. v0.10.5
  3. ~ D:\workspace\javascript>npm -v
  4. 1.2.19

在系统中,我们已经安装好了Nodejs和npm。win7安装nodejs请参考文章:Nodejs开发框架Express3.0开发手记–从零开始

全局安装yo

  1. ~ D:\workspace\javascript>npm install -g yo

如果你还没有安装grunt,bower,也需要一起安装

  1. ~ D:\workspace\javascript>npm install -g grunt-cli bower

接下我们看一下yo命令行操作
1). 通过help查看帮助


  1. ~ D:\workspace\javascript>yo --help
  2. Yeoman is a mask worn by the following members of the open-source community:
  3. Paul Irish, Addy Osmani, Mickael Daniel, Sindre Sorhus, Eric Bidelman,
  4. Frederick Ros, Brian Ford, Pascal Hartig, Stephen Sawchuk, and countless
  5. other contributors.
  6. Usage: yo GENERATOR [args] [options]
  7. General options:
  8. -h, --help # Print generator's options and usage
  9. -f, --force # Overwrite files that already exist
  10. Please choose a generator below.
  11. Angular
  12. angular:app
  13. angular:common
  14. angular:constant
  15. angular:controller
  16. angular:decorator
  17. angular:directive
  18. angular:factory
  19. angular:filter
  20. angular:main
  21. angular:provider
  22. angular:route
  23. angular:service
  24. angular:value
  25. angular:view
  26. Karma
  27. karma:app
  28. Mocha
  29. mocha:app
  30. mocha:generator
  31. Webapp
  32. webapp:app

yo的命令很简单:yo GENERATOR,例如:yo webapp, yo angular
Please choose a generator below: 下面列出了我的系统中,已经安装的generator的库,例如:Angular,Karma,Mocha,Webapp

2). 查询yo的支持库


  1. ~ D:\workspace\javascript>yo
  2. Yeoman is a mask worn by the following members of the open-source community:
  3. Paul Irish, Addy Osmani, Mickael Daniel, Sindre Sorhus, Eric Bidelman,
  4. Frederick Ros, Brian Ford, Pascal Hartig, Stephen Sawchuk, and countless
  5. other contributors.
  6. [?] What would you like to do? (Use arrow keys)
  7. > Install a generator
  8. Find some help
  9. Get me out of here!

命令行会提示,我们想要的操作。这里选择”Install a generator”


  1. [?] What would you like to do? Install a generator
  2. [?] Search NPM for generators:web

系统继续提示,我们要查找的包,我们输入web

3). 安装generator-webapp库


  1. [?] Here's what I found. Install one? (Use arrow keys)
  2. > generator-armadillo
  3. generator-bones
  4. generator-btapp
  5. generator-fe
  6. generator-flight
  7. generator-hazdev-webapp
  8. generator-hbswebapp
  9. generator-html5-site
  10. generator-jing
  11. generator-lessapp
  12. generator-nodestrap
  13. generator-sails
  14. generator-server-configs
  15. generator-starter
  16. generator-starttter
  17. generator-ultimate
  18. generator-webapp
  19. generator-webapp-bfytw
  20. generator-webapp-fintan
  21. generator-weblog
  22. generator-website
  23. Search again
  24. Return home

yo列出了,所有在官方已经注册的,web关键字相关的包,我们选择“generator-webapp”

yo通过npm开始下载generator-webapp安装包


  1. [?] What would you like to do? Install a generator
  2. [?] Search NPM for generators: web
  3. [?] Here's what I found. Install one? generator-webapp
  4. generator-webapp@0.2.7 D:\toolkit\nodejs\node_modules\generator-webapp
  5. ├── cheerio@0.12.1 (entities@0.3.0, underscore@1.4.4, htmlparser2@3.1.4, cheerio-select@0.0.3)
  6. └── yeoman-generator@0.12.3 (dargs@0.1.0, diff@1.0.5, debug@0.7.2, async@0.2.9, mime@1.2.11, mkdirp@0.3.5, isbinaryfi
  7. le@0.1.9, underscore.string@2.3.3, shelljs@0.1.4, iconv-lite@0.2.11, lodash@1.3.1, rimraf@2.1.4, nopt@2.1.2, cli-table@0
  8. .2.0, tar@0.1.18, glob@3.2.6, inquirer@0.2.4, request@2.21.0)
  9. I just installed your generator by running:
  10. npm install -g generator-webapp

然后,我们退出yo命令行,选择“Get me out of here!”


  1. [?] What would you like to do? Get me out of here!
  2. Bye from us! Chat soon.
  3. Addy Osmani
  4. Sindre Sorhus
  5. Brian Ford
  6. Eric Bidelman
  7. Paul Irish
  8. Mickael Daniel
  9. Pasca1 Hartig
  10. Stephen S.wchuk
  11. Frederick R0s

上面的退出的文字是开发者的名字,很有意思一点是,竖着看最中间一行的文字排列是,yeoman1.0,也就是yeoman这个项目的得名了。

4). 注:上面的过程,也可以直接用命令

  1. npm install -g generator-webapp

3. Yeoman工具包yo – 快速构建一个web项目

当我们安装了generator-webapp后,我们就可以,以非常优雅的方式来构建工程了。

1). 创建nodejs-yo目录,并构建webapp工程


  1. ~ D:\workspace\javascript>mkdir nodejs-yo
  2. ~ D:\workspace\javascript>cd nodejs-yo
  3. ~ D:\workspace\javascript\nodejs-yo>yo webapp
  4. _-----_
  5. | |
  6. |--(o)--| .--------------------------.
  7. `---------´ | Welcome to Yeoman, |
  8. ( _´U`_ ) | ladies and gentlemen! |
  9. /___A___\ '__________________________'
  10. | ~ |
  11. __'.___.'__
  12. ´ ` |° ´ Y `
  13. Out of the box I include HTML5 Boilerplate, jQuery and Modernizr.
  14. [?] What more would you like?
  15. [ ] Twitter Bootstrap for Sass
  16. >[X] RequireJS
  17. [X] Autoprefixer for your CSS

对选项的解释:

  • Twitter Bootstrap for Sass:SASS是一种CSS的开发工具,提供了许多便利的写法,大大节省了设计者的时间,使得CSS的开发,变得简单和可维护。
  • RequireJS 可以帮助用户异步按需的加载 JavaScript 代码,并解决 JavaScript 模块间的依赖关系,提升了前端代码的整体质量和性能。
  • Autoprefixer解析CSS文件并且添加浏览器前缀到CSS规则里,使用Can I Use的数据来决定哪些前缀是需要的。

上面3个选择,由于Sass是基于Ruby的,我本机没有安装ruby,取消Twitter Bootstrap for Sass的勾选。

2). 查看安装日志:


  1. Out of the box I include HTML5 Boilerplate, jQuery and Modernizr.
  2. [?] What more would you like? RequireJS, Autoprefixer for your CSS
  3. create Gruntfile.js
  4. create package.json
  5. create .gitignore
  6. create .gitattributes
  7. create .bowerrc
  8. create bower.json
  9. create .jshintrc
  10. create .editorconfig
  11. create app/favicon.ico
  12. create app/404.html
  13. create app/robots.txt
  14. create app/.htaccess
  15. create app/styles/main.css
  16. create app/scripts/app.js
  17. create app/scripts/main.js
  18. create app/index.html
  19. create app/scripts/hello.coffee
  20. invoke mocha:app
  21. create test\index.html
  22. create test\lib\chai.js
  23. create test\lib\expect.js
  24. create test\lib\mocha\mocha.css
  25. create test\lib\mocha\mocha.js
  26. create test\spec\test.js
  27. I'm all done. Running bower install & npm install for you to install the required dependencies. If this fails, try running the command yourself.

项目骨架已经建好了。还包括了很多的配置文件.gitignore,bower.json,Gruntfile.js,favicon.ico, robots.txt, .htaccess。太神奇了,Cool!!

一下子装备都齐全了,打仗的事就交给我们自己吧。

4. Yeoman工具包bower,grunt的组合用法

有关bower的介绍,请参考:bower解决js的依赖管理
有关grunt的介绍,请参考:grunt让Nodejs规范起来

1). 使用bower命令查看项目依赖:


  1. ~ D:\workspace\javascript\nodejs-yo>bower list
  2. bower check-new Checking for new versions of the project dependencies..
  3. nodejs-yo#0.0.0 D:\workspace\javascript\nodejs-yo
  4. ├── jquery#1.9.1 (latest is 2.0.3)
  5. ├── modernizr#2.6.2
  6. └── requirejs#2.1.8

我们发现生成的webapp中,有4个依赖库,jquery,modernizr,requirejs。

2). 使用grunt命令执行单元测试


  1. D:\workspace\javascript\nodejs-yo>grunt test
  2. Running "clean:server" (clean) task
  3. Cleaning ".tmp"...OK
  4. Running "concurrent:test" (concurrent) task
  5. Running "copy:styles" (copy) task
  6. Running "coffee:dist" (coffee) task
  7. Running "autoprefixer:dist" (autoprefixer) task
  8. File ".tmp/styles/main.css" created.
  9. Running "connect:test" (connect) task
  10. Starting connect web server on localhost:9000.
  11. Running "mocha:all" (mocha) task
  12. Testing: http://localhost:9000/index.html
  13. .
  14. 1 test complete (107 ms)
  15. >> 1 passed! (0.11s)
  16. Done, without errors.

3). 使用grunt命令启动一个node服务器


  1. ~ D:\workspace\javascript\nodejs-yo>grunt server
  2. Running "server" task
  3. Running "clean:server" (clean) task
  4. Running "concurrent:server" (concurrent) task
  5. Warning: Running "compass:dist" (compass) task
  6. Warning: You need to have Ruby and Compass installed and in your system PATH for this task to work. More info: https://g
  7. ithub.com/gruntjs/grunt-contrib-compass Use --force to continue. Use --force to continue.
  8. Aborted due to warnings.

启动时,我们发现报错了。由于我没有装Ruby的环境,所以compass:dist的命令不能运行。

强制启动grunt


  1. ~ D:\workspace\javascript\nodejs-yo>grunt server --force
  2. D:\workspace\javascript\nodejs-yo>grunt server --force
  3. Running "server" task
  4. Running "clean:server" (clean) task
  5. Cleaning ".tmp"...OK
  6. Running "concurrent:server" (concurrent) task
  7. Running "coffee:dist" (coffee) task
  8. Warning: Running "compass:dist" (compass) task
  9. Warning: You need to have Ruby and Compass installed and in your system PATH for this task to work. More info: https://
  10. ithub.com/gruntjs/grunt-contrib-compass Use --force to continue. Used --force, continuing.
  11. Running "compass:dist" (compass) task
  12. Warning: You need to have Ruby and Compass installed and in your system PATH for this task to work. More info: https://
  13. ithub.com/gruntjs/grunt-contrib-compass Use --force to continue.
  14. Running "copy:styles" (copy) task
  15. Running "autoprefixer:dist" (autoprefixer) task
  16. File ".tmp/styles/main.css" created.
  17. Running "connect:livereload" (connect) task
  18. Starting connect web server on localhost:9000.
  19. Running "open:server" (open) task
  20. Running "watch" task
  21. Waiting...

后台日志显示了“watch”的task,说明代码文件被监控着,如有修改网页会自动更新。

浏览器被自动打开,http://localhost:9000/

4). 修改app/index.html代码,增加对canvas的检查。


  1. ~ vi app/index.html
  2. <script>
  3. window.onload = function () {
  4. if (canvasSupported()) {
  5. alert('canvas supported');
  6. }
  7. };
  8. function canvasSupported() {
  9. var canvas = document.createElement('canvas');
  10. return (canvas.getContext && canvas.getContext('2d'));
  11. }
  12. </script>

浏览器显示:

5). 通过bower增加库jquery-pjax


  1. ~ D:\workspace\javascript\nodejs-yo>bower install jquery-pjax
  2. bower jquery-pjax#* not-cached git://github.com/defunkt/jquery-pjax.git#*
  3. bower jquery-pjax#* resolve git://github.com/defunkt/jquery-pjax.git#*
  4. bower jquery-pjax#* download https://github.com/defunkt/jquery-pjax/archive/v1.7.3.tar.gz
  5. bower jquery-pjax#* extract archive.tar.gz
  6. bower jquery-pjax#* resolved git://github.com/defunkt/jquery-pjax.git#1.7.3
  7. bower jquery#>=1.8 cached git://github.com/components/jquery.git#2.0.3
  8. bower jquery#>=1.8 validate 2.0.3 against git://github.com/components/jquery.git#>=1.8
  9. bower jquery-pjax#~1.7.3 install jquery-pjax#1.7.3
  10. jquery-pjax#1.7.3 app\bower_components\jquery-pjax
  11. └── jquery#2.0.3
  12. ~ D:\workspace\javascript\nodejs-yo>bower list
  13. bower check-new Checking for new versions of the project dependencies..
  14. nodejs-yo#0.0.0 D:\workspace\javascript\nodejs-yo
  15. ├── jquery#1.9.1 (latest is 2.0.3)
  16. ├─┬ jquery-pjax#1.7.3 extraneous
  17. └── jquery#1.9.1 (2.0.3 available)
  18. ├── modernizr#2.6.2
  19. └── requirejs#2.1.8

Yeoman已经成功地帮助我们打造出一个项目的原型了,接下来我们可以针对自己的项目,自己熟悉的框架组合,构建一个属于自己的骨架模板,并发布到Yeoman的generator库中。

yeoman(转)的更多相关文章

  1. Yeoman 官网教学案例:使用 Yeoman 构建 WebApp

    STEP 1:设置开发环境 与yeoman的所有交互都是通过命令行.Mac系统使用terminal.app,Linux系统使用shell,windows系统可以使用cmder/PowerShell/c ...

  2. Yeoman 学习笔记

    yoeman 简介:http://www.infoq.com/cn/news/2012/09/yeoman yeoman 官网: http://yeoman.io/ yeoman 是快速创建骨架应用程 ...

  3. 开发属于自己的yeoman脚手架(generator-reactpackage)

    自从前后端开始分离之后,前端项目工程化也显得越来越重要了,之前写过一篇搭建基于Angular+Requirejs+Grunt的前端项目教程,有兴趣的可以点这里去看 但是有些项目可以使用这种方式,但有些 ...

  4. 利用yeoman快速搭建React+webpack+es6脚手架

    自从前后端开始分离之后,前端项目工程化也显得越来越重要了,之前写过一篇搭建基于Angular+Requirejs+Grunt的前端项目教程,有兴趣的可以点这里去看 但是有些项目可以使用这种方式,但有些 ...

  5. 与你相遇好幸运,制作自己的Yeoman Generator

    使用别人写好的生成器: npm install -g yonpm install -g generator-angularyo angular 如何自己制作符合自己心仪的生成器呢: https://g ...

  6. Windows 使用 Yeoman generators 创建 ASP.NET 应用程序

    上一篇:<Windows 搭建 .NET 跨平台环境并运行应用程序> 阅读目录: Install Node.js Install yeoman-generators Create ASP. ...

  7. 前端项目构建之yeoman

    各位好啊,我又和大家见面了,也许你已经不记得大明湖畔的容嬷嬷,但是只要记得博客园中的我就好,希望我的博客能像一股清风,为你驱散炎热的酷暑,好了,废话不多说,开始上干货,我今天带给大家的是前端工程化开发 ...

  8. 在Mac上开发使用yeoman构建Asp.net core项目并且实现分层引用

    1.Yeoman? yeoman是一个自动化脚手架工具.它提供很多generator,generator相当于VisualStudio的模板,用来初始化项目.更多的就不多说了,写一遍都写不完,自己看吧 ...

  9. 安装Yeoman,遇到的问题

    在安装Yeoman遇到了一些报错,具体的步骤就不多说了,直接上码,具体的两处报错的地方. 第一个,创建webapp,直接报权限的错误,这时才留意到在它之前还有一个错误. [root@iZ289zd8c ...

  10. 编写自定义Yeoman生成器

    转载自JSCON-简时空:<自定义Yeoman生成器> 1.Getting Started 1.1.设置Node模块 Yeoman提供了generator-generator方便快速编写自 ...

随机推荐

  1. 虚拟机virtualBox设置共享文件后,linux配置

    1.在/mnt下创建共享目录 mkdir /mnt/share 2.关联外部目录 mount -t vboxsf 共享文件夹名 /mnt/share/ 如:mount -t vboxsf BaiduS ...

  2. 百度音乐API抓取

    百度音乐API抓取 前段时间做了一个本地音乐的播放器 github地址,想实现在线播放的功能,于是到处寻找API,很遗憾,不是歌曲不全就是质量不高.在网上发现这么一个APIMRASONG博客,有“获取 ...

  3. nullcom HackIM2016 -- Programming Question 4

    One of the NullCon vidoes talked about a marvalous Russian Gift. The Vidoe was uploaded on [May of 2 ...

  4. VS中展开和折叠代码

    VS2005代码编辑器的展开和折叠代码确实很方便和实用.以下是展开代码和折叠代码所用到的快捷键,很常用: Ctrl + M + O: 折叠所有方法 Ctrl + M + M: 折叠或者展开当前方法 C ...

  5. 运用String类实现一个模拟用户登录程序

    package Test; import java.util.Scanner; // 模拟用户登录程序 // 思路: // 1.用两个String类分别接收用户名和密码 // 2.判断输入的用户名和密 ...

  6. was部分更新

    在WAS中,应用的配置是从config/cells....目录下读取:而资源从/installedApps目录下读取 故当配置文件(例web.xml)发生改变时,只更新应用程序资源文件/install ...

  7. java_js_json_日期格式化

    调用方法: var createBeginTime= createBeginTime.Format("yyyy-MM-dd 00:00:00"); 方法: Date.prototy ...

  8. [刘阳Java]_斗胆介绍一下Eclipse快捷键大全[超详细]_第6讲

    斗胆让我在这里介绍一下Eclipse快捷键有哪些 ctrl+shirt+r 打开资源 这组快捷键可以让你开打Eclipse工作区中任何一个文件,你只需要输入你想查找的文件名字即可,而且绝对支持模糊检索 ...

  9. python学习之——安装Beautifulsoup、requests、lxml

    安装Beautiful soup: 1.下载安装包,解压到python的安装目录: 2.cmd 进入安装包解压后的存放位置: 3.使用命令:python  setup.py build   , pyt ...

  10. [goa]golang微服务框架学习--安装使用

      当项目逐渐变大之后,服务增多,开发人员增加,单纯的使用go来写服务会遇到风格不统一,开发效率上的问题. 之前研究go的微服务架构go-kit最让人头疼的就是定义服务之后,还要写很多重复的框架代码, ...