Running a command in a Grunt Task

Asked
Active3 hr before
Viewed126 times

6 Answers

commandrunning
90%

This blog acquaints you with running Shell command through Grunt task - Grunt Shell.,    grunt.loadNpmTasks('grunt-shell');  ,    grunt.registerTask('default', ['shell']);  ,Get All Deleted SharePoint Sites From Tenant Using Power Automate

+`
88%

Alternatively you could load in grunt plugins to help this:, I think using shelljs is a great solution because it enables your node app to access the shell, and it gives you finer control over it than the grunt addons alone. – Nick Steele Oct 3 '15 at 5:11 , 12 A better approach is to use a plugin (or write your own) to keep your grunt config as config and not code. grunt-shell & grunt-exec are two examples. – papercowboy Nov 1 '12 at 23:10 , I could not immediately get grunt-shell to work with Windows+Cygwin but I had better luck with grunt-exec. – Nathan Mar 10 '14 at 18:44

grunt-shell example:

shell: {
   make_directory: {
      command: 'mkdir test'
   }
}

or grunt-exec example:

exec: {
   remove_logs: {
      command: 'rm -f *.log'
   },
   list_files: {
      command: 'ls -l **',
      stdout: true
   },
   echo_grunt_version: {
      command: function(grunt) {
         return 'echo ' + grunt.version;
      },
      stdout: true
   }
}
load more v
72%

This will put the grunt command in your system path, allowing it to be run from any directory.,For example, this will install the latest version of Grunt in your project folder, adding it to your devDependencies:,The Installing grunt guide has detailed information about installing specific, production or in-development, versions of Grunt and grunt-cli.,Every Gruntfile (and gruntplugin) uses this basic format, and all of your Grunt code must be specified inside this function:

In order to get started, you'll want to install Grunt's command line interface (CLI) globally. You may need to use sudo (for OSX, *nix, BSD etc) or run your command shell as Administrator (for Windows) to do this.

npm install - g grunt - cli
load more v
65%

Grunt.js uses power of Node.js to work and it runs from your command line. All the configuration is done in regular Javascript (or CoffeeScript).,Inside the above function we define our configuration for various tasks, load them and create custom tasks like this:,Run those tasks in the command line while you work.,Tasks can be dependent on the successful execution of other tasks.

Having Node.js is a prerequisite. First you need to install Grunt for the command line as a global module, after which you have grunt command available globally.

sudo npm install - g grunt - cli
load more v
75%

Create a file in your project root directory with the name “Gruntfile.js” and define the Grunt Task in it to run shell commands.,All the shell commands mentioned in the task has started executing.,After the successful installation of Grunt, now install Grunt Shell by running the following command.,Grunt shell is here to help us in this regard.

Gruntfile.js

module.exports = function(grunt) {
   grunt.loadNpmTasks('grunt-shell');
   grunt.initConfig({
      shell: {
         command: ["npm install bower", "bower install angular", "bower install angularjs", "bower install bootstrap", "bower install jquery", "bower install tether"].join('&&')
      }
   });
   grunt.registerTask('default', ['shell']);
};
40%

Grunt plugin for executing shell commands.,command (alias: cmd): The shell command to be executed. Must be a string or a function that returns a string.,Install grunt-exec using npm:,cwd: Current working directory of the shell command. Defaults to the directory containing your Gruntfile.

Install grunt-exec using npm:

$ npm install grunt - exec--save - dev
grunt.loadNpmTasks('grunt-exec');
exec: {
   echo_something: 'echo "This is something"'
}
exec: {
   echo_name: {
      cmd: function(firstName, lastName) {
         var formattedName = [lastName.toUpperCase(), firstName.toUpperCase()].join(', ');
         return 'echo ' + formattedName;
      }
   }
}
grunt.initConfig({
   exec: {
      remove_logs: {
         command: 'rm -f *.log',
         stdout: false,
         stderr: false
      },
      list_files: {
         cmd: 'ls -l **'
      },
      list_all_files: 'ls -la',
      echo_grunt_version: {
         cmd: function() {
            return 'echo ' + this.version;
         }
      },
      echo_name: {
         cmd: function(firstName, lastName) {
            var formattedName = [lastName.toUpperCase(), firstName.toUpperCase()].join(', ');
            return 'echo ' + formattedName;
         }
      }
   }
});

Testing

$ cd grunt - exec
$ npm test
load more v

Other "command-running" queries related to "Running a command in a Grunt Task"