-
-
Notifications
You must be signed in to change notification settings - Fork 105
/
Copy pathindex.js
164 lines (131 loc) · 3.67 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
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
'use strict';
const os = require('os');
const Conf = require('conf');
const got = require('got');
const hookStd = require('hook-std');
const loudRejection = require('loud-rejection');
const cleanStack = require('clean-stack');
const dotProp = require('dot-prop');
const CacheConf = require('cache-conf');
const updateNotification = require('./lib/update-notification');
const alfy = module.exports;
updateNotification();
const getIcon = name => `/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/${name}.icns`;
const getEnv = key => process.env[`alfred_${key}`];
alfy.meta = {
name: getEnv('workflow_name'),
version: getEnv('workflow_version'),
uid: getEnv('workflow_uid'),
bundleId: getEnv('workflow_bundleid')
};
alfy.alfred = {
version: getEnv('version'),
theme: getEnv('theme'),
themeBackground: getEnv('theme_background'),
themeSelectionBackground: getEnv('theme_selection_background'),
themeSubtext: Number(getEnv('theme_subtext')),
data: getEnv('workflow_data'),
cache: getEnv('workflow_cache'),
preferences: getEnv('preferences'),
preferencesLocalHash: getEnv('preferences_localhash')
};
alfy.input = process.argv[2];
alfy.output = items => {
console.log(JSON.stringify({items}, null, '\t'));
};
alfy.matches = (input, list, item) => {
input = input.toLowerCase().normalize();
return list.filter(x => {
if (typeof item === 'string') {
x = dotProp.get(x, item);
}
if (typeof x === 'string') {
x = x.toLowerCase();
}
if (typeof item === 'function') {
return item(x, input);
}
return x.includes(input);
});
};
alfy.inputMatches = (list, item) => alfy.matches(alfy.input, list, item);
alfy.log = text => {
console.error(text);
};
alfy.error = error => {
const stack = cleanStack(error.stack || error);
const copy = `
\`\`\`
${stack}
\`\`\`
-
${alfy.meta.name} ${alfy.meta.version}
Alfred ${alfy.alfred.version}
${process.platform} ${os.release()}
`.trim();
alfy.output([{
title: error.stack ? `${error.name}: ${error.message}` : error,
subtitle: 'Press ⌘L to see the full error and ⌘C to copy it.',
valid: false,
text: {
copy,
largetype: stack
},
icon: {
path: exports.icon.error
}
}]);
};
alfy.config = new Conf({
cwd: alfy.alfred.data
});
alfy.cache = new CacheConf({
configName: 'cache',
cwd: alfy.alfred.cache,
version: alfy.meta.version
});
alfy.fetch = async (url, options) => {
options = {
json: true,
...options
};
if (typeof url !== 'string') {
return Promise.reject(new TypeError(`Expected \`url\` to be a \`string\`, got \`${typeof url}\``));
}
if (options.transform && typeof options.transform !== 'function') {
return Promise.reject(new TypeError(`Expected \`transform\` to be a \`function\`, got \`${typeof options.transform}\``));
}
const rawKey = url + JSON.stringify(options);
const key = rawKey.replace(/\./g, '\\.');
const cachedResponse = alfy.cache.get(key, {ignoreMaxAge: true});
if (cachedResponse && !alfy.cache.isExpired(key)) {
return Promise.resolve(cachedResponse);
}
let response;
try {
response = await got(url, options);
} catch (error) {
if (cachedResponse) {
return cachedResponse;
}
throw error;
}
const data = options.transform ? options.transform(response.body) : response.body;
if (options.maxAge) {
alfy.cache.set(key, data, {maxAge: options.maxAge});
}
return data;
};
alfy.debug = getEnv('debug') === '1';
alfy.icon = {
get: getIcon,
info: getIcon('ToolbarInfo'),
warning: getIcon('AlertCautionIcon'),
error: getIcon('AlertStopIcon'),
alert: getIcon('Actions'),
like: getIcon('ToolbarFavoritesIcon'),
delete: getIcon('ToolbarDeleteIcon')
};
loudRejection(alfy.error);
process.on('uncaughtException', alfy.error);
hookStd.stderr(alfy.error);