Prevent /usr/local/bin/hyper overwriting (#2885)

Silently fail installation at startup.
Add a menu item to manually install it with a notification feedback.
Fix plugin update menu item.

Fixes #2884
This commit is contained in:
CHaBou 2018-04-22 22:13:23 +02:00 committed by GitHub
parent 2de45245bf
commit dfe5ab89fa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 88 additions and 49 deletions

View file

@ -1,6 +1,7 @@
const {app} = require('electron'); const {app} = require('electron');
const {openConfig} = require('./config'); const {openConfig} = require('./config');
const {updatePlugins} = require('./plugins'); const {updatePlugins} = require('./plugins');
const {installCLI} = require('./utils/cli-install');
const commands = { const commands = {
'window:new': () => { 'window:new': () => {
@ -99,6 +100,9 @@ const commands = {
}, },
'editor:break': focusedWindow => { 'editor:break': focusedWindow => {
focusedWindow && focusedWindow.rpc.emit('session break req'); focusedWindow && focusedWindow.rpc.emit('session break req');
},
'cli:install': () => {
installCLI(true);
} }
}; };

View file

@ -36,6 +36,7 @@ const plugs = {
}; };
const yarn = resolve(__dirname, '../../bin/yarn-standalone.js'); const yarn = resolve(__dirname, '../../bin/yarn-standalone.js');
const cliScriptPath = resolve(__dirname, '../../bin/hyper'); const cliScriptPath = resolve(__dirname, '../../bin/hyper');
const cliLinkPath = '/usr/local/bin/hyper';
const icon = resolve(__dirname, '../static/icon96x96.png'); const icon = resolve(__dirname, '../static/icon96x96.png');
@ -66,5 +67,6 @@ module.exports = {
defaultPlatformKeyPath, defaultPlatformKeyPath,
plugs, plugs,
yarn, yarn,
cliScriptPath cliScriptPath,
cliLinkPath
}; };

View file

