UNPKG

3.22 kBJavaScriptView Raw
1var AutoLaunch, Promise,
2 bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
3
4Promise = require('es6-promise').Promise;
5
6module.exports = AutoLaunch = (function() {
7
8 /* Public */
9 function AutoLaunch(opts) {
10 this.fixOpts = bind(this.fixOpts, this);
11 var versions;
12 if (opts.name == null) {
13 throw new Error('You must specify a name');
14 }
15 this.opts = {};
16 this.opts.appName = opts.name;
17 this.opts.isHiddenOnLaunch = opts.isHidden != null ? opts.isHidden : false;
18 versions = typeof process !== "undefined" && process !== null ? process.versions : void 0;
19 if (opts.path != null) {
20 this.opts.appPath = opts.path;
21 } else if ((versions != null) && ((versions.nw != null) || (versions['node-webkit'] != null) || (versions.electron != null))) {
22 this.opts.appPath = process.execPath;
23 } else {
24 throw new Error("You must give a path (this is only auto-detected for NW.js and Electron apps)");
25 }
26 this.fixOpts();
27 this.api = null;
28 if (/^win/.test(process.platform)) {
29 this.api = require('./AutoLaunchWindows');
30 } else if (/darwin/.test(process.platform)) {
31 this.api = require('./AutoLaunchMac');
32 } else if (/linux/.test(process.platform)) {
33 this.api = require('./AutoLaunchLinux');
34 }
35 }
36
37 AutoLaunch.prototype.fixMacExecPath = function(path) {
38 return path.replace(/(^.+?[^\/]+?\.app)\/Contents\/(Frameworks\/((\1|[^\/]+?) Helper)\.app\/Contents\/MacOS\/\3|MacOS\/Electron)/, '$1');
39 };
40
41 AutoLaunch.prototype.removeNwjsLoginItem = function() {
42 return this.api.disable({
43 appName: 'nwjs Helper'
44 }, function() {
45 return null;
46 });
47 };
48
49 AutoLaunch.prototype.fixOpts = function() {
50 var tempPath;
51 this.opts.appPath = this.opts.appPath.replace(/\/$/, '');
52 if (/darwin/.test(process.platform)) {
53 this.opts.appPath = this.fixMacExecPath(this.opts.appPath);
54 }
55 if (this.opts.appPath.indexOf('/') !== -1) {
56 tempPath = this.opts.appPath.split('/');
57 this.opts.appName = tempPath[tempPath.length - 1];
58 } else if (this.opts.appPath.indexOf('\\') !== -1) {
59 tempPath = this.opts.appPath.split('\\');
60 this.opts.appName = tempPath[tempPath.length - 1];
61 this.opts.appName = this.opts.appName.substr(0, this.opts.appName.length - '.exe'.length);
62 }
63 if (/darwin/.test(process.platform)) {
64 if (this.opts.appName.indexOf('.app', this.opts.appName.length - '.app'.length) !== -1) {
65 return this.opts.appName = this.opts.appName.substr(0, this.opts.appName.length - '.app'.length);
66 }
67 }
68 };
69
70 AutoLaunch.prototype.enable = function() {
71 if (this.api == null) {
72 return Promise.reject(new Error('Platform not supported'));
73 }
74 return this.api.enable(this.opts);
75 };
76
77 AutoLaunch.prototype.disable = function() {
78 if (this.api == null) {
79 return Promise.reject(new Error('Platform not supported'));
80 }
81 return this.api.disable(this.opts);
82 };
83
84 AutoLaunch.prototype.isEnabled = function() {
85 if (this.api == null) {
86 return Promise.reject(new Error('Platform not supported'));
87 }
88 return this.api.isEnabled(this.opts);
89 };
90
91 return AutoLaunch;
92
93})();