grunt-run-grunt

1.0.1 • Public • Published

grunt-run-grunt

Build Status Coverage Status Dependency Status npm version

Grunt task to run Gruntfiles in a child process. Gruntception!

Console output capture is not reliable on Windows as Node.js there doesn't always flush buffers before exiting. Until this is fixed Windows users should use the Vagrantfile instead (see below). ‼️

Getting Started

This plugin requires Grunt >=1.0.0

If you haven't used Grunt before, be sure to check out the Getting Started guide, as it explains how to create a Gruntfile as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:

npm install grunt-run-grunt --save-dev

Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:

grunt.loadNpmTasks('grunt-run-grunt');

The "run_grunt" task

"Yo dawg! I herd you like grunt, so I put some grunt in your grunt so you can grunt while you grunt." 😆

Use the run_grunt task to spawn new processes that run grunt-cli and optionally do work on the result data. It will use the global $ grunt command, just like when you'd run grunt manually.

Main use-case is testing your gruntfile or grunt-plugins, but it is also suited for creative use of gruntfiles and grunt-cli output.

For example use it to verify the final output of various reporters and formatters. Alternately parse the output of the "$grunt --help" command and work with the list of tasks and aliases (without instrumenting the gruntfile in any way).

If you need something similar to run grunt in a production build environment or don't really care about the content of the cli output then you are probably looking for grunt-hub instead. If you need to run tasks from one Gruntfile concurrently use grunt-concurrent or grunt-parallel.

In the future there will also be a way to use this as a standard Node.js module, so you can run grunt from inside standard scripts. Why? Who knows?

Usage

In your project's Gruntfile, add a section named run_grunt to the data object passed into grunt.initConfig().

grunt.initConfig({
  run_grunt: {
    options: {
      minimumFiles: 2
    },
    simple_target: {
      options: {
        log: false,
        process: function (res) {
          if (res.fail) {
            res.output = 'new content'
            grunt.log.writeln('bork bork');
          }
        }
      },
      src: ['Gruntfile.js', 'other/Gruntfile.js']
    },
  },
})

Options

grunt-cli options

help: boolean,
base: 'string',
'no-color': boolean,
debug: boolean,
stack: boolean,
force: boolean,
tasks: boolean,
npm: 'string',
'no-write': boolean,
verbose: boolean,
version: boolean

Custom options

// pass the tasks to run: either a string or array-of-strings
task: ['clean', 'jshint:strictTarget', 'mocha:subTarget']
 
// process the result data object
process: function (result) {
  // see below for result structure
},
 
// log child console output
log: true,
 
// indent child console output with this string
indentLog: '  |  ',
 
// save raw output to file
logFile: null,
 
// modify env variables
env: {},
 
// apply an output parser (see below for values)
parser: '',
 
// how many parallel grunts to run
concurrent: <number> cpu-cores,
 
// expect at least this many files
minimumFiles: 1,
 
// change the cwd of the gruntfile
cwd: null,
 
//--- experimental options
 
// generate cli command
debugCli: false,
 
// save .bat and shellscripts
writeShell: null,
 
// don't fail
expectFail: false
 
// pass options that will be available in the executed gruntfile with grunt.option('myOption')
// NOTE: will overwrite cli options with the same name!
gruntOptions: {}

Process result object

// status, can override
fail: false,
output: 'string',
 
// parsed data
parsed: {
  'parseHelp' : {}
  ..
},
 
// raw grunt.util.spawn callback arguments
error,
res,
code,
 
// used parameters
src: 'path/to/Gruntfile.js',
cwd: 'string',
tasks: [],
options: {},
 
//cleaned src
gruntfile: 'Gruntfile',
 
// timing
start: Date.now(),
end: 0,
duration: 0

Parsers

parseHelp

Parse grunt -h output

  • Use the process option to access the data.
  • Returns result.parsed.parseHelp object with task and alias names.
  • Requires help option (in a later version this will be forced)

Release History

See the CHANGELOG.

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using Grunt.

Development

Requirements

There is a Vagrantfile and set of Chef cookbooks to use with Vagrant for easy testing on a Linux VM. It will install a node.js from nvm, install the dependencies and enable grunt.

$ bundle install
$ librarian-chef install
$ vagrant up

License

Copyright (c) 2017 Bart van der Schoor

Licensed under the MIT license.

Package Sidebar

Install

npm i grunt-run-grunt

Weekly Downloads

211

Version

1.0.1

License

MIT

Unpacked Size

21.8 kB

Total Files

9

Last publish

Collaborators

  • alexey
  • bartvds