-
Notifications
You must be signed in to change notification settings - Fork 4
/
progid.js
125 lines (102 loc) · 3.13 KB
/
progid.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
'use strict';
const Registry = require('winreg')
const path = require('path')
const {app} = require('electron')
const Q = require('q')
const {$create, $set, $destroy} = require('./util')
const ShellOption = require('./shelloption')
const Regedit = require('./regedit')
const debug = require('./debug')
function ProgId({
progExt = '',
appName = app.name,
description = undefined,
friendlyAppName = undefined,
hive = Registry.HKCU,
squirrel = false,
icon,
shell = [],
extensions = []
}) {
this.progId = progExt ? `${appName}.${progExt}` : `${appName}`
this.appName = appName
this.description = description
this.hive = hive
this.icon = icon
this.squirrel = squirrel
this.friendlyAppName = friendlyAppName
this.extensions = extensions
this.shell = bindShells(this, shell)
this.BASE_KEY = `\\Software\\Classes\\${this.appName}`
Regedit.add(this)
}
function bindShells(prog, shell) {
if (Array.isArray(shell) && shell.length === 0) {
shell.push(new ShellOption({}))
}
return shell.map((s) => s.bindProg(prog))
}
ProgId.prototype.uninstall = function () {
if(process.platform !== 'win32') {
return false;
}
let self = this
let registry = new Registry({
hive: this.hive,
key: this.BASE_KEY
})
return $destroy(registry)
};
ProgId.prototype.install = function () {
if(process.platform !== 'win32') {
return false;
}
let self = this
let registry = new Registry({
hive: this.hive,
key: this.BASE_KEY
})
return $create(registry)
.then(() => registerDescription())
.then(() => registerIcon())
.then(() => registerShellCommands())
.then(() => registerFileAssociations())
.then(() => debug(`Installed registry "${this.progId}" sucessfully`))
function registerDescription() {
if (!self.description) return
return $set(registry, Registry.DEFAULT_VALUE, Registry.REG_SZ, self.description)
}
function registerIcon() {
if (!self.icon) return
let iconPath
if (path.isAbsolute(self.icon)) {
iconPath = self.icon
} else {
iconPath = path.join(process.execPath, '..', self.icon)
}
let defaultIcon = new Registry({
hive: self.hive,
key: `${self.BASE_KEY}\\DefaultIcon`
})
return $create(defaultIcon)
.then(() => $set(defaultIcon, Registry.DEFAULT_VALUE, Registry.REG_SZ, iconPath))
}
function registerShellCommands() {
let shells = self.shell.map(shell => shell.install())
return Q.all(shells)
}
function registerFileAssociations() {
let extensions = self.extensions.map((ext) => registerFileExtension(ext))
return Q.all(extensions)
}
function registerFileExtension(ext) {
let registry = new Registry({
hive: self.hive,
key: `\\Software\\Classes\\.${ext}\\OpenWithProgids`
})
return $create(registry).then(() =>
$set(registry, self.progId, Registry.REG_SZ, '')
)
}
};
module.exports = ProgId