forked from bikenik/alfred-ldoce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
106 lines (96 loc) · 2.53 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
'use strict'
const jsonfile = require('jsonfile')
const alfredNotifier = require('alfred-notifier')
const alfy = require('alfy')
const WorkflowError = require('./src/utils/error')
const {errorAction} = require('./src/utils/error')
const set = require('./src/cmd/set')
const del = require('./src/cmd/del')
const decks = require('./src/anki/anki-decks')
const api = require('./src/api/index')
alfredNotifier()
const myVar = process.argv[3]
let query
let introMessage = [{
subtitle: `Current deck is ⇒ ${alfy.config.get('default-deck')}`
}]
if (myVar === 'headword') {
query = {
headword: `${alfy.input}`,
limit: 50
}
introMessage[0].title = 'Search headwords ...'
}
if (myVar === 'search') {
query = {
search: `${alfy.input}`,
limit: 50
}
introMessage[0].title = 'Search generic ...'
}
const fileAnkiDecks = './src/input/anki-decks.json'
const commands = [set, del]
const option = async input => {
for (const command of commands) {
if (command.match(input)) {
return command(input)
}
}
// No matches, show all commands
if (/!.*/.test(input)) {
const options = commands.map(command => ({
title: command.meta.name,
subtitle: `${command.meta.help} | Usage: ${command.meta.usage}`,
autocomplete: command.meta.autocomplete,
text: {
largetype: `${command.meta.help} | Usage: ${command.meta.usage}`
},
valid: false
}))
return alfy.inputMatches(options, 'title')
}
if (input === '') {
const ankiDecks = await decks()
if (ankiDecks === null) {
throw new WorkflowError(`Decks was not found, check your Anki profile`, errorAction('profile'))
}
jsonfile.writeFile(fileAnkiDecks, ankiDecks, {
spaces: 2
}, function (err) {
if (err !== null) {
console.log(err)
}
})
return introMessage
}
}
(async () => {
try {
let out = await option(alfy.input)
if (out || /!.*/.test(alfy.input)) {
alfy.output(out)
} else {
api.fetching(query)
}
} catch (err) {
const messages = []
if (err.tip) {
messages.push(err.tip)
}
messages.push('Activate this item to try again.')
messages.push('⌘L to see the stack trace')
alfy.output([{
title: err.title ? err.title : `Error: ${err.message}`,
subtitle: err.subtitle ? err.subtitle : messages.join(' | '),
autocomplete: err.autocomplete ? err.autocomplete : '',
icon: err.icon ? err.icon : {path: alfy.icon.error},
valid: err.valid ? err.valid : false,
variables: err.variables ? err.variables : {variables: {}},
text: {
largetype: err.stack,
copy: err.stack
},
mods: err.mods ? err.mods : {mods: {}}
}])
}
})()