-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
116 lines (103 loc) · 3.02 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
const path = require("path");
const fg = require("fast-glob");
const os = require("os");
const arvish = require("arvish");
const { getIcon: getWin32Icon } = require("./win32");
const { getIcon: getDarwinIcon } = require("./darwin");
const { getIcon: getLinuxIcon } = require("./linux");
const conf = require("./conf");
const sep = path.sep;
const getIcon =
process.platform === "darwin"
? getDarwinIcon
: process.platform === "win32"
? getWin32Icon
: getLinuxIcon;
const getTargetPaths = (inputStr) => {
return conf.applicationFolders[process.platform].map((applicationPath) => {
switch (process.platform) {
case "win32":
return `${applicationPath}${sep}**${sep}*${inputStr}*`
.split("\\")
.join("/");
case "darwin":
return `${applicationPath}${sep}*${inputStr}*`;
case "linux":
return `${applicationPath}${sep}**${sep}*${inputStr}*`;
}
});
};
const getPluginItem = async ({ inputStr }) => {
if (inputStr === "")
return {
items: [],
};
const configItems = [
process.platform !== "linux"
? {
command: "@cache/arvis-app-launcher-plugin",
title: "Cache app icons of arvis-app-launcher-plugin",
subtitle: "@cache/arvis-app-launcher-plugin",
variables: {
action: "cache",
},
}
: undefined,
];
return new Promise((resolve, reject) => {
const globOpts = {
absolute: true,
caseSensitiveMatch: false,
concurrency: conf.concurrency ? conf.concurrency : os.cpus().length,
cwd: path.parse(process.cwd()).root,
deep: process.platform === "darwin" ? 1 : conf.deep,
dot: false,
onlyFiles: process.platform === "darwin" ? false : true,
suppressErrors: true,
};
const getFileOrDirName = (filePath) => {
const name = filePath.endsWith("/")
? filePath
.substring(0, filePath.length - 1)
.split("/")
.pop()
: filePath.split("/").pop();
if (name.includes(".")) {
return name.split(".")[0];
} else {
return name;
}
};
const targetPaths = getTargetPaths(inputStr);
fg(targetPaths, globOpts)
.then((targetApps) => {
const items = [
...targetApps.map((appPath) => {
const appName = getFileOrDirName(appPath);
return {
title: appName,
subtitle: appPath,
arg: appPath,
icon: {
path: arvish.config.has('cachingComplete') ?
`${arvish.env.cache}${sep}icons${sep}${getIcon(appName)}` :
`${__dirname}${sep}icon.png`,
},
variables: {
action: "open",
},
};
}),
...configItems,
];
resolve({
items,
});
})
.catch((err) => {
console.error("arvis-app-launcher-plugin throws an Error", err);
resolve({ items: [] });
});
});
};
module.exports = getPluginItem;