Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Improve plugin autoloading #183

Merged
merged 1 commit into from
Feb 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 29 additions & 9 deletions packages/cli/index.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,57 @@
const { promises: fs } = require('fs');
const path = require('path');

// find plugins in a directory, matching a regexp, ignoring registered plugins
function findPlugins(dir, regexp, registered) {
// find plugins in a directory, matching a pattern, ignoring registered plugins
function findPlugins(dir, pattern, registered) {
let segments = pattern.split('/');
let regexp = new RegExp(`^${segments.pop().replace('*', '.*')}`);
dir = path.join(dir, ...segments);

return fs.readdir(dir).then(f => f.reduce((plugins, dirname) => {
// exclude CLI's own directory and any directory not matching the pattern
if (dirname === 'cli' || !regexp.test(dirname)) return plugins;

let { name, oclif } = require(`${dir}/${dirname}/package.json`);
try {
let { name, oclif } = require(`${dir}/${dirname}/package.json`);

if (!registered.includes(name) && oclif && oclif.bin === 'percy') {
return plugins.concat(name);
} else {
return plugins;
// plugin's package.json have a percy oclif binary defined
if (!registered.includes(name) && oclif && oclif.bin === 'percy') {
return plugins.concat(name);
}
} catch {
// ignore directories without a package.json
}

return plugins;
}, []));
}

// automatically register/unregister plugins by altering the CLI's package.json within node_modules
async function autoRegisterPlugins() {
let nodeModules = path.resolve(__dirname, '../..');
let pkgPath = path.resolve(__dirname, 'package.json');
let pkg = require(pkgPath);

// if not installed within node_modules, look within own node_modules
/* istanbul ignore else: always true during tests */
if (path.basename(nodeModules) !== 'node_modules') {
nodeModules = path.resolve(__dirname, 'node_modules');
}

// ensure registered plugins can be resolved
let registered = pkg.oclif.plugins.filter(plugin => {
if (pkg.dependencies[plugin]) return true;
try { return !!require.resolve(plugin); } catch {}
return false;
});

// find unregistered plugins
let unregistered = await Promise.all([
findPlugins(path.resolve(__dirname, '..'), /^.*/, registered), // @percy/*
findPlugins(path.resolve(__dirname, '../..'), /^percy-cli-.*/, registered) // percy-cli-*
findPlugins(nodeModules, '@percy/*', registered),
findPlugins(nodeModules, 'percy-cli-*', registered)
]).then(p => Array.from(new Set(p.flat())));

// if any unregistered or unresolved registered, modify plugin registry
if (unregistered.length || registered.length !== pkg.oclif.plugins.length) {
pkg.oclif.plugins = registered.concat(unregistered);
await fs.writeFile(pkgPath, JSON.stringify(pkg, null, 2) + '\n');
Expand Down
7 changes: 4 additions & 3 deletions packages/cli/test/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ export function pluginMocker() {
}

for (let [name, plugin] of Object.entries(packages)) {
let dir = path.resolve(__dirname, name.startsWith('@percy') ? '../..' : '../../..');
let dir = path.resolve(__dirname, '../node_modules');
if (name.startsWith('@percy')) dir = path.resolve(dir, '@percy');
let dirname = name.replace('@percy', '');

mock.dir[dir].push(dirname);
Expand All @@ -27,8 +28,8 @@ export function pluginMocker() {
};

mock.dir = {
[path.resolve(__dirname, '../..')]: [],
[path.resolve(__dirname, '../../..')]: []
[path.resolve(__dirname, '../node_modules/@percy')]: [],
[path.resolve(__dirname, '../node_modules')]: []
};

mockRequire(pkgPath, mock.pkg);
Expand Down