<commit_before>
// link with no args: symlink the folder to the global location
// link with package arg: symlink the global to the local

var npm = require("../npm")
  , symlink = require("./utils/link")
  , fs = require("./utils/graceful-fs")
  , log = require("./utils/log")
  , asyncMap = require("./utils/async-map")
  , chain = require("./utils/chain")
  , path = require("path")
  , relativize = require("./utils/relativize")
  , rm = require("./utils/rm-rf")
  , output = require("./utils/output")
  , build = require("./build")

module.exports = link

link.usage = "npm link (in package dir)"
           + "\nnpm link <pkg> (link global into local)"

link.completion = function (opts, cb) {
  var dir = path.join(npm.config.get("prefix"), "lib", "node_modules")
  fs.readdir(dir, function (er, files) {
    cb(er, files.filter(function (f) {
      return f.charAt(0) !== "."
    }))
  })
}

function link (args, cb) {
  if (npm.config.get("global")) {
    return cb(new Error("link should never be --global.\n"
                       +"Please re-run this command with --local"))
  }
  if (args.length) return linkInstall(args, cb)
  linkPkg(npm.prefix, cb)
}

function linkInstall (pkgs, cb) {
  var gp = npm.config.get("prefix")
  asyncMap(pkgs, function (pkg, cb) {
    function n (er, data) {
      if (er) return cb(er, data)
      pp = data[0][0]
      pkg = path.basename(pp)
      target = path.resolve(npm.dir, pkg)
      next()
    }

    var t = path.resolve(gp, "lib")
      , pp = path.resolve(t, "node_modules", pkg)
      , rp = null
      , target = path.resolve(npm.dir, pkg)

    // if it's a folder or a random not-installed thing, then
    // link or install it first
    if (pkg.indexOf("/") !== -1 || pkg.indexOf("\\") !== -1) {
      return fs.lstat(path.resolve(pkg), function (er, st) {
        if (er || !st.isDirectory()) {
          npm.commands.install(t, pkg, n)
        } else {
          rp = path.resolve(pkg)
          linkPkg(rp, n)
        }
      })
    }

    fs.lstat(pp, function (er, st) {
      if (er) {
        log.error(pkg, "not installed globally")
        rp = pp
        return npm.commands.install(t, pkg, n)
      } else if (!st.isSymbolicLink()) {
        rp = pp
        next()
      } else {
        return fs.realpath(pp, function (er, real) {
          if (er) log.warn(pkg, "invalid symbolic link")
          else rp = real<commit_msg>implicitly install globally properly<commit_after>
// link with no args: symlink the folder to the global location
// link with package arg: symlink the global to the local

var npm = require("../npm")
  , symlink = require("./utils/link")
  , fs = require("./utils/graceful-fs")
  , log = require("./utils/log")
  , asyncMap = require("./utils/async-map")
  , chain = require("./utils/chain")
  , path = require("path")
  , relativize = require("./utils/relativize")
  , rm = require("./utils/rm-rf")
  , output = require("./utils/output")
  , build = require("./build")

module.exports = link

link.usage = "npm link (in package dir)"
           + "\nnpm link <pkg> (link global into local)"

link.completion = function (opts, cb) {
  var dir = path.join(npm.config.get("prefix"), "lib", "node_modules")
  fs.readdir(dir, function (er, files) {
    cb(er, files.filter(function (f) {
      return f.charAt(0) !== "."
    }))
  })
}

function link (args, cb) {
  if (npm.config.get("global")) {
    return cb(new Error("link should never be --global.\n"
                       +"Please re-run this command with --local"))
  }
  if (args.length) return linkInstall(args, cb)
  linkPkg(npm.prefix, cb)
}

function linkInstall (pkgs, cb) {
  var gp = npm.config.get("prefix")
  asyncMap(pkgs, function (pkg, cb) {
    function n (er, data) {
      if (er) return cb(er, data)
      // install returns [ [pkgId, folder], ... ]
      // but we definitely installed just one thing.
      pp = data[0][1]
      pkg = path.basename(pp)
      target = path.resolve(npm.dir, pkg)
      next()
    }

    var t = path.resolve(gp, "lib")
      , pp = path.resolve(t, "node_modules", pkg)
      , rp = null
      , target = path.resolve(npm.dir, pkg)

    // if it's a folder or a random not-installed thing, then
    // link or install it first
    if (pkg.indexOf("/") !== -1 || pkg.indexOf("\\") !== -1) {
      return fs.lstat(path.resolve(pkg), function (er, st) {
        if (er || !st.isDirectory()) {
          npm.commands.install(t, pkg, n)
        } else {
          rp = path.resolve(pkg)
          linkPkg(rp, n)
        }
      })
    }

    fs.lstat(pp, function (er, st) {
      if (er) {
        rp = pp
        return npm.commands.install(t, pkg, n)
      } else if (!st.isSymbolicLink()) {
        rp = pp
        next()
      } else {
        return fs.realpath(pp, function (er, real) {
          if (er) log.warn(pkg, "invalid symbolic link")
          else rp = real
{'fraction_non_alphanumeric': 0.1681361492720935, 'fraction_numerical': 0.002050440844781628, 'mean_word_length': 2.3251533742331287, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '2036106', 'n_tokens_mistral': 1690, 'n_tokens_neox': 1629, 'n_words': 445}