-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcache.js
116 lines (102 loc) · 2.87 KB
/
cache.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
'use strict'
const sh = require('shelljs')
const alfy = require('alfy')
const tyme = require('tyme2')
const cache = module.exports
const TYME_PATH = '~/Library/Application Scripts/de.lgerckens.Tyme2/'
const TYME_HOOKS_FILE = 'tyme2_applescript_hooks.scpt'
cache.createTaskOutput = () => {
Promise.all([tyme.projects(), tyme.tasks()])
.then(data => {
const [projects, tasks] = data
const items = tasks
.sort((a, b) => {
return new Date(b.lastUpdate) - new Date(a.lastUpdate)
})
.map(task => {
const index = projects.findIndex(
obj => obj.id === task.relatedprojectid
)
const project = projects[index]
return {
title: task.name,
subtitle: project ? project.name : '',
variables: {
task: JSON.stringify(task),
},
}
})
alfy.cache.set('handler.getTasks', items)
})
.catch(console.log)
}
cache.createTasksNotesOutput = () => {
tyme
.tasks()
.then(tasks =>
Promise.all(tasks.map(task => tyme.taskRecordsByTaskId(task.id, 10)))
)
.then(tasks => {
tasks.forEach(item => {
const task = item.task
const taskRecords = item.taskRecords
const items = [
{
title: '-- Add new note --',
},
...taskRecords
.sort((a, b) => new Date(b.timeend) - new Date(a.timeend))
.map(taskRecord => taskRecord.note)
.filter((elem, pos, arr) => {
return arr.indexOf(elem) == pos
})
.map(note => ({
title: note,
arg: note,
})),
]
alfy.cache.set(`handle.getNotes.${task.id}`, items)
})
})
.catch(console.log)
}
cache.updateProject = id => {
cache.createTaskOutput()
}
cache.updateTask = id => {
cache.createTaskOutput()
}
cache.updateTaskForTaskRecordId = id => {
const taskRecords = tyme
.taskRecordById(id)
.then(data => tyme.taskRecordsByTaskId(data.taskRecord.relatedtaskid, 10))
.then(data => {
const task = data.task
const taskRecords = data.taskRecords
const items = [
{
title: '-- Add new note --',
},
...taskRecords
.sort((a, b) => new Date(b.timeend) - new Date(a.timeend))
.map(taskRecord => taskRecord.note)
.filter((elem, pos, arr) => {
return arr.indexOf(elem) == pos
})
.map(note => ({
title: note,
arg: note,
})),
]
cache.createTaskOutput()
alfy.cache.set(`handle.getNotes.${task.id}`, items)
})
.catch(console.log)
}
cache.default = () => {
alfy.cache.clear()
if (!sh.test('-f', TYME_PATH + TYME_HOOKS_FILE))
sh.cp(TYME_HOOKS_FILE, TYME_PATH)
cache.createTaskOutput()
cache.createTasksNotesOutput()
}