From e936f18c4b6f2838273cccf407e5fdfe641278e5 Mon Sep 17 00:00:00 2001 From: sidneys Date: Tue, 28 Feb 2017 01:11:24 +0100 Subject: [PATCH 1/4] gitignore: remove dist folder --- .gitignore | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 76add87..b512c09 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1 @@ -node_modules -dist \ No newline at end of file +node_modules \ No newline at end of file From e8784dd2ed20a4df4ac585937b9023d3ae52a6e2 Mon Sep 17 00:00:00 2001 From: sidneys Date: Tue, 28 Feb 2017 01:11:36 +0100 Subject: [PATCH 2/4] npmignore: remove dist folder --- .npmignore | 1 - 1 file changed, 1 deletion(-) diff --git a/.npmignore b/.npmignore index cd8b52d..909dfd8 100644 --- a/.npmignore +++ b/.npmignore @@ -1,5 +1,4 @@ * -!dist/* !README.md !LICENSE.md !package.json \ No newline at end of file From 2cc1bb0a12ed0c0e43a2b6a5943a0aae12f061f9 Mon Sep 17 00:00:00 2001 From: sidneys Date: Tue, 28 Feb 2017 01:12:17 +0100 Subject: [PATCH 3/4] add compiled javascript --- dist/AutoLaunchLinux.js | 35 +++++++++++++++ dist/AutoLaunchMac.js | 67 ++++++++++++++++++++++++++++ dist/AutoLaunchWindows.js | 67 ++++++++++++++++++++++++++++ dist/fileBasedUtilities.js | 56 ++++++++++++++++++++++++ dist/index.js | 90 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 315 insertions(+) create mode 100644 dist/AutoLaunchLinux.js create mode 100644 dist/AutoLaunchMac.js create mode 100644 dist/AutoLaunchWindows.js create mode 100644 dist/fileBasedUtilities.js create mode 100644 dist/index.js diff --git a/dist/AutoLaunchLinux.js b/dist/AutoLaunchLinux.js new file mode 100644 index 0000000..63744de --- /dev/null +++ b/dist/AutoLaunchLinux.js @@ -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"; + } +}; diff --git a/dist/AutoLaunchMac.js b/dist/AutoLaunchMac.js new file mode 100644 index 0000000..e8ceb5d --- /dev/null +++ b/dist/AutoLaunchMac.js @@ -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 " " + argument + ""; + }).join('\n'); + data = "\n\n\n\n Label\n " + appName + "\n ProgramArguments\n \n " + programArgumentsSection + "\n \n RunAtLoad\n \n\n"; + 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"; + } +}; diff --git a/dist/AutoLaunchWindows.js b/dist/AutoLaunchWindows.js new file mode 100644 index 0000000..ac154e7 --- /dev/null +++ b/dist/AutoLaunchWindows.js @@ -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); + }); + }); + } +}; diff --git a/dist/fileBasedUtilities.js b/dist/fileBasedUtilities.js new file mode 100644 index 0000000..ed74776 --- /dev/null +++ b/dist/fileBasedUtilities.js @@ -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)); + } +}; diff --git a/dist/index.js b/dist/index.js new file mode 100644 index 0000000..3547675 --- /dev/null +++ b/dist/index.js @@ -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; + +})(); From 8d69afbf173eef2ef9190f3c1829c925d4cfa5df Mon Sep 17 00:00:00 2001 From: sidneys Date: Tue, 28 Feb 2017 01:30:28 +0100 Subject: [PATCH 4/4] npmignore: remove folders, add gitignore, travis, test --- .npmignore | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.npmignore b/.npmignore index 909dfd8..dc048d7 100644 --- a/.npmignore +++ b/.npmignore @@ -1,4 +1,4 @@ -* -!README.md -!LICENSE.md -!package.json \ No newline at end of file +.gitignore +.travis.yml +node_modules +test