-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
66 lines (66 loc) · 2.19 KB
/
Copy pathindex.js
File metadata and controls
66 lines (66 loc) · 2.19 KB
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
import alfy from 'alfy';
import got from 'got';
import sortOn from 'sort-on';
const KEY = process.env.key?.trim() || '';
const URL = process.env.url?.trim() || '';
const VENDOR = process.env.vendor?.trim() || 'satispress';
const CMD = process.env.require ? 'composer require ' : '';
export const cache = alfy.cache;
const CACHE_KEY_RESPONSE = 'packages';
const CACHE_KEY_FETCHING = 'fetching';
const CACHE_OPTIONS = { maxAge: 60 * 60000 }; // minutes to milliseconds
const updatePackagesCache = async () => {
got.get(URL, {
headers: {
Authorization: `Basic ${Buffer.from(`${KEY}:satispress`).toString('base64')}`,
},
})
.json()
.then((response) => {
cache.set(CACHE_KEY_RESPONSE, response, CACHE_OPTIONS);
cache.set(CACHE_KEY_FETCHING, false);
})
.catch((reason) => {
console.log(reason);
});
};
const getPackages = () => {
const cachedResponse = cache.get(CACHE_KEY_RESPONSE, null);
if (!cachedResponse) {
return [];
}
return sortOn(Object.keys(cachedResponse.packages).map((key) => {
const versions = Object.keys(cachedResponse.packages[key]);
const latestVersion = versions[versions.length - 1];
const title = key.replace(`${VENDOR}/`, '');
return {
title,
subtitle: `${cachedResponse.packages[key][latestVersion].description} (${title})`,
arg: `${CMD}${key}`,
};
}), 'title');
};
const main = () => {
if (KEY === '') {
alfy.error('Add missing API key to environment variables!');
return;
}
if (URL === '') {
alfy.error('Add missing URL to environment variables! (e.g. https://example.com/satispress/packages.json)');
return;
}
const packages = getPackages();
if (packages.length > 0) {
alfy.output(alfy.inputMatches(packages, 'subtitle'));
return;
}
alfy.output([{
title: 'Loading packages',
}], { rerunInterval: 1 });
if (cache.get(CACHE_KEY_FETCHING, false) !== true) {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
cache.set(CACHE_KEY_FETCHING, true);
updatePackagesCache();
}
};
main();