<commit_before>/*
 * install.js: Commands for installing and deploying starter apps.
 *
 * (C) 2010, Nodejitsu Inc.
 *
 */
 
var fs = require('fs'),
    path = require('path'),
    exists = fs.exists || path.exists,
    common = require('../common'),
    cpr = common.cpr,
    rimraf = common.rimraf,
    npmModule = require('npm'),
    wizard = require('wizard'),
    jitsu = require('../../jitsu');

//
// "nodeapps" is a hash of aliases and npm packages,
//  this mapping allows us to give short names to all node apps
//
var nodeapps = {
  "helloworld": {
    "package": "nodeapps-helloworld",
    "description": "   demo `hello world` http server"
  },
  "express": {
    "package": "nodeapps-express",
    "description": "      express.js boilerplate"
  },
  "socket.io": {
    "package": "nodeapps-socket.io",
    "description": "    socket.io boilerplate"
  },
  "http-server": {
    "package": "http-server",
    "description": "  a robust and customizable http server"
  }
};


/*

"web-http-client": {
  "package": "web-http-client",
  "description": "web based http-client ( with server-side proxy )"
},
"custom-vimeo-site": {
  "package": "custom-vimeo-site",
  "description": "custom homepage for your vimeo videos"
},

"my-nodeapps": {
  "package": "nodeapps-my-nodeapps",
  "description": "simple site to display your node apps"
}

*/

