是否有可能提示用户输入任何grunt任务

Is it possible to prompt user for input in any grunt task?

本文关键字:任何 grunt 任务 输入 用户 有可能 提示 是否      更新时间:2023-09-26

我正在尝试使用Grunt在一个项目中创建一个目录,用于博客的新帖子。它实际上会在posts目录中创建一个名为YYYYMMDDDD-PostNameInPascalCase的目录。

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

任何想法吗?

自从上次问这个问题以来已经有一段时间了,但是Github上有一个项目试图做提问者正在寻找的事情。它被称为grunt-prompt,这是url: 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
        }
      ]
    }
  },
},
})

是的,你可以这样做:

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。