Skip to content

Commit

Permalink
fix: add debug logs for unresolvable plugins (#74)
Browse files Browse the repository at this point in the history
* fix: add debug logs for unresolvable plugins

and remove the need for "lib/isPlugin.js"

also, add test for undefined $NODE_PATH edge case

* fix: add debug log for plugins missing a "register" export

And remove the `require.resolve` check.
  • Loading branch information
aleclarson authored and mateodelnorte committed Dec 12, 2018
1 parent 6c5d529 commit 546d75b
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 28 deletions.
7 changes: 7 additions & 0 deletions lib/__tests__/__snapshots__/findPlugins.js.snap
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
17 changes: 13 additions & 4 deletions lib/__tests__/findPlugins.js
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down Expand Up @@ -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', '');
Expand Down
10 changes: 3 additions & 7 deletions lib/findPlugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 */
Expand All @@ -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);
}
});
Expand Down
13 changes: 0 additions & 13 deletions lib/isPlugin.js

This file was deleted.

10 changes: 6 additions & 4 deletions lib/registerPlugin.js
Original file line number Diff line number Diff line change
@@ -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}'`);
}
};

0 comments on commit 546d75b

Please sign in to comment.