1
0
Fork 0
auth.rxbn.de/bin/cli/module.js

91 lines
2.5 KiB
JavaScript

/*
* This file is part of the authRXBN single sign-on package.
*
* (c) Ruben Meyer <contact@rxbn.de>
*/
let vorpal = require('vorpal')();
let fs = require('fs');
/**
* read command files and interpret them
* @author Ruben Meyer
* @todo options, types, hide command, parsing, help, autocompletion, allow unknown options
*/
let cmdPath = global['__dirname']+'/bin/cli/cmds';
fs.readdir(cmdPath, (err, files) => {
if(files.length > 0)
files.forEach((file) => {
let cmd = require(cmdPath+'/'+file); // read file
// exported data is an object
if(typeof cmd == 'object' && typeof cmd.command !== 'undefined') {
// set initial building steps
let builder = vorpal.command(cmd.command);
// description
if(typeof cmd.description !== 'undefined') builder = builder.description(cmd.description);
// aliases
if(typeof cmd.alias !== 'undefined') {
if(typeof cmd.alias === 'object' && Array.isArray(cmd.alias)) builder = builder['alias'](...cmd.alias);
if(typeof cmd.alias === 'string' && cmd.alias.split(',').length >= 2) {
let args = cmd.alias.split(',');
for(let i = 0; i < args.length; i++)
args[i] = args[i].trim();
builder = builder['alias'](...cmd.alias);
}
}
// action
if(typeof cmd.action !== 'undefined' && typeof cmd.action === 'function') {
if(typeof cmd.actionDependencies !== 'undefined') {
let dependencies = [];
let actionDependencies = {};
// format input
if(typeof cmd.actionDependencies === 'object') {
if(Array.isArray(cmd.actionDependencies))
cmd.actionDependencies.forEach((dependency) => dependencies.push(dependency));
} else if(typeof cmd.actionDependencies === 'string') {
let strArr = cmd.actionDependencies.split(',');
for(let i = 0; i < strArr.length; i++)
dependencies.push(strArr[i].trim());
}
// retrieve dependencies; unknown dependencies wont be handled
dependencies.forEach((dependency) => {
switch(dependency) {
case 'vorpal':
actionDependencies['vorpal'] = vorpal;
break;
}
});
builder = builder['action'](cmd.action(actionDependencies));
} else {
builder = builder['action'](cmd.action);
}
}
}
});
});
/**
@TODO remove code
isJson = (str) => {
try {
let o = JSON.parse(str);
return (o && typeof o === "object") ? true : false;
} catch (e) {
return false;
}
};
*/
vorpal.delimiter('auth@rxbn$').show();
module.exports = vorpal;