Bower是一个客户端技术的软件包管理器,它可用于搜索、安装和卸载如JavaScript、HTML、CSS之类的网络资源。
为了安装bower,你首先需要安装如下文件:
在 Windows 上安装 Git 同样轻松,有个叫做 msysGit 的项目提供了安装包,可以到 GitHub 的页面上下载 exe 安装文件并运行:
完成安装之后,就可以使用命令行的 git
工具(已经自带了 ssh 客户端)了,另外还有一个图形界面的 Git 项目管理工具。
一旦你已经安装了上面所说的所有必要文件,进入window下的cmd或Git下的命令栏键入以下命令安装Bower:
npm install -g bower
安装完bower之后就可以使用所有的bower命令了。可以键入help 命令来查看bower可以完成那些操作,如下:
Usage:
bower <command> [<args>] [<options>]
Commands:
cache Manage bower cache
help Display help information about Bower
home Opens a package homepage into your favorite browser
info Info of a particular package
init Interactively create a bower.json file
install Install a package locally
link Symlink a package folder
list List local packages - and possible updates
login Authenticate with GitHub and store credentials
lookup Look up a single package URL by name
prune Removes local extraneous packages
register Register a package
search Search for packages by name
update Update a local package
uninstall Remove a local package
unregister Remove a package from the registry
version Bump a package version
Options:
-f, --force Makes various commands more forceful
-j, --json Output consumable JSON
-l, --loglevel What level of logs to report
-o, --offline Do not hit the network
-q, --quiet Only output important information
-s, --silent Do not output anything, besides errors
-V, --verbose Makes output more verbose
--allow-root Allows running commands as root
-v, --version Output Bower version
--no-color Disable colors
See 'bower help <command>' for more information on a specific command.
Bower是一个软件包管理器,所以你可以在应用程序中用它来安装新的软件包。举例来看一下来如何使用Bower安装JQuery,在你想要安装该包的地方创建一个新的文件夹,然后用命令切换到你所建的文件夹(cmd下用【磁盘:】来切换磁盘,Git Bash里用【cd 磁盘:】切换,切换文件夹都是【cd 文件夹名】),注:【】不需要打,再键入如下命令:
bower install jquery
上述命令完成以后,你会在你刚才创建的目录下看到一个bower_components的文件夹,其中目录如下:
$ tree bower_components/ bower_components/ └── jquery ├── README.md ├── bower.json ├── component.json ├── composer.json ├── jquery-migrate.js ├── jquery-migrate.min.js ├── jquery.js ├── jquery.min.js ├── jquery.min.map └── package.json 1 directory, 10 files
包的使用
现在就可以在应用程序中使用jQuery包了,在jQuery里创建一个简单的html5文件:<!doctype html> <html> <head> <title>Learning Bower</title> </head> <body> <button>Animate Me!!</button> <div style="background:red;height:100px;width:100px;position:absolute;"> </div> <script type="text/javascript" src="bower_components/jquery/jquery.min.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("button").click(function(){ $("div").animate({left:'250px'}); }); }); </script> </body> </html>
正如你所看到的,你刚刚引用jquery.min.js文件,现阶段完成。所有包的列表
如果你想找出所有安装在应用程序中的包,可以使用list命令:
$ bower list bower check-new Checking for new versions of the project dependencies.. blog /Users/shekhargulati/day1/blog └── jquery#2.0.3 extraneous包的搜索
假如你想在你的应用程序中使用twitter的bootstrap框架,但你不确定包的名字,这时你可以使用search 命令:
$ bower search bootstrap Search results: bootstrap git://github.com/twbs/bootstrap.git angular-bootstrap git://github.com/angular-ui/bootstrap-bower.git sass-bootstrap git://github.com/jlong/sass-twitter-bootstrap.git包的信息
如果你想看到关于特定的包的信息,可以使用info 命令来查看该包的所有信息:
$ bower info bootstrap bower bootstrap#* not-cached git://github.com/twbs/bootstrap.git#* bower bootstrap#* resolve git://github.com/twbs/bootstrap.git#* bower bootstrap#* download https://github.com/twbs/bootstrap/archive/v3.0.0.tar.gz bower bootstrap#* extract archive.tar.gz bower bootstrap#* resolved git://github.com/twbs/bootstrap.git#3.0.0 { name: 'bootstrap', version: '3.0.0', main: [ './dist/js/bootstrap.js', './dist/css/bootstrap.css' ], ignore: [ '**/.*' ], dependencies: { jquery: '>= 1.9.0' }, homepage: 'https://github.com/twbs/bootstrap' } Available versions: - 3.0.0 - 3.0.0-rc1 - 3.0.0-rc.2 - 2.3.2 .....如果你想得到单个包的信息,也可以使用info 命令:
$ bower info bootstrap#3.0.0 bower bootstrap#3.0.0 cached git://github.com/twbs/bootstrap.git#3.0.0 bower bootstrap#3.0.0 validate 3.0.0 against git://github.com/twbs/bootstrap.git#3.0.0 { name: 'bootstrap', version: '3.0.0', main: [ './dist/js/bootstrap.js', './dist/css/bootstrap.css' ], ignore: [ '**/.*' ], dependencies: { jquery: '>= 1.9.0' }, homepage: 'https://github.com/twbs/bootstrap'包的卸载
卸载包可以使用uninstall 命令:
$ bower uninstall jquerybower.json文件的使用
bower.json文件的使用可以让包的安装更容易,你可以在应用程序的根目录下创建一个名为“bower.json”的文件,并定义它的依赖关系。使用bower init 命令来创建bower.json文件:
$ bower init [?] name: blog [?] version: 0.0.1 [?] description: [?] main file: [?] keywords: [?] authors: Shekhar Gulati <shekhargulati84@gmail.com> [?] license: MIT [?] homepage: [?] set currently installed components as dependencies? Yes [?] add commonly ignored files to ignore list? Yes [?] would you like to mark this package as private which prevents it from being accidentally published to the registry? No { name: 'blog', version: '0.0.1', authors: [ 'Shekhar Gulati <shekhargulati84@gmail.com>' ], license: 'MIT', ignore: [ '**/.*', 'node_modules', 'bower_components', 'test', 'tests' ], dependencies: { jquery: '~2.0.3' } } [?] Looks good? Yes可以查看该文件:
{ "name": "blog", "version": "0.0.1", "authors": [ "Shekhar Gulati <shekhargulati84@gmail.com>" ], "license": "MIT", "ignore": [ "**/.*", "node_modules", "bower_components", "test", "tests" ], "dependencies": { "jquery": "~2.0.3" } }注意看,它已经加入了jQuery依赖关系。
现在假设也想用twitter bootstrap,我们可以用下面的命令安装twitter bootstrap并更新bower.json文件:
$ bower install bootstrap --save它会自动安装最新版本的bootstrap并更新bower.json文件:
"name": "blog", "version": "0.0.1", "authors": [ "Shekhar Gulati <shekhargulati84@gmail.com>" ], "license": "MIT", "ignore": [ "**/.*", "node_modules", "bower_components", "test", "tests" ], "dependencies": { "jquery": "~2.0.3", "bootstrap": "~3.0.0" } }