Replace all the text with specified replacement using grunt replace

萝らか妹 提交于 2019-11-29 17:42:45

this is what can be done if unique ids are to be added. Assuming that you already have an array of all the id you want to add when you run your task.

In this case the id's are the file names

create your own wrapper task

var path = require('path');
var fs = require('fs');

    grunt.initconfig({
    wrap:{
            html:{
                header:'<script type="text/ng-template" ',
                footer:'</script>',
                src:'./yourPathToFile/',
                dest'./yourPathToDest/'
            }
        }
    });

grunt.registerMultiTask('wrap', 'wrap header and footer with custom id', function(){
 var data = this.data;
 getListOfFiles(data.src);

 function getListOfFiles(expand_path){
       var listOfFiles = fs.readdirSync(expand_path);
            for(var i=0; i<listOfFiles.length; i++){
                var completePath = expand_path + listOfFiles[i];
                var extension = path.extname(completePath);
                if(fs.lstatSync(completePath).isDirectory()){
                    var newDirPath = completePath + '/';
                    console.log('true------ : \n',newDirPath);
                    getListofFiles(newDirPath);
                }
                else if(extension == '.html'){
                    console.log('F:\n', completePath);
                    fullSrcPath = path.resolve(completePath);
                    content = grunt.file.read(fullSrcPath);
                    scriptId = 'id="' + listOfFiles[i]+'">';
                    header = (grunt.template.process(data.header));
                    footer = (grunt.template.process(data.footer));
                    wholeFile = header + scriptId + content + footer;
                    grunt.file.write(fullSrcPath, wholeFile);
                }
            } 
 }

});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!