hyper/app/utils/cli-install.ts

122 lines
4 KiB
TypeScript
Raw Normal View History

2019-11-28 05:17:01 -09:00
import pify from 'pify';
import fs from 'fs';
import path from 'path';
import notify from '../notify';
import {cliScriptPath, cliLinkPath} from '../config/paths';
2021-01-10 18:58:48 -09:00
import {Registry, loadRegistry} from './registry';
import type {ValueType} from 'native-reg';
2020-02-14 23:17:45 -09:00
const readlink = pify(fs.readlink);
const symlink = pify(fs.symlink);
const checkInstall = () => {
return readlink(cliLinkPath)
2020-03-25 02:15:08 -08:00
.then((link) => link === cliScriptPath)
.catch((err) => {
if (err.code === 'ENOENT') {
return false;
}
throw err;
});
};
const addSymlink = () => {
2020-03-25 02:15:08 -08:00
return checkInstall().then((isInstalled) => {
if (isInstalled) {
console.log('Hyper CLI already in PATH');
return Promise.resolve();
}
console.log('Linking HyperCLI');
return symlink(cliScriptPath, cliLinkPath);
});
};
const addBinToUserPath = () => {
2020-11-22 17:43:55 -09:00
return new Promise<void>((resolve, reject) => {
2021-01-10 18:58:48 -09:00
if (!loadRegistry()) {
reject('Failed to load Registry Module');
return;
}
2020-02-14 23:17:45 -09:00
try {
const envKey = Registry.openKey(Registry.HKCU, 'Environment', Registry.Access.ALL_ACCESS)!;
// C:\Users\<user>\AppData\Local\hyper\app-<version>\resources\bin
const binPath = path.dirname(cliScriptPath);
// C:\Users\<user>\AppData\Local\hyper
const basePath = path.resolve(binPath, '../../..');
2020-02-14 23:17:45 -09:00
const items = Registry.enumValueNames(envKey);
2020-03-25 02:15:08 -08:00
const pathItem = items.find((item) => item.toUpperCase() === 'PATH');
2020-02-14 23:17:45 -09:00
const pathItemName = pathItem || 'PATH';
let newPathValue = binPath;
2021-01-10 18:58:48 -09:00
let type: ValueType = Registry.ValueType.SZ;
if (pathItem) {
2020-02-14 23:17:45 -09:00
type = Registry.queryValueRaw(envKey, pathItem)!.type;
if (type !== Registry.ValueType.SZ && type !== Registry.ValueType.EXPAND_SZ) {
reject(`Registry key type is ${type}`);
return;
}
const value = Registry.queryValue(envKey, pathItem) as string;
const pathParts = value.split(';');
const existingPath = pathParts.includes(binPath);
if (existingPath) {
console.log('Hyper CLI already in PATH');
resolve();
return;
}
// Because version is in path we need to remove old path if present and add current path
newPathValue = pathParts
2020-03-25 02:15:08 -08:00
.filter((pathPart) => !pathPart.startsWith(basePath))
.concat([binPath])
.join(';');
}
console.log('Adding HyperCLI path (registry)');
2020-02-14 23:17:45 -09:00
Registry.setValueRaw(envKey, pathItemName, type, Registry.formatString(newPathValue));
Registry.closeKey(envKey);
resolve();
} catch (error) {
reject(error);
}
});
};
2020-06-19 04:51:34 -08:00
const logNotify = (withNotification: boolean, title: string, body: string, details?: {error?: any}) => {
2019-12-25 00:52:32 -09:00
console.log(title, body, details);
withNotification && notify(title, body, details);
};
2019-12-25 00:52:32 -09:00
export const installCLI = (withNotification: boolean) => {
if (process.platform === 'win32') {
addBinToUserPath()
.then(() =>
logNotify(
withNotification,
'Hyper CLI installed',
'You may need to restart your computer to complete this installation process.'
)
)
2020-03-25 02:15:08 -08:00
.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}`))
2020-03-25 02:15:08 -08:00
.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}`;
console.error(err);
logNotify(withNotification, 'Hyper CLI installation failed', error);
});
} else {
withNotification &&
2018-10-13 06:35:51 -08:00
notify('Hyper CLI installation', 'Command is added in PATH only at package installation. Please reinstall.');
}
};