-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
91 lines (72 loc) · 1.8 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
//Import dependencies
var log = require('nutty-log');
//Import libs
var parse_args = require('./lib/args.js');
//nutty object
var nutty = {};
//Middlewares list
nutty._middlewares = [];
//Settings
nutty._settings = { name: '', description: '', version: '', body: true };
//Add a setting value
nutty.set = function(key, value)
{
//Check for undefined key or value
if(typeof key !== 'string' || typeof value === 'undefined'){ return; }
//Change the setting value
nutty._settings[key] = value;
};
//get a setting value
nutty.get = function(key)
{
//Check for undefined
if(typeof key !== 'string'){ return; }
//Return the settings value
return nutty._settings[key];
};
//Use a middleware
nutty.use = function(fn)
{
//Check for function
if(typeof fn !== 'function'){ return; }
//Save as a middleware
nutty._middlewares.push(fn);
//Continue
return;
};
//Run the cli
nutty.run = function()
{
//Check the number of middlewares
if(nutty._middlewares.length === 0)
{
//Display error in console and exit
return log.error('No middlewares on CLI');
}
//Parse the arguments
var args = parse_args();
//Middlewares recursive caller
var middlewares_recursive = function(index)
{
//Check the index value
if(index >= nutty._middlewares.length){ return; }
//Call the middleware
nutty._middlewares[index](args, function(error)
{
//Check for undefined error
if(typeof error === 'undefined'){ var error = null; }
//Check for error
if(error && error instanceof Error)
{
//Throw the error
throw error;
}
//Next middleware on the list
return middlewares_recursive(index + 1);
});
};
//Read all the middlewares recursive
return middlewares_recursive(0);
};
//Exports to node
module.exports = nutty;