-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserver.js
executable file
·165 lines (134 loc) · 4.6 KB
/
server.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
#!/usr/bin/env node
const body_parser = require('body-parser')
const cors = require('cors')
const express = require('express')
const proxy = require('express-http-proxy');
const {spawn} = require('child_process');
const readline = require('readline')
const PORT = process.env.PORT || 5000
const TASK = 'task'
const TASKOPTS = ['rc.gc=off', 'rc.json.array=on']
const DEVSERVER = 'http://localhost:8000' // Proxies this from /dev (and allows CORS)
const ALWAYS_FILTER = '(+PENDING or (+WAITING +SCHEDULED))'
const api = express()
api.use(body_parser.json())
api.use(cors({origin: DEVSERVER}))
api.get('/tasks', (req, res) => {
let filter = req.query.filter
// This bit is terrible.
// But the alternative is having everything again in memory.
let filter_s = (filter) ? ("\"filter\":\"" + filter + "\",") : ""
res.write("{" + filter_s + "\"tasks\":")
export_filtered_tasks(filter, {
on_data: (chunk) => res.write(chunk),
on_exit: (c, s) => {
res.write("}") &&
res.end(c || s ? format_status(c, s) : '')
}
})
})
api.post('/tasks', (req, res) => {
import_task(req.body, {on_exit: (c, s) => res.end(format_status(c, s))})
})
api.get('/timew', (req, res) => {
check_timew({
on_exit: (timew_present) => res.json({enabled: timew_present})
})
})
const server = express()
server.use(express.static('.'))
server.use('/api', api)
server.use('/dev', proxy(DEVSERVER))
server.listen(PORT, () => console.log(`listening on port ${PORT}`))
const export_filtered_tasks = (filter, callbacks) => {
tw_active_filter({
on_exit: (f) => export_tasks(`${f} ${filter || ''}`, callbacks)
})
}
const export_tasks = (filter, {on_data, on_exit}) => {
const f = `${ALWAYS_FILTER} ${filter || ''}`
const tw = spawn_tw([f, 'export'])
tw.stdout.on('data', on_data)
tw.on('exit', on_exit)
}
const import_task = ({command, task}, {on_exit}) => {
// Not using task import because it eats values not present in the imported data and we REALLY DON'T want that.
if (!task.uuid || task.uuid.length != 36) return on_exit(0, "UUID not present or invalid")
let cmd = [task.uuid, 'rc.recurrence.confirmation=no']
// Check command is in whitelist
if (!['mod', 'done', 'start', 'stop'].includes(command))
return on_exit(0, "Unsupported command");
cmd.push(command);
// Special case for modified attributes
if (command == 'mod') {
for (var attr in task) if (task.hasOwnProperty(attr) && attr != 'uuid') cmd.push(attr+':'+(task[attr] || ''))
}
const tw = spawn_tw(cmd)
console.log("[import] running command: task " + cmd.join(' '))
tw.on('exit', on_exit)
}
const format_status = (code, status) => `{"status": "${status || code || ''}"}`
const check_timew = ({on_exit}) => {
const tw = spawn_tw(['diagnostics'])
const rl = readline.createInterface({
input: tw.stdout
})
let response_sent = false
rl.on('line', (line) => {
if (line.includes('on-modify.timewarrior')) {
on_exit(true)
response_sent = true
}
})
rl.on('close', () => {
if (!response_sent) on_exit(false)
})
}
const spawn_tw = (opts) => {
return spawn(TASK, TASKOPTS.concat(opts))
}
const tw_get = (dom, {on_exit}) => {
const tw = spawn_tw(['_get', dom]).stdout
let output = ''
tw.on('data', (data) => {
if (data) output += data.toString()
})
tw.on('close', () => {
on_exit(output.trim())
})
}
const tw_active_context = ({on_exit}) => {
// Get active context name
tw_get('rc.context', {on_exit})
}
const tw_context_filter = (context, {on_exit}) => {
// Get the filter associated with a given context
if (!context) return on_exit('')
tw_get(`rc.context.${context}`, {
on_exit: (f) =>
on_exit((f) ? `(${f})` : '')
})
}
const tw_active_context_filter = ({on_exit}) => {
// Get active context filter
tw_active_context({
on_exit: (context) =>
tw_context_filter(context, {on_exit})
})
}
const tw_report_filter = (report, {on_exit}) => {
// Get the filter associated with a given report
tw_get(`rc.report.${report}.filter`, {
on_exit: (f) => on_exit((f) ? `(${f})` : '')
})
}
const tw_active_filter = ({on_exit}) => {
// Get the combination of:
// Current context filter AND next report filter
tw_report_filter('next', {
on_exit: (report_f) =>
tw_active_context_filter({
on_exit: (context_f) => on_exit(`${context_f} ${report_f}`)
})
})
}