forked from atom/atom
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwin-shell.js
More file actions
104 lines (94 loc) · 2.7 KB
/
Copy pathwin-shell.js
File metadata and controls
104 lines (94 loc) · 2.7 KB
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
const Registry = require('winreg');
const Path = require('path');
const getAppName = require('../get-app-name');
const appName = getAppName();
const exeName = Path.basename(process.execPath);
const appPath = `"${process.execPath}"`;
const fileIconPath = `"${Path.join(
process.execPath,
'..',
'resources',
'cli',
'file.ico'
)}"`;
class ShellOption {
constructor(key, parts) {
this.isRegistered = this.isRegistered.bind(this);
this.register = this.register.bind(this);
this.deregister = this.deregister.bind(this);
this.update = this.update.bind(this);
this.key = key;
this.parts = parts;
}
isRegistered(callback) {
new Registry({
hive: 'HKCU',
key: `${this.key}\\${this.parts[0].key}`
}).get(this.parts[0].name, (err, val) =>
callback(err == null && val != null && val.value === this.parts[0].value)
);
}
register(callback) {
let doneCount = this.parts.length;
this.parts.forEach(part => {
let reg = new Registry({
hive: 'HKCU',
key: part.key != null ? `${this.key}\\${part.key}` : this.key
});
return reg.create(() =>
reg.set(part.name, Registry.REG_SZ, part.value, () => {
if (--doneCount === 0) return callback();
})
);
});
}
deregister(callback) {
this.isRegistered(isRegistered => {
if (isRegistered) {
new Registry({ hive: 'HKCU', key: this.key }).destroy(() =>
callback(null, true)
);
} else {
callback(null, false);
}
});
}
update(callback) {
new Registry({
hive: 'HKCU',
key: `${this.key}\\${this.parts[0].key}`
}).get(this.parts[0].name, (err, val) => {
if (err != null || val == null) {
callback(err);
} else {
this.register(callback);
}
});
}
}
exports.appName = appName;
exports.fileHandler = new ShellOption(
`\\Software\\Classes\\Applications\\${exeName}`,
[
{ key: 'shell\\open\\command', name: '', value: `${appPath} "%1"` },
{ key: 'shell\\open', name: 'FriendlyAppName', value: `${appName}` },
{ key: 'DefaultIcon', name: '', value: `${fileIconPath}` }
]
);
let contextParts = [
{ key: 'command', name: '', value: `${appPath} "%1"` },
{ name: '', value: `Open with ${appName}` },
{ name: 'Icon', value: `${appPath}` }
];
exports.fileContextMenu = new ShellOption(
`\\Software\\Classes\\*\\shell\\${appName}`,
contextParts
);
exports.folderContextMenu = new ShellOption(
`\\Software\\Classes\\Directory\\shell\\${appName}`,
contextParts
);
exports.folderBackgroundContextMenu = new ShellOption(
`\\Software\\Classes\\Directory\\background\\shell\\${appName}`,
JSON.parse(JSON.stringify(contextParts).replace('%1', '%V'))
);