Building Our Plug-in’s Logic
Our plug-in’s main goal is to let a user open a web page or a URL with Grunt. Ideally, we’d like it to work like this:
| $ grunt open:index.html |
And then it would open that page in Google Chrome. To do this, we’ll have to detect the platform we’re running on so we can find out how to launch Chrome, and we’ll have to use some mechanism to make Grunt launch an external program. Grunt comes with grunt.util.spawn, which is perfect for this.
Calling External Apps from Grunt
To call an external file from Grunt, we use Node.js’s built-in child_process module.
The exec method is a perfect fit for this situation. It takes in two arguments: the command we want to run and a callback function that executes when the ...
Get Automate with Grunt now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.