Nodejs calling Grunt directly (non-call batching)

Source: Internet
Author: User

Under Windows, we do JS build work, are accustomed to install GRUNT-CLI, only need command line grunt ... All the construction work is done automatically. This is a perfect situation, but recently to do a server version of the automated build system, in Nodejs call batch execution grunt is very dwarfish, and various problems. Is there a better, more beautiful way?

Grunt is originally the Nodejs program, after installation performance as a node_module, then what is the CLI? This is just a command-line interface written by Nodejs. Therefore, Nodejs can definitely call grunt directly at the JS level.

All we need to do is to uncover the veil of the CLI.

First, look at the location of the CLI after installation:

(Win7)

C:\Users\kenkozheng\AppData\Roaming\npm\node_modules\grunt-cli

The main CLI code is a bunch of parameter determination processing, but the final actual key point is grunt.cli ()

#!/usr/bin/env node ' use strict ';p Rocess.title= ' Grunt ';//especially badass external libs.varFindup = require (' Findup-sync '));varResolve = require (' resolve ')). sync;//Internal Libs.varOptions = require ('.. /lib/cli '). options;varCompletion = require ('.. /lib/completion ');varinfo = require ('.. /lib/info ');varPath = require (' path ');varBasedir =process.cwd ();varGruntpath;//Do stuff based on CLI options.if(' Completion 'inchoptions) {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 ();

So we go back to our own Nodejs project, first install the Grunt module, and then in the JS code to gently write the two sentences:

var grunt = require (' grunt '); Console.log (GRUNT.CLI); Grunt.cli ({    + '/applications/5/check_out /gruntfile.js '});

It's all right, it's going very well.

However, there is a small pit, tossing the Kenko a little time, that is, gruntfile must be an absolute path, not a relative path.

Finally, I have to praise the grunt code. If we do not understand the parameters of the CLI, only need Console.log (GRUNT.CLI), this is not a cold output function () {XXXX}, but a parameter description!!! Have to admire the author's intentions.

Nodejs calling Grunt directly (non-call batching)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.