From 91b9da90829ce9f51cfff2b614314ae5a9fefd3b Mon Sep 17 00:00:00 2001 From: Tobias Gurtzick Date: Sat, 8 Jun 2019 14:53:46 +0200 Subject: [PATCH] fix(plugin): handle non existent dependencies and improve UX fixes #628 Signed-off-by: Tobias Gurtzick --- index.js | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index dd8701e3..0340e613 100644 --- a/index.js +++ b/index.js @@ -9,14 +9,26 @@ function loadPluginList (options) { try { fs.accessSync(path.join(options.cwd, 'package.json'), fs.constants.R_OK); } catch (err) { - return {}; + throw new Error( + 'There was no package.json found in the current working dir!', + options.cwd + ); } - var plugins = JSON.parse( - fs.readFileSync(path.join(options.cwd, 'package.json'), 'utf-8') - ); + + try { + var plugins = JSON.parse( + fs.readFileSync(path.join(options.cwd, 'package.json'), 'utf-8') + ); + } catch (err) { + throw new Error('Error parsing package.json', err); + } + var targets = []; - plugins = Object.assign(plugins.dependencies, plugins.devDependencies); + plugins = Object.assign( + plugins.dependencies || {}, + plugins.devDependencies || {} + ); for (var plugin in plugins) { if (plugin.startsWith('db-migrate-plugin')) targets.push(plugin); @@ -62,12 +74,13 @@ module.exports.getInstance = function ( var plugins = {}; try { - if (!options.noPlugins) plugins = loadPlugins(options); + if (!options || !options.noPlugins) plugins = loadPlugins(options); } catch (ex) { - log.warn(ex); + log.verbose('No plugin could be loaded!'); + log.verbose(ex); } - if (options.plugins) { + if (options && options.plugins) { plugins = Object.assign(plugins, options.plugins); }