From 3344223bfc78f66dfaf2527b0edad1bce2387bf1 Mon Sep 17 00:00:00 2001 From: Ben Gotow Date: Sun, 15 May 2016 12:19:03 -0500 Subject: [PATCH] fix(keymaps): Handle empty keymap.json, recover when saving --- .../preferences/lib/tabs/keymaps/command-item.jsx | 13 +++++++++++-- src/keymap-manager.es6 | 3 ++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/internal_packages/preferences/lib/tabs/keymaps/command-item.jsx b/internal_packages/preferences/lib/tabs/keymaps/command-item.jsx index d35055bbf..23078409d 100644 --- a/internal_packages/preferences/lib/tabs/keymaps/command-item.jsx +++ b/internal_packages/preferences/lib/tabs/keymaps/command-item.jsx @@ -83,11 +83,20 @@ export default class CommandKeybinding extends React.Component { _onFinishedEditing = () => { if (this.state.editingBinding) { const keymapPath = NylasEnv.keymaps.getUserKeymapPath(); + let keymaps = {}; try { const exists = fs.existsSync(keymapPath); - const keymaps = exists ? JSON.parse(fs.readFileSync(keymapPath)) : {}; - keymaps[this.props.command] = this.state.editingBinding; + if (exists) { + keymaps = JSON.parse(fs.readFileSync(keymapPath)); + } + } catch (err) { + console.error(err); + } + + keymaps[this.props.command] = this.state.editingBinding; + + try { fs.writeFileSync(keymapPath, JSON.stringify(keymaps, null, 2)); } catch (err) { NylasEnv.showErrorDialog(`Nylas was unable to modify your keymaps at ${keymapPath}. ${err.toString()}`); diff --git a/src/keymap-manager.es6 b/src/keymap-manager.es6 index e3381c3ad..5585f2082 100644 --- a/src/keymap-manager.es6 +++ b/src/keymap-manager.es6 @@ -41,7 +41,8 @@ class KeymapFile { if (e.code === 'ENOENT') { return; } - throw e; + console.error(e); + return; } this._bindings = {};