@ -62,7 +62,7 @@ const config = require('./config');
config.setup(); config.setup();
const plugins = require('./plugins'); const plugins = require('./plugins');
const {addSymlink, addBinToUserPath} = require('./utils/cli-install'); const {installCLI} = require('./utils/cli-install');
const AppMenu = require('./menus/menu'); const AppMenu = require('./menus/menu');
const Window = require('./ui/window'); const Window = require('./ui/window');
const windowUtils = require('./utils/window-utils'); const windowUtils = require('./utils/window-utils');
@ -98,13 +98,6 @@ if (isDev) {
} else { } else {
//eslint-disable-next-line no-console //eslint-disable-next-line no-console
console.log('running in prod mode'); console.log('running in prod mode');
if (process.platform === 'win32') {
//eslint-disable-next-line no-console
addBinToUserPath().catch(err => console.error('Failed to add Hyper CLI path to user PATH', err));
} else {
//eslint-disable-next-line no-console
addSymlink().catch(err => console.error('Failed to symlink Hyper CLI', err));
}
} }
const url = 'file://' + resolve(isDev ? __dirname : app.getAppPath(), 'index.html'); const url = 'file://' + resolve(isDev ? __dirname : app.getAppPath(), 'index.html');
@ -183,19 +176,6 @@ app.on('ready', () =>
// expose to plugins // expose to plugins
app.createWindow = createWindow; app.createWindow = createWindow;
if (!isDev) {
// check if should be set/removed as default ssh protocol client
if (config.getConfig().defaultSSHApp && !app.isDefaultProtocolClient('ssh')) {
//eslint-disable-next-line no-console
console.log('Setting Hyper as default client for ssh:// protocol');
app.setAsDefaultProtocolClient('ssh');
} else if (!config.getConfig().defaultSSHApp && app.isDefaultProtocolClient('ssh')) {
//eslint-disable-next-line no-console
console.log('Removing Hyper from default client for ssh:// protocl');
app.removeAsDefaultProtocolClient('ssh');
}
}
// mac only. when the dock icon is clicked // mac only. when the dock icon is clicked
// and we don't have any active windows open, // and we don't have any active windows open,
// we open one // we open one
@ -228,6 +208,19 @@ app.on('ready', () =>
makeMenu(); makeMenu();
plugins.subscribe(plugins.onApp.bind(undefined, app)); plugins.subscribe(plugins.onApp.bind(undefined, app));
config.subscribe(makeMenu); config.subscribe(makeMenu);
if (!isDev) {
// check if should be set/removed as default ssh protocol client
if (config.getConfig().defaultSSHApp && !app.isDefaultProtocolClient('ssh')) {
//eslint-disable-next-line no-console
console.log('Setting Hyper as default client for ssh:// protocol');
app.setAsDefaultProtocolClient('ssh');
} else if (!config.getConfig().defaultSSHApp && app.isDefaultProtocolClient('ssh')) {
//eslint-disable-next-line no-console
console.log('Removing Hyper from default client for ssh:// protocl');
app.removeAsDefaultProtocolClient('ssh');
}
installCLI(false);
}
}) })
.catch(err => { .catch(err => {
//eslint-disable-next-line no-console //eslint-disable-next-line no-console

View file

@ -1,4 +1,4 @@
module.exports = (commands, update) => { module.exports = (commands, execCommand) => {
return { return {
label: 'Plugins', label: 'Plugins',
submenu: [ submenu: [
@ -6,8 +6,17 @@ module.exports = (commands, update) => {
label: 'Update', label: 'Update',
accelerator: commands['plugins:update'], accelerator: commands['plugins:update'],
click() { click() {
update(); execCommand('plugins:update');
} }
},
{
label: 'Install Hyper CLI command in PATH',
click() {
execCommand('cli:install');
}
},
{
type: 'separator'
} }
] ]
}; };

View file

@ -3,21 +3,16 @@ const fs = require('fs');
const path = require('path'); const path = require('path');
const Registry = require('winreg'); const Registry = require('winreg');
const {cliScriptPath} = require('../config/paths'); const notify = require('../notify');
const {cliScriptPath, cliLinkPath} = require('../config/paths');
const lstat = pify(fs.lstat);
const readlink = pify(fs.readlink); const readlink = pify(fs.readlink);
const unlink = pify(fs.unlink);
const symlink = pify(fs.symlink); const symlink = pify(fs.symlink);
const target = '/usr/local/bin/hyper';
const source = cliScriptPath;
const checkInstall = () => { const checkInstall = () => {
return lstat(target) return readlink(cliLinkPath)
.then(stat => stat.isSymbolicLink()) .then(link => link === cliScriptPath)
.then(() => readlink(target))
.then(link => link === source)
.catch(err => { .catch(err => {
if (err.code === 'ENOENT') { if (err.code === 'ENOENT') {
return false; return false;
@ -26,27 +21,20 @@ const checkInstall = () => {
}); });
}; };
const createSymlink = () => { const addSymlink = () => {
return unlink(target)
.catch(err => {
if (err.code === 'ENOENT') {
return;
}
throw err;
})
.then(() => symlink(source, target));
};
exports.addSymlink = () => {
return checkInstall().then(isInstalled => { return checkInstall().then(isInstalled => {
if (isInstalled) { if (isInstalled) {
//eslint-disable-next-line no-console
console.log('Hyper CLI already in PATH');
return Promise.resolve(); return Promise.resolve();
} }
return createSymlink(); //eslint-disable-next-line no-console
console.log('Linking HyperCLI');
return symlink(cliScriptPath, cliLinkPath);
}); });
}; };
exports.addBinToUserPath = () => { const addBinToUserPath = () => {
// Can't use pify because of param order of Registry.values callback // Can't use pify because of param order of Registry.values callback
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const envKey = new Registry({hive: 'HKCU', key: '\\Environment'}); const envKey = new Registry({hive: 'HKCU', key: '\\Environment'});
@ -68,6 +56,8 @@ exports.addBinToUserPath = () => {
const pathParts = pathItem.value.split(';'); const pathParts = pathItem.value.split(';');
const existingPath = pathParts.find(pathPart => pathPart === binPath); const existingPath = pathParts.find(pathPart => pathPart === binPath);
if (existingPath) { if (existingPath) {
//eslint-disable-next-line no-console
console.log('Hyper CLI already in PATH');
resolve(); resolve();
return; return;
} }
@ -78,7 +68,8 @@ exports.addBinToUserPath = () => {
.concat([binPath]) .concat([binPath])
.join(';'); .join(';');
} }
//eslint-disable-next-line no-console
console.log('Adding HyperCLI path (registry)');
envKey.set(pathItemName, Registry.REG_SZ, newPathValue, error => { envKey.set(pathItemName, Registry.REG_SZ, newPathValue, error => {
if (error) { if (error) {
reject(error); reject(error);
@ -89,3 +80,43 @@ exports.addBinToUserPath = () => {
}); });
}); });
}; };
const logNotify = (withNotification, ...args) => {
//eslint-disable-next-line no-console
console.log(...args);
withNotification && notify(...args);
};
exports.installCLI = withNotification => {
if (process.platform === 'win32') {
addBinToUserPath()
.then(() =>
logNotify(
withNotification,
'Hyper CLI installed',
'You may need to restart your computer to complete this installation process.'
)
)
.catch(err =>
logNotify(withNotification, 'Hyper CLI installation failed', `Failed to add Hyper CLI path to user PATH ${err}`)
);
} else if (process.platform === 'darwin') {
addSymlink()
.then(() => logNotify(withNotification, 'Hyper CLI installed', `Symlink created at ${cliLinkPath}`))
.catch(err => {
// 'EINVAL' is returned by readlink,
// 'EEXIST' is returned by symlink
const error =
err.code === 'EEXIST' || err.code === 'EINVAL'
? `File already exists: ${cliLinkPath}`
: `Symlink creation failed: ${err.code}`;
//eslint-disable-next-line no-console
console.error(err);
logNotify(withNotification, 'Hyper CLI installation failed', error);
});
} else {
withNotification &&
notify('Hyper CLI instalation', 'Command is added in PATH only at package installation. Please reinstall.');
}
};