<commit_before>var fs = require('fs');
var ff = require('ff');

var path = require('path');
var gitClient = require('../util/gitClient');
var stringify = require('../util/stringify');
var spawn = require('child_process').spawn;

var BaseCommand = require('../util/BaseCommand').BaseCommand;

var DEFAULT_PROTOCOL = "https";
var DEFAULT_DEPS = [
  {
    name: "devkit",
    ssh: "<EMAIL>@github.com:",
    https: "https://github.com/",
    repo: "gameclosure/gcapi-priv",
    tag: "2.0.0-beta.5"
  }
];

var InstallCommand = Class(BaseCommand, function (supr) {

  this.name = 'install';
  this.description = 'installs (or updates) devkit dependencies for an app or a specific dependency if one is provided';

  this.init = function () {
    supr(this, 'init', arguments);

    this.opts
      .describe('ssh', 'switches git protocol to ssh, default: false (https)');

  }

  this.exec = function (commands, args) {

    var argv = this.opts.argv;
    var protocol = argv.ssh ? 'ssh' : 'https';
    var module = args.shift();

    // ensure modules directory exists
    if (!fs.existsSync('modules')) {
      fs.mkdirSync('modules');
    }

    if (!fs.statSync('modules').isDirectory()) {
      throw new Error('"modules" must be a directory');
    }

    var directory = path.resolve(process.cwd(), 'modules');

    if (module) {
      // single module provided, install it
      this.install(directory, module);
    } else {
      // no module provided, read and install all modules from package.json
      if (!fs.existsSync('package.json')) {
        // create package.json with default dep for new games
        fs.writeFileSync('package.json', stringify({"devkitDependencies": toJSON(protocol, DEFAULT_DEPS)}));
      }

      try {
        var deps = JSON.parse(fs.readFileSync('package.json')).devkitDependencies;
      } catch (e) {
        throw new Error('Unable to read "package.json"');
      }

      Object.keys(deps).forEach(function (name) {
        this.install(directory, name, deps[name]);
      }, this);
    }
  }

  function toJSON(protocol, deps) {
    var out = {};
    deps.forEach(function (dep) {
      out[dep.name] = dep[protocol] + dep.repo + (dep.tag ? '#' + dep.tag : '');
    });
    return out;
  }

  this.install = function (directory, moduleName, url, tag) {
    var modulePath = path.join(directory, moduleName);
    var git = gitClient.get(path.join(directory, moduleName));
    if (url) {

      // find tag in url
      var i = url.indexOf('#');
      if (!tag && i >= 0) {
        var tag = url.substring(i + 1);
        url = url.substring(0, i);
      }

      this.logger.log('"' + moduleName + '" installing into', modulePath);
      var f = ff(this, function () {
        if (!fs.existsSync(modulePath)) {
          var modulesGit = gitClient.get(directory, {customFds: [0, 1, 2]});
          modulesGit('clone', url, moduleName, f());
        } else {
          git('fetch', f());
        }
      }, function () {
        if (tag) {
          git('checkout', tag, f());
        }
      }, function () {
        git('submodule', 'sync', '--recursive', f());
      }, function () {
        git('submodule', 'update', '--init', '--recursive', f());
      }, function () {
        var npm = spawn('npm', ['install'], {customFds: [0, 1, 2], cwd: modulePath});
        npm.on('close', f());
      }, function () {
        try {
          var name = require(path.join(directory, moduleName, 'package.json')).name;
        } catch (e) {
          // no package.json file in this module
        }

        // directory name should always match the name provided in package.json
        if (name && name != moduleName) {
          this.logger.warn('This module has changed names from "' +
            moduleName + '" to "' + name + '". Please update your dependency.');
        }
      }).error(function (err) {
        logger.error(err);
      });
    }
  }
});

module.exports = InstallCommand;
<commit_msg>move install scripts and submodule updates into npm scripts<commit_after>var fs = require('fs');
var ff = require('ff');

