-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.js
147 lines (119 loc) · 3.08 KB
/
main.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
var readline = require('readline')
var fs = require('fs')
var exec = require('child_process').exec
var rimraf = require('rimraf').sync
var path = require('path')
var uuid = require('node-uuid').v4
if (process.argv.length > 2) {
var username = process.argv[2]
} else {
console.log('Please provide your username as the first argument.')
process.exit()
}
var repo = 'https://git-chat-client:[email protected]/git-chat-client/git-chat-messages.git'
var dir = process.argv.length > 3 ? process.argv[3] : path.join(process.env.HOME, '.git-chat')
var uuidFile = path.join(dir, 'uuid.txt')
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: false
})
var cleaningUp = false
var commit = null
rimraf(dir)
exec('git clone ' + repo + ' ' + dir, function(error, stdout, stderr) {
if (error) {
throw new Error(error)
}
getCommit()
console.log(stdout)
console.log(stderr)
console.log("Welcome to git-chat, the chat client built on git!")
console.log("Type something in to join the conversation!")
console.log("")
push(username + " has joined")
prompt()
rl.on('line', function(line) {
prompt()
push(username + ": " + line)
})
})
var pullInterval = setInterval(pull, 1000)
rl.on('close', cleanup)
process.on('exit', function() {
rl.close()
})
process.on('SIGINT', function() {
rl.close()
})
function cleanup() {
process.stdout.clearLine()
process.stdout.cursorTo(0)
console.log("Cleaning up...")
cleaningUp = true
clearInterval(pullInterval)
push(username + " has left", function() {
setTimeout(function () {
rimraf(dir)
process.exit()
}, 1000)
})
}
function prompt() {
process.stdout.write(">> ")
}
function execInDir(cmd, fn, quiet) {
exec('cd ' + dir + ' && ' + cmd, function(error, stdout) {
if (!quiet && error) {
console.log(error)
}
if (fn) {
fn(stdout, error)
}
})
}
function push(line, fn) {
fs.writeFileSync(uuidFile, uuid())
execInDir('git add . && git commit -am "' + line + '"', function() {
function doPush() {
getCommit()
execInDir('git pull && git push', function(stdout, error) {
// Keep trying til we get it
if (error) {
doPush()
} else if(fn) {
fn()
}
}, true)
}
doPush()
}, true)
}
function pull() {
execInDir("git pull", function() {
var cmd = 'git log --format=" %H :%s" ' + commit + '..HEAD'
execInDir(cmd, function(log) {
if (log.length > 0) {
process.stdout.clearLine()
process.stdout.cursorTo(0)
var sections = log.split('\n')
if (sections.length > 0) {
exec("( speaker-test -t sine -f 1000 )& pid=$! ; sleep 0.1s ; kill -9 $pid")
}
sections.forEach(function(line) {
if (line.length > 0) {
var msg = line.split(':').slice(1).join(":")
console.log(msg)
}
})
getCommit()
prompt()
}
})
}, true)
}
function getCommit() {
execInDir('git rev-parse HEAD', function(stdout) {
commit = stdout.replace('\n', '');
})
}