forked from nhoss2/nodewiki
-
Notifications
You must be signed in to change notification settings - Fork 3
/
nodewiki.js
executable file
·222 lines (191 loc) · 5.63 KB
/
nodewiki.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
#!/usr/bin/env node
var fs = require("fs");
var http = require("http");
var path = require("path");
var connect = require("connect");
var socketio = require("socket.io");
var mod_getopt = require('posix-getopt');
var mdserver = require("./lib/mdserver");
var getDir = require("./lib/getDir");
// Defaults
var portNumberDefault = process.env.PORT || 8888;
var listenAddr = process.env.NW_ADDR || ""; // "" ==> INADDR_ANY
exports.gitMode = false; // exported for lib/mdserver.js
var portNumber = portNumberDefault;
// Process command line
var parser, option;
parser = new mod_getopt.BasicParser('a:(addr)g(git)h(help)l(local)p:(port)', process.argv);
while ((option = parser.getopt()) !== undefined) {
switch (option.option) {
case 'a':
listenAddr = option.optarg;
break;
case 'g':
if (fs.existsSync(process.cwd() + '/.git')) {
exports.gitMode = true;
} else {
console.log(
'ERROR: No git repository found\n',
'\'--git\' requires a git repository in the current directory.\n',
'Type "git init" to create one.');
process.exit(1);
}
break;
case 'h':
showHelp();
process.exit(0);
break;
case 'l':
if (listenAddr !== "") {
console.log("ERROR: Conflicting use of --addr and --local.\n",
"Use only one.");
process.exit(1);
}
listenAddr = "localhost";
break;
case 'p':
var argPort = parseInt(option.optarg);
if (typeof argPort == 'number' && argPort > 0) {
portNumber = argPort;
} else {
console.log('ERROR: %s is not a valid port number.\n', option.optarg);
process.exit(1);
}
break;
default:
/* error message already emitted by getopt() */
console.assert('?' == option.option);
showUsage();
process.exit(1);
break;
}
}
function showHelp() {
console.log('Node Wiki', '\n---------');
showUsage();
}
function showUsage() {
console.log(
'usage: nodewiki [--addr=<addr> | --local] [--git] [--help] [--port=<portnumber>]\n',
' -a | --addr IPv4 listen address (default = any)\n',
' -g | --git Commit each save to a git repository\n',
' -h | --help Print this message\n',
' -l | --local Listen on "localhost" (127.0.0.1) only.\n',
' -p | --port Use the specified port'
);
}
/*
* For backwards compatibility, handle argument style (no '--' or '-')
* options. Implmented for original options only: git, help, and <portNum>.
*/
for (var argn = parser.optind(); argn < process.argv.length; argn++) {
var arg = process.argv[argn];
if (arg == "git") {
exports.gitMode = true;
} else if (arg == "help") {
showHelp();
process.exit(1);
} else if (typeof parseInt(arg) == 'number' && (arg = parseInt(arg)) > 0) {
if (portNumber != portNumberDefault) {
console.log("WARNING: Overriding previous port number, %d", portNumber);
}
portNumber = arg;
} else {
console.log("WARNING: Unknown argument: %s", arg);
}
}
// end of command line processing
var app = connect();
app.use(connect.logger('dev'));
app.use(connect.static(__dirname + '/static'));
app.use('/', function (req, res) {
res.end(fs.readFileSync(__dirname + '/static/index.html', 'utf-8'));
});
var server = http.createServer(app);
server.listen(portNumber, listenAddr);
io = socketio.listen(server);
io.set('log level', 2);
io.sockets.on('connection', function (socket) {
var currentPath = process.cwd() + '/';
var dir = getDir.getDir(currentPath);
var links = getDir.parseLinks(dir);
var directoryDepth = 0;
var dirFolders = []; // array to hold the names of all folders in current directory
dir.forEach(function (i) {
if (i.folder === true) {
dirFolders.push(i.name);
}
});
socket.emit('navLinks', {
links: links
});
socket.on('readFile', function (file) {
console.log('readFile recieved - ' + file.name);
if (dirFolders.indexOf(file.name) > -1) { // checks if request is in the dirFolders array (meaning that the request is for a folder)
currentPath += file.name;
refreshDir();
directoryDepth += 1;
links = getDir.parseLinks(dir, directoryDepth);
mdserver.readFolder(links, socket);
} else {
mdserver.sendFile(file, currentPath, socket);
}
});
socket.on('disconnect', function () {
// if a user disconnects, reinitialise variables
var currentPath = process.cwd() + '/';
refreshDir();
var links = getDir.parseLinks(dir);
var directoryDepth = 0;
});
socket.on('saveFile', function (file) {
console.log('saveFile recieved, file: ' + file.name);
mdserver.saveFile(file, currentPath, socket);
});
socket.on('goBackFolder', function () {
if (directoryDepth > 0) {
currentPath = currentPath.substr(0, currentPath.substr(0, currentPath.length - 1).lastIndexOf('/')) + '/'; // removes current directory form the currentPath variable
refreshDir();
directoryDepth -= 1;
links = getDir.parseLinks(dir, directoryDepth);
mdserver.readFolder(links, socket);
}
});
socket.on('refreshNav', function () {
refreshNavLinks();
});
socket.on('newFolder', function (folderName) {
fs.mkdir(currentPath + folderName, 0777, function (err) {
if (err) {
socket.emit('newFolderReply', err);
} else {
refreshNavLinks();
}
});
});
function refreshNavLinks() {
refreshDir();
links = getDir.parseLinks(dir, directoryDepth);
socket.emit('navLinks', {
links: links
});
}
function refreshDir() {
dir = getDir.getDir(currentPath);
if (typeof dir != 'undefined') {
dir.forEach(function (i) {
if (i.folder === true) {
dirFolders.push(i.name);
}
});
}
}
});
if (exports.gitMode === true) {
console.log('Using git mode.');
}
if (listenAddr !== "") {
console.log("server started, addr:port = %s:%s", listenAddr, portNumber);
} else {
console.log("server started, port = " + portNumber);
}