gpt4 book ai didi

javascript - Grunt - 你能从自定义注册任务中调用 grunt-contrib-copy 并动态自定义副本吗?

转载 作者:搜寻专家 更新时间:2023-10-31 22:50:25 24 4
gpt4 key购买 nike

我知道我可以在 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 的强大功能,但具有自定义注册任务的灵 active 。

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

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.file.expand在文件规范上,然后遍历生成的数组,随心所欲地复制它们。

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

关于javascript - Grunt - 你能从自定义注册任务中调用 grunt-contrib-copy 并动态自定义副本吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25129330/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com