-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·121 lines (115 loc) · 3.48 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
111
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/env node
require.all = require('require.all');
var chalk = require('chalk');
var program = require('commander');
var Datastore = require("nedb");
var fs = require('fs');
var path = require('path');
// globals
global.__base = __dirname;
var _scriptsDir = path.join(__dirname, './scripts');
var controller = {
program: program,
db: {
load: function(name) {
controller.db.name = name;
var dbPath = path.join(controller.db.path, name + ".db");
controller.db[name] = new Datastore({ filename: dbPath, autoload: true });
},
name: "",
path: path.join(global.__base, 'data'),
exec: {
insert: function(itemKey, value, callback) {
if (callback == null) callback = controller.response;
var document = { item: itemKey, value: value };
var onInserted = function(err, doc) {
if (!err) {
callback(doc);
}
else {
callback(null, err);
}
};
controller.db[controller.db.name].insert(document, onInserted);
},
findOne: function(itemKey, callback) {
if (callback == null) callback = controller.response;
var onFound = function(err, doc) {
if (!err) {
if (doc != null) {
callback(doc);
}
}
else {
callback(null, err);
}
};
controller.db[controller.db.name].findOne({ item: itemKey }, onFound);
},
removeByKey: function(itemKey, callback) {
if (callback == null) callback = controller.response;
var onRemoved = function(err, count) {
if (!err) {
callback(count + " documents removed.");
}
else {
callback(null, err);
}
};
controller.db[controller.db.name].remove({ item: itemKey }, { multi: true }, onRemoved);
}
}
},
response: function(msg, err, color) {
var log = function(txt) {
if (color in chalk) {
console.log(chalk[color](txt));
}
else {
console.log(err ? chalk.red(txt) : txt);
}
};
if (!err) {
log(msg);
}
else {
log(err);
}
},
getOptionOrDefault: function(option, defaultValue, defaultType) {
defaultType = defaultType || 'string';
return typeof option === defaultType ? option : defaultValue;
},
getTemplateFile: function(template, file, callback) {
var templatePath = path.join(global.__base, "templates/" + template + "/" + file);
if (!fs.existsSync(templatePath)) {
callback(null, `${templatePath} template does not exist. see help for more information.`);
}
else {
fs.readFile(templatePath, {encoding: 'utf-8'}, function(err, data) {
if (err) {
return callback(null, err);
}
callback(data, null);
});
}
},
createTemplateFile: function(fileName, data, callback, templateType) {
templateType = templateType ? templateType : 'Template file';
fs.writeFile(fileName, data, function(err) {
if (err) {
return callback(null, err);
}
callback(`${templateType} created: ${fileName}`);
});
}
};
// require.all scripts in the _scriptsDir directory that follow the naming convention [tool name]-tool.js
var scripts = require.all({ dir: _scriptsDir, match: /-tool\.js/i })(controller);
program
.command("*")
.action(function(cmd){
//console.log(options);
controller.response('command not found "' + cmd + '"');
});
program.parse(process.argv);