我想递归地将扩展名为.json的/src中的所有文件复制到我的/out目录中。我目前在tasks.json 中复制静态文件夹中的所有文件(无论扩展名如何(,如下所示
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "copyStatic",
"command" : "cp",
"args": ["-f", "-r", "${workspaceFolder}/src/static", "${workspaceFolder}/out/"],
}
]
}
我试着使用我在其他地方看到的/**/符号,比如这个
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "copyJson",
"command" : "cp",
"args": ["-f", "-r", "${workspaceFolder}/src/**/*.json", "${workspaceFolder}/out/"],
}
]
}
但它没有工作-得到一个错误cp: /src/**/*.json: No such file or directory
有什么想法可以在tasks.json中做到这一点吗?我想深度复制,所以包括像这样的文件
/src/foo.json --> /out/foo.json
/src/folder/bar.json --> /out/folder/bar.json
感谢
gulp
解决方案非常简单:
const gulp = require("gulp");
function copyJSONFiles() {
return gulp.src('src/**/*.json') // get all the json files from 'src' directory
.pipe(gulp.dest('out')); // move them to the workspace/out directory
}
exports.default = copyJSONFiles;
// /src/foo.json --> /out/foo.json
// /src/folder/bar.json --> /out/folder/bar.json
这个名为gulpfile.js
的文件将进入顶层的工作区文件夹。它仅通过终端中的gulp
命令触发。
将创建out
文件夹,并在其中保留src
下的文件夹结构。
正如我在10月13日的评论中所说
"command" : "find ./async -name '*.json' -exec cp --parents {} out/ ';'",
将保留src目录(此处为async
(的文件夹结构,但不幸的是,它位于out/async
之下。这就是--parents
选项的用途。
然而,不使用--parents
选项只会产生一个json文件的平面文件夹,这似乎不是您想要的。
可能有一个纯脚本版本会使父文件夹变平,从而删除其中的src
文件夹。但是,狼吞虎咽的版本非常容易。
使用cp
很难实现复杂的查询。幸运的是,默认情况下,find
会递归搜索,并且可以与-exec cp
结合使用来实际复制这些文件。
以下命令可以完成任务:
"command" : "find src/ -name "*.json" -exec cp {} out/ ;"