/**
 * Module dependencies.
 */
var path = require('path'),
  spawn  = require('child_process').spawn,
  temp = require('temp'),
  fs = require('fs'),
  fsExt = require('./deps/core_ext/fs-ext'),
  Graph = require('./deps/graph').Graph;

/**
 * Create a new undirected graph
 * @constructor
 * @param {String} id The graphID
 * @return {Graph}
 * @api public
 */
exports.graph = function(id) {
  var graph = new Graph(null, id);
  graph.type = 'graph';
  return graph;
};

/**
 * Create a new directed graph
 * @constructor
 * @param {String} id The graphID
 * @return {Graph}
 * @api public 
 */
exports.digraph = function(id) {
  var graph = new Graph(null, id);
  graph.type = 'digraph';
  return graph;  
};

function _parse(file, callback, errback) {
  var gvprScript = path.join(__dirname, "ext", "gvpr", "dot2js.g"),
    parameters = ["-f" + gvprScript, file],
    cmd = "gvpr",
    __graph_eval,
    err = '',
    out = '',
    graphviz = spawn(cmd, parameters);

  graphviz.stdout.on('data', function(data) {
    out += data;
    eval(data.toString());
  });
  graphviz.stderr.on('data', function(data) {
    err += data;
  });
  graphviz.stdin.end();
  graphviz.on('exit', function(code) {
    if(code !== 0 || __graph_eval === undefined) {
      if(errback) { 
        errback(code, out, err); 
      }
    } else {
      callback(__graph_eval);
    }
  });
}
/**
 * Create a new graph from a dot script
 * @constructor
 * @param {String} file_or_script The DOT script or file
 * @param {Function} callback
 * @param {Function} errback
 * @api public 
 */
exports.parse = function(file_or_script, callback, errback) {
  if(fsExt.exist(file_or_script)) {
    _parse(file_or_script, callback, errback);
  } else {
    temp.open('node-graphviz', function(err, info) {
      fs.write(info.fd, file_or_script);
      fs.close(info.fd, function(err) {
        _parse(info.path, callback, errback);
      });
    });
  }
};

@@ -576,34 +576,33 @@
 %7D%0A * @api public
- 
 %0A
+
  */%0Aexports.digr
@@ -698,18 +698,16 @@
 n graph;
-  
 %0A%7D;%0A%0Afun
@@ -1260,17 +1260,16 @@
 rback) %7B
- 
 %0A       
@@ -1293,17 +1293,16 @@
 t, err);
- 
 %0A      %7D
@@ -1549,17 +1549,16 @@
 i public
- 
 %0A */%0Aexp
@@ -1773,83 +1773,279 @@
     
-fs.write(info.fd, file_or_script);%0A      fs.close(info.fd, function(err) %7B%0A
+if(err) %7B%0A        return errback(err);%0A      %7D%0A      fs.write(info.fd, file_or_script, function(err) %7B%0A        if(err) %7B%0A          return errback(err);%0A        %7D%0A        fs.close(info.fd, function(err) %7B%0A          if(err) %7B%0A            return errback(err);%0A          %7D%0A  
     
@@ -2086,16 +2086,28 @@
 rback);%0A
+        %7D);%0A
       %7D)

Fix missing error handling