Javascript Grunt-能否从自定义注册任务中调用Grunt contrib copy并动态自定义副本?

Javascript Grunt-能否从自定义注册任务中调用Grunt contrib copy并动态自定义副本?,javascript,node.js,bash,gruntjs,grunt-contrib-copy,Javascript,Node.js,Bash,Gruntjs,Grunt Contrib Copy,我知道我可以在grunt.config中设置一个任务,该任务将grunt contrib将文件从src复制到dest,并且查看grunt文档,我知道我可以使用grunt.file.copy复制单个文件 但是,是否可以在自定义注册的任务中动态创建grunt contrib copy任务,以适应从bash发送的参数?我想创建一个新目录grunt.file.mkdir(“some/path/appfoldername”),然后将文件从另一个目标复制到该文件夹,但在运行自定义任务之前,我不知道文件夹名称

我知道我可以在grunt.config中设置一个任务,该任务将grunt contrib将文件从src复制到dest,并且查看grunt文档,我知道我可以使用grunt.file.copy复制单个文件

但是,是否可以在自定义注册的任务中动态创建grunt contrib copy任务,以适应从bash发送的参数?我想创建一个新目录grunt.file.mkdir(“some/path/appfoldername”),然后将文件从另一个目标复制到该文件夹,但在运行自定义任务之前,我不知道文件夹名称。比如:

grunt create_app:appfoldername
因此,我可以一次复制一个以上的文件,但文件会发生变化,因此我需要grunt contrib copy的功能,但具有自定义注册任务的灵活性

如果我的解释不够清楚,我会这样想:

grunt.registerTask('createCopy', 'Create a copy', function(folderName) {

    var cwd = grunt.template.process("some/path/to/app");
    var src = grunt.template.process("some/path/to/src");
    var dest = grunt.template.process("some/path/to/dest") + folderName;

    grunt.task.run('copy: {  
       files: { 
          cwd: cwd,
          src: src,
          dest: dest
       }           
    }');
}
更新

grunt.registerTask('mkapp', 'Create Application', function(appName) {

    // Check appName is not empty or undefined
    if(appName !== "" && appName !== undefined) { 

        // Require node path module, since not a global
        var path = require("path");

        // Resolve directory path using templates
        var relPath = grunt.template.process("<%= root %>/<%= dirs.src %>/application/");
        var srcPath = relPath + "default_install";
        var destPath = relPath + appName;

        // Create new application folder
        grunt.file.mkdir(destPath, 0444);

        // Return unique array of all file paths which match globbing pattern
        var options = { cwd: srcPath, filter: 'isFile' };
        var globPattern = "**/*"

        grunt.file.expand(options, globPattern).forEach(function(srcPathRelCwd) {

            // Copy a source file to a destination path, creating directories if necessary
            grunt.file.copy(
                path.join(srcPath, srcPathRelCwd), 
                path.join(destPath, srcPathRelCwd)
            );
        });
    }
});
grunt.registerTask('mkapp','Create Application',函数(appName){
//检查appName是否为空或未定义
如果(appName!==“”&&appName!==未定义){
//需要节点路径模块,因为不是全局路径
var路径=要求(“路径”);
//使用模板解析目录路径
var relPath=grunt.template.process(“//application/”);
var srcPath=relPath+“默认安装”;
var destPath=relPath+appName;
//创建新的应用程序文件夹
mkdir(destPath,0444);
//返回匹配全局模式的所有文件路径的唯一数组
var options={cwd:srcPath,过滤器:'isFile'};
var globPattern=“***”
grunt.file.expand(options,globPattern.forEach)(函数(srcPathRelCwd){
//将源文件复制到目标路径,必要时创建目录
grunt.file.copy(
join(srcPath,srcPathRelCwd),
join(destPath,srcPathRelCwd)
);
});
}
});
是。只需调用文件规格,然后在生成的数组上循环,随意复制它们

grunt.file.expand(specs).forEach(function(src) {
  grunt.file.copy(src, dest);
});

如果希望跨平台工作,可以使用节点的路径API创建具有正确路径分隔符等的文件路径

grunt.file.expand(options, globPattern).forEach(function(srcPathRelCwd) {
    // Copy a source file to a destination path, creating directories if necessary
    grunt.file.copy(
        path.join(srcPath, srcPathRelCwd), 
        path.join(destPath, srcPathRelCwd)
    );
});

嗨@ryanb,我尝试了一些变体,但我不知道应该设置哪些选项?即使使用了,我还是在上面添加了代码,以防万一。如果我删除grunt.file.copy(src,destPath)并保留writeln(),它会显示所有的文件路径,我也可以写出destPath。它似乎不喜欢这些文件夹?一直说“警告:无法读取'cache'文件(错误代码:enoint)”,但当我删除缓存文件夹时,它会在下一个文件夹配置中抛出一个错误。明白了!呸,对于任何想要复制此解决方案的人,在上面的更新中发布结果。感谢@ryanband,不要忘记join不是一个全局模块,需要使用var path=require(“path”);