Answer a question

I want to copy, recursively, all files from /src with the extension .json to my /out directory. I currently copy all files in my static folder (regardless of extension) like this, in tasks.json

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "copyStatic",
            "command" : "cp",
            "args": ["-f", "-r", "${workspaceFolder}/src/static", "${workspaceFolder}/out/"],
        }
    ]
}

I tried using the /**/ notation I'd seen elsewhere like this

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "copyJson",
            "command" : "cp",
            "args": ["-f", "-r", "${workspaceFolder}/src/**/*.json", "${workspaceFolder}/out/"],
        }
    ]
}

But it didn't work - got an error cp: /src/**/*.json: No such file or directory

Any ideas how to do this in tasks.json? I want to deep copy so include files like

/src/foo.json --> /out/foo.json
/src/folder/bar.json --> /out/folder/bar.json

Thanks

Answers

A gulp solution is quite easy:

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

This file, called gulpfile.js, goes into your workspace folder at the top level. It is triggered with just the gulp command in the terminal.

The out folder will be created and the folder structure under src will be preserved within it.


As I said in my comment on October 13

"command" : "find ./async -name '*.json' -exec cp --parents {} out/ ';'",

will preserve the folder structure of the src directory (here async) but unfortunately under out/async. That is the purpose of the --parents option.

However, not using the --parents options results in just a flat folder of json files which it doesn't seem you want.

There is probably a pure script version that will flatten that parent folder removing the src folder therein. But the gulp version is awfully easy.

Logo

开发云社区提供前沿行业资讯和优质的学习知识,同时提供优质稳定、价格优惠的云主机、数据库、网络、云储存等云服务产品

更多推荐