-
Notifications
You must be signed in to change notification settings - Fork 114
Expand file tree
/
Copy pathautoLaunchAPIWindows.js
More file actions
134 lines (119 loc) · 4.33 KB
/
Copy pathautoLaunchAPIWindows.js
File metadata and controls
134 lines (119 loc) · 4.33 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
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
import fs from 'fs';
import path from 'path';
import Winreg from 'winreg';
import AutoLaunchAPI from './autoLaunchAPI.js';
const runRegKey = new Winreg({
hive: Winreg.HKCU,
key: '\\Software\\Microsoft\\Windows\\CurrentVersion\\Run',
});
const startupApprovedRegKey = new Winreg({
hive: Winreg.HKCU,
key: '\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\StartupApproved\\Run',
});
export default class AutoLaunchAPIWindows extends AutoLaunchAPI {
/* Public */
constructor(init) {
super(init);
}
// Returns a Promise
enable() {
return new Promise((resolve, reject) => {
let args = '';
let pathToAutoLaunchedApp;
const hiddenArg = this.options.launchInBackground;
const extraArgs = this.options.extraArguments;
const updateDotExe = path.join(path.dirname(process.execPath), '..', 'update.exe');
// If they're using Electron and Squirrel.Windows, point to its Update.exe instead of the actual appPath
// Otherwise, we'll auto-launch an old version after the app has updated
if (((process.versions != null ? process.versions.electron : undefined) != null) && fs.existsSync(updateDotExe)) {
pathToAutoLaunchedApp = `"${updateDotExe}"`;
args = ` --processStart "${path.basename(process.execPath)}"`;
// Manage arguments
if (hiddenArg || extraArgs) {
args += ' --process-start-args';
if (hiddenArg) {
args += ` "${(hiddenArg !== true) ? hiddenArg : '--hidden'}"`;
}
// Add any extra arguments
if (extraArgs) {
args += ' "';
args += extraArgs.join('" "');
args += '"';
}
}
} else {
// If this is an AppX (from Microsoft Store), the path doesn't point to a directory per se,
// but it's made of "DEV_ID.APP_ID!PACKAGE_NAME". It's used to identify the app in the AppsFolder.
// To launch the app, explorer.exe must be call in combination with its path relative to AppsFolder
if (process.windowsStore) {
pathToAutoLaunchedApp = `"explorer.exe" shell:AppsFolder\\${this.appPath}`;
} else {
pathToAutoLaunchedApp = `"${this.appPath}"`;
}
// Manage arguments
if (hiddenArg) {
args = [(hiddenArg !== true) ? hiddenArg : ' --hidden'];
}
// Add any extra arguments
if (extraArgs) {
args += ' ';
args += extraArgs.join(' ');
}
}
runRegKey.set(this.appName, Winreg.REG_SZ, `"${pathToAutoLaunchedApp}"${args}`, (err) => {
if (err != null) {
return reject(err);
}
// Enable the startup task in StartupApproved
startupApprovedRegKey.set(this.appName, Winreg.REG_BINARY, Buffer.from('03000000', 'hex'), (err_) => {
if (err_ != null) {
return reject(err_);
}
return resolve();
});
return resolve();
});
});
}
// Returns a Promise
disable() {
return new Promise((resolve, reject) => {
runRegKey.remove(this.appName, (err) => {
if (err != null) {
// The registry key should exist but, in case it fails because it doesn't exist,
// resolve false instead of rejecting with an error
if (err.message.indexOf('The system was unable to find the specified registry key or value') !== -1) {
return resolve(false);
}
return reject(err);
}
// Disable the startup task in StartupApproved
startupApprovedRegKey.set(this.appName, Winreg.REG_BINARY, Buffer.from('02000000', 'hex'), (err_) => {
if (err_ != null) {
return reject(err_);
}
return resolve();
});
return resolve();
});
});
}
// Returns a Promise which resolves to a {Boolean}
isEnabled() {
return new Promise((resolve) => {
runRegKey.valueExists(this.appName, (err, exists) => {
if (err != null) {
return resolve(false);
}
// Check if the startup task is enabled
startupApprovedRegKey.get(this.appName, (err_, item) => {
if (err_ != null || !item || item.value.toString('hex') !== '03000000') {
return resolve(false);
}
return resolve(true);
});
return resolve(exists);
});
});
}
}