hunk.code_window: import fs = require('mz/fs')
   import path = require('path')
   import linkDir from 'link-dir'
   import symlinkDir from 'symlink-dir'
   import exists = require('path-exists')
   import logger from 'pnpm-logger'
   import R = require('ramda')
   import pLimit = require('p-limit')
  
hunk.labels: keep keep replace keep keep keep keep keep
hunk.after_edit: import linkDir = require('link-dir')
  
hunk.file_path: src/link/index.ts
hunk.type: replace
file: import tape = require('tape')
  import promisifyTape from 'tape-promise'
  import {installPkgs, install} from '../../src'
  import {
    prepare,
    testDefaults,
    execPnpmSync,
  } from '../utils'
  import path = require('path')
  import loadJsonFile = require('load-json-file')
  import rimraf = require('rimraf-then')
  
  const pkgRoot = path.join(__dirname, '..', '..')
  const pnpmPkg = loadJsonFile.sync(path.join(pkgRoot, 'package.json'))
  
  const test = promisifyTape(tape)
  
  test('run pre/postinstall scripts', async function (t) {
    const project = prepare(t)
    await installPkgs(['pre-and-postinstall-scripts-example'], testDefaults({saveDev: true}))
  
    const generatedByPreinstall = project.requireModule('pre-and-postinstall-scripts-example/generated-by-preinstall')
    t.ok(typeof generatedByPreinstall === 'function', 'generatedByPreinstall() is available')
  
    const generatedByPostinstall = project.requireModule('pre-and-postinstall-scripts-example/generated-by-postinstall')
    t.ok(typeof generatedByPostinstall === 'function', 'generatedByPostinstall() is available')
  
    await rimraf('node_modules')
  
    // testing that the packages are not installed even though they are in shrinkwrap
    // and that their scripts are not tried to be executed
  
    await install(testDefaults({production: true}))
  
    {
      const generatedByPreinstall = project.requireModule('pre-and-postinstall-scripts-example/generated-by-preinstall')
      t.ok(typeof generatedByPreinstall === 'function', 'generatedByPreinstall() is not available')
  
      const generatedByPostinstall = project.requireModule('pre-and-postinstall-scripts-example/generated-by-postinstall')
      t.ok(typeof generatedByPostinstall === 'function', 'generatedByPostinstall() is not available')
    }
  })
  
  test('run install scripts', async function (t) {
    const project = prepare(t)
    await installPkgs(['install-script-example'], testDefaults())
  
    const generatedByInstall = project.requireModule('install-script-example/generated-by-install')
    t.ok(typeof generatedByInstall === 'function', 'generatedByInstall() is available')
  })
  
  test('installation fails if lifecycle script fails', t => {
    const project = prepare(t, {
      scripts: {
        preinstall: 'exit 1'
      },
    })
  
    const result = execPnpmSync('install')
  
    t.equal(result.status, 1, 'installation failed')
  
    t.end()
  })
  
  test('lifecycle script runs with the correct user agent', t => {
    const project = prepare(t, {
      scripts: {
        preinstall: 'node --eval "console.log(process.env.npm_config_user_agent)"'
      },
    })
  
    const result = execPnpmSync('install')
  
    t.equal(result.status, 0, 'installation was successfull')
    const expectedUserAgentPrefix = `${pnpmPkg.name}/${pnpmPkg.version} `
    t.ok(result.stdout.toString().indexOf(expectedUserAgentPrefix) !== -1, 'correct npm_config_user_agent value')
  
    t.end()
  })
  
  test('preinstall is executed before general installation', t => {
    const project = prepare(t, {
      scripts: {
        preinstall: 'echo "Hello world!"'
      }
    })
  
    const result = execPnpmSync('install')
  
    t.equal(result.status, 0, 'installation was successfull')
    t.ok(result.stdout.toString().indexOf('Hello world!') !== -1, 'preinstall script was executed')
  
    t.end()
  })
  
  test('postinstall is executed after general installation', t => {
    const project = prepare(t, {
      scripts: {
        postinstall: 'echo "Hello world!"'
      }
    })
  
    const result = execPnpmSync('install')
  
    t.equal(result.status, 0, 'installation was successfull')
    t.ok(result.stdout.toString().indexOf('Hello world!') !== -1, 'postinstall script was executed')
  
    t.end()
  })
  
  test('postinstall is not executed after named installation', t => {
    const project = prepare(t, {
      scripts: {
        postinstall: 'echo "Hello world!"'
      }
    })
  
    const result = execPnpmSync('install', 'is-negative')
  
    t.equal(result.status, 0, 'installation was successfull')
    t.ok(result.stdout.toString().indexOf('Hello world!') === -1, 'postinstall script was not executed')
  
    t.end()
  })
  
  test('prepublish is not executed after installation with arguments', t => {
    const project = prepare(t, {
      scripts: {
        prepublish: 'echo "Hello world!"'
      }
    })
  
    const result = execPnpmSync('install', 'is-negative')
  
    t.equal(result.status, 0, 'installation was successfull')
    t.ok(result.stdout.toString().indexOf('Hello world!') === -1, 'prepublish script was not executed')
  
    t.end()
  })
  
  test('prepublish is executed after argumentless installation', t => {
    const project = prepare(t, {
      scripts: {
        prepublish: 'echo "Hello world!"'
      }
    })
  
    const result = execPnpmSync('install')
  
    t.equal(result.status, 0, 'installation was successfull')
    t.ok(result.stdout.toString().indexOf('Hello world!') !== -1, 'prepublish script was executed')
  
    t.end()
  })
  