-
Notifications
You must be signed in to change notification settings - Fork 11
/
linux.js
135 lines (121 loc) · 3.08 KB
/
linux.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import { remote } from 'electron'
import path from 'path'
import fs from 'fs'
import uniq from 'lodash/uniq'
import flatten from 'lodash/flatten'
import getAbbr from './getAbbr'
import { shellCommand } from 'cerebro-tools'
let appDirs = [
path.join(remote.app.getPath('home'), '.local', 'share'),
path.join('/usr', 'share'),
path.join('/usr', 'share', 'ubuntu'),
path.join('/usr', 'share', 'gnome'),
path.join('/usr', 'local', 'share'),
path.join('/var', 'lib', 'snapd', 'desktop')
]
if (!!process.env.XDG_DATA_DIRS) {
appDirs = [
...appDirs,
...process.env.XDG_DATA_DIRS.split(':')
]
}
// Icon resolutions in priority of checking
const iconResolutions = [
'scalable',
'1024x1024',
'512x512',
'256x256',
'192x192',
'128x128',
'96x96',
'72x72',
'64x64',
'48x48',
'40x40',
'36x36',
'32x32',
'24x24',
'22x22',
'20x20',
'16x16'
]
// Directories when we are trying to find an icon
const iconDirs = uniq(flatten([
...iconResolutions.map(resolution => (
appDirs.map(dir => path.join(dir, 'icons', 'hicolor', resolution, 'apps'))
)),
path.join('/usr', 'share', 'pixmaps'),
path.join('/usr', 'share', 'app-install', 'icons')
])).filter(fs.existsSync)
const iconExtension = [
'svg',
'png'
]
export const PATTERNS = []
export const DIRECTORIES = uniq([
...appDirs.map(dir => path.join(dir, 'applications')),
path.join('usr', 'share', 'app-install', 'desktop')
]).filter(fs.existsSync)
export const EXTENSIONS = ['desktop']
export const openApp = ({ exec }) => {
if (exec) {
// Replace %u and other % arguments in exec script
// https://github.com/KELiON/cerebro/pull/62#issuecomment-276511320
const cmd = exec.replace(/%./g, '')
shellCommand(cmd)
}
}
const parseDesktopFile = (filePath, mapping) => {
const content = fs.readFileSync(filePath, 'utf-8')
return Object.keys(mapping).reduce((acc, key) => {
let value = ''
const regexp = new RegExp(`^${mapping[key]}=(.+)$`, 'm')
const match = content.match(regexp)
if (match) {
value = match[1]
}
return {
...acc,
[key]: value
}
}, {})
}
const getId = (filePath) => {
const match = filePath.match(/\/applications\/(.+)$/)
return match ? match[1] : filePath
}
const findIcon = (icon) => {
if (path.isAbsolute(icon)) {
return icon
}
return flatten(iconExtension.map(ext =>
iconDirs.map(dir => path.join(dir, `${icon}.${ext}`))
)).find(fs.existsSync)
}
export const toString = ({ name, exec }) => {
const binaryName = exec
.split('/')
.pop()
.split(' ')
.shift()
return `${name} ${getAbbr(name)} ${binaryName}`
}
export const formatPath = (filePath) => {
const parsedData = parseDesktopFile(filePath, {
name: 'Name',
description: 'Comment',
exec: 'Exec',
hidden: 'NoDisplay',
icon: 'Icon'
})
const filename = path.basename(filePath)
return {
...parsedData,
filename,
icon: findIcon(parsedData.icon),
hidden: !!parsedData.hidden || !parsedData.exec,
id: getId(filePath),
name: parsedData.name || filename.replace(/\.(desktop)/, ''),
path: filePath
}
}