目录

0.安装node

1.安装grunt

1.1全局安装grunt-cli.

1.2局部安装grunt到当前项目的目录

1.3grunt-cli与grunt的区别

2. 全局安装

3.局部安装

4. grunt可执行文件的位置


0.安装node

首先下载并安装node, 下载地址为: https://nodejs.org/zh-cn/ .

1.安装grunt

1.1全局安装grunt-cli.

使用grunt命令之前需要首先全局安装grunt-cli.

~$ npm install grunt-cli -g

全局安装之后可以通过以下命令来查看安装的版本:

~$: grunt --version
grunt-cli v1.3.2

注意此时执行grunt --version则只会显示grunt-cli的版本,此时可以执行的grunt命令只有:

1. grunt --version

2. grunt --completion

如果执行其他grunt 命令, 比如gurnt --help 则会提示没有局部安装gurnt: ‘If you're seeing this message, grunt hasn't been installed locally to your project.’

~$: grunt --help
grunt-cli: The grunt command line interface (v1.3.2)If you're seeing this message, grunt hasn't been installed locally to
your project. For more information about installing and configuring grunt,
please see the Getting Started guide:https://gruntjs.com/getting-started

出现这个提示的原因就是还没有在当前执行gurnt命令的目录局部安装grunt。

1.2局部安装grunt到当前项目的目录

通过以下命令可以在当前目录局部安装grunt

~$npm install grunt

局部安装grunt之后可以通过grunt --version来查看安装的版本:

~$: grunt --version
grunt-cli v1.3.2
grunt v1.0.3

注意此时执行grunt --version则会同时显示grunt-cli和gurnt的版本,此时执行gurnt --help命令则会显示grunt命令的help信息:

~$: grunt --help
Grunt: The JavaScript Task Runner (v1.0.3)Usagegrunt [options] [task [task ...]]Options--help, -h  Display this help text.--base, -b  Specify an alternate base path. By default, all file paths arerelative to the Gruntfile. (grunt.file.setBase) *--no-color  Disable colored output.--gruntfile  Specify an alternate Gruntfile. By default, grunt looks in thecurrent or parent directories for the nearest Gruntfile.js orGruntfile.coffee file.--debug, -d  Enable debugging mode for tasks that support it.--stack  Print a stack trace when exiting with a warning or fatal error.--force, -f  A way to force your way past warnings. Want a suggestion? Don'tuse this option, fix your code.--tasks  Additional directory paths to scan for task and "extra" files.(grunt.loadTasks) *--npm  Npm-installed grunt plugins to scan for task and "extra" files.(grunt.loadNpmTasks) *--no-write  Disable writing files (dry run).--verbose, -v  Verbose mode. A lot more information output.--version, -V  Print the grunt version. Combine with --verbose for more info.--completion  Output shell auto-completion rules. See the grunt-clidocumentation for more information.--require  Specify a language interpreter to require first if you arewriting your Gruntfile in a language Grunt doesn't support bydefault.Options marked with * have methods exposed via the grunt API and should instead
be specified inside the Gruntfile wherever possible.Available tasks
(no tasks found)The list of available tasks may change based on tasks directories or grunt
plugins specified in the Gruntfile or via command-line options.For more information, see http://gruntjs.com/

1.3grunt-cli与grunt的区别

grunt-cli可以理解为grunt的命令行管理工具,它的作用有两个:

1. 使grunt命令可以在任意目录运行:因为全局安装的时候在/usr/local/bin目录下创建了一个可执行文件grunt

2. 查找局部安装的grunt,并将需要执行的gurnt 任务交给局部安装的grunt执行,如果没有局部安装,则会提示‘If you're seeing this message, grunt hasn't been installed locally to your project.’

grunt可以理解为grunt命令指定的任务的配置和执行者,grunt会根据当前项目的配置文件GruntFile.js完成以下工作:

1. 初始化配置任务

2. 加载任务

3. 注册任务

4. 执行任务

