fix(lifecycle): npm scripts can find bin files of dependencies

This commit is contained in:
zkochan
2016-10-15 19:36:50 +03:00
parent e9ab2e07e0
commit 3c76e2a403
4 changed files with 35 additions and 21 deletions

View File

@@ -13,6 +13,8 @@ if (process.platform === 'win32') {
PATH = e
}
})
} else {
PATH = 'PATH'
}
export type RunScriptOptions = {

View File

@@ -147,7 +147,7 @@ test('overwriting (is-positive@3.0.0 with is-positive@latest)', async function (
test('forcing', async function (t) {
prepare()
await installPkgs(['magic-hook@2.0.0'])
await installPkgs(['magic-hook@2.0.0'], testDefaults())
const distPath = path.join(process.cwd(), 'node_modules/.store/magic-hook@2.0.0/_/dist')
await rimraf(distPath)
@@ -458,28 +458,47 @@ test('packages should find the plugins they use when symlinks are preserved', as
t.skip('this test only for NodeJS with --preserve-symlinks support')
return
}
prepare()
await installPkgs([local('pkg-that-uses-plugins'), local('plugin-example')], testDefaults({ save: true }))
const result = spawnSync('pkg-that-uses-plugins', [], {
env: extendPathWithLocalBin()
prepare({
scripts: {
test: 'pkg-that-uses-plugins'
}
})
t.equal(result.stdout.toString(), 'plugin-example\n', 'package executable have found its plugin')
await installPkgs([local('pkg-that-uses-plugins'), local('plugin-example')], testDefaults({ save: true }))
const result = spawnSync('npm', ['test'])
t.ok(result.stdout.toString().indexOf('My plugin is plugin-example') !== -1, 'package executable have found its plugin')
t.equal(result.status, 0, 'executable exited with success')
})
test('run js bin file', async function (t) {
prepare()
prepare({
scripts: {
test: 'hello-world-js-bin'
}
})
await installPkgs([local('hello-world-js-bin')], testDefaults({ save: true }))
const result = spawnSync('hello-world-js-bin', [], {
env: extendPathWithLocalBin()
})
t.equal(result.stdout.toString(), 'Hello world!\n', 'package executable printed its message')
const result = spawnSync('npm', ['test'])
t.ok(result.stdout.toString().indexOf('Hello world!') !== -1, 'package executable printed its message')
t.equal(result.status, 0, 'executable exited with success')
})
const pnpmBin = path.join(__dirname, '../src/bin/pnpm.ts')
test('bin files are found by lifecycle scripts', t => {
prepare({
scripts: {
postinstall: 'hello-world-js-bin'
}
})
const result = spawnSync('ts-node', [pnpmBin, 'install', local('hello-world-js-bin')])
t.equal(result.status, 0, 'installation was successfull')
t.ok(result.stdout.toString().indexOf('Hello world!') !== -1, 'postinstall script was executed')
t.end()
})
test('installation via the CLI', t => {
prepare()
const result = spawnSync('ts-node', [pnpmBin, 'install', 'rimraf@2.5.1'])
@@ -607,12 +626,3 @@ test('should update subdep on second install', async function (t) {
t.ok(await exists('node_modules/.store/dep-of-pkg-with-1-dep@1.1.0'), 'should update to dep-of-pkg-with-1-dep@1.1.0')
})
function extendPathWithLocalBin () {
return {
PATH: [
path.join(process.cwd(), 'node_modules', '.bin'),
process.env.PATH
].join(path.delimiter)
}
}

View File

@@ -1,3 +1,3 @@
#!/usr/bin/env node
'use strict'
console.log(require('../plugin-example/package.json').name)
console.log('My plugin is', require('../plugin-example/package.json').name)

View File

@@ -2,6 +2,7 @@ import mkdirp = require('mkdirp')
import fs = require('fs')
import path = require('path')
import {stripIndent} from 'common-tags'
import globalPath from './globalPath'
const root = process.cwd()
process.env.ROOT = root
@@ -14,6 +15,7 @@ const npmrc = stripIndent`
fetch-retry-maxtimeout = 180000
registry = http://localhost:4873/
quiet = true
global-path = ${globalPath}
`
fs.writeFileSync(path.join(tmpPath, '.npmrc'), npmrc, 'utf-8')