-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.js
110 lines (103 loc) · 2.18 KB
/
plugin.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
var fs=require("fs");
(function()
{
var args=process.argv;
if (args.length<3)
{
console.log("not enough arguments to run this tool.");
process.exit(0);
}
var commandList=[{arg: "list",fn: List},
{arg: "install",fn: Install},
{arg: "remove",fn: Remove}];
for (var i=0;i<commandList.length;i++)
{
if (args[2]==commandList[i].arg)
{
setTimeout(commandList[i].fn,0,args);
return true;
}
}
console.log("Bad command.\r\nPlease use list, install and remove only.");
return false;
})();
function List(args)
{
fs.readFile("./plugins/config/list.conf",function(err,buf)
{
if (err)
{
console.log("list.conf has lost, please check it if it exists.");
process.exit(0);
}
var plugins=String(buf).split("|");
for (var i=0;i<plugins.length;i++)
{
console.log(i+"-->"+plugins[i]);
}
});
}
function Install(args)
{
fs.readFile("./plugins/config/list.conf",function(err,buf)
{
if (err)
{
console.log("list.conf has lost, please check it if it exists.");
process.exit(0);
}
var plugins=String(buf).split("|");
fs.readFile("./plugins/"+args[3]+".js",function(err,buf)
{
if (err)
{
console.log("Cannot find "+args[3]+".js in plugin folder.");
process.exit(0);
}
plugins.push(args[3]);
fs.writeFile("./plugins/config/list.conf",plugins.join("|"),function(err)
{
if (err)
{
console.log("Writing conf file failed!");
process.exit(0);
}
console.log("Plugin installed.\r\nPlease restart server.");
});
});
});
}
function Remove(args)
{
fs.readFile("./plugins/config/list.conf",function(err,buf)
{
if (err)
{
console.log("list.conf has lost, please check it if it exists.");
process.exit(0);
}
var plugins=String(buf).split("|");
function Delete(i)
{
plugins.splice(i,1);
fs.writeFile("./plugins/config/list.conf",plugins.join("|"),function(err)
{
if (err)
{
console.log("Writing conf file failed.");
process.exit(0);
}
console.log("Plugin "+args[3]+" has been removed.");
});
}
for (var i=0;i<plugins.length;i++)
{
if (plugins[i]==args[3])
{
Delete(i);
return true;
}
}
console.log("Cannot find "+args[3]+" module.");
});
}