From eec5edce4d95069d7cc1631da2d1a7f71b92c2dd Mon Sep 17 00:00:00 2001 From: Ben Gotow Date: Tue, 8 Mar 2016 10:34:12 -0800 Subject: [PATCH] fix(launch): Do not create global symlinks to `N1` and `apm`. Closes #1553 --- spec/command-installer-spec.es6 | 75 ------------------------------ src/command-installer.coffee | 82 --------------------------------- src/nylas-env.coffee | 9 ---- 3 files changed, 166 deletions(-) delete mode 100644 spec/command-installer-spec.es6 delete mode 100644 src/command-installer.coffee diff --git a/spec/command-installer-spec.es6 b/spec/command-installer-spec.es6 deleted file mode 100644 index aa466f7ae..000000000 --- a/spec/command-installer-spec.es6 +++ /dev/null @@ -1,75 +0,0 @@ -import CommandInstaller from '../src/command-installer' -import fs from 'fs-plus' - -describe("CommandInstaller", () => { - beforeEach(() => { - this.resourcePath = "/resourcePath"; - this.callback = jasmine.createSpy('callback') - - spyOn(CommandInstaller, "symlinkCommand").andCallFake((sourcePath, destinationPath, callback) => { - callback() - }) - }); - - it("Installs N1 if it doesn't already exist", () => { - spyOn(fs, "readlink").andCallFake((path, fn) => { - expect(path).toBe("/usr/local/bin/N1") - fn(new Error("not found"), undefined) - }) - CommandInstaller.installN1Command(this.resourcePath, false, this.callback) - expect(CommandInstaller.symlinkCommand).toHaveBeenCalled() - expect(this.callback).toHaveBeenCalled() - expect(this.callback.calls[0].args[0]).toBeUndefined() - }); - - it("Leaves the N1 link alone if exists and is already correct", () => { - spyOn(fs, "readlink").andCallFake((path, fn) => { - expect(path).toBe("/usr/local/bin/N1") - fn(null, this.resourcePath + "/N1.sh") - }) - CommandInstaller.installN1Command(this.resourcePath, false, this.callback) - expect(CommandInstaller.symlinkCommand).not.toHaveBeenCalled() - expect(this.callback).toHaveBeenCalled() - }); - - it("Overrides the N1 link if it exists but is not correct", () => { - spyOn(fs, "readlink").andCallFake((path, fn) => { - expect(path).toBe("/usr/local/bin/N1") - fn(null, this.resourcePath + "/totally/wrong/path") - }) - CommandInstaller.installN1Command(this.resourcePath, false, this.callback) - expect(CommandInstaller.symlinkCommand).toHaveBeenCalled() - expect(this.callback).toHaveBeenCalled() - }); - - it("Installs apm if it doesn't already exist", () => { - spyOn(fs, "readlink").andCallFake((path, fn) => { - expect(path).toBe("/usr/local/bin/apm") - fn(new Error("not found"), undefined) - }) - CommandInstaller.installApmCommand(this.resourcePath, false, this.callback) - expect(CommandInstaller.symlinkCommand).toHaveBeenCalled() - expect(this.callback).toHaveBeenCalled() - expect(this.callback.calls[0].args[0]).toBeUndefined() - }); - - it("Leaves the apm link alone if exists and is already correct", () => { - spyOn(fs, "readlink").andCallFake((path, fn) => { - expect(path).toBe("/usr/local/bin/apm") - fn(null, this.resourcePath + "/apm/node_modules/.bin/apm") - }) - CommandInstaller.installApmCommand(this.resourcePath, false, this.callback) - expect(CommandInstaller.symlinkCommand).not.toHaveBeenCalled() - expect(this.callback).toHaveBeenCalled() - }); - - it("Leaves the apm link alone it exists and is not correct since it likely refers to Atom's apm", () => { - spyOn(fs, "readlink").andCallFake((path, fn) => { - expect(path).toBe("/usr/local/bin/apm") - fn(null, this.resourcePath + "/pointing/to/Atom/apm") - }) - CommandInstaller.installApmCommand(this.resourcePath, false, this.callback) - expect(CommandInstaller.symlinkCommand).not.toHaveBeenCalled() - expect(this.callback).toHaveBeenCalled() - }); -}); diff --git a/src/command-installer.coffee b/src/command-installer.coffee deleted file mode 100644 index 2c566540a..000000000 --- a/src/command-installer.coffee +++ /dev/null @@ -1,82 +0,0 @@ -path = require 'path' -_ = require 'underscore' -async = require 'async' -fs = require 'fs-plus' -mkdirp = require 'mkdirp' -runas = require 'runas' - -module.exports = - getInstallDirectory: -> - "/usr/local/bin" - - installShellCommandsInteractively: -> - showErrorDialog = (error) -> - NylasEnv.confirm - message: "Failed to install shell commands" - detailedMessage: error.message - - resourcePath = NylasEnv.getLoadSettings().resourcePath - @installN1Command resourcePath, true, (error) => - if error? - showErrorDialog(error) - else - @installApmCommand resourcePath, true, (error) -> - if error? - showErrorDialog(error) - else - NylasEnv.confirm - message: "Commands installed." - detailedMessage: "The shell commands `n1` and `apm` are installed." - - installN1Command: (resourcePath, askForPrivilege, callback) -> - commandPath = path.join(resourcePath, 'N1.sh') - @createSymlink commandPath, askForPrivilege, callback, {override: true} - - installApmCommand: (resourcePath, askForPrivilege, callback) -> - commandPath = path.join(resourcePath, 'apm', 'node_modules', '.bin', 'apm') - @createSymlink commandPath, askForPrivilege, callback, {override: false} - - createSymlink: (commandPath, askForPrivilege, callback, {override}) -> - return unless process.platform is 'darwin' - - commandName = path.basename(commandPath, path.extname(commandPath)) - destinationPath = path.join(@getInstallDirectory(), commandName) - - fs.readlink destinationPath, (error, realpath) => - if realpath is commandPath - callback() - return - else if realpath and realpath isnt commandPath and not override - callback() - return - else - @symlinkCommand commandPath, destinationPath, (error) => - if askForPrivilege and error?.code is 'EACCES' - try - error = null - @symlinkCommandWithPrivilegeSync(commandPath, destinationPath) - catch error - - callback?(error) - - symlinkCommand: (sourcePath, destinationPath, callback) -> - fs.unlink destinationPath, (error) -> - if error? and error?.code != 'ENOENT' - callback(error) - else - mkdirp path.dirname(destinationPath), (error) -> - if error? - callback(error) - else - fs.symlink sourcePath, destinationPath, callback - - symlinkCommandWithPrivilegeSync: (sourcePath, destinationPath) -> - if runas('/bin/rm', ['-f', destinationPath], admin: true) != 0 - throw new Error("Failed to remove '#{destinationPath}'") - - if runas('/bin/mkdir', ['-p', path.dirname(destinationPath)], admin: true) != 0 - throw new Error("Failed to create directory '#{destinationPath}'") - - if runas('/bin/ln', ['-s', sourcePath, destinationPath], admin: true) != 0 - throw new Error("Failed to symlink '#{sourcePath}' to '#{destinationPath}'") - diff --git a/src/nylas-env.coffee b/src/nylas-env.coffee index 771eef137..f4afa3e3c 100644 --- a/src/nylas-env.coffee +++ b/src/nylas-env.coffee @@ -674,7 +674,6 @@ class NylasEnvConstructor extends Model window.requestAnimationFrame => @displayWindow() unless initializeInBackground - @registerCommands() @loadConfig() @keymaps.loadBundledKeymaps() @themes.loadBaseStylesheets() @@ -696,14 +695,6 @@ class NylasEnvConstructor extends Model @restoreWindowDimensions() @getCurrentWindow().setMinimumSize(875, 250) - registerCommands: -> - {resourcePath} = @getLoadSettings() - CommandInstaller = require './command-installer' - CommandInstaller.installN1Command resourcePath, false, (error) -> - console.warn error.message if error? - CommandInstaller.installApmCommand resourcePath, false, (error) -> - console.warn error.message if error? - # Call this method when establishing a secondary application window # displaying a specific set of packages. #