Javascript 是否可以在任何grunt任务中提示用户输入?

Javascript 是否可以在任何grunt任务中提示用户输入?,javascript,node.js,gruntjs,Javascript,Node.js,Gruntjs,我正在尝试使用Grunt在项目中创建一个目录,以便在博客中发布新文章。它将在名为YYYYMMDDDD PostNameInPascalCase的posts目录中创建一个目录 为了做到这一点,我必须在每次执行任务时提示用户输入post name。我知道grunt init会提示用户从项目模板创建项目,但我很好奇,对于已经建立的项目,在Gruntfile.js文件中是否有这样做的方法 有什么想法吗?是的,你可以这样做: grunt.registerTask('makePost', 'Make a n

我正在尝试使用Grunt在项目中创建一个目录,以便在博客中发布新文章。它将在名为
YYYYMMDDDD PostNameInPascalCase
posts
目录中创建一个目录

为了做到这一点,我必须在每次执行任务时提示用户输入post name。我知道grunt init会提示用户从项目模板创建项目,但我很好奇,对于已经建立的项目,在
Gruntfile.js
文件中是否有这样做的方法


有什么想法吗?

是的,你可以这样做:

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

有关如何传递某些参数的更多信息和来源,请查看。

上次提出此问题已经有一段时间了,但Github上有一个项目试图实现提问者的期望。它被称为
grunt提示符
,下面是url:。这基本上是一种将提示集成到GrunFile中的方法。从项目自述中,您可以执行以下操作:

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
        }
      ]
    }
  },
},
})
grunt.initConfig({
提示:{
目标:{
选项:{
问题:[
{
config:'config.name',//任意名称或任何其他grunt任务的配置
键入:“”、//列表、复选框、确认、输入、密码
消息:“询问用户的问题”,
默认值:'值',//如果未输入任何内容,则为默认值
选项:“数组|函数(答案)”,
验证:函数(值),//如果有效则返回true,如果无效则返回错误消息
过滤器:函数(值),//修改答案
when:function(answers)//仅当此函数返回true时询问此问题
}
]
}
},
},
})

你是说在任何地方?比如在运行grunt contrib compass之类的东西之前询问您的输入?@coma是的-没错。我感觉我在这里许愿。即使它需要修改一个特定的任务,那也没关系。你应该更新你的答案以包含新的方法:我现在使用Gulp而不是Grunt,所以如果这个答案已经过时,请随时更新它。