var handleError = require( '../lib/handleError' );
var github = require( '../lib/github' );

// GET /packages
exports.index = function ( req, res, next ) {
  req.models.Packages.find( {}, function ( err, packages ) {
    if ( !err ) {
      res.json( packages );
    } else {
      handleError( err, next );
    }
  });
};

// POST /packages
exports.create = function ( req, res, next ) {
  var pkg = new req.models.Packages( req.body );
  github.getContent( pkg, pkg.script, function ( err, response, body ) {
    if ( err || !body || !body.content ) { handleError( err, next ); }
    pkg.scriptFile = github.base64ToBuffer( body.content );
    pkg.save(function ( err ) {
      if ( !err ) {
        res.json({ success: true, message: 'Package added' });
      } else {
        handleError( err, next );
      }
    });
  });
};

// GET /packages/:name
exports.show = function ( req, res, next ) {
  req.models.Packages.findOne({ name: req.params.name }, function ( err, pkg ) {
    if ( !err ) {
      res.json( pkg ? pkg : {}); 
    } else {
      handleError( err, next );
    }
  });
};

// GET /packages/search/:name
exports.search = function ( req, res, next ) {
  req.models.Packages.search( req.params.name, function ( err, packages ) {
    if ( !err ) {
      res.json( packages );
    } else {
      handleError( err, next );
    }
  });
};

// GET /packages/:name/script.js
exports.getScript = function ( req, res, next ) {
  req.models.Packages.findOne({ name: req.params.name }, function ( err, pkg ) {
    if ( !err ) {
      res.send( pkg.scriptFile );
    } else {
      handleError( err, next );
    }
  });
};

@@ -431,16 +431,85 @@
 body );%0A
+  pkg.validateWithoutScript(function ( err ) %7B%0A    if ( !err ) %7B%0A    
   github
@@ -569,24 +569,28 @@
 e, body ) %7B%0A
+    
     if ( err
@@ -619,16 +619,26 @@
 tent ) %7B
+%0A         
  handleE
@@ -655,19 +655,40 @@
  next );
- %7D%0A
+%0A        %7D else %7B%0A      
     pkg.
@@ -735,24 +735,30 @@
 .content );%0A
+      
     pkg.save
@@ -773,24 +773,30 @@
 n ( err ) %7B%0A
+      
       if ( !
@@ -795,32 +795,38 @@
   if ( !err ) %7B%0A
+      
         res.json
@@ -874,24 +874,30 @@
 ' %7D);%0A      
+ 
+     
 %7D else %7B%0A   
@@ -885,32 +885,38 @@
        %7D else %7B%0A
+      
         handleEr
@@ -943,17 +943,100 @@
     
+  
-%7D%0A    %7D);
+    %7D%0A          %7D);%0A        %7D%0A      %7D);%0A    %7D else %7B%0A      handleError( err, next );%0A    %7D
 %0A  %7D

Validate new modules, then check repo before saving