将 JSON 个文件从一个目录复制到另一个目录

copy JSON files from one dir to another

在我的 web 项目中,我有以下目录结构

|- target
|- foo
|- bar
|- baz

我正在尝试编写一个 Grunt 任务,它将所有 JSON 文件从名称与提供给构建

的参数相匹配的目录复制到 target 目录中
grunt.registerTask('flavor', function(srcDir) {
  var from = './' + srcDir + '/*.json';
  var dest = './target/';

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

但是当我用

调用它时
grunt flavor:foo

我收到一个错误

Warning: Unable to write "./target/" file (Error code: EISDIR). Use --force to continue.

需要自己写任务吗?如果你不这样做,我用 grunt-contrib-copy.

实现了这一点
copy: {
            default: {
                files: [
                    {
                        expand: true,
                        src: ['foo/**', 'bar/**'],
                        dest: 'target/'
                    }
                ]
            }
        }

如@DanielApt 所述,您应该只使用 grunt-contrib-copy. To build on his answer regarding on your comment about build-parameters, you can get them to the task via grunt-option

方式一:运行 不同的目标

 grunt.initConfig({
    copy: {
        foo: {
            files: [
                {
                    'expand': 'true',
                    'cwd': 'foo/',
                    'src': [**],
                    'dest': 'dist/en'
                }
             ]
          },
        bar: {
            files: [/**/]
        },
        baz: {
            files: [/**/]
        }
    }
});

var target = grunt.option("target") || "foo";

grunt.registerTask("default", ["copy:" + target]);

// run with grunt --target=foo

方式 2:带模板的任意文件夹:

var target = grunt.option("target") || "foo";

grunt.initConfig({
    target: target,
    copy: {
        default_target: {
            files: [
                {
                    'expand': 'true',
                    'cwd': '<%= target %>/',
                    'src': [**],
                    'dest': 'dist/en'
                }
             ]
        },
    }
});

grunt.registerTask("default", ["copy"]);

// run with grunt --target=anyfolderhere