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

module.exports = function (options) {

  if (typeof options === 'undefined') {
    options = {};
  }

  if (typeof options.path === 'undefined') {
    options.path = 'tsconfig.json';
  }

  if (typeof options.indent === 'undefined') {
    options.indent = 2;
  }

  if (typeof options.absolute === 'undefined') {
    options.absolute = false;
  }

  if (typeof options.newline_eof === 'undefined') {
    options.newline_eof = false;
  }

  if (typeof options.relative_dir === 'undefined') {
    options.relative_dir = '.';
  }

  if (typeof options.posix === 'undefined') {
    options.posix = false;
  }

  var readConfig = function (callback) {
    fs.readFile(options.path, function (err, data) {
      if (err) {
        callback(err, null)
      } else {
        try {
          callback(null, JSON.parse(data));
        } catch (err) {
          callback(err, null);
        }
      }
    });
  };

  var writeConfig = function (config, callback) {
    try {
      var data = JSON.stringify(config, null, options.indent);
    } catch (err) {
      callback(err);
      return;
    }
    if (options.newline_eof) {
      data += "\n";
    }
    fs.writeFile(options.path, data, callback);
  };

  var files = [];

  var handle = function (file) {
    var filePath = file.path;
    if (options.absolute) {
      filePath = path.resolve(filePath);
    } else {
      filePath = path.relative(options.relative_dir, filePath);
    }
    if (options.posix) {
      filePath = path.posix.normalize(filePath);
    }
    files.push(filePath);
    this.emit('data', file);
  };

  var end = function () {
    var _this = this;
    readConfig(function (err, config) {
      if (err) { throw err; }
      config.files = files.sort();
      writeConfig(config, function (err) {
        if (err) { throw err; }
        _this.emit('end');
      });
    });
  };

  return through(handle, end);
};

@@ -93,354 +93,203 @@
 );%0A%0A
-module.exports = function (options) %7B%0A%0A  if (typeof options === 'undefined') %7B%0A    options = %7B%7D;%0A  %7D%0A%0A  if (typeof options.path === 'undefined') %7B%0A    options.path = 'tsconfig.json';%0A  %7D%0A%0A  if (typeof options.indent === 'undefined') %7B%0A    options.indent = 2;%0A  %7D%0A%0A  if (typeof options.absolute === 'undefined') %7B%0A    options.absolute = false;%0A  %7D
+var defaults = %7B%0A  path:         'tsconfig.json',%0A  index:        2,%0A  newline_eof:  2,%0A  absolute:     false,%0A  relative_dir: '.',%0A  posix:        false%0A%7D;%0A%0Amodule.exports = function (options) %7B
 %0A%0A  
@@ -306,28 +306,16 @@
  options
-.newline_eof
  === 'un
@@ -341,125 +341,71 @@
 ions
-.newline_eof = false;%0A  %7D%0A%0A  if (typeof options.relative_dir === 'undefined') %7B%0A    options.relative_dir = '.';%0A  %7D%0A%0A
+ = %7B%7D;%0A  %7D%0A%0A  Object.keys(defaults).forEach(function (key) %7B%0A  
   if
@@ -420,24 +420,23 @@
  options
-.posix =
+%5Bkey%5D !
 == 'unde
@@ -453,34 +453,51 @@
     
+  
 options
-.posix = false
+%5Bkey%5D = defaults%5Bkey%5D
 ;%0A  
+ 
-%7D
+ %7D%0A  %7D);
 %0A%0A  

improve default value handling