Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add compiled javascript #56

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
node_modules
dist
node_modules
9 changes: 4 additions & 5 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
*
!dist/*
!README.md
!LICENSE.md
!package.json
.gitignore
.travis.yml
node_modules
test
35 changes: 35 additions & 0 deletions dist/AutoLaunchLinux.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
var fileBasedUtilities, untildify;

untildify = require('untildify');

fileBasedUtilities = require('./fileBasedUtilities');

module.exports = {

/* Public */
enable: function(arg) {
var appName, appPath, data, hiddenArg, isHiddenOnLaunch;
appName = arg.appName, appPath = arg.appPath, isHiddenOnLaunch = arg.isHiddenOnLaunch;
hiddenArg = isHiddenOnLaunch ? ' --hidden' : '';
data = "[Desktop Entry]\nType=Application\nVersion=1.0\nName=" + appName + "\nComment=" + appName + "startup script\nExec=" + appPath + hiddenArg + "\nStartupNotify=false\nTerminal=false";
return fileBasedUtilities.createFile({
data: data,
directory: this.getDirectory(),
filePath: this.getFilePath(appName)
});
},
disable: function(appName) {
return fileBasedUtilities.removeFile(this.getFilePath(appName));
},
isEnabled: function(appName) {
return fileBasedUtilities.isEnabled(this.getFilePath(appName));
},

/* Private */
getDirectory: function() {
return untildify('~/.config/autostart/');
},
getFilePath: function(appName) {
return "" + (this.getDirectory()) + appName + ".desktop";
}
};
67 changes: 67 additions & 0 deletions dist/AutoLaunchMac.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
var applescript, fileBasedUtilities, untildify,
indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };

applescript = require('applescript');

untildify = require('untildify');

fileBasedUtilities = require('./fileBasedUtilities');

module.exports = {

/* Public */
enable: function(arg) {
var appName, appPath, data, isHiddenOnLaunch, isHiddenValue, mac, programArguments, programArgumentsSection, properties;
appName = arg.appName, appPath = arg.appPath, isHiddenOnLaunch = arg.isHiddenOnLaunch, mac = arg.mac;
if (mac.useLaunchAgent) {
programArguments = [appPath];
if (isHiddenOnLaunch) {
programArguments.push('--hidden');
}
programArgumentsSection = programArguments.map(function(argument) {
return " <string>" + argument + "</string>";
}).join('\n');
data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n<plist version=\"1.0\">\n<dict>\n <key>Label</key>\n <string>" + appName + "</string>\n <key>ProgramArguments</key>\n <array>\n " + programArgumentsSection + "\n </array>\n <key>RunAtLoad</key>\n <true/>\n</dict>\n</plist>";
return fileBasedUtilities.createFile({
data: data,
directory: this.getDirectory(),
filePath: this.getFilePath(appName)
});
}
isHiddenValue = isHiddenOnLaunch ? 'true' : 'false';
properties = "{path:\"" + appPath + "\", hidden:" + isHiddenValue + ", name:\"" + appName + "\"}";
return this.execApplescriptCommand("make login item at end with properties " + properties);
},
disable: function(appName, mac) {
if (mac.useLaunchAgent) {
return fileBasedUtilities.removeFile(this.getFilePath(appName));
}
return this.execApplescriptCommand("delete login item \"" + appName + "\"");
},
isEnabled: function(appName, mac) {
if (mac.useLaunchAgent) {
return fileBasedUtilities.isEnabled(this.getFilePath(appName));
}
return this.execApplescriptCommand('get the name of every login item').then(function(loginItems) {
return (loginItems != null) && indexOf.call(loginItems, appName) >= 0;
});
},

/* Private */
execApplescriptCommand: function(commandSuffix) {
return new Promise(function(resolve, reject) {
return applescript.execString("tell application \"System Events\" to " + commandSuffix, function(err, result) {
if (err != null) {
return reject(err);
}
return resolve(result);
});
});
},
getDirectory: function() {
return untildify('~/Library/LaunchAgents/');
},
getFilePath: function(appName) {
return "" + (this.getDirectory()) + appName + ".plist";
}
};
67 changes: 67 additions & 0 deletions dist/AutoLaunchWindows.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
var Winreg, fs, path, regKey;

fs = require('fs');

path = require('path');

Winreg = require('winreg');

regKey = new Winreg({
hive: Winreg.HKCU,
key: '\\Software\\Microsoft\\Windows\\CurrentVersion\\Run'
});

module.exports = {

/* Public */
enable: function(arg) {
var appName, appPath, isHiddenOnLaunch;
appName = arg.appName, appPath = arg.appPath, isHiddenOnLaunch = arg.isHiddenOnLaunch;
return new Promise(function(resolve, reject) {
var args, pathToAutoLaunchedApp, ref, updateDotExe;
pathToAutoLaunchedApp = appPath;
args = '';
updateDotExe = path.join(path.dirname(process.execPath), '..', 'update.exe');
if ((((ref = process.versions) != null ? ref.electron : void 0) != null) && fs.existsSync(updateDotExe)) {
pathToAutoLaunchedApp = updateDotExe;
args = " --processStart \"" + (path.basename(process.execPath)) + "\"";
if (isHiddenOnLaunch) {
args += ' --process-start-args "--hidden"';
}
} else {
if (isHiddenOnLaunch) {
args += ' --hidden';
}
}
return regKey.set(appName, Winreg.REG_SZ, "\"" + pathToAutoLaunchedApp + "\"" + args, function(err) {
if (err != null) {
return reject(err);
}
return resolve();
});
});
},
disable: function(appName) {
return new Promise(function(resolve, reject) {
return regKey.remove(appName, function(err) {
if (err != null) {
if (err.message.indexOf('The system was unable to find the specified registry key or value') !== -1) {
return resolve(false);
}
return reject(err);
}
return resolve();
});
});
},
isEnabled: function(appName) {
return new Promise(function(resolve, reject) {
return regKey.get(appName, function(err, item) {
if (err != null) {
return resolve(false);
}
return resolve(item != null);
});
});
}
};
56 changes: 56 additions & 0 deletions dist/fileBasedUtilities.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
var fs, mkdirp;

