/**
 * Module dependencies
 */

var nodepath = require('path');
var _ = require('@sailshq/lodash');
var chalk = require('chalk');
var captains = require('captains-log');

var rconf = require('../lib/app/configuration/rc')();
var Sails = require('../lib/app');
var SharedErrorHelpers = require('../errors');



/**
 * `sails migrate`
 *
 * Load (but don't lift) the Sails app in our working directory, using the
 * appropriate version of Sails, and only the ORM hook (plus other essentials).
 * Then simply exit.  (Useful for quickly running auto-migrations by hand.)
 *
 * > This uses the locally-installed Sails, if available.
 * > Otherwise, it uses the currently-running Sails (which,
 * > 99.9% of the time, is the globally-installed version.)
 *
 * @stability EXPERIMENTAL
 * @see http://sailsjs.com/documentation/reference/command-line-interface/sails-migrate
 */

module.exports = function() {

  // Get a temporary logger just for use in `sails console`.
  // > This is so that logging levels are configurable, even when a
  // > Sails app hasn't been loaded yet.
  var cliLogger = CaptainsLog(rconf.log);

  // Now grab our dictionary of configuration overrides to pass in
  // momentarily when we lift (or load) our Sails app.  This is the
  // dictionary of configuration settings built from `.sailsrc` file(s),
  // command-line options, and environment variables.
  // (No need to clone, since it's not being used anywhere else)
  var configOverrides = rconf;

  // Determine whether to use the local or global Sails install.
  var sailsApp = (function _determineAppropriateSailsAppInstance(){

    // Use the app's locally-installed Sails dependency (in `node_modules/sails`),
    // assuming it's extant and valid.
    // > Note that we always assume the current working directory to be the
    // > root directory of the app.
    var appPath = process.cwd();
    var localSailsPath = nodepath.resolve(appPath, 'node_modules/sails');
    if (Sails.isLocalSailsValid(localSailsPath, appPath)) {
      cliLogger.verbose('Using locally-installed Sails.');
      cliLogger.silly('(which is located at `'+localSailsPath+'`)');
      return require(localSailsPath);
    }// --•

    // Otherwise, since no workable locally-installed Sails exists,
    // run the app using the currently running version of Sails.
    // > This is probably always the global install.
    cliLogger.info('No local Sails install detected; using globally-installed Sails.');

    return Sails();

  })();//†

  // FUTURE: if no orm hook, then fail with an error explaining you can't
  // really run auto-migrations without that

  // TODO: load only orm hook + essentials

  // Load the Sails app
  sailsApp.load(configOverrides, function(err) {
    if (err) {
      return SharedErrorHelpers.fatal.failedToLoadSails(err);
    }// --•

    // Tear down the Sails app
    sailsApp.lower();

  });//_∏_  </after loading Sails app>

};

/**
 * Module dependencies
 */

var nodepath = require('path');
var captains = require('captains-log');
var rconf = require('../lib/app/configuration/rc')();
var Sails = require('../lib/app');
var SharedErrorHelpers = require('../errors');



/**
 * `sails migrate`
 *
 * Load (but don't lift) the Sails app in our working directory, using the
 * appropriate version of Sails, and only the ORM hook (plus other essentials).
 * Then simply exit.  (Useful for quickly running auto-migrations by hand.)
 *
 * > This uses the locally-installed Sails, if available.
 * > Otherwise, it uses the currently-running Sails (which,
 * > 99.9% of the time, is the globally-installed version.)
 *
 * @stability EXPERIMENTAL
 * @see http://sailsjs.com/documentation/reference/command-line-interface/sails-migrate
 */

module.exports = function() {

  // Get a temporary logger just for use in this file.
  // > This is so that logging levels are configurable, even when a
  // > Sails app hasn't been loaded yet.
  var cliLogger = captains(rconf.log);

  // Now grab our dictionary of configuration overrides to pass in
  // momentarily when we lift (or load) our Sails app.  This is the
  // dictionary of configuration settings built from `.sailsrc` file(s),
  // command-line options, and environment variables.
  // (No need to clone, since it's not being used anywhere else)
  var configOverrides = rconf;

  // Determine whether to use the local or global Sails install.
  var sailsApp = (function _determineAppropriateSailsAppInstance(){

    // Use the app's locally-installed Sails dependency (in `node_modules/sails`),
    // assuming it's extant and valid.
    // > Note that we always assume the current working directory to be the
    // > root directory of the app.
    var appPath = process.cwd();
    var localSailsPath = nodepath.resolve(appPath, 'node_modules/sails');
    if (Sails.isLocalSailsValid(localSailsPath, appPath)) {
      cliLogger.verbose('Using locally-installed Sails.');
      cliLogger.silly('(which is located at `'+localSailsPath+'`)');
      return require(localSailsPath);
    }// --•

    // Otherwise, since no workable locally-installed Sails exists,
    // run the app using the currently running version of Sails.
    // > This is probably always the global install.
    cliLogger.info('No local Sails install detected; using globally-installed Sails.');

    return Sails();

  })();//†

  // FUTURE: if no orm hook, then fail with an error explaining you can't
  // really run auto-migrations without that

  // TODO: load only orm hook + essentials

  // Load the Sails app
  sailsApp.load(configOverrides, function(err) {
    if (err) {
      return SharedErrorHelpers.fatal.failedToLoadSails(err);
    }// --•

    // Tear down the Sails app
    sailsApp.lower();

  });//_∏_  </after loading Sails app>

};

fix copy/paste errors in prev commit