diff --git a/app/config.js b/app/config.js index 1f8ed28d..b2da8b79 100644 --- a/app/config.js +++ b/app/config.js @@ -6,6 +6,7 @@ const gaze = require('gaze'); const Config = require('electron-config'); const notify = require('./notify'); const _paths = require('./config/paths'); +const _openConfig = require('./config/open'); // local storage const winCfg = new Config({ @@ -123,6 +124,10 @@ exports.getConfig = function () { return cfg.config; }; +exports.openConfig = function () { + return _openConfig(); +}; + exports.getPlugins = function () { return { plugins: cfg.plugins, diff --git a/app/config/open.js b/app/config/open.js new file mode 100644 index 00000000..ed8e457a --- /dev/null +++ b/app/config/open.js @@ -0,0 +1,33 @@ +const {shell} = require('electron'); +const {confPath} = require('./paths'); + +module.exports = () => Promise.resolve(shell.openItem(confPath)); + +if (process.platform === 'win32') { + const exec = require('child_process').exec; + + // This mimics shell.openItem, true if it worked, false if not. + const openNotepad = file => new Promise(resolve => { + exec(`start notepad.exe ${file}`, error => { + resolve(!error); + }); + }); + + // 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 canOpenNative = () => new Promise((resolve, reject) => { + exec('ftype JSFile', (error, stdout) => { + 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); + } + }); + }); + + module.exports = () => canOpenNative() + .then(() => shell.openItem(confPath)) + .catch(() => openNotepad(confPath)); +} diff --git a/app/menus/menus/darwin.js b/app/menus/menus/darwin.js index cd28ba68..4a5a2fd0 100644 --- a/app/menus/menus/darwin.js +++ b/app/menus/menus/darwin.js @@ -1,8 +1,8 @@ // This menu label is overrided by OSX to be the appName // The label is set to appName here so it matches actual behavior -const {app, shell} = require('electron'); +const {app} = require('electron'); const {accelerators} = require('../../accelerators'); -const {confPath} = require('../../config/paths'); +const {openConfig} = require('../../config'); module.exports = function () { return { @@ -18,7 +18,7 @@ module.exports = function () { label: 'Preferences...', accelerator: accelerators.preferences, click() { - shell.openItem(confPath); + openConfig(); } }, { diff --git a/app/menus/menus/edit.js b/app/menus/menus/edit.js index bb6c0a10..a0596e36 100644 --- a/app/menus/menus/edit.js +++ b/app/menus/menus/edit.js @@ -1,6 +1,5 @@ -const {shell} = require('electron'); const {accelerators} = require('../../accelerators'); -const {confPath} = require('../../config/paths'); +const {openConfig} = require('../../config'); module.exports = function () { const submenu = [ @@ -52,7 +51,7 @@ module.exports = function () { label: 'Preferences...', accelerator: accelerators.preferences, click() { - shell.openItem(confPath); + openConfig(); } } );