fix: don't fail when the pnpm CLI executed through piping

This commit is contained in:
Zoltan Kochan
2021-01-24 14:03:44 +02:00
parent ed0df047d9
commit 43de80034a
4 changed files with 38 additions and 12 deletions

View File

@@ -0,0 +1,6 @@
---
"@pnpm/cli-meta": patch
"@pnpm/store-connection-manager": patch
---
Don't fail when the code is executed through piping to Node's stdin.

View File

@@ -7,15 +7,19 @@ const defaultManifest = {
version: '0.0.0',
}
let pkgJson
try {
pkgJson = {
...defaultManifest,
...loadJsonFile.sync<DependencyManifest>(
path.join(path.dirname(require.main!.filename), '../package.json')
),
}
} catch (err) {
if (!require.main) {
pkgJson = defaultManifest
} else {
try {
pkgJson = {
...defaultManifest,
...loadJsonFile.sync<DependencyManifest>(
path.join(path.dirname(require.main.filename), '../package.json')
),
}
} catch (err) {
pkgJson = defaultManifest
}
}
const packageManager = {

View File

@@ -135,12 +135,26 @@ test('exit code from plugin is used to end the process', () => {
expect(result.stdout.toString()).toMatch(/is-positive/)
})
const PNPM_CLI = path.join(__dirname, '../dist/pnpm.js')
test('the bundled CLI is independent', async () => {
const project = prepare()
await fs.copyFile(path.join(__dirname, '../dist/pnpm.js'), 'pnpm.js')
await fs.copyFile(PNPM_CLI, 'pnpm.js')
await execa('node', ['./pnpm.js', 'add', 'is-positive'])
await project.has('is-positive')
})
test('the bundled CLI can be executed from stdin', async () => {
const project = prepare()
const nodeProcess = execa('node', ['-', 'add', 'is-positive'])
fs.createReadStream(PNPM_CLI).pipe(nodeProcess.stdin!)
await nodeProcess
await project.has('is-positive')
})

View File

@@ -1,7 +1,9 @@
import PnpmError from '@pnpm/error'
import diable = require('@zkochan/diable')
const pnpm = require.main!.filename
export default (storePath: string) => {
return diable.daemonize(pnpm, ['server', 'start', '--store-dir', storePath], { stdio: 'inherit' })
if (!require.main) {
throw new PnpmError('CANNOT_START_SERVER', 'pnpm server cannot be started when pnpm is streamed to Node.js')
}
return diable.daemonize(require.main.filename, ['server', 'start', '--store-dir', storePath], { stdio: 'inherit' })
}