fs = require('fs');

mkdirp = require('mkdirp');

module.exports = {

/* Public */
createFile: function(arg) {
var data, directory, filePath;
directory = arg.directory, filePath = arg.filePath, data = arg.data;
return new Promise(function(resolve, reject) {
return mkdirp(directory, function(mkdirErr) {
if (mkdirErr != null) {
return reject(mkdirErr);
}
return fs.writeFile(filePath, data, function(writeErr) {
if (writeErr != null) {
return reject(writeErr);
}
return resolve();
});
});
});
},
isEnabled: function(filePath) {
return new Promise((function(_this) {
return function(resolve, reject) {
return fs.stat(filePath, function(err, stat) {
if (err != null) {
return resolve(false);
}
return resolve(stat != null);
});
};
})(this));
},
removeFile: function(filePath) {
return new Promise((function(_this) {
return function(resolve, reject) {
return fs.stat(filePath, function(statErr) {
if (statErr != null) {
return resolve();
}
return fs.unlink(filePath, function(unlinkErr) {
if (unlinkErr != null) {
return reject(unlinkErr);
}
return resolve();
});
});
};
})(this));
}
};
90 changes: 90 additions & 0 deletions dist/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
var AutoLaunch, isPathAbsolute,
bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };

isPathAbsolute = require('path-is-absolute');

module.exports = AutoLaunch = (function() {

/* Public */
function AutoLaunch(arg) {
var isHidden, mac, name, path, versions;
name = arg.name, isHidden = arg.isHidden, mac = arg.mac, path = arg.path;
this.fixOpts = bind(this.fixOpts, this);
this.isEnabled = bind(this.isEnabled, this);
this.disable = bind(this.disable, this);
this.enable = bind(this.enable, this);
if (name == null) {
throw new Error('You must specify a name');
}
this.opts = {
appName: name,
isHiddenOnLaunch: isHidden != null ? isHidden : false,
mac: mac != null ? mac : {}
};
versions = typeof process !== "undefined" && process !== null ? process.versions : void 0;
if (path != null) {
if (!isPathAbsolute(path)) {
throw new Error('path must be absolute');
}
this.opts.appPath = path;
} else if ((versions != null) && ((versions.nw != null) || (versions['node-webkit'] != null) || (versions.electron != null))) {
this.opts.appPath = process.execPath;
} else {
throw new Error('You must give a path (this is only auto-detected for NW.js and Electron apps)');
}
this.fixOpts();
this.api = null;
if (/^win/.test(process.platform)) {
this.api = require('./AutoLaunchWindows');
} else if (/darwin/.test(process.platform)) {
this.api = require('./AutoLaunchMac');
} else if (/linux/.test(process.platform)) {
this.api = require('./AutoLaunchLinux');
} else {
throw new Error('Unsupported platform');
}
}

AutoLaunch.prototype.enable = function() {
return this.api.enable(this.opts);
};

AutoLaunch.prototype.disable = function() {
return this.api.disable(this.opts.appName, this.opts.mac);
};

AutoLaunch.prototype.isEnabled = function() {
return this.api.isEnabled(this.opts.appName, this.opts.mac);
};


/* Private */

AutoLaunch.prototype.fixMacExecPath = function(path) {
return path.replace(/(^.+?[^\/]+?\.app)\/Contents\/(Frameworks\/((\1|[^\/]+?) Helper)\.app\/Contents\/MacOS\/\3|MacOS\/Electron)/, '$1');
};

AutoLaunch.prototype.fixOpts = function() {
var tempPath;
this.opts.appPath = this.opts.appPath.replace(/\/$/, '');
if (/darwin/.test(process.platform)) {
this.opts.appPath = this.fixMacExecPath(this.opts.appPath);
}
if (this.opts.appPath.indexOf('/') !== -1) {
tempPath = this.opts.appPath.split('/');
this.opts.appName = tempPath[tempPath.length - 1];
} else if (this.opts.appPath.indexOf('\\') !== -1) {
tempPath = this.opts.appPath.split('\\');
this.opts.appName = tempPath[tempPath.length - 1];
this.opts.appName = this.opts.appName.substr(0, this.opts.appName.length - '.exe'.length);
}
if (/darwin/.test(process.platform)) {
if (this.opts.appName.indexOf('.app', this.opts.appName.length - '.app'.length) !== -1) {
return this.opts.appName = this.opts.appName.substr(0, this.opts.appName.length - '.app'.length);
}
}
};

return AutoLaunch;

})();