Thursday, October 10, 2013

Get a list of files in two folders and tag them with comments denoting a version number in node.js!

The following assumes we will use this grunt-jenkins-build-number trick.

grunt.registerMultiTask('whatever', 'versioning stuff', function() {
   var version = 'tis: ' + process.env.BUILD_NUMBER;
   var fs = require('fs');
   var directories = ["foo","foo/bar"];
   directories.forEach(function(directory) {
      fs.readdir(directory, function(err, list) {
         list.forEach(function(file) {
            var route = directory + '/' + file;
            if (route.indexOf(".html") >= 0) {
               fs.readFile(route, function (err, data) {
                  fs.writeFile(route, '<!-- ' + version + " -->\r\n" + data, function (err) {});
               });
            }
            if (route.indexOf(".css") >= 0) {
               fs.readFile(route, function (err, data) {
                  fs.writeFile(route, '/* ' + version + " */\r\n" + data, function (err) {});
               });
            }
            if (route.indexOf(".js") >= 0) {
               fs.readFile(route, function (err, data) {
                  fs.writeFile(route, '/* ' + version + " */\r\n" + data, function (err) {});
               });
            }
         });
      });
   });
});

 
 

The following helped me:

 
 

I take back all of my hate for node!

No comments:

Post a Comment