2013-05-15 3 views
15

테스트 용도로 node.js 코드에서 잡담 작업을 만들고 실행하고 싶습니다.명령 줄을 사용하지 않고 API로 불평 작업 실행

var foo = function() { 
    var grunt = require("grunt"); 

    var options = {"blahblah": null} // ...creating dynamic grunt options, such as concat and jshint 
    grunt.initConfig(options); 
    grunt.registerTask('default', [/*grunt subtasks*/]); 
} 

그러나 이것은 작동하지 않습니다. 그런트는 어떤 작업도 수행하지 않는 것 같습니다. 커맨드 라인없이 비정상적인 작업을 외부에서 실행할 수있는 API가 있지만 실행 방법을 모른다는 것은 거의 확실합니다.

어떤 방법이 있습니까?

답변

20

하실 수 있습니다. 나는 현재 Grunt가 명령 줄 도구이기 때문에 왜 누군가가이 작업을 수행해야하는지 알지 못합니다. 경고 : 이런 식으로 그르unt을 사용하지 않는 것이 좋습니다. 그러나 여기있다 :

var grunt = require('grunt'); 

// hack to avoid loading a Gruntfile 
// You can skip this and just use a Gruntfile instead 
grunt.task.init = function() {}; 

// Init config 
grunt.initConfig({ 
    jshint: { 
    all: ['index.js'] 
    } 
}); 

// Register your own tasks 
grunt.registerTask('mytask', function() { 
    grunt.log.write('Ran my task.'); 
}); 

// Load tasks from npm 
grunt.loadNpmTasks('grunt-contrib-jshint'); 

// Finally run the tasks, with options and a callback when we're done 
grunt.tasks(['mytask', 'jshint'], {}, function() { 
    grunt.log.ok('Done running tasks.'); 
}); 
+1

그것은 작동합니다. 고맙습니다. Grunt를 동적으로 실행하여 웹 기반 테스트 환경을 설정하기를 원했습니다. 그것을 달성하기 위해, 나는 Grunt를 nodeJS 코드에서 실행하거나, Grunt의 CLI 명령을 작성하기 위해 exec()와 같은 것을 사용해야했습니다. 나는 전자가 더 자연 스럽다고 생각했다. – Kivol

+6

나는 보통 그런 식으로 테스트하기 위해'grunt.util.spawn ({grunt : true, args : [ 'taskname']}, function() {})'을 사용합니다. Grunt는 사용자가 Grunt를 실행하는 방법과 더 밀접하게 관련됩니다. –

+0

감사! 나는 그렇게 할 것입니다. – Kivol

13

당신은 툴툴 사람들에 의해 유지되는 프로젝트 인이 작업을 수행하고있는 grunt-cli보고 코드에서 꿀꿀를 실행하는 방법에 대한 영감을 얻을 수 있습니다.

그란트는 grunt-cli/bin/grunt의 코드에서 시작되며 옵션에 대한 자세한 내용은 grunt/lib/grunt/cli.js에서 확인할 수 있습니다.

내가 같은 개인 프로젝트에서 사용 :

var grunt = require("grunt"); 
grunt.cli({ 
    gruntfile: __dirname + "/path/to/someGruntfile.js", 
    extra: {key: "value"} 
}); 

의 핵심 "추가는"여기

grunt.option("extra")로 gruntfile 내부에서 사용할 수 있습니다 것은 실행하는 다른 방법을 설명하는 bloggpost입니다 과격한 업무 : http://andrewduthie.com/2014/01/14/running-grunt-tasks-without-grunt-cli/

+0

재미있는 blogpost입니다. 여기에 대한 링크가 업데이트되었습니다 : http://andrewduthie.com/2014/01/14/running-grunt-tasks-without-grunt-cli –

관련 문제