简体   繁体   English

Grunt Copy任务将覆盖目标父目录

[英]Grunt Copy task overwrites destination parent directory

I'm fairly new to Grunt. 我对Grunt很陌生。 I am trying to copy each sub directory from one location and place each in another destination directory. 我正在尝试从一个位置复制每个子目录,并将每个子目录放置在另一个目标目录中。 I am doing this in a custom grunt task because I cannot be sure how many sub directories will exist in the source location. 我正在执行自定义任务,因为无法确定源位置中将存在多少个子目录。 The issue I am facing is each time a copy is performed the previous directory structure is overridden. 我面临的问题是每次执行复制时,都会覆盖以前的目录结构。

I've tried setting 'cwd' to root of the sub directories from which I am copying but the problem remains. 我尝试将“ cwd”设置为要从中复制的子目录的根目录,但问题仍然存在。

Folder structure: 资料夹结构:

container
     |
      - dist/  *I want to copy each src folder here
     |
     GruntFile.js

subapps
     |
      - thing1/dist
     |            *I want to copy each of these dirs (thing1/dist, thing2/dist) to container/dist eg: container/dist/thing1/dist, container/dist/thing2/dist
      - thing2/dist

My task: 我的任务:

grunt.registerTask('copySubApps', function () {

grunt.file.expand({filter : 'isDirectory'}, '../subapps/*/').forEach(function (subapp) {
  var subAppName = path.basename(subapp);
  var subAppDest = grunt.template.process('dist/subapps/<%= appName %>/dist/',{data:{appName:subAppName}});
  grunt.config('subAppDest', subAppDest);
  grunt.config('subAppName', subAppName);
  grunt.task.run('copy:subapp');
});

}); });

Configuration: 组态:

copy: {
  subapp: {
    expand: true,
    src: '**/*',
    dest: '<%= subAppDest %>',
    cwd: '../subapps/<%= subAppName %>/dist'
  }
}

Each subsequent copy overwrites the one preceding it. 随后的每个副本都覆盖其前面的副本。 How do I avoid this? 如何避免这种情况? I'm sure that this is a pretty naive approach so suggestions and guidance are very welcome. 我确信这是一种非常幼稚的方法,因此非常欢迎提出建议和指导。 :-) Once this is working I need to further filter which items from each sub folder are copied. :-)一旦工作,我需要进一步过滤每个子文件夹中要复制的项目。 eg subapps/thing1/style, !subapps/thing1/common etc. 例如subapps / thing1 / style,!subapps / thing1 / common等。

Thanks! 谢谢!

Sorry to answer my own question but my original understanding of Grunt task configuration was lacking. 很抱歉回答我自己的问题,但是我对Grunt任务配置缺乏最初的了解。 Hopefully this can help someone else who is struggling to understand how to create task targets dynamically. 希望这可以帮助正在努力了解如何动态创建任务目标的其他人。

Here's what I came up with: 这是我想出的:

    grunt.file.expand({filter: 'isDirectory'}, '../subapps/*/').forEach(function (subapp) {
      var subAppName = path.basename(subapp);
      var subAppDest = grunt.template.process('dist/subapps/<%= appName %>/', {data: {appName: subAppName}});
      var distWorkingDir = grunt.template.process('../subapps/<%= appName %>/', {data: {appName: subAppName}});
      var templatesWorkingDir = grunt.template.process('../subapps/<%= appName %>/src/', {data: {appName: subAppName}});
      //add a copy target for each subApp dir
      var distTarget = subAppName + '_dist';
      var templateTarget = subAppName + '_template';
      subAppTargetNames.push('copy:' + distTarget);
      subAppTargetNames.push('copy:' + templateTarget);
      subAppTargets.copy[distTarget] = {
        expand: true,
        src: 'dist/*',
        dest: subAppDest,
        cwd: distWorkingDir
      };
      subAppTargets.copy[templateTarget] = {
        expand: true,
        src: 'templates/*',
        dest: subAppDest,
        cwd: templatesWorkingDir
      };
    });
    //Merge new copy targets with existing copy configuration.
    grunt.config.merge(subAppTargets);
//Run each of the newly created copy targets
    grunt.task.run(subAppTargetNames);
  });

Basically the above does the following: 基本上,上面的操作如下:

  1. finds all sub directories of 'subapps'. 查找“ subapps”的所有子目录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM