<commit_before>/*
 *   Copyright 2014-2015 <NAME> (http://guybedford.com)
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */

require('core-js/es6/string');

var config = require('./config');
var Promise = require('rsvp').Promise;
var asp = require('rsvp').denodeify;
var pkg = require('./package');
var semver = require('./semver');
var PackageName = require('./config/package-name');
var ui = require('./ui');
var path = require('path');
var link = require('./link');
var globalConfig = require('./global-config');

var rimraf = require('rimraf');

var alphabetize = require('./common').alphabetize;
var cascadeDelete = require('./common').cascadeDelete;
var hasProperties = require('./common').hasProperties;
var readJSON = require('./common').readJSON;

var fs = require('graceful-fs');

var primaryRanges = {};
var secondaryRanges = {};

var installedResolves = {};
var installingResolves = {};

var installed;
var installing = {
  baseMap: {},
  depMap: {}
};

// NB remove assertions for release
// function assert(statement, name) {
//  if (!statement)
//    throw new TypeError('Assertion Failed: ' + name);
// }

/*
 * Main install API wrapper
 *
 * install('jquery')
 * install('jquery', {options})
 * install('jquery', 'github:components/jquery')
 * install('jquery', 'github:components/jquery', {options})
 * install(true) - from package.json
 * install(true, {options}) - from package.json
 *
 * options.force - skip cache
 * options.inject
 * options.link means symlink linked versions in ranges to jspm_packages where available
 * options.lock - lock existing tree dependencies
 * options.latest - new install tree has all deps installed to latest - no rollback deduping
 * options.unlink
 * options.quick - lock and skip hash checks
 * options.dev - store in devDependencies
 * options.update - we're updating the targets
 *
 * options.summary - show fork and resolution summary
 */
exports.install = function(targets, options) {
  if (typeof targets === 'string') {
    var name = targets;
    targets = {};
    targets[name] = typeof options === 'string' ? options : '';
    options = typeof options === 'object' ? options : arguments[2];
  }
  options = options || {};

  return config.load()
  .then(function() {
    installed = installed || config.loader;

    if (options.force)
      config.force = true;

    if (options.link || options.quick)
      options.lock = true;

    var d, existingTargets = {};
    for (d in config.pjson.devDependencies)
      existingTargets[d] = config.pjson.devDependencies[d];
    for (d in config.pjson.dependencies)
      existingTargets[d] = config.pjson.dependencies[d];

    if (targets === true)
      targets = existingTargets;
    // check and set targets for update
    else if (targets && options.update)
      for (d in targets) {
        if (!existingTargets[d])
          throw '%' + d + '% is not an existing dependency to update.';
        targets[d] = existingTargets[d];
      }

    targets = pkg.processDeps(targets, globalConfig.config.defaultRegistry);

    return Promise.all(Object.keys(targets).map(function(name) {
      return install(name, targets[name], options);
    }))
    .then(function() {
      return saveInstall();
    })
    .then(function() {
      // after every install, show fork and resolution summary
      if (options.summary !== false)
        showVersions(true);
    });
  });
};

/*
 * install('jquery', 'jquery', { latest, lock, parent, inject, link, unlink, override } [, seen])
 *
 * Install modes:
 *  - Default  a. The new install tree is set to use exact latest versions of primaries,
 *                including for existing primaries.
 *                Secondaries tend to their latest ideal version.
 *             b. Forks within the new tree are deduped for secondaries by checking for
 *                rollback of the higher version.
 *             c. Forks against the existing tree are handled by upgrading the existing
 *                tree, at both primary and secondary levels, with the secondary fork
 *                potentially rolling back as well.
 *             (this is `jspm install package`)
 *
 *  - Lock     No existing dependencies are altered.
 *             New installs otherwise follow default behaviour for secondary deduping.
 *             (this is reproducible installs, `jspm install` without arguments)
 *
 *  - Latest   Secondaries set exactly to latest.
 *             Forks against existing tree follow default behaviour.
 *             (this is `jspm update`)
 *
 * Lock and Latest can be combined, which won't do anything for existing
 * installs but will give latest secondaries on new installs.
 *
 * Secondary installs are those with a parent.
 *
 * Seen allows correct completion with circular package installs
 *
 */

