【发布时间】:2015-12-24 01:52:44
【问题描述】:
我正在使用load-grunt-config 和grunt-prompt,我正在开发一个init 任务,它在两个文件夹之间复制一些php 模板。
现在模板文件名是硬编码的,但我宁愿 grunt 扫描正确的文件夹并动态提供文件名。
我尝试过使用grunt.file.expand,但我无法让它工作。是否可以扫描一个文件夹并以 grunt-prompt 期望的格式返回一个文件名数组(或对象,不确定你会怎么称呼它)?
// -------------------------------------
// Grunt prompt
// -------------------------------------
module.exports = {
// ----- Initialization prompt ----- //
init: {
options: {
questions: [{
// Set the authors name
config: 'init.author.name',
type: 'input',
message: 'What is your name?'
}, {
// Set the name of the project
config: 'init.project.name',
type: 'input',
message: 'What is the name of your project?'
}, {
// Select templates to be used
config: 'init.php.templates',
type: 'checkbox',
message: 'Which templates do you want to use?',
choices: [{
name: '404.php',
checked: false
}, {
name: 'archive.php',
checked: false
}, {
name: 'comments.php',
checked: false
}]
}]
}
}
};
顺便说一句,我找到了这个答案:https://stackoverflow.com/a/22270703/1694077,这与问题有关。但它没有详细说明如何具体解决这个问题。此外,我需要更具体的语法,而不仅仅是文件名数组:
[{
name: '404.php'
}, {
name: 'archive.php'
}]
【问题讨论】:
-
你为什么不自己做呢?
-
@Vinz243;你的意思是手动提供文件列表?好吧,我已经在这样做了(正如您在代码中看到的那样),但它相当脆弱,因为有人可能会不小心删除文件或添加文件。这会导致提示和任何后续的 grunt 任务出现意外行为。
-
@Vinz243;你是什么意思?这已经是一项艰巨的任务了。
-
供您扫描。或者我没有真正理解你的问题。
-
@Vinz243;啊对。好吧,据我所知,没有一个繁重的任务可以做到这一点。我认为有一个内置函数可以执行此操作(grunt.file.expand)。但我不知道在这种情况下如何使用它..
标签: javascript node.js gruntjs