2013-05-19 70 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