How to use grunt to copy a series of files to the same directory they come from?

17 Views Asked by At

I have a series of directories like...

component A
 - t9n
   - componentAT9n.json
component B
 - t9n
   - componentBT9N.json

Where each of these directories I need to duplicate the one file ending in t9n.json to _en.json ultimately ending up with...

component A
 - t9n
   - componentAT9n.json
   - componentAT9n_en.json
component B
 - t9n
   - componentBT9N.json
   - componentBT9n_en.json

Using grunt is how I'm trying to do it, but I'm having a hard time figuring out how to copy each file it matches to the same directory.

Is this something I can accomplish just using grunt and grunt-contrib-copy ? Or is there maybe another plugin to do this?

In the copy task, I know I can use glob patterns to dynamically grab the source, but the destination I'm unsure of.

files: [
  {
    cwd: "src/app/js",
    src: ["**/t9n/*.json"],
    dest: ???,
    expand: true
  }
]
1

There are 1 best solutions below

0
Carson Wood On

This situation ended up being just too complicated that I had to make a custom grunt task for it, this is what I did.

      // Copies the root t9n file for each t9n directory to an _en.json file
      grunt.registerTask("create-en-t9n-files", function() {
        grunt.file
          .expand("path/to/files/**/t9n/t9n+([A-Za-z0-9]).json")
          .forEach(function(source) {
            var destinationPath = source.split("/t9n/")[0] + "/t9n/";
            var fileName = source.split("/t9n/")[1].split(".json")[0];
            var enName = fileName + "_en.json";
            grunt.file.copy(source, destinationPath + enName);
          });
      });