fix(launch): Do not create global symlinks to N1 and apm. Closes #1553

This commit is contained in:
Ben Gotow 2016-03-08 10:34:12 -08:00
parent fb4fe3fd5f
commit eec5edce4d
3 changed files with 0 additions and 166 deletions

View file

@ -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()
});
});

View file

@ -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}'")

View file

@ -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.
#