developer tip

Grunt 태스크에서 명령 실행

optionbox 2020. 8. 30. 08:11
반응형

Grunt 태스크에서 명령 실행


내가 사용하고 그런트 내 프로젝트에 (자바 스크립트 프로젝트를위한 작업 기반 명령 줄 빌드 도구). 사용자 지정 태그를 만들었고 여기에 명령을 실행할 수 있는지 궁금합니다.

명확히하기 위해 클로저 템플릿을 사용하려고하는데 "작업"은 jar 파일을 호출하여 Soy 파일을 자바 스크립트 파일로 미리 컴파일해야합니다.

명령 줄에서이 jar를 실행하고 있지만 작업으로 설정하고 싶습니다.


또는 다음을 돕기 위해 grunt 플러그인을로드 할 수 있습니다.

grunt-shell 예 :

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

또는 grunt-exec 예 :

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
  }
}

확인 grunt.util.spawn:

grunt.util.spawn({
  cmd: 'rm',
  args: ['-rf', '/tmp'],
}, function done() {
  grunt.log.ok('/tmp deleted');
});

해결책을 찾았으므로 여러분과 공유하고 싶습니다.

노드 아래에서 grunt를 사용하고 있으므로 터미널 명령을 호출하려면 'child_process'모듈이 필요합니다.

예를 들면

var myTerminal = require("child_process").exec,
    commandToBeExecuted = "sh myCommand.sh";

myTerminal(commandToBeExecuted, function(error, stdout, stderr) {
    if (!error) {
         //do something
    }
});

If you are using the latest grunt version (0.4.0rc7 at the time of this writing) both grunt-exec and grunt-shell fail (they don't seem to be updated to handle the latest grunt). On the other hand, child_process's exec is async, which is a hassle.

I ended up using Jake Trent's solution, and adding shelljs as a dev dependency on my project so I could just run tests easily and synchronously:

var shell = require('shelljs');

...

grunt.registerTask('jquery', "download jquery bundle", function() {
  shell.exec('wget http://jqueryui.com/download/jquery-ui-1.7.3.custom.zip');
});

Guys are pointing child_process, but try to use execSync to see output..

grunt.registerTask('test', '', function () {
        var exec = require('child_process').execSync;
        var result = exec("phpunit -c phpunit.xml", { encoding: 'utf8' });
        grunt.log.writeln(result);
});

For async shell commands working with Grunt 0.4.x use https://github.com/rma4ok/grunt-bg-shell.

참고URL : https://stackoverflow.com/questions/10456865/running-a-command-in-a-grunt-task

반응형