-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
139 lines (124 loc) · 3.86 KB
/
index.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
'use strict';
import alfy from 'alfy';
import {searchTasks, searchProjects, searchFolders, searchTags, searchInbox, searchNotes} from "./query_factory.js";
import {listPerspectives} from "./omnifocus.js";
import {createTask, createProject, createFolder, createTag} from "./result_factory.js"
import yargs from 'yargs'
import {hideBin} from "yargs/helpers";
const argv = yargs(hideBin(process.argv)).argv
const NO_RESULTS = [{
icon: {
path: "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/AlertNoteIcon.icns"
},
title: "No results"
}]
const PERSPECTIVE = 'v'
const TASK = 't'
const PROJECT = 'p'
const FOLDER = 'f'
const TAG = 'c'
const INBOX = 'i'
const NOTE = 'n'
const SINGLE_QUOTE = "'"
const ESC_SINGLE_QUOTE = "''"
function main() {
if (alfy.config.get('dbPath') === undefined) {
alfy.output(createError("'dbPath' is missing", "Use get-of-db and set-of-db keywords"))
} else {
runWorkflow()
}
}
function runWorkflow() {
let results = undefined
let query = argv.query
if (query !== undefined) {
query = '' + argv.query // Fix for a numerical query since following test fails when running .includes
if (query.includes(SINGLE_QUOTE)) {
query = query.replaceAll(SINGLE_QUOTE, ESC_SINGLE_QUOTE)
}
}
try {
switch (argv.type) {
case PERSPECTIVE:
perspectiveQuery(query);
break;
case TASK:
results = searchTasks(query, argv.completedOnly, argv.flaggedOnly, argv.activeOnly, argv.everything);
break;
case PROJECT:
results = searchProjects(query, argv.activeOnly);
break;
case FOLDER:
results = searchFolders(query);
break;
case TAG:
results = searchTags(query);
break;
case INBOX:
results = searchInbox(query);
break;
case NOTE:
results = searchNotes(query, argv.activeOnly, argv.flaggedOnly);
break;
}
if (results !== undefined) {
outputResults(results)
}
}
catch (e) {
alfy.output(createError(e.message, e.context))
}
}
function perspectiveQuery(query) {
if (query !== undefined) {
listPerspectives().then(perspectives => {
alfy.output(perspectives.filter(function (item) {
if (item.title.toLowerCase().indexOf(argv.query.toLowerCase()) >= 0) {
return {title: item.title, subtitle: item.subtitle, icon: item.icon}
}
}))
})
} else {
listPerspectives().then(perspectives => {
alfy.output(perspectives)
})
}
}
function outputResults(results) {
let items = NO_RESULTS
if (results !== null && results.length > 0) {
items = []
switch (argv.type) {
case PROJECT:
results.forEach(result => {
items.push(createProject(result))
})
break;
case FOLDER:
results.forEach(result => {
items.push(createFolder(result))
})
break;
case TAG:
results.forEach(result => {
items.push(createTag(result))
})
break;
default: // Search task, search inbox
results.forEach(result => {
items.push(createTask(result))
})
}
}
alfy.output(items)
}
function createError(title, subtitle) {
return [{
icon: {
path: "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/AlertStopIcon.icns"
},
title: title,
subtitle: subtitle
}]
}
main()