-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathurl-handler.js
132 lines (111 loc) · 3.43 KB
/
url-handler.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
'use strict'
const { isWindows, isLinux } = require('which-runtime')
module.exports = function register (executable) {
if (isLinux) registerLinuxHandler(executable)
if (isWindows) registerWindowsHandler(executable)
}
function registerLinuxHandler (executable) {
if (!executable) return
const fs = require('bare-fs')
const os = require('bare-os')
const { join } = require('bare-path')
const { spawnSync } = require('bare-subprocess')
if (!checkXdgMime()) return
const APP_NAME = 'Pear'
const ICON_NAME = 'pear'
const DESKTOP_FILE_NAME = 'pear.desktop'
const APPLICATIONS_DIR = join(os.homedir(), '.local', 'share', 'applications')
const DESKTOP_FILE_PATH = join(APPLICATIONS_DIR, DESKTOP_FILE_NAME)
const MIME_TYPES = ['x-scheme-handler/pear']
try {
if (!checkExists(APPLICATIONS_DIR)) {
fs.mkdirSync(APPLICATIONS_DIR, { recursive: true })
}
if (!checkDesktopFile(executable)) {
fs.writeFileSync(DESKTOP_FILE_PATH, generateDesktopFile(executable), { encoding: 'utf-8' })
}
for (const mimeType of MIME_TYPES) {
if (!checkMimeType(mimeType)) {
registerMimeType(mimeType)
}
}
} catch (err) {
console.error('could not install protocol handler:', err)
}
function checkXdgMime () {
try {
spawnSync('xdg-mime', ['--version'])
return true
} catch (err) {
return false
}
}
function checkExists (path) {
try {
fs.accessSync(path)
return true
} catch (err) {
return false
}
}
function checkDesktopFile () {
try {
return fs.readFileSync(DESKTOP_FILE_PATH, 'utf-8').includes(`Exec=${executable} run %U`)
} catch (err) {
if (err.code !== 'ENOENT') throw err
return false
}
}
function checkMimeType (mimeType) {
try {
return spawnSync('xdg-mime', ['query', 'default', mimeType]).stdout.toString() === DESKTOP_FILE_NAME
} catch {
return false
}
}
function registerMimeType (mimeType) {
try {
spawnSync('xdg-mime', ['default', DESKTOP_FILE_NAME, mimeType])
return true
} catch {
return false
}
}
function generateDesktopFile (executable) {
return `\
[Desktop Entry]
Name=${APP_NAME}
Exec=${executable} %U
Terminal=false
Icon=${ICON_NAME}
Type=Application
StartupWMClass=${APP_NAME}
X-AppImage-Version=1.0.1
Comment=${APP_NAME}
MimeType=${MIME_TYPES.join(';')};
NoDisplay=true
`
}
}
function registerWindowsHandler (executable) {
if (!executable) return
const { spawnSync } = require('bare-subprocess')
const PROTOCOL = 'pear'
const HANDLER_NAME = 'Pear Application'
const HANDLER_COMMAND = `"${executable}" "%1"`
const REGISTRY_PATH = `HKCU\\Software\\Classes\\${PROTOCOL}`
const REGISTRY_COMMAND_PATH = `${REGISTRY_PATH}\\shell\\open\\command`
try {
if (spawnSync('reg', ['query', REGISTRY_PATH, '/v', 'URL Protocol']).status !== 0) {
spawnSync('reg', ['add', REGISTRY_PATH, '/v', 'URL Protocol', '/t', 'REG_SZ', '/d', '', '/f'])
spawnSync('reg', ['add', REGISTRY_PATH, '/v', '', '/t', 'REG_SZ', '/d', HANDLER_NAME, '/f'])
}
const currentHandler = spawnSync('reg', ['query', REGISTRY_COMMAND_PATH])
.stdout.toString()?.match(/REG_SZ\s+"([^"]+)"/)?.[1]
if (currentHandler !== executable) {
spawnSync('reg', ['add', REGISTRY_COMMAND_PATH, '/v', '', '/t', 'REG_SZ', '/d', HANDLER_COMMAND, '/f'])
}
} catch (err) {
console.error('could not install protocol handler:', err)
}
}