LoginSignup
1
1

More than 5 years have passed since last update.

grunt-shellでGruntでshellを実行する

Posted at

インストール

$ npm install --save-dev grunt-shell

単純にコマンドを出力する

Gruntfile.js
module.exports = function(grunt) {
    grunt.loadNpmTasks('grunt-shell');

    grunt.initConfig({
        shell: {
            hello: {
                command: 'echo hello world!'
            }
        }
    });

    grunt.registerTask('default', ['shell']);
};

実行

$ grunt

> Running "shell:makeDir" (shell) task
> hello world!

> Done, without errors.

コールバックを実行する

Gruntfile.js
module.exports = function(grunt) {

    grunt.loadNpmTasks('grunt-shell');

    grunt.registerTask('ls', function() {
        var exec = require('child_process').exec;

        var done = this.async();
        // 実行するコマンド
        var command = 'ls';
        var options = {
            timeout: 10000
        };
        var callback = function(err, stdout, stderr) {
            // コールバック処理
            console.log(stdout);
            done();
        };

        exec(command, options, callback);
    });
};
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1