CleverStack/cleverstack-cli

View on GitHub
bin/clever-repl

Summary

Maintainability
Test Coverage
#!/usr/bin/env node

var path      = require('path')
  , lib       = GLOBAL.lib = require(path.join(__dirname, '..', 'lib'))
  , program   = GLOBAL.program = require('commander')
  , fs        = require('fs')
  , os        = require('os')
  , _         = require('lodash')
  , replFile  = path.join(__dirname, '..', 'lib', 'repl.js');

lib
  .util
  .locations
  .get()
  .then(function(projects) {
    var project = _.find(projects, function(p) {
      return p.name === 'backend';
    });

    if (typeof project === 'undefined') {
      lib.utils.fail('Couldn\'t find the backend CleverStack directory! Possible reasons could include:\n* Not running the REPL command within the root folder of the project\n* Not setting the correct NODE_PATH environment variable');
    }

    if (!process.env.NODE_ENV || !fs.existsSync(path.join(project.moduleDir, 'config', process.env.NODE_ENV + 'js'))) {
      process.env.NODE_ENV = 'local';
    }

    var processEnv        = process.env;
    processEnv.NODE_PATH  = path.resolve(path.join(project.moduleDir, 'lib')) + path.sep + (os.platform() === 'win32' ? ';' : ':') + path.resolve(path.join(project.moduleDir, 'modules')) + path.sep;
    processEnv.projectDir = project.moduleDir;
    processEnv.libDir     = path.resolve(path.join(__dirname, '..', 'index'));

    var proc = require('child_process').spawn('node', [ replFile ], { cwd: process.cwd(), env: processEnv, stdio: 'inherit' });
    proc.on('exit', function() {
      process.exit();
    });
  }).catch(function(err) {
    lib.utils.fail(err);
  });