这里安装grunt的过程使用了全局安装和局部安装,下面我们来看一下这两种安装方式的区别.

2. 全局安装

通过npm install -g modulename可以全局安装一个npm模块,全局安装的模块可以运行在任意位置.全局安装完成了以下操作:

  1. 在/usr/local/lib目录下创建一个node_modules 目录,然后下载我们指定的包到这个目录中
  2. 如果这个包是一个命令行工具,则npm install的同时会在/usr/local/bin中创建一个指向当前模块的package.json文件中的bin属性指定的文件的link,这样在命令行中就可以执行该命令了.

以grunt-cli为例:

grunt-cli被安装到/usr/local/lib/node_modules/grunt-cli,如下图所示:

grunt命令被安装到了/usr/local/bin中:

通过图标也可以看出/usr/local/bin/grunt是一个链接文件,我们来看一下这个链接指向哪里:

~$ls -l /usr/local/bin/grunt
lrwxr-xr-x  1 root  wheel  39  1  1 22:20 /usr/local/bin/grunt -> ../lib/node_modules/grunt-cli/bin/grunt

/usr/local/bin/grunt指向/usr/local/lib/node_modules/grunt-cli/bin/grunt,所以执行grunt命令的时候其实运行的是/usr/local/lib/node_modules/grunt-cli/bin/grunt文件中的代码.

3.局部安装

通过npm install modulename可以局部安装一个模块到当前目录, 局部安装的模块只能在当前目录及其子目录下运行:

  1. 在当前目录下创建一个node_modules 目录,然后下载我们指定的包到这个目录中
  2. 如果这个包是一个命令行工具,则npm install的同时会在当前目录/node_modules /.bin中创建一个指向当前模块的package.json文件中的bin属性指定的文件的link,这样在命令行中就可以执行该命令了.

4. grunt可执行文件的位置

在linux/mac系统下,grunt命令位于/usr/local/bin目录下,因此可以在任意目录下执行grunt命令.在命令行中执行which命令可以查看grunt命令的位置:

~$which grunt
/usr/local/bin/grunt

这里的/usr/local/bin/grunt 是一个指向 /usr/local/lib/node_modules/grunt-cli/bin/grunt 链接文件, 使用ls -l 命令可以验证:

~$ls -l /usr/local/bin/grunt
lrwxr-xr-x  1 root  wheel  39  1  1 22:20 /usr/local/bin/grunt -> ../lib/node_modules/grunt-cli/bin/grunt

所以执行grunt命令其实执行的是/usr/local/lib/node_modules/grunt-cli/bin/grunt文件.

那么/usr/local/bin/grunt 是什么时候创建的呢? 我们来看grunt-cli的package.json文件,其中包含如下bin属性:

  "bin": {"grunt": "bin/grunt"},

所以在npm install -g grunt-cli的时候会在/usr/local/bin下创建一个grunt文件指向/usr/local/lib/node_modules/grunt-cli/bin/grunt.

