Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I rename files with Grunt, based on the respective file's parent folder name?

I have a the following structure:

src/     modules/         module1/             js/                 main.js             scss/                 main.scss             index.html         module2/             js/                 main.js             scss/                 main.scss             index.html 

I'd like to run a grunt task to copy these out to the following structure:

dev/     js/         module1.js         module2.js     css/         module1.css         module2.css     module1.html     module2.html 

Is there a way to do this with an existing grunt plugin? If not, how could I achieve this?

like image 244
keirog Avatar asked Mar 07 '13 12:03

keirog


People also ask

How do I rename a file to the same name as the folder it is in?

Windows rename function This is a built-in function on Windows 10, whereby you can select all or multiple files in the folder and click the Rename button on the ribbon. This will enter the name edit mode for the last file and you can enter the new name, in this case the same name as the folder, and tap enter.

How do you rename files?

Open File Explorer by going to My Computer, or by pressing Windows Key + E on your keyboard. Find the file you want to rename, select it and select Rename on the ribbon (or press F2 on your keyboard). Type the new name you want the file to have and press Enter.


1 Answers

This can be done using the grunt-contrib-copy plugin.

The main thing to note is that you can change the destination programmatically by using a rename function (which takes in the destination and source of each file).

Here is a (somewhat brittle) sample Gruntfile.js that should copy to your desired structure:

module.exports = function(grunt) {   // Project configuration.   grunt.initConfig({     copy: {       main: {         files: [           {             expand: true,              cwd: 'src/modules/',              src: ['**/*.js'],              dest: 'dev/js/',              rename: function(dest, src) {               // use the source directory to create the file               // example with your directory structure               //   dest = 'dev/js/'               //   src = 'module1/js/main.js'               return dest + src.substring(0, src.indexOf('/')) + '.js';             }           },           {             expand: true,              cwd: 'src/modules/',              src: ['**/*.scss'],              dest: 'dev/css/',              rename: function(dest, src) {               return dest + src.substring(0, src.indexOf('/')) + '.css';             }           },           {             expand: true,              cwd: 'src/modules/',              src: ['**/*.html'],              dest: 'dev/',              rename: function(dest, src) {               return dest + src.substring(0, src.indexOf('/')) + '.html';             }           }         ]       }     }   });    grunt.loadNpmTasks('grunt-contrib-copy');    // Default task(s).   grunt.registerTask('default', ['copy']); }; 
like image 82
Gloopy Avatar answered Oct 17 '22 00:10

Gloopy