Tuesday, November 25, 2014

beautify javascript with nodejs and beautify.js

1- install nodejs if you don't install nodejs yet.
root@nodejs:/# apt-get install nodejs

2- download the beautify.js library and use it
- create a directory call beautyjs
user@nodejs:/$ mkdir ~/beautyjs
root@nodejs:/$ cd ~/beautyjs
- download beautify.js and store it in ~/beautyjs
- create a script call bf.js in ~/beautyjs with the following content
var Beautify = require("./beautify.js");
var fs = require("fs");

var myfile = "./" + process.argv[2];
fs.exists(myfile, function(fileok){
  if(fileok)fs.readFile(myfile, function(error, data) {
       console.log( Beautify.js_beautify("'" + data + "'"));
  });
  else console.log("file not found");
});

3- use the beautify.js
$ nodejs bf.js <messy-script.js>
- example
user@nodejs:/$ cd ~/beautyjs
user@nodejs:~beautyjs$ nodejs bf.js converse.min.js

- before

-after






No comments:

Post a Comment