mirror of
https://github.com/quine-global/hyper.git
synced 2026-01-18 06:28:40 -09:00
Check default app (#1918)
* Check .js default app * Cleanup fallback check * Check all keys * Check for empty strings
This commit is contained in:
parent
ca849425ac
commit
056c88f741
1 changed files with 60 additions and 16 deletions
|
|
@ -4,7 +4,56 @@ const {cfgPath} = require('./paths');
|
||||||
module.exports = () => Promise.resolve(shell.openItem(cfgPath));
|
module.exports = () => Promise.resolve(shell.openItem(cfgPath));
|
||||||
|
|
||||||
if (process.platform === 'win32') {
|
if (process.platform === 'win32') {
|
||||||
const exec = require('child_process').exec;
|
const Registry = require('winreg');
|
||||||
|
const {exec} = require('child_process');
|
||||||
|
|
||||||
|
// 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.
|
||||||
|
const getFileExtKeys = () => new Promise((resolve, reject) => {
|
||||||
|
Registry({
|
||||||
|
hive: Registry.HKCU,
|
||||||
|
key: '\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts\\.js'
|
||||||
|
})
|
||||||
|
.keys((error, keys) => {
|
||||||
|
if (error) {
|
||||||
|
reject(error);
|
||||||
|
} else {
|
||||||
|
resolve(keys || []);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
const hasDefaultSet = async () => {
|
||||||
|
const keys = await getFileExtKeys();
|
||||||
|
|
||||||
|
const valueGroups = await Promise.all(keys.map(key => new Promise((resolve, reject) => {
|
||||||
|
key.values((error, items) => {
|
||||||
|
if (error) {
|
||||||
|
reject(error);
|
||||||
|
}
|
||||||
|
resolve(items.map(item => item.value || '') || []);
|
||||||
|
});
|
||||||
|
})));
|
||||||
|
|
||||||
|
const values = valueGroups
|
||||||
|
.reduce((allValues, groupValues) => ([...allValues, ...groupValues]), [])
|
||||||
|
.filter(value => value && typeof value === 'string');
|
||||||
|
|
||||||
|
// No default app set
|
||||||
|
if (values.length === 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// WScript is in default apps list
|
||||||
|
if (values.some(value => value.includes('WScript.exe'))) {
|
||||||
|
const userDefaults = values.filter(value => value.endsWith('.exe') && !value.includes('WScript.exe'));
|
||||||
|
|
||||||
|
// WScript.exe is overidden
|
||||||
|
return (userDefaults.length > 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
|
||||||
// This mimics shell.openItem, true if it worked, false if not.
|
// This mimics shell.openItem, true if it worked, false if not.
|
||||||
const openNotepad = file => new Promise(resolve => {
|
const openNotepad = file => new Promise(resolve => {
|
||||||
|
|
@ -13,21 +62,16 @@ if (process.platform === 'win32') {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// Windows opens .js files with WScript.exe by default
|
module.exports = () => hasDefaultSet()
|
||||||
// If the user hasn't set up an editor for .js files, we fallback to notepad.
|
.then(yes => {
|
||||||
const canOpenNative = () => new Promise((resolve, reject) => {
|
if (yes) {
|
||||||
exec('ftype JSFile', (error, stdout) => {
|
return shell.openItem(cfgPath);
|
||||||
if (error) {
|
|
||||||
reject(error);
|
|
||||||
} else if (stdout && stdout.includes('WScript.exe')) {
|
|
||||||
reject(new Error('WScript is the default editor for .js files'));
|
|
||||||
} else {
|
|
||||||
resolve(true);
|
|
||||||
}
|
}
|
||||||
|
console.warn('No default app set for .js files, using notepad.exe fallback');
|
||||||
|
return openNotepad(cfgPath);
|
||||||
|
})
|
||||||
|
.catch(err => {
|
||||||
|
console.error('Open config with default app error:', err);
|
||||||
|
return openNotepad(cfgPath);
|
||||||
});
|
});
|
||||||
});
|
|
||||||
|
|
||||||
module.exports = () => canOpenNative()
|
|
||||||
.then(() => shell.openItem(cfgPath))
|
|
||||||
.catch(() => openNotepad(cfgPath));
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue