Skip to content

Commit

Permalink
Add support for Alfred 4 and fix opening second instance - fixes #4 #5
Browse files Browse the repository at this point in the history
  • Loading branch information
SamVerschueren committed Aug 22, 2020
1 parent 88ebf77 commit 23a8236
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 36 deletions.
76 changes: 48 additions & 28 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'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');
Expand All @@ -10,47 +11,66 @@ const utils = require('./lib/utils');

const output = [];

const update = pkg => execa('npm', ['install', '-g', pkg.name]).catch(error => {
output.push(error.message);
});
const update = async pkg => {
try {
return await execa('npm', ['install', '-g', pkg.name]);
} catch (error) {
output.push(error.message);
}
};

const checkAndUpdate = filePath => readPkg(filePath)
.then(pkg => {
if (!pkg.name || !pkg.version) {
return;
const checkAndUpdate = async filePath => {
const pkg = await readPkg(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;

return latestVersion(pkg.name)
.then(version => {
if (semver.gt(version, pkg.version)) {
return update(pkg);
}
})
.catch(() => { });
});
try {
const alfredPreferences = await resolveAlfredPrefs();

resolveAlfredPrefs()
.then(prefs => {
const workflowDir = path.join(prefs, 'workflows');
alfredVersion = alfredPreferences.version || 4;

const workflowDir = path.join(alfredPreferences.path, 'workflows');

// Retrieve all the symlinks from the workflows directory
return utils.findSymlinks(workflowDir);
})
.then(filePaths => {
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));

return Promise.all(promises);
})
.then(result => {
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 => {
} catch (error) {
fs.writeFileSync('output', error.message);
console.log('Something went wrong');
})
.then(() => execa('open', ['-n', '-a', 'Alfred 3']));
} 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();
}
})();
21 changes: 14 additions & 7 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,24 @@ const pify = require('pify');

const fsP = pify(fs);

exports.findSymlinks = dir => fsP.readdir(dir)
.then(files => {
const promises = files.map(file => {
const filePath = path.join(dir, file);
exports.findSymlinks = async dir => {
const files = await fsP.readdir(dir);

return fsP.lstat(filePath).then(stats => stats.isSymbolicLink() && filePath);
});
const promises = files.map(async file => {
const filePath = path.join(dir, file);

return Promise.all(promises).then(files => files.filter(Boolean));
const stats = await fsP.lstat(filePath);

if (stats.isSymbolicLink()) {
return filePath;
}
});

const symlinks = await Promise.all(promises);

return symlinks.filter(Boolean);
};

exports.toMessage = updates => {
if (updates === 0) {
return 'No workflows updated';
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"latest-version": "^2.0.0",
"pify": "^2.3.0",
"read-pkg": "^1.1.0",
"resolve-alfred-prefs": "^1.0.0",
"resolve-alfred-prefs": "^2.0.0",
"run-node": "^0.1.1",
"semver": "^5.3.0"
},
Expand Down

0 comments on commit 23a8236

Please sign in to comment.