2013-05-19 14 views
8

我正在尝试使用Grunt在项目中为博客中的新帖子制作一个目录。它基本上会在名为YYYYMMDDDD-PostNameInPascalCaseposts目录内创建一个目录。是否可以在任何咕噜任务中提示用户输入?

为了做到这一点,我必须在每次执行任务时提示用户输入帖子名称。我知道grunt-init提示用户从项目模板中创建项目,但我很好奇,如果有一种方法可以在Gruntfile.js文件中为已建立的项目执行此操作。

有什么想法?

+0

你的意思是任何?就像在运行像grunt-contrib-compass之类的软件之前询问你的输入一样? – coma

+0

@coma是 - 确切地说。我有一种感觉,我很想在这里。即使它需要修改一个特定的任务,那也可以。 – mbeasley

回答

13

上次问这个问题已经有一段时间了,但Github上有一个项目试图去做问题的人。它被称为grunt-prompt,这里是网址:https://github.com/dylang/grunt-prompt。这基本上是一种将提示集成到Gruntfile中的方法。从项目自述你会做这样的事情:

grunt.initConfig({ 
    prompt: { 
    target: { 
     options: { 
     questions: [ 
     { 
      config: 'config.name', // arbitray name or config for any other grunt task 
      type: '<question type>', // list, checkbox, confirm, input, password 
      message: 'Question to ask the user', 
      default: 'value', // default value if nothing is entered 
      choices: 'Array|function(answers)', 
      validate: function(value), // return true if valid, error message if invalid 
      filter: function(value), // modify the answer 
      when: function(answers) // only ask this question when this function returns true 
     } 
     ] 
    } 
    }, 
}, 
}) 
+1

您应该更新您的答案,以包含新的[然后](http://jsfiddle.net/xoLbweys)方法:https://github.com/dylang/grunt-prompt#release-history –

+1

我现在用Gulp代替Grunt,所以如果这个答案已经过时,请随时更新它。 –

1

是的,你可以做这样的事情:

grunt.registerTask('makePost', 'Make a new post dir.', function(n) { 
    if (n == null) { 
    grunt.log.warn('Post name must be specified, like makePost:PostNameGoesHere.'); 
    } 

    // Run other tasks here 
    grunt.task.run('foo:' + n, 'bar:' + n, 'baz:' + n); 
}); 

有关如何传递一些参数查看详细信息和源看看Grunt FAQ