fix(keymaps): Handle empty keymap.json, recover when saving

This commit is contained in:
Ben Gotow 2016-05-15 12:19:03 -05:00
parent f865b48f4e
commit e23d597493
2 changed files with 13 additions and 3 deletions

View file

@ -83,11 +83,20 @@ export default class CommandKeybinding extends React.Component {
_onFinishedEditing = () => { _onFinishedEditing = () => {
if (this.state.editingBinding) { if (this.state.editingBinding) {
const keymapPath = NylasEnv.keymaps.getUserKeymapPath(); const keymapPath = NylasEnv.keymaps.getUserKeymapPath();
let keymaps = {};
try { try {
const exists = fs.existsSync(keymapPath); const exists = fs.existsSync(keymapPath);
const keymaps = exists ? JSON.parse(fs.readFileSync(keymapPath)) : {}; if (exists) {
keymaps[this.props.command] = this.state.editingBinding; 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)); fs.writeFileSync(keymapPath, JSON.stringify(keymaps, null, 2));
} catch (err) { } catch (err) {
NylasEnv.showErrorDialog(`Nylas was unable to modify your keymaps at ${keymapPath}. ${err.toString()}`); NylasEnv.showErrorDialog(`Nylas was unable to modify your keymaps at ${keymapPath}. ${err.toString()}`);

View file

@ -41,7 +41,8 @@ class KeymapFile {
if (e.code === 'ENOENT') { if (e.code === 'ENOENT') {
return; return;
} }
throw e; console.error(e);
return;
} }
this._bindings = {}; this._bindings = {};