hyper/app/config/open.ts

87 lines
2.8 KiB
TypeScript
Raw Normal View History

2019-11-28 05:17:01 -09:00
import {shell} from 'electron';
import {cfgPath} from './paths';
2020-02-21 05:17:03 -09:00
import * as regTypes from '../typings/native-reg';
2020-02-21 05:17:03 -09:00
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);
}
const {exec} = require('child_process') as typeof import('child_process');
2020-02-21 05:17:03 -09:00
const getUserChoiceKey = async () => {
try {
// Load FileExts keys for .js files
const fileExtsKeys = Registry.openKey(
Registry.HKCU,
'Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts\\.js',
Registry.Access.READ
);
const keys = fileExtsKeys ? Registry.enumKeyNames(fileExtsKeys) : [];
Registry.closeKey(fileExtsKeys);
2020-02-21 05:17:03 -09:00
// Find UserChoice key
2020-03-25 02:15:08 -08:00
const userChoice = keys.find((k) => k.endsWith('UserChoice'));
2020-02-21 05:17:03 -09:00
return userChoice
? `Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts\\.js\\${userChoice}`
: userChoice;
} catch (error) {
console.error(error);
return;
}
};
2020-02-21 05:17:03 -09:00
const hasDefaultSet = async () => {
const userChoice = await getUserChoiceKey();
if (!userChoice) return false;
2020-02-21 05:17:03 -09:00
try {
// Load key values
const userChoiceKey = Registry.openKey(Registry.HKCU, userChoice, Registry.Access.READ)!;
const values: string[] = Registry.enumValueNames(userChoiceKey).map(
2020-03-25 02:15:08 -08:00
(x) => (Registry.queryValue(userChoiceKey, x) as string) || ''
2020-02-21 05:17:03 -09:00
);
Registry.closeKey(userChoiceKey);
2020-02-21 05:17:03 -09:00
// Look for default program
const hasDefaultProgramConfigured = values.every(
2020-03-25 02:15:08 -08:00
(value) => value && typeof value === 'string' && !value.includes('WScript.exe') && !value.includes('JSFile')
2020-02-21 05:17:03 -09:00
);
2020-02-21 05:17:03 -09:00
return hasDefaultProgramConfigured;
} catch (error) {
console.error(error);
return false;
}
};
2020-02-21 05:17:03 -09:00
// This mimics shell.openItem, true if it worked, false if not.
const openNotepad = (file: string) =>
2020-03-25 02:15:08 -08:00
new Promise<boolean>((resolve) => {
exec(`start notepad.exe ${file}`, (error) => {
2020-02-21 05:17:03 -09:00
resolve(!error);
});
});
2020-02-21 05:17:03 -09:00
return hasDefaultSet()
2020-03-25 02:15:08 -08:00
.then((yes) => {
if (yes) {
return shell.openItem(cfgPath);
}
console.warn('No default app set for .js files, using notepad.exe fallback');
return openNotepad(cfgPath);
})
2020-03-25 02:15:08 -08:00
.catch((err) => {
console.error('Open config with default app error:', err);
return openNotepad(cfgPath);
});
2020-02-21 05:17:03 -09:00
} else {
return Promise.resolve(shell.openItem(cfgPath));
}
};