forked from silverwind/droppy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
droppy.js
executable file
·220 lines (196 loc) · 6.01 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
#!/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",
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("daemonize-process")();
}
if (argv._[0] === "build") {
console.info("Building resources ...");
require("./server/resources.js").build(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, 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}, (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, (proc, cb) => {
ps.kill(proc.pid, err => {
if (err) return cb(err);
cb(null, proc.pid);
});
}, (err, pids) => {
if (err) {
log.error(err);
process.exit(1);
} else {
pids.forEach(pid => {
console.info("Killed PID " + pid);
process.exit(0);
});
}
});
}
});
} else if (cmd === "version") {
console.info(pkg.version);
} else if (cmd === "config") {
const paths = require("./server/paths.js").get();
const cfg = require("./server/cfg.js");
const edit = function() {
findEditor(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, err => {
if (err && err.code === "ENOENT") {
require("mkdirp")(paths.config, () => {
cfg.init(null, 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(() => {
printUsers(db.get("users"));
});
} else if (cmd === "add") {
if (args.length !== 2 && args.length !== 3) printHelp();
db = require("./server/db.js");
db.load(() => {
db.addOrUpdateUser(args[0], args[1], args[2] === "p", () => {
printUsers(db.get("users"));
});
});
} else if (cmd === "del") {
if (args.length !== 1) printHelp();
db = require("./server/db.js");
db.load(() => {
db.delUser(args[0], () => {
printUsers(db.get("users"));
});
});
}
} else {
printHelp();
}
function printHelp() {
let help = "Usage: " + pkg.name + " command [options]\n\n Commands:";
Object.keys(cmds).forEach(command => {
help += "\n " + cmds[command];
});
help += "\n\n Options:";
Object.keys(opts).forEach(option => {
help += "\n " + opts[option];
});
console.info(help);
process.exit();
}
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(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.includes(userEditor)) {
editors.unshift(userEditor);
}
(function find(editor) {
try {
cb(which.sync(editor));
} catch (err) {
if (editors.length) {
find(editors.shift());
} else {
cb();
}
}
})(editors.shift());
}