-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdarwin.js
66 lines (53 loc) · 1.56 KB
/
darwin.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
const fs = require("fs");
const fse = require("fs-extra");
const fileIcon = require("file-icon");
const _ = require("lodash");
const bundleId = require("bundle-id");
const arvish = require("arvish");
const getIcon = (appName) => {
if (arvish.config.has('cachingComplete')) {
return `${appName}.app.png`;
}
return "icon.png";
};
const cacheIcon = async () => {
try {
const inApplicationFolder = [
...await fse.readdir("/Applications"),
...await fse.readdir("/System/Applications"),
...await fse.readdir("/System/Applications/Utilities"),
];
const apps = _.filter(inApplicationFolder, (appName) =>
appName.endsWith(".app")
);
const appBundleIds = await Promise.allSettled(
apps.map(async (appName) => {
return {
appName,
bundleId: await bundleId(appName),
};
})
);
const appBundleIdsSuccess = appBundleIds
.filter((item) => item.status === "fulfilled")
.map((item) => item.value);
const buffers = await fileIcon.buffer(
appBundleIdsSuccess.map((item) => item.bundleId),
{ size: 64 }
);
let idx = 0;
if (!(await fse.pathExists(`${arvish.env.cache}/icons`))) {
await fse.mkdir(`${arvish.env.cache}/icons`);
}
buffers.forEach((buffer) => {
fs.writeFileSync(
`${arvish.env.cache}/icons/${appBundleIdsSuccess[idx++].appName}.png`,
buffer
);
});
arvish.config.set("cachingComplete", true);
} catch (err) {
console.log('Error occured', err);
}
};
module.exports = { getIcon, cacheIcon };