-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
76 lines (60 loc) · 1.8 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
'use strict';
const path = require('path');
const fs = require('fs');
const {spawn} = require('child_process');
const resolveAlfredPrefs = require('resolve-alfred-prefs');
const readPkg = require('read-pkg');
const latestVersion = require('latest-version');
const semver = require('semver');
const execa = require('execa');
const utils = require('./lib/utils');
const output = [];
const update = async pkg => {
try {
return await execa('npm', ['install', '-g', pkg.name]);
} catch (error) {
output.push(error.message);
}
};
const checkAndUpdate = async filePath => {
const pkg = await readPkg({cwd: filePath});
if (!pkg.name || !pkg.version) {
return;
}
try {
const version = await latestVersion(pkg.name);
if (semver.gt(version, pkg.version)) {
return update(pkg);
}
} catch (_) {
// Do nothing
}
};
(async () => {
let alfredVersion;
try {
const alfredPreferences = await resolveAlfredPrefs();
alfredVersion = alfredPreferences.version || 4;
const workflowDir = path.join(alfredPreferences.path, 'workflows');
// Retrieve all the symlinks from the workflows directory
const filePaths = await utils.findSymlinks(workflowDir);
// Iterate over all the workflows, check if they are outdated and update them
const promises = filePaths.map(filePath => checkAndUpdate(filePath));
const result = await Promise.all(promises);
if (output.length > 0) {
throw new Error(output.join('\n'));
}
console.log(utils.toMessage(result.filter(Boolean).length));
} catch (error) {
fs.writeFileSync('output', error.message);
console.log('Something went wrong');
} finally {
// Kill the Alfred application and restart it
const process = spawn(`pkill Alfred && open -n -a 'Alfred ${alfredVersion}'`, {
detached: true,
stdio: 'ignore',
shell: true
});
process.unref();
}
})();