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

Use native npm ls to find modules #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
105 changes: 71 additions & 34 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ var PLUGIN_PREFIX = 'bem-tools-';

var fs = require('fs'),
path = require('path'),
npmls = require('npmls'),
npmRootPath = require('global-modules'),
uniq = require('lodash.uniq');
npm = require('npm');

var bem = require('coa').Cmd()
.name(process.argv[1])
Expand All @@ -21,47 +19,86 @@ var bem = require('coa').Cmd()
})
.end();

var globalModules = [],
localModules = [];
function getBemToolsPlugins() {
var globalModules, localModules;

try {
globalModules = npmls(true);
} catch (err) {
if (err.code !== 'ENOENT') throw new Error(err);
}
return new Promise(function(resolve, reject) {
npm.load(function() {
npm.config.set('global', true);
require('npm/lib/ls')([], true, function(err, ls) {
if (err) return reject(err);

globalModules = _getBemToolsPlugins(ls.dependencies);

if (localModules) {
resolve(Object.assign(globalModules, localModules));
}
});

npm.config.set('global', false)
require('npm/lib/ls')([], true, function(err, ls) {
if (err) return reject(err);

localModules = _getBemToolsPlugins(ls.dependencies);

if (globalModules) {
resolve(Object.assign(globalModules, localModules));
}
});
});
});
};

function _getBemToolsPlugins(tree) {
var plugins = {},
deps = Object.keys(tree);

deps.forEach(function(depName) {
var dep = tree[depName];

try {
localModules = npmls();
} catch (err) {
if (err.code !== 'ENOENT') throw new Error(err);
if (!plugins[depName] && depName !== 'bem-tools-core' && depName.indexOf(PLUGIN_PREFIX) === 0) {
plugins[depName] = dep._where;
}
});

// higher level deps override inner ones
deps.forEach(function(depName) {
var dep = tree[depName] || {};

if (dep.dependencies) {
plugins = Object.assign(_getBemToolsPlugins(dep.dependencies), plugins);
}
});

return plugins;
}

var plugins = uniq(localModules.concat(globalModules).filter(function(module) {
return module.indexOf(PLUGIN_PREFIX) === 0 && module !== 'bem-tools-core';
}));

plugins.forEach(function(plugin) {
var commandName = plugin.replace(PLUGIN_PREFIX, ''),
localPluginDir = path.join('node_modules', plugin),
globalPluginDir = path.join(npmRootPath, plugin);
pluginPath = path.resolve(path.join(fs.existsSync(localPluginDir) ? localPluginDir: globalPluginDir, 'cli')),
pluginModule = null;
try {
pluginModule = require(pluginPath);
} catch(err) {
// TODO: implement verbose logging
// console.warn('Cannot find module', plugin);
}
getBemToolsPlugins().then(function(plugins) {
Object.keys(plugins).forEach(function(plugin) {
var commandName = plugin.replace(PLUGIN_PREFIX, ''),
localPluginDir = path.join('node_modules', plugin),
// globalPluginDir = path.join(npmRootPath, plugin);
// pluginPath = path.resolve(path.join(fs.existsSync(localPluginDir) ? localPluginDir: globalPluginDir, 'cli')),
pluginPath = path.resolve(plugins[plugin], 'node_modules', plugin, 'cli'),
pluginModule = null;

pluginModule && bem.cmd().name(commandName).apply(pluginModule).end();
});
try {
pluginModule = require(pluginPath);
} catch(err) {
// TODO: implement verbose logging
// console.warn('Cannot find module', plugin);
}

pluginModule && bem.cmd().name(commandName).apply(pluginModule).end();
});

bem.run(process.argv.slice(2));
}).catch(console.error);

bem.act(function(opts, args) {
if (!Object.keys(opts).length && !Object.keys(args).length) {
return this.usage();
}
});

bem.run(process.argv.slice(2));

module.exports = bem;
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
"license": "MPL-2.0",
"dependencies": {
"coa": "^1.0.1",
"lodash.uniq": "^4.2.1",
"npmls": "^3.0.0",
"global-modules": "^0.2.0"
"npm": "^3.10.8"
}
}