我如何动态生成一个文件名列表,以便在使用Grunt的任务中使用? [英] How can I dynamically generate a list of filenames for use in a task with Grunt?
问题描述
我使用 load-grunt-config 和 grunt-prompt ,我正在开发一个init任务,它在两个文件夹之间复制一些php模板。
现在模板文件名是硬编码的,但我宁愿让grunt扫描正确的文件夹并动态提供文件名。
我尝试过使用 grunt.file.expand ,但我无法让它工作。是否可以扫描文件夹并以grunt-prompt预期的格式返回文件名的数组(或对象,不知道你会怎样称呼它)?
// -------------------------------------
// Grunt提示符
// -------------------------------------
module.exports = {
// -----初始化提示----- //
初始化:{
选项:{
questions:[{
//设置作者名称
config:'init.author.name',
类型:'input',
'b $ b},{
//设置项目名称
config:'init.project.name',
type:'input ',
message:'你的项目名称是什么?'
},{
//选择要使用的模板
config:'init.php.templates',
类型:'复选框',
消息:'您想使用哪些模板?',
选择:[{
name:'404.php',
checked:false
},{
name:'archive.php',
选中:false
},{
name:'comments.php',
checked:false
]]
}]
}
}
};
顺便说一句,我找到了这个答案: https://stackoverflow.com/a/22270703/1694077 ,它涉及到这个问题。但没有详细说明如何专门解决这个问题。此外,我需要更具体的语法,而不仅仅是一个文件名数组:
[{
name:'404.php '
},{
name:'archive.php'
}]
基本原理
这是一种使用Grunt的文件匹配功能获取文件列表的方法。以下代码将在名为 templates
的子目录中查找模板。您只需将您的 php
文件放在那里,脚本就会找到它。注意我没有使用 load-grunt-config
,因为它不是获取文件列表的特定问题的一个因素。
关键是使用 grunt.file.expand
来获取文件。
module.exports = function(grunt){
//列出模板目录中的所有文件。
var templates = grunt.file.expand({filter:isFile,cwd:templates},
[*]);
//使用grunt-prompt可以使用的实际选项。
var options = templates.map(function(t){
return {name:t,checked:false};
});
grunt.initConfig({
提示符:{
init:{
options:{
questions:[{
// Set作者姓名
config:'init.author.name',
类型:'input',
message:'你叫什么名字?'
},{
//设置项目名称
config:'init.project.name',
类型:'input',
message:'你的项目名称是什么?'
$,$ {
//选择要使用的模板
config:'init.php.templates',
类型:'checkbox',
message:'你想用?',
选择:选择
}]
}
}
}
});
grunt.task.loadNpmTasks(grunt-prompt);
grunt.registerTask(default,[prompt]);
};
您可以使用比*$ c $更复杂的东西作为一种模式。例如,如果您要在其中存在其他类型的文件,而您不想列出
,则会显示*。php
。我还为过滤器
选项使用 isFile
来避免列出目录。在列出文件之前,我使用 cwd
将工作目录更改为 templates
意味着返回的文件名在它们的名字中不包含 templates /
。也可以这样做:
var templates = grunt.file.expand({filter:isFile} ,[templates / *]);
并获取包含 默认情况下, 然后在 I'm using load-grunt-config and grunt-prompt, and I'm developing an init task, which copies some php templates between two folders. Right now the template filenames are hardcoded, but I'd rather have grunt scan the right folder and provide the filenames dynamically. I've tried using grunt.file.expand, but I'm unable to get it to work. Is it possible to scan a folder and return an array (or object, not sure what you'd call it) of filenames in the format that grunt-prompt expects?
By the way, I have found this answer: https://stackoverflow.com/a/22270703/1694077, which relates to the problem. But it does not go into detail about how one would specifically address this problem. Also, I need more specific syntax than just an array of filenames:
Here's a way to do it that uses Grunt's file matching capabilities to get a list of files. The following code will seek templates in a subdirectory named The key is to use You could use something more sophisticated than and get a list of files that include the By default, The And then in
这篇关于我如何动态生成一个文件名列表,以便在使用Grunt的任务中使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!模板/ $ c的文件列表
$ b 随着
load-grunt-config
load-grunt-config
需要一个 package.json
文件(因为它调用 load-grunt-tasks
)。这是我用过的:
{
依赖关系:{
load-grunt -config:^ 0.8.0,
grunt-prompt:^ 1.1.0,
grunt:^ 0.4.4
}
Gruntfile.js
变成:
module.exports = function(grunt){
grunt.registerTask(default,[ 提示]);
require('load-grunt-config')(grunt);
};
grunt / prompt.js
你需要这个:
module.exports = function(grunt){
//列出templates目录中的所有文件。
var templates = grunt.file.expand({filter:isFile,cwd:templates},
[*]);
//使用grunt-prompt可以使用的实际选项。
var options = templates.map(function(t){
return {name:t,checked:false};
});
return {
init:{
options:{
questions:[{
//设置作者名称
config:'init .author.name',
类型:'input',
message:'你叫什么名字?'
},{
//设置项目名称
config:'init.project.name',
类型:'input',
message:'您的项目名称是什么?'
},{
/ /选择要使用的模板
config:'init.php.templates',
类型:'checkbox',
message:'您想使用哪个模板?',
选项:选项
}]
}
}
};
};
// -------------------------------------
// 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
}]
}]
}
}
};
[{
name: '404.php'
}, {
name: 'archive.php'
}]
Basic Principle
templates
. You just need to put your php
files there and the script will find it. Note I've omitted the use of load-grunt-config
since it is not a factor in the specific problem of getting a list of files.grunt.file.expand
to get the files.module.exports = function (grunt) {
// List all files in the templates directory.
var templates = grunt.file.expand({filter: "isFile", cwd: "templates"},
["*"]);
// Make actual choices out of them that grunt-prompt can use.
var choices = templates.map(function (t) {
return { name: t, checked: false};
});
grunt.initConfig({
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: choices
}]
}
}
}
});
grunt.task.loadNpmTasks("grunt-prompt");
grunt.registerTask("default", ["prompt"]);
};
"*"
as a pattern. For instance, if you are going to have other types of files there that you don't want to list "*.php"
would be indicated. I also use isFile
for the filter
option to avoid listing directories. And I use cwd
to change the working directory to templates
before listing the files, which means the file names returned do not include templates/
in their name. It would also be possible to do this instead:var templates = grunt.file.expand({filter: "isFile"}, ["templates/*"]);
templates/
directory in their name.With
load-grunt-config
load-grunt-config
wants a package.json
file (because it calls load-grunt-tasks
). This is what I've used:{
"dependencies": {
"load-grunt-config": "^0.8.0",
"grunt-prompt": "^1.1.0",
"grunt": "^0.4.4"
}
}
Gruntfile.js
becomes:module.exports = function (grunt) {
grunt.registerTask("default", ["prompt"]);
require('load-grunt-config')(grunt);
};
grunt/prompt.js
you need this:module.exports = function(grunt) {
// List all files in the templates directory.
var templates = grunt.file.expand({filter: "isFile", cwd: "templates"},
["*"]);
// Make actual choices out of them that grunt-prompt can use.
var choices = templates.map(function (t) {
return { name: t, checked: false};
});
return {
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: choices
}]
}
}
};
};