-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathshelloption.js
119 lines (97 loc) · 3.42 KB
/
shelloption.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
const Registry = require('winreg')
const path = require('path')
const {app} = require('electron')
const {$create, $set} = require('./util')
function ShellOption({
verb = 'open',
action = undefined,
icon = undefined,
args = ["%1"],
friendlyAppName = undefined,
selected = false,
squirrel = undefined,
command = undefined
}) {
this.verb = verb
this.action = action
this.selected = selected
this.friendlyAppName = friendlyAppName
this.icon = icon
this.args = args
this.squirrel = squirrel
this.command = command
}
ShellOption.OPEN = 'open'
ShellOption.OPEN_NEW = 'opennew'
ShellOption.PRINT = 'print'
ShellOption.EXPLORE = 'explore'
ShellOption.FIND = 'find'
ShellOption.OPEN_AS = 'openas'
ShellOption.PROPERTIES = 'properties'
ShellOption.EDIT = 'edit'
ShellOption.PREVIEW = 'preview'
ShellOption.prototype.bindProg = function (progid) {
this.progid = progid
if (this.squirrel === undefined) this.squirrel = this.progid.squirrel
if (this.friendlyAppName === undefined) this.friendlyAppName = this.progid.friendlyAppName
if (this.squirrel) this.args.unshift('--process-start-args')
return this
};
ShellOption.prototype.install = function () {
if (!this.progid) {
throw new Error('ShellOption must be part of a ProgId')
return
}
let self = this
let registry = new Registry({
hive: this.progid.hive,
key: `${this.progid.BASE_KEY}\\shell\\${this.verb}\\command`
})
return $create(registry)
.then(() => registerCommand(registry))
.then(() => registerAction(registry.parent))
.then(() => registerIcon(registry.parent))
.then(() => registerFriendlyAppName(registry.parent))
.then(() => registerSelected(registry.parent.parent))
function registerCommand(registry) {
if (!self.command) {
if (self.squirrel) {
let exec = self.squirrel === true ? `${app.name}.exe` : self.squirrel
self.command = path.join(path.dirname(process.execPath), '..', exec)
} else {
self.command = process.execPath
}
}
let command = [`"${self.command}"`].concat(self.args.map((arg) => `"${arg}"`))
return $set(registry, Registry.DEFAULT_VALUE, Registry.REG_SZ, command.join(' '))
}
function registerAction(registry) {
if (self.action === undefined && self.verb === 'open') {
self.action = `Open with ${self.progid.appName}`
}
if (!self.action) return
return $set(registry, Registry.DEFAULT_VALUE, Registry.REG_SZ, self.action)
}
function registerIcon(registry) {
if (!self.icon) return
let iconPath
if (path.isAbsolute(self.icon)) {
iconPath = self.icon
} else {
iconPath = path.join(process.execPath, '..', self.icon)
}
return $set(registry, 'Icon', Registry.REG_SZ, iconPath)
}
function registerSelected(registry) {
if (self.selected !== true) return
return $set(registry, Registry.DEFAULT_VALUE, Registry.REG_SZ, self.verb)
}
function registerFriendlyAppName(registry) {
const KEY = 'FriendlyAppName'
if (self.friendlyAppName) {
if (self.friendlyAppName === true) self.friendlyAppName = app.name
return $set(registry, KEY, Registry.REG_SZ, self.friendlyAppName)
}
}
};
module.exports = ShellOption