forked from blackbaud/skyux-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
110 lines (92 loc) · 2.42 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*jshint node: true*/
'use strict';
const path = require('path');
const glob = require('glob');
const logger = require('@blackbaud/skyux-logger');
/**
* Returns results of glob.sync from specified directory and our glob pattern.
* @param {string} dir
* @returns {Array} Array of matching patterns
*/
function getGlobs(dirs) {
let globs = [];
dirs.forEach(dir => {
const joined = path.join(dir, '/skyux-builder*/package.json');
globs = globs.concat(glob.sync(joined));
});
return globs;
}
/**
* Only log a message if verbose is enabled.
* @param {boolean} verbose
* @param {string} msg
*/
function log(isVerbose, msg) {
if (isVerbose) {
logger.info(msg);
}
}
/**
* Processes an argv object.
* Reads package.json if it exists.
* @name processArgv
* @param [Object] argv
*/
function processArgv(argv) {
let verbose = argv.verbose;
let command = argv._[0];
// Allow shorthand "-v" for version
if (argv.v) {
command = 'version';
}
// Allow shorthand "-h" for help
if (argv.h) {
command = 'help';
}
switch (command) {
case 'version':
verbose = true;
require('./lib/version').logVersion(argv);
break;
case 'new':
require('./lib/new')(argv);
break;
case 'help':
case undefined:
verbose = true;
require('./lib/help')(argv);
break;
default:
logger.info(`SKY UX processing command ${command}`);
break;
}
// Look globally and locally for matching glob pattern
const dirs = [
`${process.cwd()}/node_modules/*`, // local (where they ran the command from)
`${__dirname}/..`, // global, if scoped package (where this code exists)
`${__dirname}/../..`, // global, if not scoped package
];
let modulesCalled = {};
getGlobs(dirs).forEach(pkg => {
const dirName = path.dirname(pkg);
let pkgJson = {};
let module;
try {
module = require(dirName);
pkgJson = require(pkg);
} catch (err) {
log(verbose, `Error loading module: ${pkg}`);
}
if (module && typeof module.runCommand === 'function') {
const pkgName = pkgJson.name || dirName;
if (modulesCalled[pkgName]) {
log(verbose, `Multiple instances found. Skipping passing command to ${pkgName}`);
} else {
log(verbose, `Passing command to ${pkgName}`);
module.runCommand(command, argv);
modulesCalled[pkgName] = true;
}
}
});
}
module.exports = processArgv;