<commit_before>
// activate a version of a package
// this presumes that it's been installed
// no need to worry about dependencies, because they were
// installed into the child package anyhow.

var mkdir = require("./utils/mkdir-p")
  , npm = require("../npm")
  , fs = require("fs")
  , log = require("./utils/log")
  , path = require("path")
  , rm = require("./utils/rm-rf")
  , chain = require("./utils/chain")
  , lifecycle = require("./utils/lifecycle")
  , readJson = require("./utils/read-json")
  , link = require("./utils/link")
  , linkIfExists = link.ifExists
  , shimIfExists = require("./utils/write-shim").ifExists
  , semver = require("./utils/semver")
  , asyncMap = require("./utils/async-map")

module.exports = activate

function activate (args, cb) {
  // make sure package and version exists.
  // If there's already an active version, then deactivate it.
  // first, link .npm/{pkg}/{version} to .npm/{pkg}/{active}
  // then, link the things in the root without a version to the active one.
  asyncMap(args, preActivate, function (er, data) {
    if (er) return cb(er)
    asyncMap(data, activate_, 4, function (er) {
      if (er) return cb(er)
      asyncMap(data, postActivate, cb)
    })
  })
}

function activate_ (data, cb) {
  var pkg = data.name
    , version = data.version
    , from = path.join(npm.dir, pkg, version)
    , to = path.join(npm.dir, pkg, "active")
    , fromMain = path.join(npm.dir, pkg, "active", "main.js")
    , toMain = path.join(npm.root, pkg+".js")
    , fromLib = path.join(npm.root, pkg + "-" + version)
    , toLib = path.join(npm.root, pkg)
  link(from, to, cb)
  shimIfExists(fromMain, toMain, cb)
  linkIfExists(fromLib, toLib, cb)
  linkBins(data, cb)
}

function postActivate (data, cb) {
  chain
    ( [lifecycle, data, "activate"]
    , [lifecycle, data, "postactivate"]
    , cb
    )
}

function preActivate (arg, cb) {
  var args = arg.split("@")
    , pkg = args.shift()
    , version = args.join("@")
    , to = path.join(npm.dir, pkg, "active")
    , jsonFile = path.join(npm.dir, pkg, version, "package", "package.json")

  if (!version) cb(new Error("Please specify a version to activate: "+pkg))

  chain
    ( function (cb) { npm.commands.deactivate([pkg], function () { cb() }) }
    , function (cb) {
        fs.lstat(to, function (er, stat) {
          if (!er) return fs.readlink(to, function (er, active) {
            cb(new Error("Implicit deactivate failed.\n"+
              pkg+"@"+path.dirname(active)+" still active."))
          })
          else cb()
        })
      }
    , function (er) {
        if (er) cb(er)
        readJson(jsonFile, function (er, data) {
          if (er) return cb(er)
          data.version = version
          data._id = data.name + "-" + data.version
          npm.set(data)
          lifecycle(data, "preactivate", function (er) {
            cb(er, data)
          })
        })
      }
    )
}

function linkBins (pkg, cb) {
  var binroot = npm.config.get('binroot')
  if (!pkg.bin || !binroot) return cb()
  chain(Object.keys(pkg.bin).map(function (i) {
    var to = path.join(binroot, i)
      , from = to+"-"+pkg.version
    return function (cb) {
      fs.stat(to, function (er) {
        if (!er) cb()
        linkIfExists(from, to, cb)
      })
    }
  }).concat(cb))
}<commit_msg>make the maps async<commit_after>
// activate a version of a package
// this presumes that it's been installed
// no need to worry about dependencies, because they were
// installed into the child package anyhow.

var mkdir = require("./utils/mkdir-p")
  , npm = require("../npm")
  , fs = require("fs")
  , log = require("./utils/log")
  , path = require("path")
  , rm = require("./utils/rm-rf")
  , chain = require("./utils/chain")
  , lifecycle = require("./utils/lifecycle")
  , readJson = require("./utils/read-json")
  , link = require("./utils/link")
  , linkIfExists = link.ifExists
  , shimIfExists = require("./utils/write-shim").ifExists
  , semver = require("./utils/semver")
  , asyncMap = require("./utils/async-map")

module.exports = activate

function activate (args, cb) {
  // make sure package and version exists.
  // If there's already an active version, then deactivate it.
  // first, link .npm/{pkg}/{version} to .npm/{pkg}/{active}
  // then, link the things in the root without a version to the active one.
  asyncMap(args, preActivate, function (er, data) {
    if (er) return cb(er)
    asyncMap(data, activate_, 4, function (er) {
      if (er) return cb(er)
      asyncMap(data, postActivate, cb)
    })
  })
}

function activate_ (data, cb) {
  var pkg = data.name
    , version = data.version
    , from = path.join(npm.dir, pkg, version)
    , to = path.join(npm.dir, pkg, "active")
    , fromMain = path.join(npm.dir, pkg, "active", "main.js")
    , toMain = path.join(npm.root, pkg+".js")
    , fromLib = path.join(npm.root, pkg + "-" + version)
    , toLib = path.join(npm.root, pkg)
  link(from, to, cb)
  shimIfExists(fromMain, toMain, cb)
  linkIfExists(fromLib, toLib, cb)
  linkBins(data, cb)
}

function postActivate (data, cb) {
  chain
    ( [lifecycle, data, "activate"]
    , [lifecycle, data, "postactivate"]
    , cb
    )
}

function preActivate (arg, cb) {
  var args = arg.split("@")
    , pkg = args.shift()
    , version = args.join("@")
    , to = path.join(npm.dir, pkg, "active")
    , jsonFile = path.join(npm.dir, pkg, version, "package", "package.json")

  if (!version) cb(new Error("Please specify a version to activate: "+pkg))

  chain
    ( function (cb) { npm.commands.deactivate([pkg], function () { cb() }) }
    , function (cb) {
        fs.lstat(to, function (er, stat) {
          if (!er) return fs.readlink(to, function (er, active) {
            cb(new Error("Implicit deactivate failed.\n"+
              pkg+"@"+path.dirname(active)+" still active."))
          })
          else cb()
        })
      }
    , function (er) {
        if (er) return cb(er)
        readJson(jsonFile, function (er, data) {
          if (er) return cb(er)
          data.version = version
          data._id = data.name + "-" + data.version
          npm.set(data)
          lifecycle(data, "preactivate", function (er) { cb(er, data) })
        })
      }
    )
}

function linkBins (pkg, cb) {
  var binroot = npm.config.get('binroot')
  if (!pkg.bin || !binroot) return cb()
  asyncMap(Object.keys(pkg.bin), function (i, cb) {
    var to = path.join(binroot, i)
      , from = to+"-"+pkg.version
    linkIfExists(from, to, cb)
  }, cb)
}
{'fraction_non_alphanumeric': 0.16740900015358623, 'fraction_numerical': 0.0003071724773460298, 'mean_word_length': 2.654320987654321, 'pattern_counts': {'":': 0, '<': 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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7080932', 'n_tokens_mistral': 2261, 'n_tokens_neox': 2195, 'n_words': 577}