-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
75 lines (59 loc) · 2.03 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
'use strict';
const path = require('path');
const execa = require('execa');
const electronUtil = require('electron-util/node');
const macosVersion = require('macos-version');
const binary = path.join(electronUtil.fixPathForAsarUnpack(__dirname), 'open-with');
const isAlreadySorted = macosVersion.isGreaterThanOrEqualTo('10.15');
const isSupported = macosVersion.isGreaterThanOrEqualTo('10.14.4');
const formatApps = appList => appList.filter(app => Boolean(app.url)).sort((a, b) => {
if (a.isDefault !== b.isDefault) {
return b.isDefault - a.isDefault;
}
if (isAlreadySorted) {
return 0;
}
if (a.url.includes('Applications') !== b.url.includes('Applications')) {
return b.url.includes('Applications') - a.url.includes('Applications');
}
return path.parse(a.url).name.localeCompare(path.parse(b.url).name);
});
const callBinary = async (subCommand, argument) => {
if (!isSupported) {
return [];
}
try {
const {stdout} = await execa(binary, [subCommand, argument]);
return formatApps(JSON.parse(stdout));
} catch {
return [];
}
};
const callBinarySync = (subCommand, argument) => {
if (!isSupported) {
return [];
}
try {
const {stdout} = execa.sync(binary, [subCommand, argument]);
return formatApps(JSON.parse(stdout));
} catch {
return [];
}
};
exports.getAppsThatOpenFile = filePath => callBinary('apps-for-file', path.resolve(filePath));
exports.getAppsThatOpenFile.sync = filePath => callBinarySync('apps-for-file', path.resolve(filePath));
exports.getAppsThatOpenType = fileType => callBinary('apps-for-type', fileType);
exports.getAppsThatOpenType.sync = fileType => callBinarySync('apps-for-type', fileType);
exports.getAppsThatOpenExtension = fileExtension => callBinary('apps-for-extension', fileExtension);
exports.getAppsThatOpenExtension.sync = fileExtension => callBinarySync('apps-for-extension', fileExtension);
exports.openFileWithApp = (filePath, appUrl) => {
if (!isSupported) {
return false;
}
try {
execa.sync(binary, ['open', filePath, appUrl]);
return true;
} catch {
return false;
}
};