[英]Grunt - Can you call grunt-contrib-copy from within a custom registered task and customize the copy on the fly?
I know I can setup a task in grunt.config that will grunt-contrib-copy files from src to dest, and looking through Grunt docs I know I can copy single files using grunt.file.copy. 我知道我可以在grunt.config中设置一个任务,它将grunt-contrib-copy文件从src复制到dest,并查看Grunt文档,我知道我可以使用grunt.file.copy复制单个文件。
But, is it possible to create grunt-contrib-copy tasks on the fly in a custom registered task to accommodate arguments being sent from the bash? 但是,是否可以在自定义注册任务中动态创建grunt-contrib-copy任务以适应从bash发送的参数? I want to create a new directory grunt.file.mkdir("some/path/appfoldername") and then copy files to that folder from another destination, but I won't know the folder name until after the custom task is run. 我想创建一个新目录grunt.file.mkdir(“some / path / appfoldername”),然后将文件从另一个目标复制到该文件夹,但在运行自定义任务之前我不会知道文件夹名称。 So something like: 所以类似于:
grunt create_app:appfoldername
So I could copy single files over one at a time, but the files will change so want the power of grunt-contrib-copy, but with the flexibility of a custom registered task. 所以我可以一次复制一个文件,但文件会改变,所以需要grunt-contrib-copy的强大功能,但具有自定义注册任务的灵活性。
I imagine something like this if my explanation isn't clear enough: 如果我的解释不够清楚,我会想象这样的事情:
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
}
}');
}
UPDATE UPDATE
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)
);
});
}
});
Yes. 是。 Just call grunt.file.expand on the file specs and then loop over the resulting array, copying them however you like. 只需在文件规范上调用grunt.file.expand ,然后遍历生成的数组,然后根据需要复制它们。
grunt.file.expand(specs).forEach(function(src) {
grunt.file.copy(src, dest);
});
If you want this to work cross-platform, you can use Node's path API to create the file paths with correct path separators and such. 如果您希望跨平台工作,可以使用Node的路径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)
);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.