grunt源码解析1——如何安装grunt:grunt命令是怎样运行起来的相关推荐

  1. Spark任务提交后是如何完成提交过程的?源码解析!

    Spark任务提交后是如何完成提交过程的?源码解析! 我们熟知的提交命令: spark­submit ­v ­­class xxx ­­master spark://xxx7077 .... 然后我们 ...

  2. Soul网关源码解析目录

    Soul网关源码解析目录 Soul网关源码解析文章列表     对用Java写的高性能网关:Soul,进行一波学习和研究,下面是相关的文章记录 掘金 了解与初步运行 Soul网关源码解析(一) 概览 ...

  3. *现在感觉librealsense和realsense-ros的安装挺简单的(普通X86平台)(现在发现都有两种安装方式,下载源码编译或者二进制安装)

    下面说的就是在普通X86平台上,不是在ARM平台,不在树莓派,TX2这些平台上. 之前潜意识里似乎还觉得会比较麻烦,实际我现在真正再看一下,回看一下,不是这样的.可能就像装双系统一样,实际并不麻烦,跟 ...

  4. 看看 Grunt 的源码(一):grunt-cli 源码解析

    由于将来工作需要最近学习了Grunt,至于Grunt是什么大家百度下就好了,我就不多说了.对于它内部的实现比较感兴趣,所以看了看源码.今天先来说说grunt命令行工具grunt-cli的实现. gru ...

  5. AlphaFold2源码解析(1)--安装使用

    AlphaFold2源码解析(1)–安装使用 AlphaFold2有两种安装方式: 具体可以参考我之前写的博客: Alphafold docker 安装: 参考GitHub:https://githu ...

  6. Ambari架构源码解析

    Ambari架构源码解析 一.Ambari介绍 Ambari是hadoop分布式集群配置管理工具,是由hortonworks主导的开源项目.它已经成为apache基金会的孵化器项目,已经成为hadoo ...

  7. webpack那些事:浅入深出-源码解析构建优化

    基础知识回顾 入口(entry) module.exports = {entry: './path/to/my/entry/file.js' }; //或者 module.exports = {ent ...

  8. Android之EasyPermissions源码解析

    转载请标明出处:[顾林海的博客] 个人开发的微信小程序,目前功能是书籍推荐,后续会完善一些新功能,希望大家多多支持! 前言 我们知道在Android中想要申请权限就需要在AndroidManifest ...

  9. iOS开发之Masonry框架-源码解析

    Masonry是iOS在控件布局中经常使用的一个轻量级框架.Masonry让NSLayoutConstraint使用起来更为简洁.Masonry简化了NSLayoutConstraint的使用方式,让 ...

  10. iOS开发之Masonry框架源码解析

    Masonry是iOS在控件布局中经常使用的一个轻量级框架,Masonry让NSLayoutConstraint使用起来更为简洁.Masonry简化了NSLayoutConstraint的使用方式,让 ...

最新文章

  1. mysql 创建库 5.7_MySQL数据库之MySQL5.7创建用户时报错
  2. android签名的应用-- 禁止未经授权签名的apk安装
  3. SolrPerformanceFactors--官方文档
  4. Visual Studio 2008 单元测试
  5. 为jupyter_notebook增加目录
  6. BUUCTF-- Linux Labs 1---SSH远程登陆
  7. 【python】编程语言入门经典100例--11
  8. C++ 中两个数据交换总结
  9. Atitit.常用的gc算法
  10. 计算机磁盘文件怎么加密,怎么加密磁盘-文件夹加密超级大师加密磁盘的方法 - 河东软件园...
  11. 自己做的SIP软电话
  12. 【Linux】解决shell脚本中syntax error:unexpected end of file问题
  13. 给学计算机的男友买什么礼物,毕业季,男朋友初入职场送礼好物推荐!
  14. 降维算法总结(超全!附代码)
  15. 【路径规划】基于matlab蚁群算法机器人大规模栅格地图最短路径规划【含Matlab源码 1860期】
  16. TIDB-分布式关系型数据库讲解
  17. spring boot 2.1学习笔记【异常】lombok.javac.apt.LombokProcessor could not be initialized
  18. Python打包工具
  19. 计算机基础 电子工业出版,电子工业出版社21世纪计算机基础教育系列教材2吴功宜吴英编着.ppt...
  20. 【CSS】元素的伪类

热门文章

  1. 代码查重工具SIM,添加图形界面GUI,附下载链接
  2. 媒体连接(2)...
  3. 《21天学通C++(第五版)》 [美] Jesse Liberty Bradley Jones著——个人学习笔记
  4. 程序猿生存指南-45 迁徙的鸟
  5. python检测刀具_科研一角|Python语言在人工智能加工中心机器人方面的应用
  6. 有关计算机的论文参考外文文献,最新计算机论文参考文献 计算机外文文献怎么找...
  7. STA之RC Corner
  8. 利用@media与@media screen进行响应式布局
  9. android7.1修改默认休眠时间为1分钟
  10. 浅议化学与社会的关系——兼议绿色化学重要性