Grunt: How can I change file mode bits (chmod)?

I like to generate a shell script inside the grunt build file and set the execution bit.

In my task i do the following:

grunt.registerTask('createScript', 'Creates the script', function() {
    var ejs = require('ejs');
    //...
    grunt.file.write(
        './build/myScript.sh',
        ejs.render( grunt.file.read('myScript.sh.ejs'), { locals:myParams } )
    );
});

It seems that neither grunt.file.write nor grunt.file have an option to specify the file mode bits. (see API grunt.file)

How can I set the bits?

Because grunt runs in node, we can simply use the file system module fs of node. fs has a method chmod() / chmodSync().

The code example could than look like this:

grunt.registerTask('createScript', 'Creates the script', function() {
    var ejs = require('ejs');
    var fs = require('fs');
    //...
    grunt.file.write(
        './build/myScript.sh',
        ejs.render( grunt.file.read('myScript.sh.ejs'), { locals:myParams } )
    );
    fs.chmodSync('./build/myScript.sh', '777');
});