forked from silverwind/droppy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
droppy.js
executable file
·190 lines (166 loc) · 5.07 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
#!/usr/bin/env node
"use strict";
var argv = require("minimist")(process.argv.slice(2), {
boolean: ["color", "d", "daemon", "dev"]
});
if (!argv.dev) {
process.env.NODE_ENV = "production";
}
var fs = require("graceful-fs");
var pkg = require("./package.json");
process.title = pkg.name;
process.chdir(__dirname);
var cmds = {
start : "start Start the server",
update : "update Self-Update (may require root)",
config : "config Edit the config",
list : "list List users",
add : "add <user> <pass> Add a user",
del : "del <user> Delete a user",
build : "build Build client resources",
version : "version, -v Print version",
};
var 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 color in terminal",
nocolor : "--no-color Force disable color in terminal",
};
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.log("Building resources ...");
require("./server/resources.js").build(function(err) {
console.log(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) {
var ut = require("untildify");
var 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);
}
var cmd = argv._[0];
var args = argv._.slice(1);
if (cmds[cmd]) {
var db;
switch (cmd) {
case "start":
require("./server/server.js")(null, true, argv.dev, function(err) {
if (err) {
require("./server/log.js").error(err);
process.exit(1);
}
});
break;
case "version":
console.info(pkg.version);
break;
case "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); }
});
break;
case "config":
var paths = require("./server/paths.js").get();
var cfg = require("./server/cfg.js");
var edit = function edit() {
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();
}
});
break;
case "list":
db = require("./server/db.js");
db.init(function() {
printUsers(db.get("users"));
});
break;
case "add":
if (args.length !== 2) return printHelp();
db = require("./server/db.js");
db.init(function() {
db.addOrUpdateUser(args[0], args[1], true, function() {
printUsers(db.get("users"));
});
});
break;
case "del":
if (args.length !== 1) return printHelp();
db = require("./server/db.js");
db.init(function() {
db.delUser(args[0], function() {
printUsers(db.get("users"));
});
});
break;
}
} else {
printHelp();
}
function printHelp() {
var 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) {
console.info("Current Users: " + Object.keys(users).join(", "));
}
function findEditor(cb) {
var editors = ["vim", "nano", "vi", "npp", "pico", "emacs", "notepad"];
var basename = require("path").basename;
var which = require("which");
var 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());
}