module.exports = function (requestedApp, callback) {

  var app = {
    name : requestedApp,
    config: {
      path: null,
      schemaPath: null,
      schema : null
    }
  };

  npmModule.load({ exit: false }, function (err) {
    if (err) {
      return callback(err);
    }

    if (typeof app.name === 'string') {
      return createApp({starter: app.name}, callback);
    }

    promptforAppName(callback);
  });

  function createApp(results, cb) {
    if (Object.keys(nodeapps).indexOf(results['starter']) === -1) {
      jitsu.log.error('Sorry, ' + results['starter'].magenta + ' is not a node app');
      return cb('err');
    }
    installApp(results['starter'], function (err, res) {
      return err ? cb(err) : postInstall(results['starter'], cb);
    });
  }

  function installApp(appName, cb) {

    var thisPath = process.cwd();

    jitsu.log.info('Installing ' + appName.magenta + ' locally');
    jitsu.log.warn('Downloading packages from npm, this may take a moment...');
    npmModule.commands.install(appName, [nodeapps[appName].package], function (err, result) {
      if (err) {
        return cb(err);
      }
      
      cpr(path.join(thisPath + '/' + appName, 'node_modules', nodeapps[appName].package), thisPath + '/' + appName, function (err) {

        if (err) {
          return cb(err);
        }

        //
        // Read current package.json
        //
        var pkg = JSON.parse(fs.readFileSync(thisPath + '/' + appName + '/package.json').toString());

        //
        // Strip out any un-needed internal ids
        //
        for(var k in pkg) {
          if (k.search("_") !== -1) {
            delete pkg[k];
          }
        }

        //
        // Write modified package.json back out
        //
        fs.writeFileSync(thisPath + '/' + appName + '/package.json', JSON.stringify(pkg, true, 2));

        return cb(err, result);
      });
    });
  }

  function warnOverwrite(appName, cb) {
    jitsu.log.warn(process.cwd().grey + ' is not empty');
    jitsu.log.warn('Creating a node app in this directory may overwrite existing files');
    jitsu.prompt.get(['proceed'], function (err, results) {
      if (err) {
        return cb(err);
      }
      if (results['proceed'] !== 'yes') {
        jitsu.log.info('Try switching to an empty directory to continue');
        return cb(null);
      }
      
      return appName
        ? createApp({ starter: appName }, cb)
        : promptforAppName(cb);
    });
  } 

  function promptforAppName(cb) {

    jitsu.log.help('The ' + 'install'.magenta + ' command downloads pre-built node applications');
    jitsu.log.help('To continue, you will need to select an application');
    jitsu.log.info('Please choose a node app so we can get you started');
    jitsu.log.info('Available node apps:');
    Object.keys(nodeapps).forEach(function (starter) {
      jitsu.log.info(starter.magenta + ' ' + nodeapps[starter].description);
    });

    jitsu.prompt.get(['starter'], function (err, results) {
      if (err) {
        jitsu.log.warn('Cancelled');
        return cb(null, 'Cancelled');
      }<commit_msg>[minor] Removed legacy function<commit_after>/*
 * install.js: Commands for installing and deploying starter apps.
 *
 * (C) 2010, Nodejitsu Inc.
 *
 */
 
var fs = require('fs'),
    path = require('path'),
    exists = fs.exists || path.exists,
    common = require('../common'),
    cpr = common.cpr,
    rimraf = common.rimraf,
    npmModule = require('npm'),
    wizard = require('wizard'),
    jitsu = require('../../jitsu');

//
// "nodeapps" is a hash of aliases and npm packages,
//  this mapping allows us to give short names to all node apps
//
var nodeapps = {
  "helloworld": {
    "package": "nodeapps-helloworld",
    "description": "   demo `hello world` http server"
  },
  "express": {
    "package": "nodeapps-express",
    "description": "      express.js boilerplate"
  },
  "socket.io": {
    "package": "nodeapps-socket.io",
    "description": "    socket.io boilerplate"
  },
  "http-server": {
    "package": "http-server",
    "description": "  a robust and customizable http server"
  }
};


/*

"web-http-client": {
  "package": "web-http-client",
  "description": "web based http-client ( with server-side proxy )"
},
"custom-vimeo-site": {
  "package": "custom-vimeo-site",
  "description": "custom homepage for your vimeo videos"
},

"my-nodeapps": {
  "package": "nodeapps-my-nodeapps",
  "description": "simple site to display your node apps"
}

*/

module.exports = function (requestedApp, callback) {

  var app = {
    name : requestedApp,
    config: {
      path: null,
      schemaPath: null,
      schema : null
    }
  };

  npmModule.load({ exit: false }, function (err) {
    if (err) {
      return callback(err);
    }

    if (typeof app.name === 'string') {
      return createApp({starter: app.name}, callback);
    }

    promptforAppName(callback);
  });

  function createApp(results, cb) {
    if (Object.keys(nodeapps).indexOf(results['starter']) === -1) {
      jitsu.log.error('Sorry, ' + results['starter'].magenta + ' is not a node app');
      return cb('err');
    }
    installApp(results['starter'], function (err, res) {
      return err ? cb(err) : postInstall(results['starter'], cb);
    });
  }

  function installApp(appName, cb) {

    var thisPath = process.cwd();

    jitsu.log.info('Installing ' + appName.magenta + ' locally');
    jitsu.log.warn('Downloading packages from npm, this may take a moment...');
    npmModule.commands.install(appName, [nodeapps[appName].package], function (err, result) {
      if (err) {
        return cb(err);
      }
      
      cpr(path.join(thisPath + '/' + appName, 'node_modules', nodeapps[appName].package), thisPath + '/' + appName, function (err) {

        if (err) {
          return cb(err);
        }

        //
        // Read current package.json
        //
        var pkg = JSON.parse(fs.readFileSync(thisPath + '/' + appName + '/package.json').toString());

        //
        // Strip out any un-needed internal ids
        //
        for(var k in pkg) {
          if (k.search("_") !== -1) {
            delete pkg[k];
          }
        }

        //
        // Write modified package.json back out
        //
        fs.writeFileSync(thisPath + '/' + appName + '/package.json', JSON.stringify(pkg, true, 2));

        return cb(err, result);
      });
    });
  }

  function promptforAppName(cb) {

    jitsu.log.help('The ' + 'install'.magenta + ' command downloads pre-built node applications');
    jitsu.log.help('To continue, you will need to select an application');
    jitsu.log.info('Please choose a node app so we can get you started');
    jitsu.log.info('Available node apps:');
    Object.keys(nodeapps).forEach(function (starter) {
      jitsu.log.info(starter.magenta + ' ' + nodeapps[starter].description);
    });

    jitsu.prompt.get(['starter'], function (err, results) {
      if (err) {
        jitsu.log.warn('Cancelled');
        return cb(null, 'Cancelled');
      }
{'fraction_non_alphanumeric': 0.1623167679656775, 'fraction_numerical': 0.001668454296269813, 'mean_word_length': 2.545416138572032, 'pattern_counts': {'":': 42, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5805362', 'n_tokens_mistral': 2766, 'n_tokens_neox': 2625, 'n_words': 643}