From 1dfbfcf0683a4fc50445883adca6a3bea5decd1e Mon Sep 17 00:00:00 2001 From: Zoltan Kochan Date: Fri, 29 Jun 2018 02:17:26 +0300 Subject: [PATCH] fix(pnpmfile): installation should fail if pnpmfile fails close #1247 --- packages/pnpm/src/requirePnpmfile.ts | 19 +++++++++++++++++-- packages/pnpm/test/install/hooks.ts | 11 +++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/packages/pnpm/src/requirePnpmfile.ts b/packages/pnpm/src/requirePnpmfile.ts index c47809e6a2..06815907ba 100644 --- a/packages/pnpm/src/requirePnpmfile.ts +++ b/packages/pnpm/src/requirePnpmfile.ts @@ -1,6 +1,6 @@ import logger from '@pnpm/logger' import chalk from 'chalk' -import path = require('path') +import fs = require('fs') export default (pnpmFilePath: string) => { try { @@ -33,7 +33,22 @@ export default (pnpmFilePath: string) => { process.exit(1) return } - if (err.code !== 'MODULE_NOT_FOUND') throw err + if (err.code !== 'MODULE_NOT_FOUND' || pnpmFileExistsSync(pnpmFilePath)) { + const errWrapper = new Error(`Error during pnpmfile execution. pnpmfile: "${pnpmFilePath}". Error: "${err.message}".`) + // tslint:disable:no-string-literal + errWrapper['code'] = 'ERR_PNPM_PNPMFILE_FAIL' + err['pnpmfile'] = pnpmFilePath + errWrapper['originalError'] = err + // tslint:enable:no-string-literal + throw errWrapper + } return undefined } } + +function pnpmFileExistsSync (pnpmFilePath: string) { + const pnpmFileRealName = pnpmFilePath.endsWith('.js') + ? pnpmFilePath + : `${pnpmFilePath}.js` + return fs.existsSync(pnpmFileRealName) +} diff --git a/packages/pnpm/test/install/hooks.ts b/packages/pnpm/test/install/hooks.ts index 6d687f2f03..d55730d780 100644 --- a/packages/pnpm/test/install/hooks.ts +++ b/packages/pnpm/test/install/hooks.ts @@ -184,6 +184,17 @@ test('prints meaningful error when there is syntax error in pnpmfile.js', async t.equal(proc.status, 1) }) +test('fails when pnpmfile.js requires a non-existend module', async (t: tape.Test) => { + const project = prepare(t) + + await fs.writeFile('pnpmfile.js', 'module.exports = require("./this-does-node-exist")', 'utf8') + + const proc = execPnpmSync('install', 'pkg-with-1-dep') + + t.ok(proc.stdout.toString().indexOf('Error during pnpmfile execution') !== -1) + t.equal(proc.status, 1) +}) + test('ignore pnpmfile.js when --ignore-pnpmfile is used', async (t: tape.Test) => { const project = prepare(t)