#!/usr/bin/env node

var _ = require('lodash'),
  colors = require('colors'),
  sails = require('sails');

console.log("Lifting Sails in interactive mode...".blue);

(function() {
  // Try to get `rc` dependency
  var rc;
  try {
    rc = require('rc');
  } catch (e0) {
    try {
      rc = require('sails/node_modules/rc');
    } catch (e1) {
      console.error('Could not find dependency: `rc`.');
      console.error('Your `.sailsrc` file(s) will be ignored.');
      console.error('To resolve this, run:');
      console.error('npm install rc --save');
      rc = function () { return {}; };
    }
  }

  // Start server
  sails.lift(_.merge(rc('sails'), {
    log: {
      noShip: true
    },
    hooks: {
      http: false,
      sockets: false,
      views: false
    }
  }), function(err) {

    var repl = require('repl').start('sails> ');

    repl.on('exit', function() {
      process.exit();
    });

    require('promirepl').promirepl(repl);

  });

})();

@@ -14,16 +14,84 @@
 v node%0A%0A
+/**%0A * n.b.: hella copy-pasted from sails/bin/sails-console.js%0A */%0A%0A
 var _ = 
@@ -139,16 +139,38 @@
 lors'),%0A
+  fs = require('fs'),%0A
   sails 
@@ -938,125 +938,1263 @@
 ');%0A
-%0A    repl.on('exit', function() %7B%0A      process.exit();%0A    %7D);%0A%0A    require('promirepl').promirepl(repl);%0A%0A  %7D);%0A%0A%7D)();
+    try %7B%0A      history(repl, require('path').join(sails.config.paths.tmp, '.node_history'));%0A    %7D catch (e) %7B%0A      console.log('Error finding console history:', e);%0A    %7D%0A    repl.on('exit', function() %7B%0A      process.exit();%0A    %7D);%0A%0A    require('promirepl').promirepl(repl);%0A%0A  %7D);%0A%0A%7D)();%0A%0A%0A/**%0A* REPL History%0A* Pulled directly from https://github.com/tmpvar/repl.history%0A* with the slight tweak of setting historyIndex to -1 so that%0A* it works as expected.%0A*/%0A%0Afunction history(repl, file) %7B%0A%0Atry %7B%0A  var stat = fs.statSync(file);%0A  repl.rli.history = fs.readFileSync(file, 'utf-8').split('%5Cn').reverse();%0A  repl.rli.history.shift();%0A  repl.rli.historyIndex = -1;%0A%7D catch (e) %7B%7D%0A%0Avar fd = fs.openSync(file, 'a'),%0A  reval = repl.eval;%0A%0Arepl.rli.addListener('line', function(code) %7B%0A  if (code && code !== '.history') %7B%0A    fs.write(fd, code + '%5Cn');%0A  %7D else %7B%0A    repl.rli.historyIndex++;%0A    repl.rli.history.pop();%0A  %7D%0A%7D);%0A%0Aprocess.on('exit', function() %7B%0A  fs.closeSync(fd);%0A%7D);%0A%0Arepl.commands%5B'.history'%5D = %7B%0A  help: 'Show the history',%0A  action: function() %7B%0A    var out = %5B%5D;%0A    repl.rli.history.forEach(function(v, k) %7B%0A      out.push(v);%0A    %7D);%0A    repl.outputStream.write(out.reverse().join('%5Cn') + '%5Cn');%0A    repl.displayPrompt();%0A  %7D%0A%7D;%0A%7D%0A
 %0A

add history to console