This repository has been archived by the owner on Jun 10, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
assistant-cli.js
73 lines (69 loc) · 2.63 KB
/
assistant-cli.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
// pour utiliser assistant-plugins depuis une ligne de commande
// Commande : node assistant-cli.js Commande
//
// Exemple : node assistant-cli.js "notifier_ceci est un message de test"
var path = require("path");
var dirname = '../../';
var configuration = require(path.join(dirname,'configuration'));
var plugins={}, addons=[], dependencies;
function PromiseChain(arr, fct) {
var dfd = Promise.resolve();
var res = arr.map(function(a) {
dfd = dfd.then(function() {
return fct(a)
});
return dfd
});
return Promise.all(res)
}
// on va définir un plugin "assistant" pour modifier la configuration
plugins.assistant = {
saveConfig:function(plugin, config) {
if (plugin && config) {
var configuration = require(path.join(dirname,'configuration'));
configuration.plugins[plugin] = config;
var jsonfile = require('jsonfile');
jsonfile.writeFile(path.join(dirname, 'configuration.json'), configuration, {spaces: 2, EOL: '\r\n'}, function(err) {
if (err) console.error("[assistant-"+plugin+"] Erreur lors de la sauvegarde de la configuration : "+err);
else console.log("[assistant-"+plugin+"] Configuration sauvegardée.");
})
}
}
};
// chargement des plugins
dependencies = require(path.join(dirname,"package")).dependencies;
for (var plugin in dependencies) {
if (plugin.startsWith("assistant-") && plugin !== "assistant-plugins") {
addons.push(plugin.slice(10));
}
}
var packageCurrent = require(path.join(dirname,'/node_modules/assistant-plugins/package'));
PromiseChain(addons, function(plugin) {
plugin = plugin.trim();
var packagePlugin = require(path.join(dirname,'/node_modules/assistant-'+plugin+'/package'));
console.log("[assistant] Chargement du plugin '"+plugin+"' (v"+packagePlugin.version+")");
return require(path.join(dirname,'/node_modules/assistant-'+plugin)).init(configuration.plugins[plugin], plugins)
.then(function(resource) {
plugins[plugin] = resource;
})
})
.then(function() {
var commandes = process.argv[2].split("|");
console.log("[assistant] Commande reçue: ",commandes);
return PromiseChain(commandes, function(commande) {
// on regarde le keyword et on transmet au plug associé
var plugin = commande.split("_")[0];
if (!plugins[plugin]) {
console.log("[assistant] Erreur : la commande « "+commande+" » a été reçue, cependant le plugin '"+plugin+"' n'a pas été chargé !");
} else {
console.log("[assistant] Appel du plugin '"+plugin+"'");
return plugins[plugin].action(commande.split("_").slice(1).join("_"));
}
})
})
.then(function() {
process.exit()
})
.catch(function(err) {
console.log(err)
})