如何动态生成用于Grunt任务的文件名列表?

7 javascript node.js gruntjs

我正在使用load-grunt-configgrunt-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
        }]
      }]
    }
  }
};
Run Code Online (Sandbox Code Playgroud)

顺便说一句,我找到了这个答案:https://stackoverflow.com/a/22270703/1694077,这与问题有关.但它没有详细说明如何专门解决这个问题.另外,我需要比文件名数组更具体的语法:

[{
  name: '404.php'
}, {
  name: 'archive.php'
}]
Run Code Online (Sandbox Code Playgroud)

Lou*_*uis 11

基本原则

这是一种使用Grunt的文件匹配功能来获取文件列表的方法.以下代码将在名为的子目录中查找模板templates.您只需将php文件放在那里,脚本就会找到它.注意我省略了使用,load-grunt-config因为它不是获取文件列表的特定问题的一个因素.

关键是用来grunt.file.expand获取文件.

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"]);
};
Run Code Online (Sandbox Code Playgroud)

你可以使用比"*"模式更复杂的东西.例如,如果您要在其中指定其他类型的文件,则表示您不想列出"*.php".我还使用isFilefilter选项来避免列出目录.我用cwd改变工作目录templates 之前上市的文件,这意味着返回做文件名包含templates/在他们的名字.也可以这样做:

var templates = grunt.file.expand({filter: "isFile"}, ["templates/*"]);
Run Code Online (Sandbox Code Playgroud)

并获取包含templates/其名称中的目录的文件列表.

load-grunt-config

默认情况下,load-grunt-config想要一个package.json文件(因为它调用load-grunt-tasks).这是我用过的:

{
  "dependencies": {
    "load-grunt-config": "^0.8.0",
    "grunt-prompt": "^1.1.0",
    "grunt": "^0.4.4"
  }
}
Run Code Online (Sandbox Code Playgroud)

Gruntfile.js变为:

module.exports = function (grunt) {

    grunt.registerTask("default", ["prompt"]);
    require('load-grunt-config')(grunt);
};
Run Code Online (Sandbox Code Playgroud)

然后在grunt/prompt.js你需要这个:

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
                }]
            }
        }
    };
};
Run Code Online (Sandbox Code Playgroud)