/*
 * jspm.install('jquery')
 * jspm.install('jquery', 'github:components/jquery@^2.0.0')
 * jspm.install('jquery', '2')
 * jspm.install('jquery', 'github:components/jquery')
 * jspm.install('jquery', { force: true });
 * jspm.install({ jquery: '1.2.3' }, { force: true })
 */
function install(name, target, options, seen) {
  // we install a target range, to an exact version resolution
  var resolution;
  var dependencyDownloads;
  var existing;

  return Promise.resolve()
  .then(function() {
    if (options.link)
      return Promise.resolve(target);

    return pkg.locate(target);
  })
  .then(function(located) {
    target = located;

    config.loader.ensureRegistry(located.registry, options.inject);

    if (options.link)
      return link.lookup(target, options.edge);

    // lock if necessary
    if (options.lock && (resolution = getInstalledMatch(target, options.parent, name)))
      return Promise.resolve();

    // perform a full version lookup
    return pkg.lookup(target, options.edge);
  })
  .then(function(getLatestMatch) {
    if (!getLatestMatch)
      return storeResolution();

    // --- version constraint solving ---

    // a. The new install tree is set to use exact latest versions of primaries, including for existing primaries.
    //    Secondaries tend to their latest ideal version.
    resolution = getLatestMatch(target.version);

    if (!resolution)
      throw 'No version match found for `' + target.exactName + '`';

    // if no version range was specified on install, install to semver-compatible with the latest
    if (!options.parent && !target.version && !options.link) {
      if (resolution.version.match(semver.semverRegEx))
        target.setVersion('^' + resolution.version);<commit_msg>version mismatch to give parent package<commit_after>/*
 *   Copyright 2014-2015 <NAME> (http://guybedford.com)
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */

require('core-js/es6/string');

var config = require('./config');
var Promise = require('rsvp').Promise;
var asp = require('rsvp').denodeify;
var pkg = require('./package');
var semver = require('./semver');
var PackageName = require('./config/package-name');
var ui = require('./ui');
var path = require('path');
var link = require('./link');
var globalConfig = require('./global-config');

var rimraf = require('rimraf');

var alphabetize = require('./common').alphabetize;
var cascadeDelete = require('./common').cascadeDelete;
var hasProperties = require('./common').hasProperties;
var readJSON = require('./common').readJSON;

var fs = require('graceful-fs');

var primaryRanges = {};
var secondaryRanges = {};

var installedResolves = {};
var installingResolves = {};

var installed;
var installing = {
  baseMap: {},
  depMap: {}
};

// NB remove assertions for release
// function assert(statement, name) {
//  if (!statement)
//    throw new TypeError('Assertion Failed: ' + name);
// }

/*
 * Main install API wrapper
 *
 * install('jquery')
 * install('jquery', {options})
 * install('jquery', 'github:components/jquery')
 * install('jquery', 'github:components/jquery', {options})
 * install(true) - from package.json
 * install(true, {options}) - from package.json
 *
 * options.force - skip cache
 * options.inject
 * options.link means symlink linked versions in ranges to jspm_packages where available
 * options.lock - lock existing tree dependencies
 * options.latest - new install tree has all deps installed to latest - no rollback deduping
 * options.unlink
 * options.quick - lock and skip hash checks
 * options.dev - store in devDependencies
 * options.update - we're updating the targets
 *
 * options.summary - show fork and resolution summary
 */
exports.install = function(targets, options) {
  if (typeof targets === 'string') {
    var name = targets;
    targets = {};
    targets[name] = typeof options === 'string' ? options : '';
    options = typeof options === 'object' ? options : arguments[2];
  }
  options = options || {};

  return config.load()
  .then(function() {
    installed = installed || config.loader;

    if (options.force)
      config.force = true;

    if (options.link || options.quick)
      options.lock = true;

    var d, existingTargets = {};
    for (d in config.pjson.devDependencies)
      existingTargets[d] = config.pjson.devDependencies[d];
    for (d in config.pjson.dependencies)
      existingTargets[d] = config.pjson.dependencies[d];

    if (targets === true)
      targets = existingTargets;
    // check and set targets for update
    else if (targets && options.update)
      for (d in targets) {
        if (!existingTargets[d])
          throw '%' + d + '% is not an existing dependency to update.';
        targets[d] = existingTargets[d];
      }

    targets = pkg.processDeps(targets, globalConfig.config.defaultRegistry);

    return Promise.all(Object.keys(targets).map(function(name) {
      return install(name, targets[name], options);
    }))
    .then(function() {
      return saveInstall();
    })
    .then(function() {
      // after every install, show fork and resolution summary
      if (options.summary !== false)
        showVersions(true);
    });
  });
};

/*
 * install('jquery', 'jquery', { latest, lock, parent, inject, link, unlink, override } [, seen])
 *
 * Install modes:
 *  - Default  a. The new install tree is set to use exact latest versions of primaries,
 *                including for existing primaries.
 *                Secondaries tend to their latest ideal version.
 *             b. Forks within the new tree are deduped for secondaries by checking for
 *                rollback of the higher version.
 *             c. Forks against the existing tree are handled by upgrading the existing
 *                tree, at both primary and secondary levels, with the secondary fork
 *                potentially rolling back as well.
 *             (this is `jspm install package`)
 *
 *  - Lock     No existing dependencies are altered.
 *             New installs otherwise follow default behaviour for secondary deduping.
 *             (this is reproducible installs, `jspm install` without arguments)
 *
 *  - Latest   Secondaries set exactly to latest.
 *             Forks against existing tree follow default behaviour.
 *             (this is `jspm update`)
 *
 * Lock and Latest can be combined, which won't do anything for existing
 * installs but will give latest secondaries on new installs.
 *
 * Secondary installs are those with a parent.
 *
 * Seen allows correct completion with circular package installs
 *
 */

/*
 * jspm.install('jquery')
 * jspm.install('jquery', 'github:components/jquery@^2.0.0')
 * jspm.install('jquery', '2')
 * jspm.install('jquery', 'github:components/jquery')
 * jspm.install('jquery', { force: true });
 * jspm.install({ jquery: '1.2.3' }, { force: true })
 */
function install(name, target, options, seen) {
  // we install a target range, to an exact version resolution
  var resolution;
  var dependencyDownloads;
  var existing;

  return Promise.resolve()
  .then(function() {
    if (options.link)
      return Promise.resolve(target);

    return pkg.locate(target);
  })
  .then(function(located) {
    target = located;

    config.loader.ensureRegistry(located.registry, options.inject);

    if (options.link)
      return link.lookup(target, options.edge);

    // lock if necessary
    if (options.lock && (resolution = getInstalledMatch(target, options.parent, name)))
      return Promise.resolve();

    // perform a full version lookup
    return pkg.lookup(target, options.edge);
  })
  .then(function(getLatestMatch) {
    if (!getLatestMatch)
      return storeResolution();

    // --- version constraint solving ---

    // a. The new install tree is set to use exact latest versions of primaries, including for existing primaries.
    //    Secondaries tend to their latest ideal version.
    resolution = getLatestMatch(target.version);

    if (!resolution) {
      if (options.parent)
        throw 'Installing `' + options.parent + '`, no version match for `' + target.exactName + '`';
      else
        throw 'No version match found for `' + target.exactName + '`';
    }

    // if no version range was specified on install, install to semver-compatible with the latest
    if (!options.parent && !target.version && !options.link) {
      if (resolution.version.match(semver.semverRegEx))
        target.setVersion('^' + resolution.version);
{'fraction_non_alphanumeric': 0.12191023037602408, 'fraction_numerical': 0.0029409705202716897, 'mean_word_length': 3.1421113689095126, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7609000', 'n_tokens_mistral': 3987, 'n_tokens_neox': 3850, 'n_words': 1317}