Rename folder templates using gruntjs command line task

I am trying to create a custom init task for a personal template in Grunt .

These are the js that generate my new project after grunt init:mytemplate

 exports.description = 'Try Grunt'; exports.warnOn = '*'; exports.template = function(grunt, init, done) { grunt.helper('prompt', {type: 'skin'}, [ grunt.helper('prompt_for', 'name', 'trygrunt'), grunt.helper('prompt_for', 'title', 'Im Trying GruntJS'), grunt.helper('prompt_for', 'author_name', 'Myself') ], function(err, props) { var files = init.filesToCopy(props); init.copyAndProcess(files, props); done(); }); }; 

Everything works fine: files and folders are correctly created or renamed from the root folder of a custom template based on rename.json info.

Question: how can I dynamically rename folders, not just files?

i.e.

 { "libs/name.js": "libs/{%= name %}.js" //this works fine "src/name": "src/{%= name %}" //this doesn't work } 
+4
source share
3 answers

The init.filesToCopy method init.filesToCopy considers renames.json for a specific file (not a directory) if it first creates a files object. It is best to programmatically change the files object between init.filesToCopy and init.copyAndProcess .

+3
source

This is possible by modifying the result of the init.filesToCopy object. However, you need to change the key, not the value of each pair.

For example, I have a lib / folder that I want to copy to application /

 var files = init.filesToCopy(props), libFolder = 'app'; // Repath the files correctly for (var file in files) { if (file.indexOf('lib/') > -1) { var path = files[file], newFile = file.replace('lib/', libFolder + '/'); files[newFile] = path; delete files[file]; } } init.copyAndProcess(files, props); 

It's also worth noting that rename.json works with lib / value, not a new folder.

One thing I did was use the props.main value to retrieve the libFolder value (for example, libFolder = props.main.split ('/') [0])

+3
source

Another way to achieve this and circumvent the limitation of the impossibility of renaming folders is to: 1) configure the grunt-contrib-copy copy task to copy folders and files and apply any names you need to the new folders / files, and then 2) perform the grunt-contrib-clean task, clean old files / folders, which will have the same network effect as renaming folders.

0
source

All Articles