Grunt:我怎样才能更改文件模式位(chmod)?

时间:2013-02-13 10:32:29

标签: node.js gruntjs

我喜欢在grunt构建文件中生成一个shell脚本并设置执行位。

在我的任务中,我执行以下操作:

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 } )
    );
});

似乎grunt.file.writegrunt.file都没有指定文件模式位的选项。 (见API grunt.file

如何设置位?

1 个答案:

答案 0 :(得分:1)

因为grunt在节点中运行,我们可以简单地使用节点的文件系统模块fsfs有一个方法chmod() / chmodSync()

代码示例可能如下所示:

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');
});