From 037193e54339682361ac587952b5f6390867dc9e Mon Sep 17 00:00:00 2001 From: Ruy Adorno Date: Thu, 29 Oct 2020 11:37:15 -0400 Subject: [PATCH] fix: do not throw on missing package.json This fixes running `npm exec` in folders that does not have a `package.json` since `runScript` throws a `ENOENT` error. --- lib/run-script.js | 4 +++- test/run-script.js | 16 ++++++++++++++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/lib/run-script.js b/lib/run-script.js index 3be39b0..e179a32 100644 --- a/lib/run-script.js +++ b/lib/run-script.js @@ -6,7 +6,9 @@ const runScript = options => { validateOptions(options) const {pkg, path} = options return pkg ? runScriptPkg(options) - : rpj(path + '/package.json').then(pkg => runScriptPkg({...options, pkg})) + : rpj(path + '/package.json') + .then(pkg => runScriptPkg({...options, pkg})) + .catch(() => runScriptPkg({...options, pkg: { scripts: {} }})) } module.exports = runScript diff --git a/test/run-script.js b/test/run-script.js index 8df2712..f95b15d 100644 --- a/test/run-script.js +++ b/test/run-script.js @@ -1,10 +1,11 @@ const requireInject = require('require-inject') -const runScript = requireInject('../lib/run-script.js', { +const mocks = { '../lib/run-script-pkg.js': async x => x, '../lib/validate-options.js': () => {}, 'read-package-json-fast': async x => x, -}) +} +const runScript = requireInject('../lib/run-script.js', mocks) const t = require('tap') @@ -14,6 +15,17 @@ t.test('no package provided, look up the package', t => pkg: 'foo/package.json', }))) +t.test('no package provided, look up the package fails', t => { + const runScript = requireInject('../lib/run-script.js', { + ...mocks, + 'read-package-json-fast': () => Promise.reject(new Error('ERR')) + }) + return runScript({ path: 'foo' }).then(res => t.strictSame(res, { + path: 'foo', + pkg: { scripts: {} }, + })) +}) + t.test('package provided, skip look up', t => runScript({ path: 'foo', pkg: 'bar' }).then(res => t.strictSame(res, { path: 'foo',