hyper/app/utils/system-context-menu.ts

61 lines
1.7 KiB
TypeScript
Raw Normal View History

2021-04-09 05:23:38 -08:00
import * as Registry from 'native-reg';
2021-01-10 18:58:48 -09:00
import type {HKEY} from 'native-reg';
const appPath = `"${process.execPath}"`;
const regKeys = [
`Software\\Classes\\Directory\\Background\\shell\\Hyper`,
`Software\\Classes\\Directory\\shell\\Hyper`,
`Software\\Classes\\Drive\\shell\\Hyper`
];
const regParts = [
{key: 'command', name: '', value: `${appPath} "%V"`},
2023-06-18 07:21:06 -08:00
{name: '', value: 'Open &Hyper here'},
{name: 'Icon', value: `${appPath}`}
];
2021-01-10 18:58:48 -09:00
function addValues(hyperKey: HKEY, commandKey: HKEY) {
2020-02-21 05:17:03 -09:00
try {
Registry.setValueSZ(hyperKey, regParts[1].name, regParts[1].value);
} catch (error) {
console.error(error);
}
try {
Registry.setValueSZ(hyperKey, regParts[2].name, regParts[2].value);
} catch (err) {
console.error(err);
}
try {
Registry.setValueSZ(commandKey, regParts[0].name, regParts[0].value);
} catch (err_) {
console.error(err_);
}
}
export const add = () => {
regKeys.forEach((regKey) => {
try {
const hyperKey =
Registry.openKey(Registry.HKCU, regKey, Registry.Access.ALL_ACCESS) ||
Registry.createKey(Registry.HKCU, regKey, Registry.Access.ALL_ACCESS);
const commandKey =
Registry.openKey(Registry.HKCU, `${regKey}\\${regParts[0].key}`, Registry.Access.ALL_ACCESS) ||
Registry.createKey(Registry.HKCU, `${regKey}\\${regParts[0].key}`, Registry.Access.ALL_ACCESS);
addValues(hyperKey, commandKey);
Registry.closeKey(hyperKey);
Registry.closeKey(commandKey);
} catch (error) {
console.error(error);
}
});
};
export const remove = () => {
regKeys.forEach((regKey) => {
try {
Registry.deleteTree(Registry.HKCU, regKey);
} catch (err) {
console.error(err);
}
});
};