forked from sindresorhus/alfy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
171 lines (135 loc) · 3.96 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
165
166
167
168
169
170
171
import os from 'node:os';
import process from 'node:process';
import {createRequire} from 'node:module';
import Conf from 'conf';
import got from 'got';
import hookStd from 'hook-std';
import loudRejection from 'loud-rejection';
import cleanStack from 'clean-stack';
import dotProp from 'dot-prop';
import AlfredConfig from 'alfred-config';
import updateNotification from './lib/update-notification.js';
const require = createRequire(import.meta.url);
const CacheConf = require('cache-conf');
const alfy = {};
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, {rerunInterval} = {}) => {
console.log(JSON.stringify({items, rerun: rerunInterval}, null, '\t'));
};
alfy.matches = (input, list, item) => {
input = input.toLowerCase().normalize();
return list.filter(listItem => {
if (typeof item === 'string') {
listItem = dotProp.get(listItem, item);
}
if (typeof listItem === 'string') {
listItem = listItem.toLowerCase().normalize();
}
if (typeof item === 'function') {
return item(listItem, input);
}
return listItem.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: alfy.icon.error,
},
}]);
};
alfy.config = new Conf({
cwd: alfy.alfred.data,
});
alfy.userConfig = new AlfredConfig();
alfy.cache = new CacheConf({
configName: 'cache',
cwd: alfy.alfred.cache,
version: alfy.meta.version,
});
alfy.fetch = async (url, options) => {
options = {
...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, {searchParams: options.query}).json();
} catch (error) {
if (cachedResponse) {
return cachedResponse;
}
throw error;
}
const data = options.transform ? options.transform(response) : response;
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);
export default alfy;