diff --git a/app/config/open.ts b/app/config/open.ts index 398eb69b..4575383a 100644 --- a/app/config/open.ts +++ b/app/config/open.ts @@ -1,21 +1,16 @@ import {shell} from 'electron'; import {cfgPath} from './paths'; -import * as regTypes from '../typings/native-reg'; +import {Registry, loadRegistry} from '../utils/registry'; export default () => { // Windows opens .js files with WScript.exe by default // If the user hasn't set up an editor for .js files, we fallback to notepad. if (process.platform === 'win32') { - try { - // eslint-disable-next-line no-var, @typescript-eslint/no-var-requires - var Registry: typeof regTypes = require('native-reg'); - } catch (err) { - console.error(err); - } // eslint-disable-next-line @typescript-eslint/no-var-requires const {exec} = require('child_process') as typeof import('child_process'); const getUserChoiceKey = async () => { + if (!loadRegistry()) return; try { // Load FileExts keys for .js files const fileExtsKeys = Registry.openKey( @@ -38,6 +33,7 @@ export default () => { }; const hasDefaultSet = async () => { + if (!loadRegistry()) return false; const userChoice = await getUserChoiceKey(); if (!userChoice) return false; diff --git a/app/ext-modules.d.ts b/app/ext-modules.d.ts index 443d58ee..812dcafb 100644 --- a/app/ext-modules.d.ts +++ b/app/ext-modules.d.ts @@ -6,3 +6,109 @@ declare module 'default-shell' { const val: string; export default val; } + +declare module 'native-reg' { + export enum HKEY { + CLASSES_ROOT = 2147483648, + CURRENT_USER = 2147483649, + LOCAL_MACHINE = 2147483650, + USERS = 2147483651, + PERFORMANCE_DATA = 2147483652, + PERFORMANCE_TEXT = 2147483728, + PERFORMANCE_NLSTEXT = 2147483744, + CURRENT_CONFIG = 2147483653, + DYN_DATA = 2147483654, + CURRENT_USER_LOCAL_SETTINGS = 2147483655 + } + export enum CreateKeyOptions { + NON_VOLATILE = 0, + VOLATILE = 1, + CREATE_LINK = 2, + BACKUP_RESTORE = 4 + } + export enum OpenKeyOptions { + OPEN_LINK = 8 + } + export enum Access { + QUERY_VALUE = 1, + SET_VALUE = 2, + CREATE_SUB_KEY = 4, + ENUMERATE_SUB_KEYS = 8, + NOTIFY = 16, + CREATE_LINK = 32, + WOW64_64KEY = 256, + WOW64_32KEY = 512, + READ = 131097, + WRITE = 131078, + EXECUTE = 131097, + ALL_ACCESS = 983103 + } + export enum ValueType { + NONE = 0, + SZ = 1, + EXPAND_SZ = 2, + BINARY = 3, + DWORD = 4, + DWORD_LITTLE_ENDIAN = 4, + DWORD_BIG_ENDIAN = 5, + LINK = 6, + MULTI_SZ = 7, + RESOURCE_LIST = 8, + FULL_RESOURCE_DESCRIPTOR = 9, + RESOURCE_REQUIREMENTS_LIST = 10, + QWORD = 11, + QWORD_LITTLE_ENDIAN = 11 + } + export enum GetValueFlags { + RT_ANY = 65535, + RT_REG_NONE = 1, + RT_REG_SZ = 2, + RT_REG_EXPAND_SZ = 4, + RT_REG_BINARY = 8, + RT_REG_DWORD = 16, + RT_REG_MULTI_SZ = 32, + RT_REG_QWORD = 64, + RT_DWORD = 24, + RT_QWORD = 72, + NO_EXPAND = 268435456, + SUBKEY_WOW6464KEY = 65536, + SUBKEY_WOW6432KEY = 131072 + } + export const HKCR = HKEY.CLASSES_ROOT; + export const HKCU = HKEY.CURRENT_USER; + export const HKLM = HKEY.LOCAL_MACHINE; + export const HKU = HKEY.USERS; + export type Value = Buffer & { + type: ValueType; + }; + export function isHKEY(hkey: any): hkey is HKEY; + export function createKey(hkey: HKEY, subKey: string, access: Access, options?: CreateKeyOptions): HKEY; + export function openKey(hkey: HKEY, subKey: string, access: Access, options?: OpenKeyOptions): HKEY | null; + export function openCurrentUser(access?: Access): HKEY; + export function loadAppKey(file: string, access: Access): HKEY | null; + export function enumKeyNames(hkey: HKEY): string[]; + export function enumValueNames(hkey: HKEY): string[]; + export function queryValueRaw(hkey: HKEY, valueName: string): Value | null; + export function getValueRaw(hkey: HKEY, subKey: string, valueName: string, flags?: GetValueFlags): Value | null; + export function setValueRaw(hkey: HKEY, valueName: string, valueType: ValueType, data: Buffer): void; + export function deleteKey(hkey: HKEY, subKey: string): boolean; + export function deleteTree(hkey: HKEY, subKey: string): boolean; + export function deleteKeyValue(hkey: HKEY, subKey: string, valueName: string): boolean; + export function deleteValue(hkey: HKEY, valueName: string): boolean; + export function closeKey(hkey: HKEY | null | undefined): void; + export type ParsedValue = number | string | string[] | Buffer; + export function parseValue(value: Value | null): ParsedValue | null; + export function parseString(value: Buffer): string; + export function parseMultiString(value: Buffer): string[]; + export function formatString(value: string): Buffer; + export function formatMultiString(values: string[]): Buffer; + export function formatDWORD(value: number): Buffer; + export function formatQWORD(value: number): Buffer; + export function setValueSZ(hkey: HKEY, valueName: string, value: string): void; + export function setValueEXPAND_SZ(hkey: HKEY, valueName: string, value: string): void; + export function setValueMULTI_SZ(hkey: HKEY, valueName: string, value: string[]): void; + export function setValueDWORD(hkey: HKEY, valueName: string, value: number): void; + export function setValueQWORD(hkey: HKEY, valueName: string, value: number): void; + export function getValue(hkey: HKEY, subKey: string, valueName: string, flags?: GetValueFlags): ParsedValue | null; + export function queryValue(hkey: HKEY, valueName: string): ParsedValue | null; +} diff --git a/app/system-context-menu.ts b/app/system-context-menu.ts index c88ebb0b..071469bf 100644 --- a/app/system-context-menu.ts +++ b/app/system-context-menu.ts @@ -1,12 +1,5 @@ -import * as regTypes from './typings/native-reg'; -if (process.platform === 'win32') { - try { - // eslint-disable-next-line no-var, @typescript-eslint/no-var-requires - var Registry: typeof regTypes = require('native-reg'); - } catch (err) { - console.error(err); - } -} +import {Registry, loadRegistry} from './utils/registry'; +import type {HKEY} from 'native-reg'; const appPath = `"${process.execPath}"`; const regKeys = [ @@ -20,7 +13,8 @@ const regParts = [ {name: 'Icon', value: `${appPath}`} ]; -function addValues(hyperKey: regTypes.HKEY, commandKey: regTypes.HKEY) { +function addValues(hyperKey: HKEY, commandKey: HKEY) { + if (!loadRegistry()) return; try { Registry.setValueSZ(hyperKey, regParts[1].name, regParts[1].value); } catch (error) { @@ -39,6 +33,7 @@ function addValues(hyperKey: regTypes.HKEY, commandKey: regTypes.HKEY) { } export const add = () => { + if (!loadRegistry()) return; regKeys.forEach((regKey) => { try { const hyperKey = @@ -57,6 +52,7 @@ export const add = () => { }; export const remove = () => { + if (!loadRegistry()) return; regKeys.forEach((regKey) => { try { Registry.deleteTree(Registry.HKCU, regKey); diff --git a/app/typings/native-reg.d.ts b/app/typings/native-reg.d.ts deleted file mode 100644 index ce1a902e..00000000 --- a/app/typings/native-reg.d.ts +++ /dev/null @@ -1,105 +0,0 @@ -/// -export declare enum HKEY { - CLASSES_ROOT = 2147483648, - CURRENT_USER = 2147483649, - LOCAL_MACHINE = 2147483650, - USERS = 2147483651, - PERFORMANCE_DATA = 2147483652, - PERFORMANCE_TEXT = 2147483728, - PERFORMANCE_NLSTEXT = 2147483744, - CURRENT_CONFIG = 2147483653, - DYN_DATA = 2147483654, - CURRENT_USER_LOCAL_SETTINGS = 2147483655 -} -export declare enum CreateKeyOptions { - NON_VOLATILE = 0, - VOLATILE = 1, - CREATE_LINK = 2, - BACKUP_RESTORE = 4 -} -export declare enum OpenKeyOptions { - OPEN_LINK = 8 -} -export declare enum Access { - QUERY_VALUE = 1, - SET_VALUE = 2, - CREATE_SUB_KEY = 4, - ENUMERATE_SUB_KEYS = 8, - NOTIFY = 16, - CREATE_LINK = 32, - WOW64_64KEY = 256, - WOW64_32KEY = 512, - READ = 131097, - WRITE = 131078, - EXECUTE = 131097, - ALL_ACCESS = 983103 -} -export declare enum ValueType { - NONE = 0, - SZ = 1, - EXPAND_SZ = 2, - BINARY = 3, - DWORD = 4, - DWORD_LITTLE_ENDIAN = 4, - DWORD_BIG_ENDIAN = 5, - LINK = 6, - MULTI_SZ = 7, - RESOURCE_LIST = 8, - FULL_RESOURCE_DESCRIPTOR = 9, - RESOURCE_REQUIREMENTS_LIST = 10, - QWORD = 11, - QWORD_LITTLE_ENDIAN = 11 -} -export declare enum GetValueFlags { - RT_ANY = 65535, - RT_REG_NONE = 1, - RT_REG_SZ = 2, - RT_REG_EXPAND_SZ = 4, - RT_REG_BINARY = 8, - RT_REG_DWORD = 16, - RT_REG_MULTI_SZ = 32, - RT_REG_QWORD = 64, - RT_DWORD = 24, - RT_QWORD = 72, - NO_EXPAND = 268435456, - SUBKEY_WOW6464KEY = 65536, - SUBKEY_WOW6432KEY = 131072 -} -export declare const HKCR = HKEY.CLASSES_ROOT; -export declare const HKCU = HKEY.CURRENT_USER; -export declare const HKLM = HKEY.LOCAL_MACHINE; -export declare const HKU = HKEY.USERS; -export declare type Value = Buffer & { - type: ValueType; -}; -export declare function isHKEY(hkey: any): hkey is HKEY; -export declare function createKey(hkey: HKEY, subKey: string, access: Access, options?: CreateKeyOptions): HKEY; -export declare function openKey(hkey: HKEY, subKey: string, access: Access, options?: OpenKeyOptions): HKEY | null; -export declare function openCurrentUser(access?: Access): HKEY; -export declare function loadAppKey(file: string, access: Access): HKEY | null; -export declare function enumKeyNames(hkey: HKEY): string[]; -export declare function enumValueNames(hkey: HKEY): string[]; -export declare function queryValueRaw(hkey: HKEY, valueName: string): Value | null; -export declare function getValueRaw(hkey: HKEY, subKey: string, valueName: string, flags?: GetValueFlags): Value | null; -export declare function setValueRaw(hkey: HKEY, valueName: string, valueType: ValueType, data: Buffer): void; -export declare function deleteKey(hkey: HKEY, subKey: string): boolean; -export declare function deleteTree(hkey: HKEY, subKey: string): boolean; -export declare function deleteKeyValue(hkey: HKEY, subKey: string, valueName: string): boolean; -export declare function deleteValue(hkey: HKEY, valueName: string): boolean; -export declare function closeKey(hkey: HKEY | null | undefined): void; -export declare type ParsedValue = number | string | string[] | Buffer; -export declare function parseValue(value: Value | null): ParsedValue | null; -export declare function parseString(value: Buffer): string; -export declare function parseMultiString(value: Buffer): string[]; -export declare function formatString(value: string): Buffer; -export declare function formatMultiString(values: string[]): Buffer; -export declare function formatDWORD(value: number): Buffer; -export declare function formatQWORD(value: number): Buffer; -export declare function setValueSZ(hkey: HKEY, valueName: string, value: string): void; -export declare function setValueEXPAND_SZ(hkey: HKEY, valueName: string, value: string): void; -export declare function setValueMULTI_SZ(hkey: HKEY, valueName: string, value: string[]): void; -export declare function setValueDWORD(hkey: HKEY, valueName: string, value: number): void; -export declare function setValueQWORD(hkey: HKEY, valueName: string, value: number): void; -export declare function getValue(hkey: HKEY, subKey: string, valueName: string, flags?: GetValueFlags): ParsedValue | null; -export declare function queryValue(hkey: HKEY, valueName: string): ParsedValue | null; -//# sourceMappingURL=index.d.ts.map diff --git a/app/utils/cli-install.ts b/app/utils/cli-install.ts index 4a5cfd0d..7f3591dd 100644 --- a/app/utils/cli-install.ts +++ b/app/utils/cli-install.ts @@ -3,16 +3,8 @@ import fs from 'fs'; import path from 'path'; import notify from '../notify'; import {cliScriptPath, cliLinkPath} from '../config/paths'; - -import * as regTypes from '../typings/native-reg'; -if (process.platform === 'win32') { - try { - // eslint-disable-next-line no-var, @typescript-eslint/no-var-requires - var Registry: typeof regTypes = require('native-reg'); - } catch (err) { - console.error(err); - } -} +import {Registry, loadRegistry} from './registry'; +import type {ValueType} from 'native-reg'; const readlink = pify(fs.readlink); const symlink = pify(fs.symlink); @@ -41,6 +33,10 @@ const addSymlink = () => { const addBinToUserPath = () => { return new Promise((resolve, reject) => { + if (!loadRegistry()) { + reject('Failed to load Registry Module'); + return; + } try { const envKey = Registry.openKey(Registry.HKCU, 'Environment', Registry.Access.ALL_ACCESS)!; @@ -54,7 +50,7 @@ const addBinToUserPath = () => { const pathItemName = pathItem || 'PATH'; let newPathValue = binPath; - let type: regTypes.ValueType = Registry.ValueType.SZ; + let type: ValueType = Registry.ValueType.SZ; if (pathItem) { type = Registry.queryValueRaw(envKey, pathItem)!.type; if (type !== Registry.ValueType.SZ && type !== Registry.ValueType.EXPAND_SZ) { diff --git a/app/utils/registry.ts b/app/utils/registry.ts new file mode 100644 index 00000000..c8fd233b --- /dev/null +++ b/app/utils/registry.ts @@ -0,0 +1,16 @@ +export let Registry: typeof import('native-reg'); + +export const loadRegistry = () => { + if (process.platform === 'win32') { + if (!Registry) { + try { + Registry = require('native-reg'); + } catch (error) { + console.error(error); + return false; + } + } + return true; + } + return false; +};