简体   繁体   English

包括Gruntfile并在另一个文件中运行任务

[英]Include Gruntfile and run a task in an another file

I have an app using Grunt, that I launch in my terminal, and I want to run a task through an another app. 我有一个使用Grunt的应用程序,该应用程序在终端中启动,我想通过另一个应用程序运行任务。 So I'd like to know how can I include my Gruntfile.js to this other app, and run the task. 所以我想知道如何将我的Gruntfile.js包含到另一个应用程序中,并运行任务。

For now this new app is really basic, juste a simple local web page using NodeJS, with a button that launch the task. 目前,这个新应用程序确实非常基础,只需使用NodeJS创建一个简单的本地网页,并带有一个启动任务的按钮即可。

Gruntfile (I want to run the "archive" task) Gruntfile(我想运行“存档”任务)

module.exports = function (grunt) {
require('time-grunt')(grunt);
require('jit-grunt')(grunt, {
    ngtemplates: "grunt-angular-templates"
});

var Generator = require("./generator.js")(grunt);
var generator = new Generator();
generator.printLogo();

// Build
grunt.registerTask("build", function (fileType) {
    //definition of build task
    grunt.task.run(tasks);
});

// Archive Task.
grunt.registerTask("archive", ["build", "compress", "clean:post-rsync"]);
};

Other file : (I tried a require, It seems to work, but I can't run the "archive" task of the Gruntfile.) 其他文件:(我尝试了一个require,它似乎起作用了,但是我无法运行Gruntfile的“归档”任务。)

 var grunt = require('grunt');
 var gruntfile = require('./Gruntfile.js')(grunt);
 var app = express();

 app.get('/', function(req, res){
    res.render('test.ejs');
 });

 app.post('/create', function(req, res){
     //run grunt task "archive" here

     //gruntfile.grunt.registerTask("archive", ["build"]);
     res.redirect('/');
 });

 app.listen(8080);

Do you have any idea how could I run the task in my gruntfile in this other file ? 您知道如何在另一个文件的gruntfile中运行任务吗? (The function printLogo() is working so i'm sure the Gruntfile is include) (函数printLogo()正在工作,因此我确定包含了Gruntfile)

Thank you very much (I'm a beginner with Grunt so sorry if I miss something trivial) 非常感谢您(我是Grunt的初学者,如果错过了一些琐碎的事情,对不起)

You can just run a command from node. 您可以只从节点运行命令。 This way you don't have to worry about dependencies and what not. 这样,您就不必担心依赖关系,而不必担心。 You just spawn grunt, like you normally would, except programatically. 您只是像通常那样生成咕unt声,只是通过编程即可。

var spawn = require('child_process').spawn;

// This will run the 'archive' task of grunt
spawn('grunt', ['archive'], {
    cwd: 'path/to/grunt/project'
});

Grunt is a command line tool, the cleanest approach here would be to refactor your Gruntfile and extract your task's logics into a library. Grunt是一种命令行工具,这里最干净的方法是重构Gruntfile并将任务的逻辑提取到库中。

Then from your Gruntfile 's task you can call that library, and from your /create route you can also call your library. 然后,从Gruntfile的任务中,您可以调用该库,从/create路径中,您也可以调用您的库。

You can use grunt-hub plugin: 您可以使用grunt-hub插件:

grunt.initConfig({
  hub: {
    all: {
      src: ['../*/Gruntfile.js'],
      tasks: ['jshint', 'nodeunit'],
    },
  },
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM