/
carapace
executable file
·88 lines (74 loc) · 2.24 KB
/
carapace
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
#!/usr/local/bin/node
var path = require('path'),
async = require('async'),
carapace = require('../lib/carapace');
//
// Extract the carapace CLI arguments for this
// process.
//
var argv = carapace.cli.argv(),
script = argv._[0];
function onPluginError (info) {
//
// If any of the plugins have failed to load then short
// circuit this carapace process because it requested them all.
//
console.log('Error loading plugin: ' + info.plugin);
console.log(info.error.message);
console.dir(info.error.stack.split('\n'));
process.exit(1);
}
//
// Remark: Calling `carapace.listen()` with no
// arguments will fall-back to the default `hook-*`
// CLI options since
//
carapace.start(function () {
var passthru = carapace.cli.extract();
function configure (next) {
if (!Array.isArray(argv.plugin)) {
argv.plugin = [argv.plugin];
}
//
// Listen for errors when loading plugins
//
carapace.on('carapace::plugin::error', onPluginError);
var plugins = argv.plugin.map(function (plugin) {
return carapace.load(plugin);
});
carapace.use(plugins, function () {
var names = argv.plugin.map(function (plugin) {
return path.basename(plugin, '.js');
})
async.forEachSeries(names, function (plugin, next) {
if (!carapace[plugin]) {
return onPluginError({
plugin: plugin,
error: new Error('No plugin `' + plugin + '` was found after loading.')
});
}
carapace[plugin](argv[plugin], function (err) {
return !err ? next() : onPluginError({
plugin: plugin,
error: err
});
});
}, next);
});
}
function runAndReport () {
carapace.run(script, passthru, true, function () {
console.log('carapace has wrapped: ' + script);
function logArray (array, msg, delim) {
return array && array.length
? console.log(msg + array.join(delim))
: null;
}
logArray(passthru, 'with arguments: ', ' ');
logArray(Object.keys(carapace._module.exports), ' and exports: ', ', ');
});
}
return argv.plugin
? configure(runAndReport)
: runAndReport();
});