-
Notifications
You must be signed in to change notification settings - Fork 0
/
floo.js
executable file
·94 lines (72 loc) · 2.48 KB
/
floo.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
#!/usr/bin/env node
'use strict';
var Portal = require('./lib/portal');
var nconf = require('nconf');
var fs = require('fs');
nconf.file(require('path').resolve(__dirname, 'config.json'));
var portal = new Portal();
if(process.argv.length == 2){
var flooName = nconf.get('name');
if(flooName) {
if(!process.stdin.isTTY) portal.setStream(process.stdin);
portal.startServer(flooName, nconf.get('port') || 1337, _exit);
}else{
_exit([
"\n\n\n\n",
'Hey this is Mike Xia, and thanks so much for trying floo!',
'feel free to contact me at [email protected] with questions/comments',
'Now name your computer using',
' `floo name some-name`',
'and enjoy using floo!!',
"\n\n\n\n"].join("\n"));
}
} else if(process.argv.length >= 3) {
var command = process.argv[2];
if(command == 'name' || command == '-n') {
if(process.argv.length == 4) {
_save('name', process.argv[3])
}else {
var name = nconf.get('name') || 'not set'
_exit('floo name:' + name);
}
}else if (command == 'list' || command == '-l') {
portal.list();
}else if (command == 'port' || command == '-p') {
if(process.argv.length == 4) {
_save('port', process.argv[3])
}else {
var port = nconf.get('port') || 'default 1337'
_exit('floo port:' + port);
}
}else if (command == 'help' || command == '-h') {
console.log([
'usage:',
'',
'floo name some-name name your floo (required!)',
'floo create a floo with your clipboard',
'cat bacon.jpg | floo create a floo with piped data',
'floo list list floos on network',
].join('\n'));
}else {
if(!process.stdout.isTTY) portal.setStream(process.stdout);
portal.startClient(command, _exit);
}
}
function _save(key, value) {
if(['name', 'list', 'help', 'port'].indexOf(value) >= 0) {
_exit('you cannot name your floo `' + value + '` silly ... thats a command ');
}else if(key == 'port' && parseInt(value) == 'NaN') {
_exit('please use a number for port');
}
nconf.set(key, value);
nconf.save(function (err) {
if (err) console.log('config save error', err);
else if (key == 'name') console.log('your fireplace is now named', value);
else if (key == 'port') console.log('your port is set to', value)
_exit();
});
}
function _exit(message) {
if(message) console.log(message);
process.exit(0);
}