Skip to content Skip to sidebar Skip to footer

How To Run Grunt Build Task By Cliking On Button Through Browser?

I have made one build process through grunt and it works fine from command prompt. Now I have a application from where i want to run this task by clicking build button. So is ther

Solution 1:

I see two options:

  1. Server-side script - like PHP, Python, etc. that launches your task.
  2. A browser plugin that uses native libraries (Start an external application from a Google Chrome Extension?).

Option one is, of course, much easier. And you don't even need a full webstack if you just want to run it without arguments or anything.

Python can be as simple as:

import socket
importsubprocesss= socket.socket( socket.AF_INET, socket.SOCK_STREAM );
s.bind( ( '0.0.0.0', 17777 ) )
s.listen()

while True:
    connection, client_address = soc.accept();
    connection.send( 'HTTP/1.1 200 OK\r\n\r\n' )
    subprocess.call( ['grunt', '..'] )
    connection.close()

Then simply hit it, like <a href="host:17777">run</a> or an XHR.

For more exotic things, like status reporting, arguments, etc. you'll need something more complex. If you already have a PHP-based stack up, you can simply do something like:

<?phpecho `grunt ...`;
?>

This will, of course wait for grunt to finish. So you may look into PHP's pcntl and the like.

Another idea, if you're using something like supervisord or ramona, which have web-based control panels, is to send requests to them so that they can start a grunt process.

So the above should suffice for a tiny use-case, not so flexible. For something more robust, extensible and serious look into celery and other task queues. You can get a quite powerful command and control center using flask for example, along with a task queue. Building one in PHP shouldn't be substantially difficult either.

Good luck.

Solution 2:

Check out the grunt-serve plugin. It seems to do exactly what you're after.

Post a Comment for "How To Run Grunt Build Task By Cliking On Button Through Browser?"