/* Small APP to remove XML Balises , Attributes ... */

var cheerio = require('cheerio'),
    args = require('args'),
    kuler = require('kuler'),
    fs = require('fs'),
    isXml = require('is-xml'),
    pd = require('pretty-data'),
    argv = process.argv,
    file,
    check,
    $;


var options = args.Options.parse([
  {
    name: 'balise',
    shortName: 'b',
    help: 'Remove all asked balises',
    defaultValue : null,
    required : false
  },
  {
    name: 'input',
    shortName: 'i',
    help: 'Input folder or XML file',
    defaultValue : null,
    required : true
  },
  {
    name: 'attribut',
    shortName: 'a',
    help: 'Remove all asked attributs',
    defaultValue : null,
    required : false
  }
]);

var parsed = args.parser(argv).parse(options);

/* ----------- */
/*  CHECK ARGS */
/* ----------- */

if(!parsed.input){
  console.log(kuler("Please indicate XML File/Folder , see help" , "red"));
  return;
}

if(!(parsed.balise || parsed.attribut)){
  console.log(kuler("Please indicate balise/attribut to remove, see help" , "red"));
  return;
}

/* ----------- */
/*  DEL ELEM   */
/* ----------- */

  parsed.input = (parsed.input).toString()

  fs.stat(parsed.input , function(err, stats){
    if(err){
      console.log(kuler("File or Folder does not exist" , "red"));
      return;
    }
    // If it's an existing file.
    if(stats.isFile()){
      console.log(kuler("Checking XML file ... " , "green")); 
    }
    // If it's an existing file.
    if(stats.isDirectory()){
      console.log(kuler("Checking folder ... " , "green"));
    }
  });

  var rmAndPretty = function(path , file){
    file = fs.readFileSync(parsed.input).toString();
    check = isXml(file);      
    if(!check){
      console.error(kuler("It's not an XML file :(" , "red"));
      return;
    }
    $ = cheerio.load(file, {xmlMode: true});
    if(parsed.balise){
      $(parsed.balise).remove();
    }
    if(parsed.attribut){
      $(parsed.attribut).remove();
    }
    var xml = $.xml().replace(/^\s*$/gm, '');
    xml = pd.pd.xml(xml);
    fs.writeFile(parsed.input , xml , function(){
      console.log(kuler("Suppresion(s) éffectuées" , "green"));
    });
  };
@@ -275,16 +275,26 @@
  check,%0A
+    isRm,%0A
     $;%0A%0A
@@ -858,35 +858,36 @@
 put)%7B%0A  console.
-log
+info
 (kuler(%22Please i
@@ -989,35 +989,36 @@
 ut))%7B%0A  console.
-log
+info
 (kuler(%22Please i
@@ -1251,35 +1251,37 @@
 %7B%0A      console.
-log
+error
 (kuler(%22File or 
@@ -1401,50 +1401,90 @@
     
-console.log(kuler(%22Checking XML file ... %22
+rmAndPretty(parsed.input);%0A      process.stdout.write(kuler(%22File processed : %5Cr%22 
  , %22
@@ -1492,17 +1492,16 @@
 reen%22));
- 
 %0A    %7D%0A 
@@ -1571,59 +1571,414 @@
     
-console.log(kuler(%22Checking folder ... %22 , %22green%22)
+var nbOfFiles = 0;%0A      process.stdout.write(kuler(%22Folder detected ... %5Cr%22 , %22orange%22));%0A      fs.readdir(parsed.input, function(err, files)%7B%0A        for (var i = 0 ; i %3C files.length ; i++)%7B%0A          isRm = rmAndPretty(parsed.input + files%5Bi%5D);%0A          if(isRm)%7B%0A            nbOfFiles++;%0A          %7D%0A        %7D%0A        console.info(kuler(%22Number of files processed : %22 + nbOfFiles , %22green%22));%0A      %7D
 );%0A 
@@ -2026,15 +2026,8 @@
 path
- , file
 )%7B%0A 
@@ -2058,31 +2058,96 @@
 c(pa
-rsed.input).toString(
+th).toString();%0A    process.stdout.write(kuler(%22Checking XML file ... %5Cr%22 , %22orange%22)
 );
+ 
 %0A   
@@ -2200,52 +2200,60 @@
     
-console.error(kuler(%22It's not an XML file :(
+process.stdout.write(kuler(%22Not XML file detected %5Cr
 %22 , 
@@ -2417,24 +2417,40 @@
 t)%7B%0A      $(
+%22*%22).removeAttr(
 parsed.attri
@@ -2453,25 +2453,16 @@
 ttribut)
-.remove()
 ;%0A    %7D%0A
@@ -2553,112 +2553,41 @@
 File
-(parsed.input , xml , function()%7B%0A      console.log(kuler(%22Suppresion(s) %C3%A9ffectu%C3%A9es%22 , %22green%22));%0A    %7D)
+Sync(path , xml);%0A    return true
 ;%0A  

Remove all attributs