Search code examples
gruntjsassemblegrunt-assemble

How to prefix file names when using grunt assemble


I have returned this question to its original form so that is more readable for future readers.

I have some grunt-assemble tasks in my Gruntfile.js

assemble: {
        options: {
            flatten: true,
            layoutdir: 'test-templates/meta_test_templates',
            partials: [
                'test-templates/general_includes/**/*.hbs', 
                'test-templates/users/**/*.hbs', 
                'test-templates/content/**/*.hbs'] 
        },
        webmaster_crud: {
            options: { layout: 'webmaster_crud.hbs' },
            dest: '../compiled-tests/content/webmaster/',
            src: ['test-templates/content/content_types/*.hbs']
        }
}

I want to prefix each of the output files with the word webmaster

So the output would be:

/compiled-tests/content/webmaster/webmaster_file1.html
/compiled-tests/content/webmaster/webmaster_file2.html
etc

The packages installed in my package.json file

"devDependencies": {
    "assemble": "^0.7.3",
    "grunt": "^0.4.5",
    "grunt-assemble": "^0.4.0",
    "grunt-contrib-clean": "^0.7.0"
}

UPDATE 19/1/16 I have included the entire assemble object passed to my grunt.initConfig and I have included the dependencies in package.json.

UPDATE 12/1/16 uncommented expand command and included ensuing error message

UPDATE 12/1/16 have returned the question to its original form sans the additional bug I discovered when not including the path variable in the function.


Solution

  • It is not entirely clear to me what you are asking for, however I think what you might be looking for is Grunt's "rename" option. The following configuration might produce the result you are hoping for:

    var path = require('path');
    
    webmaster_crud: {
        options: {
            layout: 'webmaster_crud.hbs'
        },
    
        expand: true,
        flatten: true,
        rename: function (dest, matchedSrcPath) {
            var filename = 'webmaster_' + path.basename(matchedSrcPath);
            return path.join(dest, path.dirname(matchedSrcPath), filename);
        },
    
        dest: './compiled-tests/content/webmaster/',
        src: ['test-templates/content/content_types/*.hbs']
    }