-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
73 lines (62 loc) · 2.26 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
var PLUGIN_PREFIX = 'bem-tools-';
var fs = require('fs'),
path = require('path'),
npmRootPath = require('global-modules'),
cwd = process.cwd();
var bem = require('coa').Cmd()
.name(process.argv[1])
.title(['BEM plugins CLI runned.', '' +
'See https://bem.info for more info.', ''].join('\n'))
.helpful()
.opt()
.name('version').title('Version')
.short('v').long('version')
.flag()
.only()
.act(function() {
return require('./package').version;
})
.end();
function readDir(dir) {
return new Promise(function(resolve, reject) {
fs.readdir(dir, function(err, packages) {
if (err && err.code !== 'ENOENT') return reject(err);
resolve((packages || []).reduce(function(acc, package) {
acc[package] = path.resolve(dir, package);
return acc;
}, {}));
});
});
}
Promise.all([
readDir(path.join(cwd, 'node_modules')),
readDir(path.join(cwd, 'node_modules', 'bem-tools', 'node_modules')),
readDir(path.join(__dirname, 'node_modules')),
readDir(path.join(__dirname, 'node_modules', 'bem-tools', 'node_modules')),
readDir(npmRootPath),
readDir(path.join(npmRootPath, 'bem-tools', 'node_modules'))
]).then(function(packagesArray) {
var packagesHash = Object.assign.apply(Object, packagesArray.reverse());
var packages = Object.keys(packagesHash).filter(function(package) {
return package.indexOf(PLUGIN_PREFIX) === 0 && package !== 'bem-tools-core';
});
packages.forEach(function(packageName) {
var commandName = packageName.replace(PLUGIN_PREFIX, ''),
pluginPath = path.join(packagesHash[packageName], 'cli'),
pluginModule = null;
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();
}
});
module.exports = bem;