var path = require('path');
var gitClient = require('../util/gitClient');
var stringify = require('../util/stringify');
var spawn = require('child_process').spawn;

var BaseCommand = require('../util/BaseCommand').BaseCommand;

var DEFAULT_PROTOCOL = "https";
var DEFAULT_DEPS = [
  {
    name: "devkit",
    ssh: "<EMAIL>:",
    https: "https://github.com/",
    repo: "gameclosure/gcapi-priv",
    tag: "2.0.0-beta.5"
  }
];

var InstallCommand = Class(BaseCommand, function (supr) {

  this.name = 'install';
  this.description = 'installs (or updates) devkit dependencies for an app or a specific dependency if one is provided';

  this.init = function () {
    supr(this, 'init', arguments);

    this.opts
      .describe('ssh', 'switches git protocol to ssh, default: false (https)');

  }

  this.exec = function (commands, args) {

    var argv = this.opts.argv;
    var protocol = argv.ssh ? 'ssh' : 'https';
    var module = args.shift();

    // ensure modules directory exists
    if (!fs.existsSync('modules')) {
      fs.mkdirSync('modules');
    }

    if (!fs.statSync('modules').isDirectory()) {
      throw new Error('"modules" must be a directory');
    }

    var directory = path.resolve(process.cwd(), 'modules');

    if (module) {
      // single module provided, install it
      this.install(directory, module);
    } else {
      // no module provided, read and install all modules from package.json
      if (!fs.existsSync('package.json')) {
        // create package.json with default dep for new games
        fs.writeFileSync('package.json', stringify({"devkitDependencies": toJSON(protocol, DEFAULT_DEPS)}));
      }

      try {
        var deps = JSON.parse(fs.readFileSync('package.json')).devkitDependencies;
      } catch (e) {
        throw new Error('Unable to read "package.json"');
      }

      // serially install all dependencies
      var index = 0;
      var names = Object.keys(deps);
      var next = bind(this, function () {
        var name = names[index++];
        if (name) {
          this.install(directory, name, deps[name], null, next);
        }
      });
      next();
    }
  }

  function toJSON(protocol, deps) {
    var out = {};
    deps.forEach(function (dep) {
      out[dep.name] = dep[protocol] + dep.repo + (dep.tag ? '#' + dep.tag : '');
    });
    return out;
  }

  this.install = function (directory, moduleName, url, tag, cb) {
    var modulePath = path.join(directory, moduleName);
    var git = gitClient.get(path.join(directory, moduleName));
    if (url) {

      // find tag in url
      var i = url.indexOf('#');
      if (!tag && i >= 0) {
        var tag = url.substring(i + 1);
        url = url.substring(0, i);
      }

      this.logger.log('"' + moduleName + '" installing into', modulePath);
      var f = ff(this, function () {
        if (!fs.existsSync(modulePath)) {
          var modulesGit = gitClient.get(directory, {customFds: [0, 1, 2]});
          modulesGit('clone', url, moduleName, f());
        } else {
          git('fetch', f());
        }
      }, function () {
        if (tag) {
          git('checkout', tag, f());
        }
      }, function () {
        var npm = spawn('npm', ['install'], {customFds: [0, 1, 2], cwd: modulePath});
        npm.on('close', f());
      }, function () {
        try {
          var name = require(path.join(directory, moduleName, 'package.json')).name;
        } catch (e) {
          // no package.json file in this module
        }

        // directory name should always match the name provided in package.json
        if (name && name != moduleName) {
          this.logger.warn('This module has changed names from "' +
            moduleName + '" to "' + name + '". Please update your dependency.');
        }
      }).error(function (err) {
        logger.error(err);
      }).cb(cb);
    }
  }
});

module.exports = InstallCommand;

{'fraction_non_alphanumeric': 0.1623931623931624, 'fraction_numerical': 0.003393665158371041, 'mean_word_length': 2.4580617122990005, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2191623', 'n_tokens_mistral': 2462, 'n_tokens_neox': 2386, 'n_words': 642}