forked from silverwind/droppy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
droppy.js
executable file
·225 lines (201 loc) · 6.43 KB
/
droppy.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#!/usr/bin/env node
"use strict";
const argv = require("minimist")(process.argv.slice(2), {
boolean: ["color", "d", "daemon", "dev"]
});
if (!argv.dev) {
process.env.NODE_ENV = "production";
}
if (require("util").inspect.defaultOptions) {
require("util").inspect.defaultOptions.depth = null;
}
const fs = require("graceful-fs");
const pkg = require("./package.json");
process.title = pkg.name;
process.chdir(__dirname);
const cmds = {
start : "start Start the server",
stop : "stop Stop all daemonized servers",
update : "update Self-Update (may require root)",
config : "config Edit the config",
list : "list List users",
add : "add <user> <pass> [p] Add or update a user. Specify 'p' for privileged",
del : "del <user> Delete a user",
build : "build Build client resources",
version : "version, -v Print version",
};
const opts = {
configdir : "-c, --configdir <dir> Config directory. Default: ~/.droppy/config",
filesdir : "-f, --filesdir <dir> Files directory. Default: ~/.droppy/files",
daemon : "-d, --daemon Daemonize (background) process",
log : "-l, --log <file> Log to file instead of stdout",
dev : "--dev Enable developing mode",
color : "--color Force-enable colored log output",
nocolor : "--no-color Force-disable colored log output",
};
if (argv.v || argv.V || argv.version) {
console.info(pkg.version);
process.exit(0);
}
if (argv.daemon || argv.d) {
require("daemon")();
}
if (argv._[0] === "build") {
console.info("Building resources ...");
require("./server/resources.js").build(function(err) {
console.info(err || "Resources built successfully");
process.exit(err ? 1 : 0);
});
}
if (argv.configdir || argv.filesdir || argv.c || argv.f) {
require("./server/paths.js").seed(argv.configdir || argv.c, argv.filesdir || argv.f);
}
if (argv.log || argv.l) {
const ut = require("untildify");
const path = require("path");
try {
require("./server/log.js").setLogFile(fs.openSync(ut(path.resolve(argv.log || argv.l)), "a", "644"));
} catch (err) {
console.error("Unable to open log file for writing: " + err.message);
process.exit(1);
}
}
if (!argv._.length) {
printHelp();
process.exit(0);
}
const cmd = argv._[0];
const args = argv._.slice(1);
if (cmds[cmd]) {
let db;
if (cmd === "start") {
require("./server/server.js")(null, true, argv.dev, function(err) {
if (err) {
require("./server/log.js").error(err);
process.exit(1);
}
});
} else if (cmd === "stop") {
const ps = require("ps-node");
const log = require("./server/log.js");
ps.lookup({command: pkg.name}, function(err, procs) {
if (err) {
log.error(err);
process.exit(1);
} else {
procs = procs.filter(proc => Number(proc.pid) !== process.pid);
if (!procs.length) {
log.info("No processes found");
process.exit(0);
}
require("async").map(procs, function(proc, cb) {
ps.kill(proc.pid, function(err) {
if (err) return cb(err);
cb(null, proc.pid);
});
}, function(err, pids) {
if (err) {
log.error(err);
process.exit(1);
} else {
pids.forEach(function(pid) {
console.info("Killed PID " + pid);
process.exit(0);
});
}
});
}
});
} else if (cmd === "version") {
console.info(pkg.version);
} else if (cmd === "update") {
require("./server/update.js")(pkg, function(err, message) {
if (err) { console.error(new Error(err.message || err).stack); process.exit(1); }
if (message) { console.info(message); process.exit(0); }
});
} else if (cmd === "config") {
const paths = require("./server/paths.js").get();
const cfg = require("./server/cfg.js");
const edit = function() {
findEditor(function(editor) {
if (!editor) return console.error("No suitable editor found, please edit " + paths.cfgFile);
require("child_process").spawn(editor, [paths.cfgFile], {stdio: "inherit"});
});
};
fs.stat(paths.cfgFile, function(err) {
if (err && err.code === "ENOENT") {
require("mkdirp")(paths.config, function() {
cfg.init(null, function(err) {
if (err) return console.error(new Error(err.message || err).stack);
edit();
});
});
} else {
edit();
}
});
} else if (cmd === "list") {
db = require("./server/db.js");
db.load(function() {
printUsers(db.get("users"));
});
} else if (cmd === "add") {
if (args.length !== 2 && args.length !== 3) return printHelp();
db = require("./server/db.js");
db.load(function() {
db.addOrUpdateUser(args[0], args[1], args[2] === "p", function() {
printUsers(db.get("users"));
});
});
} else if (cmd === "del") {
if (args.length !== 1) return printHelp();
db = require("./server/db.js");
db.load(function() {
db.delUser(args[0], function() {
printUsers(db.get("users"));
});
});
}
} else {
printHelp();
}
function printHelp() {
let help = "Usage: " + pkg.name + " command [options]\n\n Commands:";
Object.keys(cmds).forEach(function(command) {
help += "\n " + cmds[command];
});
help += "\n\n Options:";
Object.keys(opts).forEach(function(option) {
help += "\n " + opts[option];
});
console.info(help);
}
function printUsers(users) {
if (Object.keys(users).length === 0) {
console.info("No users defined. Use 'add' to add one.");
} else {
console.info("Current Users:\n" + Object.keys(users).map(function(user) {
return " - " + user;
}).join("\n"));
}
}
function findEditor(cb) {
const editors = ["vim", "nano", "vi", "npp", "pico", "emacs", "notepad"];
const basename = require("path").basename;
const which = require("which");
const userEditor = basename(process.env.VISUAL || process.env.EDITOR);
if (editors.indexOf(userEditor) === -1) {
editors.unshift(userEditor);
}
(function find(editor) {
try {
cb(which.sync(editor));
} catch (err) {
if (editors.length) {
find(editors.shift());
} else {
cb();
}
}
})(editors.shift());
}