diff --git a/lib/__tests__/__snapshots__/findPlugins.js.snap b/lib/__tests__/__snapshots__/findPlugins.js.snap index 77e2c36..ef6fcbc 100644 --- a/lib/__tests__/__snapshots__/findPlugins.js.snap +++ b/lib/__tests__/__snapshots__/findPlugins.js.snap @@ -1,5 +1,12 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`findPlugins falls back to (npm root -g) when $NODE_PATH is empty 1`] = ` +Map { + "meta-1" => "/npm/root/meta-1", + "meta-2" => "/npm/root/@foo/meta-2", +} +`; + exports[`findPlugins finds non-scoped plugins 1`] = ` Map { "meta-foo" => "/node_modules/meta-foo", diff --git a/lib/__tests__/findPlugins.js b/lib/__tests__/findPlugins.js index 15e2194..994bfc0 100644 --- a/lib/__tests__/findPlugins.js +++ b/lib/__tests__/findPlugins.js @@ -1,10 +1,7 @@ jest.mock('fs'); -jest.mock('../isPlugin', () => { - const path = require('path'); - return filePath => /^meta-/.test(path.basename(filePath)); -}); const findPlugins = require('../findPlugins'); +const cp = require('child_process'); const fs = require('fs'); describe('findPlugins', () => { @@ -52,6 +49,18 @@ describe('findPlugins', () => { expect(findPlugins('/dev')).toMatchSnapshot(); }); + it('falls back to (npm root -g) when $NODE_PATH is empty', () => { + fs.write('/npm/root/foo/index.js', ''); + fs.write('/npm/root/meta-1/index.js', ''); + fs.write('/npm/root/@foo/meta-2/index.js', ''); + + delete process.env.NODE_PATH; + cp.execSync = jest.fn(() => '/npm/root'); + + expect(findPlugins('/')).toMatchSnapshot(); + expect(cp.execSync.mock.calls).toEqual([['npm root -g']]); + }); + it('tolerates missing "node_modules" when searching parent directories', () => { fs.write('/1/node_modules/meta-1/index.js', ''); fs.write('/1/2/3/node_modules/meta-3/index.js', ''); diff --git a/lib/findPlugins.js b/lib/findPlugins.js index 1102bf9..c32e2d5 100644 --- a/lib/findPlugins.js +++ b/lib/findPlugins.js @@ -2,7 +2,6 @@ const cp = require('child_process'); const debug = require('debug')('meta:findPlugins'); const fs = require('fs'); const path = require('path'); -const isPlugin = require('./isPlugin'); /** * Meta's custom plugin resolution logic @@ -38,10 +37,7 @@ module.exports = (cwd, searchDir = 'node_modules') => { }; function getDefaultGlobalRoot() { - return cp - .execSync('npm root -g') - .toString() - .trim(); + return (cp.execSync('npm root -g') + '').trim(); } /** Check a directory for potentially-scoped /^meta-/ packages */ @@ -54,9 +50,9 @@ function findNearbyPlugins(cwd, onPlugin) { const scopePath = filePath; fs.readdirSync(scopePath).forEach(name => { const filePath = path.join(scopePath, name); - if (isPlugin(filePath)) onPlugin(name, filePath); + if (/^meta-/.test(name)) onPlugin(name, filePath); }); - } else if (isPlugin(filePath)) { + } else if (/^meta-/.test(name)) { onPlugin(name, filePath); } }); diff --git a/lib/isPlugin.js b/lib/isPlugin.js deleted file mode 100644 index e607701..0000000 --- a/lib/isPlugin.js +++ /dev/null @@ -1,13 +0,0 @@ -const path = require('path'); - -// This is mocked during tests to factor out `isImportable` -module.exports = filePath => - /^meta-/.test(path.basename(filePath)) && isImportable(filePath); - -function isImportable(filePath) { - try { - require.resolve(filePath); - return true; - } catch (e) {} - return false; -} diff --git a/lib/registerPlugin.js b/lib/registerPlugin.js index 2083d21..4e204ec 100644 --- a/lib/registerPlugin.js +++ b/lib/registerPlugin.js @@ -1,9 +1,11 @@ const debug = require('debug')('meta:registerPlugin'); -module.exports = (program, mod) => { - const plugin = require(mod); - if (plugin.register) { - debug(`registering plugin ${mod}`); +module.exports = (program, pluginPath) => { + plugin = require(pluginPath); + if (typeof plugin.register === 'function') { + debug(`Registering plugin: '${pluginPath}'`); plugin.register(program); + } else { + debug(`Plugin has no "register" function: '${pluginPath}'`); } };