From 07ef0079fa22674652d29c61451f8c148145bae9 Mon Sep 17 00:00:00 2001 From: Albin Ekblom Date: Fri, 26 May 2017 18:23:25 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=89=20Check=20for=20WScript.exe=20(#18?= =?UTF-8?q?72)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 🎉 Check for WScript.exe * 🔥 Move openConfig * 🔥 Make openNotepad mimic openItem * 🌹 Add comments * 🔥 Cleanup windows workaround * 🔥 Cleanup * 🎉 Use openConfig on darwin too --- app/config.js | 5 +++++ app/config/open.js | 33 +++++++++++++++++++++++++++++++++ app/menus/menus/darwin.js | 6 +++--- app/menus/menus/edit.js | 5 ++--- 4 files changed, 43 insertions(+), 6 deletions(-) create mode 100644 app/config/open.js 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(); } } );