[英]Grunt - Can you call grunt-contrib-copy from within a custom registered task and customize the copy on the fly?
我知道我可以在grunt.config中设置一个任务,它将grunt-contrib-copy文件从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
}
}');
}
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)
);
});
}
});
是。 只需在文件规范上调用grunt.file.expand ,然后遍历生成的数组,然后根据需要复制它们。
grunt.file.expand(specs).forEach(function(src) {
grunt.file.copy(src, dest);
});
如果您希望跨平台工作,可以使用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.