<commit_before>
exports = module.exports = lifecycle
exports.cmd = cmd

var log = require("./log")
  , exec = require("./exec")
  , npm = require("../../npm")
  , path = require("path")
  , readJson = require("./read-json")
  , fs = require("./graceful-fs")
  , chain = require("./chain")
  , asyncMap = require("./async-map")
  , constants = require("constants")
  , output = require("./output")

function lifecycle (pkg, stage, wd, cb) {
  while (pkg && pkg._data) pkg = pkg._data
  if (typeof wd === "function") cb = wd , wd = null
  if (!pkg) return cb(new Error("Invalid package data"))
  log(pkg._id, stage)
  if (!pkg.scripts || !pkg.scripts[stage]) return cb()

  wd = validWd(wd || path.resolve(npm.dir, pkg.name))
  if ((wd.indexOf(npm.dir) !== 0 || path.basename(wd) !== pkg.name)
      && !npm.config.get("unsafe-perm")) {
    log.warn(pkg._id+" "+pkg.scripts[stage], "skipping, cannot run in "+wd)
    return cb()
  }

  // set the env variables, then run scripts as a child process.
  var env = makeEnv(pkg)
  env.npm_lifecycle_event = stage
  if (!npm.config.get("unsafe-perm")) env.TMPDIR = wd

  lifecycle_(pkg, stage, wd, env, cb)
}

function checkForLink (pkg, cb) {
  var f = path.join(npm.dir, pkg.name)
  fs.lstat(f, function (er, s) {
    cb(null, !(er || !s.isSymbolicLink()))
  })
}

function lifecycle_ (pkg, stage, wd, env, cb) {
  var PATH = []
    , p = wd.split("node_modules")
    , acc = path.resolve(p.shift())
  p.forEach(function (pp) {
    PATH.unshift(path.join(acc, "node_modules", ".bin"))
    acc = path.join(acc, "node_modules", pp)
  })
  PATH.unshift(path.join(acc, "node_modules", ".bin"))
  if (env.PATH) PATH.push(env.PATH)
  env.PATH = PATH.join(":")

  var packageLifecycle = pkg.scripts && pkg.scripts.hasOwnProperty(stage)<commit_msg>Run hooks, even if there isn't a lifecycle script for it<commit_after>
exports = module.exports = lifecycle
exports.cmd = cmd

var log = require("./log")
  , exec = require("./exec")
  , npm = require("../../npm")
  , path = require("path")
  , readJson = require("./read-json")
  , fs = require("./graceful-fs")
  , chain = require("./chain")
  , asyncMap = require("./async-map")
  , constants = require("constants")
  , output = require("./output")

function lifecycle (pkg, stage, wd, cb) {
  while (pkg && pkg._data) pkg = pkg._data
  if (typeof wd === "function") cb = wd , wd = null
  if (!pkg) return cb(new Error("Invalid package data"))
  log(pkg._id, stage)
  if (!pkg.scripts) pkg.scripts = {}

  wd = validWd(wd || path.resolve(npm.dir, pkg.name))
  if ((wd.indexOf(npm.dir) !== 0 || path.basename(wd) !== pkg.name)
      && !npm.config.get("unsafe-perm")) {
    log.warn(pkg._id+" "+pkg.scripts[stage], "skipping, cannot run in "+wd)
    return cb()
  }

  // set the env variables, then run scripts as a child process.
  var env = makeEnv(pkg)
  env.npm_lifecycle_event = stage

  // "nobody" typically doesn't have permission to write to /tmp
  // even if it's never used, sh freaks out.
  if (!npm.config.get("unsafe-perm")) env.TMPDIR = wd

  lifecycle_(pkg, stage, wd, env, cb)
}

function checkForLink (pkg, cb) {
  var f = path.join(npm.dir, pkg.name)
  fs.lstat(f, function (er, s) {
    cb(null, !(er || !s.isSymbolicLink()))
  })
}

function lifecycle_ (pkg, stage, wd, env, cb) {
  var PATH = []
    , p = wd.split("node_modules")
    , acc = path.resolve(p.shift())
  p.forEach(function (pp) {
    PATH.unshift(path.join(acc, "node_modules", ".bin"))
    acc = path.join(acc, "node_modules", pp)
  })
  PATH.unshift(path.join(acc, "node_modules", ".bin"))
  if (env.PATH) PATH.push(env.PATH)
  env.PATH = PATH.join(":")

  var packageLifecycle = pkg.scripts && pkg.scripts.hasOwnProperty(stage)
{'fraction_non_alphanumeric': 0.19087473002159827, 'fraction_numerical': 0.0005399568034557236, 'mean_word_length': 3.463855421686747, 'pattern_counts': {'":': 2, '<': 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': '883608', 'n_tokens_mistral': 1388, 'n_tokens_neox': 1333, 'n_words': 332}