This repository has been archived by the owner on Jul 14, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
59 lines (47 loc) · 2.11 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
var EventEmitter2 = require('eventemitter2').EventEmitter2,
util = require('util'),
fs = require('fs'),
path = require('path'),
extend = require('extend'),
shellHelpers = require('./utils/shellHelpers'),
execute = require('./utils/execute'),
defaultSettings = require('./settings');
// Define the shell object.
module.exports.Shell = function (options) {
// Alias 'this' so we can access in other scopes.
var shell = this;
// Invoke event emitter constructor.
EventEmitter2.call(this);
// Attach settings to shell.
shell.settings = extend(true, {}, defaultSettings, options);
// This property will store all the available command modules.
shell.cmds = {};
// Initialize shell helper methods.
shellHelpers.loadHelpers(shell);
// If shotgun is running in the browser then don't auto-load command modules.
if (typeof(window) === 'undefined') {
// Load custom command modules.
shell.loadCommandModules(shell.settings.cmdsDir);
// Load command modules installed from npm.
if (shell.settings.loadNpmCmds) {
var nodeModulesDir = path.resolve(__dirname, '..');
var nodeModules = fs.readdirSync(nodeModulesDir);
if (nodeModules)
nodeModules.forEach(function (module) {
if (module.indexOf('shotguncmd-') === 0)
shell.loadCommandModule(path.resolve(nodeModulesDir, module));
});
}
}
// Load default command modules.
if (shell.settings.defaultCmds.help && !shell.cmds.help)
shell.registerCmd('help', require('./default_cmds/help.js'));
if (shell.settings.defaultCmds.exit && !shell.cmds.exit)
shell.registerCmd('exit', require('./default_cmds/exit.js'));
if (shell.settings.defaultCmds.clear && !shell.cmds.clear)
shell.registerCmd('clear', require('./default_cmds/clear.js'));
// Define the execute function that the application will call and pass in user input.
shell.execute = execute;
};
// Make Shell an EventEmitter2.
util.inherits(module.exports.Shell, EventEmitter2);