-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.ts
94 lines (88 loc) · 3.11 KB
/
server.ts
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
import { Application, Router, helpers } from "https://deno.land/x/[email protected]/mod.ts"
import { getStringifiedTodos } from "./store.ts"
import config from "./xotodo.config.json" assert { type: "json" }
const app = new Application()
const router = new Router()
export function initServer() {
app.listen({ port: config.port })
console.log(`Listening on http://localhost:${config.port}`)
}
app.use(async (context, next) => {
try {
await context.send({
root: `web`,
index: "index.html",
})
} catch {
next()
}
})
router
.get("/api/todos", (ctx) => {
ctx.response.body = getStringifiedTodos()
})
.get("/api/file", (ctx) => {
const { path, line } = helpers.getQuery(ctx)
const content = Deno.readTextFileSync(path)
ctx.response.body = { file_path: path, content, selected_line: Number.parseInt(line) }
})
.post("/api/remove", (ctx) => {
const { path, line } = helpers.getQuery(ctx)
const content = Deno.readTextFileSync(path)
const lines = content.split('\n')
const index = Number.parseInt(line)
if (lines.length >= index && (lines[index].includes('OTODO:') || lines[index].includes('XTODO:'))) {
lines.splice(index, 1)
Deno.writeTextFileSync(path, lines.join('\n'))
ctx.response.body = "ok"
} else {
console.error("Invalid line number. No (XO)TODO found.")
Deno.exit()
}
})
.get("/api/open", (ctx) => {
const { path } = helpers.getQuery(ctx)
Deno.run({ cmd: ["open", path] })
ctx.response.body = "ok"
})
.get("/api/increment", (ctx) => {
const { path, line } = helpers.getQuery(ctx)
const content = Deno.readTextFileSync(path)
const lines = content.split('\n')
const index = Number.parseInt(line)
// example line would be "OTODO: this is a test @due: 2021-03-01 by midnight"
if (lines.length >= index && lines[index].includes('@due:')) {
try {
const dueDateStr = lines[index].split('@due:')[1].trim().substring(0, 10)
const dueDate = new Date(dueDateStr)
dueDate.setDate(dueDate.getDate() + 1)
const newDueDateStr = dueDate.toISOString().substring(0, 10)
lines[index] = lines[index].replace(dueDateStr, newDueDateStr)
Deno.writeTextFileSync(path, lines.join('\n'))
ctx.response.body = "ok"
} catch (error) {
console.error("Invalid date.", error)
Deno.exit()
}
} else {
console.error("Invalid line number. No @due: found.")
Deno.exit()
}
})
.get("/api/complete", (ctx) => {
const { path, line } = helpers.getQuery(ctx)
const content = Deno.readTextFileSync(path)
const lines = content.split('\n')
const index = Number.parseInt(line)
if (lines.length >= index && lines[index].includes('OTODO:')) {
lines[index] = lines[index].replace('OTODO:', 'XTODO:')
Deno.writeTextFileSync(path, lines.join('\n'))
ctx.response.body = "ok"
} else {
console.error("Invalid line number. No OTODO found.")
Deno.exit()
}
})
app.use(router.routes())
app.use(router.allowedMethods())
// Deno.run({ cmd: ["code", "-g", `${todo.filePath}:${todo.lineNumber || 0}`] })