<commit_before>import {PackageJson} from '@pnpm/types'
import lifecycle = require('npm-lifecycle')
import path = require('path')
import exists = require('path-exists')
import readPackageJsonCB = require('read-package-json')
import promisify = require('util.promisify')
import {LifecycleLog, lifecycleLogger} from './logger'

const readPackageJson = promisify(readPackageJsonCB)

function noop () {} // tslint:disable-line:no-empty

export {LifecycleLog}

export async function runPostinstallHooks (
  opts: {
    depPath: string,
    rootNodeModulesDir: string,
    rawNpmConfig: object,
    pkgRoot: string,
    unsafePerm: boolean,
  },
): Promise<boolean> {
  const pkg = await readPackageJson(path.join(opts.pkgRoot, 'package.json'))
  const scripts = pkg && pkg.scripts || {}

  if (!scripts.install) {
    await checkBindingGyp(opts.pkgRoot, scripts)
  }

  if (scripts.preinstall) {
    await runLifecycleHook('preinstall', pkg, opts)
  }
  if (scripts.install) {
    await runLifecycleHook('install', pkg, opts)
  }
  if (scripts.postinstall) {
    await runLifecycleHook('postinstall', pkg, opts)
  }

  return !!scripts.preinstall || !!scripts.install || !!scripts.postinstall
}

export default async function runLifecycleHook (
  stage: string,
  pkg: PackageJson,
  opts: {
    depPath: string,
    rootNodeModulesDir: string,
    rawNpmConfig: object,
    pkgRoot: string,
    stdio?: string,
    unsafePerm: boolean,
  },
) {
  lifecycleLogger.debug({
    depPath: opts.depPath,
    script: pkg!.scripts![stage],
    stage,
  })

  return lifecycle(pkg, stage, opts.pkgRoot, {
    config: opts.rawNpmConfig,
    dir: opts.rootNodeModulesDir,
    log: {
      clearProgress: noop,
      info: noop,
      level: opts.stdio === 'inherit' ? undefined : 'silent',
      pause: noop,
      resume: noop,
      showProgress: noop,
      silly: npmLog,
      verbose: npmLog,
      warn: noop,
    },
    stdio: opts.stdio || 'pipe',
    unsafePerm: opts.unsafePerm,
  })

  function npmLog (prefix: string, logid: string, stdtype: string, line: string) {
    switch (stdtype) {
      case 'stdout':
        lifecycleLogger.debug({
          depPath: opts.depPath,
          line: line.toString(),
          stage,
        })
        return
      case 'stderr':<commit_msg>fix: log command info only when stdio is 'pipe'<commit_after>import {PackageJson} from '@pnpm/types'
import lifecycle = require('npm-lifecycle')
import path = require('path')
import exists = require('path-exists')
import readPackageJsonCB = require('read-package-json')
import promisify = require('util.promisify')
import {LifecycleLog, lifecycleLogger} from './logger'

const readPackageJson = promisify(readPackageJsonCB)

function noop () {} // tslint:disable-line:no-empty

export {LifecycleLog}

export async function runPostinstallHooks (
  opts: {
    depPath: string,
    rootNodeModulesDir: string,
    rawNpmConfig: object,
    pkgRoot: string,
    unsafePerm: boolean,
  },
): Promise<boolean> {
  const pkg = await readPackageJson(path.join(opts.pkgRoot, 'package.json'))
  const scripts = pkg && pkg.scripts || {}

  if (!scripts.install) {
    await checkBindingGyp(opts.pkgRoot, scripts)
  }

  if (scripts.preinstall) {
    await runLifecycleHook('preinstall', pkg, opts)
  }
  if (scripts.install) {
    await runLifecycleHook('install', pkg, opts)
  }
  if (scripts.postinstall) {
    await runLifecycleHook('postinstall', pkg, opts)
  }

  return !!scripts.preinstall || !!scripts.install || !!scripts.postinstall
}

export default async function runLifecycleHook (
  stage: string,
  pkg: PackageJson,
  opts: {
    depPath: string,
    rootNodeModulesDir: string,
    rawNpmConfig: object,
    pkgRoot: string,
    stdio?: string,
    unsafePerm: boolean,
  },
) {
  if (opts.stdio !== 'inherit') {
    lifecycleLogger.debug({
      depPath: opts.depPath,
      script: pkg!.scripts![stage],
      stage,
    })
  }

  return lifecycle(pkg, stage, opts.pkgRoot, {
    config: opts.rawNpmConfig,
    dir: opts.rootNodeModulesDir,
    log: {
      clearProgress: noop,
      info: noop,
      level: opts.stdio === 'inherit' ? undefined : 'silent',
      pause: noop,
      resume: noop,
      showProgress: noop,
      silly: npmLog,
      verbose: npmLog,
      warn: noop,
    },
    stdio: opts.stdio || 'pipe',
    unsafePerm: opts.unsafePerm,
  })

  function npmLog (prefix: string, logid: string, stdtype: string, line: string) {
    switch (stdtype) {
      case 'stdout':
        lifecycleLogger.debug({
          depPath: opts.depPath,
          line: line.toString(),
          stage,
        })
        return
      case 'stderr':
{'fraction_non_alphanumeric': 0.12602326583369236, 'fraction_numerical': 0.0, 'mean_word_length': 3.2911275415896486, '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': '3568814', 'n_tokens_mistral': 1501, 'n_tokens_neox': 1465, 'n_words': 350}