nodejs直接调用grunt(非调用批处理)
在windows下,我们做js构建工作,都习惯安装grunt-cli,只需要命令行grunt。。。一切构建工作都自动完成了。这已经是很完美的情况了,不过最近要做一个服务器版的自动化构建系统,在nodejs中调用批处理执行grunt就显得很矬,而且各种问题。是否有更好,更漂亮的方式呢?
grunt本来就是nodejs程序,安装后表现为一个node_module,那么cli是什么呢?这只是一个nodejs写的命令行界面。所以,nodejs肯定可以直接在js层面调用grunt。
我们需要做的,只需要揭开cli的面纱。
首先,看看cli安装后的位置:
(win7)
C:\Users\kenkozheng\AppData\Roaming\npm\node_modules\grunt-cli
cli主要代码是一堆参数判断处理,但最终实际关键点是grunt.cli( )
#!/usr/bin/env node 'use strict'; process.title = 'grunt'; // Especially badass external libs.
var findup = require('findup-sync');
var resolve = require('resolve').sync; // Internal libs.
var options = require('../lib/cli').options;
var completion = require('../lib/completion');
var info = require('../lib/info');
var path = require('path'); var basedir = process.cwd();
var gruntpath; // Do stuff based on CLI options.
if ('completion' in options) {
completion.print(options.completion);
} else if (options.version) {
info.version();
} else if (options.base && !options.gruntfile) {
basedir = path.resolve(options.base);
} else if (options.gruntfile) {
basedir = path.resolve(path.dirname(options.gruntfile));
} try {
gruntpath = resolve('grunt', {basedir: basedir});
} catch (ex) {
gruntpath = findup('lib/grunt.js');
// No grunt install found!
if (!gruntpath) {
if (options.version) { process.exit(); }
if (options.help) { info.help(); }
info.fatal('Unable to find local grunt.', 99);
}
} // Everything looks good. Require local grunt and run it.
require(gruntpath).cli();
那么我们回到自己的nodejs项目中,先安装好grunt模块,然后在js代码中轻轻写上两句:
var grunt = require('grunt');
console.log(grunt.cli);
grunt.cli({
gruntfile: __dirname + '/applications/5/check_out/Gruntfile.js'
});
一切就搞掂了,非常顺利。
不过,这里有个小坑,折腾了kenko一点时间,就是gruntfile必须是绝对路径,不能是相对路径。
最后,不得不赞一下grunt的代码。如果我们不懂cli的参数,只需要console.log(grunt.cli),这不是冷冰冰的输出function(){xxxx},而是一份参数说明!!!不得不佩服作者的用心良苦。
nodejs直接调用grunt(非调用批处理)的更多相关文章
- JAVA 遍历文件夹下的所有文件(递归调用和非递归调用)
JAVA 遍历文件夹下的所有文件(递归调用和非递归调用) 1.不使用递归的方法调用. public void traverseFolder1(String path) { int fileNum = ...
- [复习] JAVA 遍历目录 (递归调用和非递归)
JAVA 遍历文件夹下的所有文件(递归调用和非递归调用) 1.不使用递归的方法调用. public void traverseFolder1(String path) { int fileNum = ...
- python绑定调用和非绑定调用
绑定调用和非绑定调用 在python中,绑定调用和非绑定调用其实是相对于类和实例来说的.抽象点说就是:在类实例化过程中,类的方法会绑定在实例之中,此时,这个实例会拥有这个类的具体属性和方法,这些属性和 ...
- nodejs进阶(2)—函数模块调用
函数调用 1. 文件内普通函数调用 创建一个js文件命名为2_callFunction.js,其中定义一个函数fun1,向返回对象输出了一段字符串“你好,我是fun1”. //------------ ...
- 【VB技巧】VB静态调用与动态调用dll详解
本文“[VB技巧]VB静态调用与动态调用dll详解”,来自:Nuclear'Atk 网络安全研究中心,本文地址:http://lcx.cc/?i=489,转载请注明作者及出处! [[请注意]]:在以下 ...
- Dubbo学习笔记4:服务消费端泛化调用与异步调用
本文借用dubbo.learn的Dubbo API方式来解释原理. 服务消费端泛化调用 前面我们讲解到,基于Spring和基于Dubbo API方式搭建简单的分布式系统时,服务消费端引入了一个SDK二 ...
- 《oracle每天一练》触发器不能调用或间接调用COMMIT,ROLLBACK等DCL语句
触发器不能调用或间接调用COMMIT,ROLLBACK等DCL语句 在触发器中不能运行 ddl语句和commit,rollback语句 ddl语句:DDL语句用语定义和管理数据库中的对象,如Creat ...
- 反射-优化及程序集等(用委托的方式调用需要反射调用的方法(或者属性、字段),而不去使用Invoke方法)
反射-优化及程序集等(用委托的方式调用需要反射调用的方法(或者属性.字段),而不去使用Invoke方法) 创建Delegate (1).Delegate.CreateDelegate(Type, ...
- java三种调用方式(同步调用/回调/异步调用)
1:同步调用:一种阻塞式调用,调用方要等待对方执行完毕才返回,它是一种单向调用 2:回调:一种双向调用模式,也就是说,被调用方在接口被调用时也会调用对方的接口: 3:异步调用:一种类似消息或事件的机制 ...
随机推荐
- C++MFC编程笔记day05 文档类-单文档和多文档应用程序
文档类 1 相关类 CDocument类-父类是CCmdTarget类,所以,文档类也能够处理菜单等 命令消息. 作用保存和管理数据. 注意事项:怎样解决断言错 ...
- 制作高仿QQ的聊天系统(下)—— Adapter & Activity
一.适配器 1.1 分页显示数据 因为聊天信息数目很多,所以adpter需要做分页处理,这里的分页处理是我自己实现的,如果有更好的办法欢迎在评论中告知.我们从友盟的反馈SDK中能得到聊天的list,我 ...
- Toast的用法(可以设置显示时间,自定义布局的,线程中的Toast)
自定义的Toast类 布局文件 <?xml version="1.0" encoding="utf-8"?> <LinearLa ...
- MECE分析法(Mutually Exclusive Collectively Exhaustive)
什么是MECE分析法? MECE,是Mutually Exclusive Collectively Exhaustive,中文意思是“相互独立,完全穷尽”. 也就是对于一个重大的议题,能够做到不重叠. ...
- Generalized normal distribution and Skew normal distribution
Density Function The Generalized Gaussian density has the following form: where (rho) is the " ...
- [转]php的public、protected、private三种访问控制模式的区别
FROM : http://blog.163.com/weiwenjuan_bj/blog/static/14035033620129304183850/?suggestedreading publi ...
- input type=file 选择图片并且实现预览效果的实例
为大家带来一篇input type=file 选择图片并且实现预览效果的实例. 通过<input />标签,给它指定type类型为file,可提供文件上传: accept:可选择上传类型, ...
- go语言之进阶篇拷贝文件案例
1.文件案例:拷贝文件 示例: package main import ( "fmt" "io" "os" ) func main() { ...
- Poly2Tri介绍[转]
https://blog.csdn.net/xys206006/article/details/83002326 这是Poly2Tri介绍的翻译博文.原文链接:http://sites-final.u ...
- [leetcode]Pascal's Triangle II @ Python
原题地址:https://oj.leetcode.com/problems/pascals-triangle-ii/ 题意: Given an index k, return the kth row ...