forked from s4y/rorrim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rorrim
executable file
·91 lines (83 loc) · 2.28 KB
/
rorrim
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
#!/usr/bin/env node
var fs = require('fs'),
server = require('./server').app,
r = require('./lib'),
optimist = require('optimist')
.usage("Serve local cache of online content.\nUsage: $0")
.boolean('t')
.alias('t', 'test')
.describe('t', 'Run the hoopty tests')
.alias('i', 'install')
.describe('i', 'Install new file into rorrim folder -- Not implemented yet')
.boolean('l')
.alias('l', 'ls')
.alias('l', 'list')
.describe('l', 'List installed files')
.boolean('h')
.alias('h', 'help')
.alias('h', '?')
.describe('h', 'Show this usage info'),
argv = optimist.argv;
if(argv.help){
optimist.showHelp();
process.exit(0);
}
process.title = "rorrim";
r.checkInit();
r.backupHostFile();
if(argv.test){ //Begin hoopty testing
console.log("Testing empty array handling of writeHostFile");
r.writeHostFile([]);
r.revertHostFile();
console.log(" done with 'empty array test'\n");
console.log("r.walkRorrimFolder():");
console.log(r.walkRorrimFolder());
r.writeHostFile(r.walkRorrimFolder());
console.log("Result after walkRorrimFolder:\n" + fs.readFileSync(r.hostfile, 'utf8'));
r.revertHostFile();
r.writeHostFile('cdnjs.cloudflare.com');
console.log("Result after call with 'cdnjs.cloudflare.com':\n" + fs.readFileSync(r.hostfile, 'utf8'));
r.revertHostFile();
}
if(argv.install){
if(argv.install === true){
optimist.showHelp();
process.exit(1);
}
r.install(argv.install);
//process.exit(0);
}
if(argv.list){
console.log(r.walkRorrimFolder());
process.exit(0);
}
try {
server.listen('80');
} catch(e) {
if(e.code == 'EACCES'){
console.log("It looks like you need root privileges to start a server on port 80.\n" +
"Try again with `sudo rorrim`");
r.revertHostFile();
process.exit(13);
} else {
r.revertHostFile();
throw e;
}
}
process.on('SIGINT', function(){
//If we get SIGINT, clean up after ourselves
console.log("Caught SIGINT");
r.revertHostFile();
server.close();
process.exit();
});
process.on('uncaughtException', function(err){
try{
console.warn("Uh Oh! uncaughtExeption! Lemme try to revert your hostfile");
r.revertHostFile();
} catch(e){
console.warn("Looks like something really bad happened!\nHere's your original hostfile:\n" + r.origHostString);
}
throw err;
});
// vim: filetype=javascript