-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
294 lines (277 loc) · 8.08 KB
/
index.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/**
* Module to test, enable and disable autostart of app
*
* Currently only windows and mac os supported.
*
* Attention: To get reliable results always set the same appName and
* executablePath. The reason is that mac relies on executablePath to
* identify the autostart item and windows on appName!
*
* Mac uses AppleScript: The only reliable way to identify a login
* item is by it's executablePath. The property name will be ignored
* on creation and instead AppleScript will set the name to the executable
* name or the folder name depending on what was set in executablePath
*
* Windows uses the Registry to create or delete autostart items. Windows
* will identify the login item by its appName
*/
'use strict';
var os = require('os');
var fs = require('fs');
var Promise = require('promise');
var registryPath = 'HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Run';
function Autorun(appName, executablePath) {
this.appName = appName || 'AutorunApp';
this.executablePath = executablePath || _getPathToExecutable();
}
module.exports = Autorun;
Autorun.prototype.isPlatformSupported = function() {
if (os.platform() === 'darwin') {
return true;
}
else if (os.platform().indexOf('win') === 0) {
return true;
}
else { // Not supported
return false;
}
};
Autorun.prototype.isSet = function(callback) {
var self = this;
return new Promise(function(resolve, reject) {
if (os.platform() === 'darwin') {
_macAppleScriptIsAutorunSet(self.executablePath).then(resolve, reject);
}
else if (os.platform().indexOf('win') === 0) {
_winRegIsAutorunSet(self.appName).then(resolve, reject);
}
else { // Not supported
var error = new Error('Your platform "' + os.platform() + '" is not supported');
error.name = 'PlatformUnsupported';
error.platform = os.platform();
reject(error);
}
}).nodeify(callback);
};
Autorun.prototype.enable = function(callback) {
var self = this;
return new Promise(function(resolve, reject) {
if (os.platform() === 'darwin') {
_macAppleScriptAutorunEnable(self.executablePath).then(resolve, reject);
}
else if (os.platform().indexOf('win') === 0) {
_winRegAutorunEnable(self.appName, self.executablePath).then(resolve, reject);
}
else { // Not supported
var error = new Error('Your platform "' + os.platform() + '" is not supported');
error.name = 'PlatformUnsupported';
error.platform = os.platform();
reject(error);
}
}).nodeify(callback);
};
Autorun.prototype.disable = function(callback) {
var self = this;
return new Promise(function(resolve, reject) {
if (os.platform() === 'darwin') {
_macAppleScriptAutorunDisable(self.executablePath).then(resolve, reject);
}
else if (os.platform().indexOf('win') === 0) {
_winRegAutorunDisable(self.appName).then(resolve, reject);
}
else { // Not supported
var error = new Error('Your platform "' + os.platform() + '" is not supported');
error.name = 'PlatformUnsupported';
error.platform = os.platform();
reject(error);
}
}).nodeify(callback);
};
function _macAppleScriptAutorunEnable(executablePath) {
return new Promise(function(resolve, reject) {
// Check if executable exists (or it won't work)
if (! fs.existsSync(executablePath)) {
var error = new Error('Could not enable autorun. Path to executable invalid.');
error.name = 'AutorunEnableFailed';
error.platform = os.platform();
reject(error);
return;
}
var applescript = require('applescript');
var script = 'tell application "System Events" to make login item at end' +
' with properties {path:"' + executablePath + '",' +
' hidden:false }';
applescript.execString(script, function(err) {
if (err) {
var error = new Error('Could not enable autorun using AppleScript');
error.name = 'AutorunEnableFailed';
error.platform = os.platform();
error.cause = err;
reject(error);
}
else {
resolve(true);
}
});
});
}
function _macAppleScriptAutorunDisable(executablePath) {
return new Promise(function(resolve, reject) {
var applescript = require('applescript');
var script = 'tell application "System Events"\n' +
'set loginList to get the properties of every login item\n' +
'repeat with loginItem in loginList\n' +
'if path of loginItem is equal to "' + executablePath + '" then\n' +
'set loginItemName to name of loginItem\n' +
'delete login item loginItemName\n' +
'return 1\n' +
'end if\n' +
'end repeat\n' +
'return 0\n' +
'end tell';
applescript.execString(script, function(err, res) {
if (err) {
var error = new Error('Could not disable autorun using AppleScript');
error.name = 'AutorunDisableFailed';
error.platform = os.platform();
error.cause = err;
reject(error);
}
else if (res === 1) {
resolve(true);
}
else {
resolve(false);
}
});
});
}
function _macAppleScriptIsAutorunSet(executablePath) {
return new Promise(function(resolve, reject) {
var applescript = require('applescript');
var script = 'tell application "System Events"\n' +
'set loginList to get the properties of every login item\n' +
'repeat with loginItem in loginList\n' +
'if path of loginItem is equal to "' + executablePath + '" then\n' +
'return 1\n' +
'end if\n' +
'end repeat\n' +
'return 0\n' +
'end tell';
applescript.execString(script, function(err, res) {
if (err) {
var error = new Error('Could not check if autostart was set using AppleScript');
error.name = 'AutorunIsSetFailed';
error.platform = os.platform();
error.cause = err;
reject(error);
}
else if (res !== undefined && res === 1) {
resolve(true);
}
else {
resolve(false);
}
});
});
}
// Helper function to execute and log out child process
var _spawnProcess = function(command, args, options, callback) {
var spawn = require('child_process').spawn;
var childProcess = spawn(command, args, options);
var err = false;
var text = '';
var errText = '';
childProcess.stdout.on('data', function(data) {
text += data.toString();
});
childProcess.stderr.on('data', function(data) {
err = true;
errText += data.toString();
});
if (typeof callback === 'function') {
childProcess.on('exit', function(exitCode) {
if (err) {
var error = new Error('ChildProcess failed');
error.name = 'ChildProcessFailed';
error.output = errText;
error.cause = err;
return callback(error, null);
}
else {
return callback(null, exitCode);
}
});
}
};
function _winRegIsAutorunSet(appName) {
return new Promise(function(resolve) {
_spawnProcess('cmd', ['/C', 'REG', 'QUERY', registryPath, '/v', appName],
{}, function(err) {
if (err) {
resolve(false);
}
else {
resolve(true);
}
});
});
}
function _winRegAutorunEnable(appName, executablePath) {
return new Promise(function(resolve, reject) {
_spawnProcess('cmd',
['/C', 'REG', 'ADD', registryPath, '/f', '/v', appName, '/t', 'REG_SZ', '/d', executablePath],
{}, function(err, exitCode) {
if (err) {
var error = new Error('Could not enable autorun using the registry');
error.name = 'AutorunEnableFailed';
error.platform = os.platform();
error.cause = err;
reject(error);
}
else if (exitCode === 0) {
resolve(true);
}
else {
resolve(false);
}
});
});
}
function _winRegAutorunDisable(appName) {
return new Promise(function(resolve, reject) {
_spawnProcess('cmd', ['/C', 'REG', 'DELETE', registryPath, '/f', '/v', appName],
{}, function(err, exitCode) {
if (err) {
var error = new Error('Could not disable autorun using the registry');
error.name = 'AutorunDisableFailed';
error.platform = os.platform();
error.cause = err;
reject(error);
}
else if (exitCode === 0) {
resolve(true);
}
else {
resolve(false);
}
});
});
}
function _getPathToExecutable() {
if (os.platform() === 'darwin') {
var app = process.cwd().match(/.*?\.app/);
if (app === null || app.length === 0) {
return process.cwd();
}
else {
return app[0];
}
}
else if (os.platform().indexOf('win') === 0) {
return process.execPath;
}
else {
return process.cwd();
}
}