简体   繁体   中英

Grunt Copy task overwrites destination parent directory

I'm fairly new to 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.

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.

Thanks!

Sorry to answer my own question but my original understanding of Grunt task configuration was lacking. 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'.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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