Skip to content

Commit

Permalink
Merge pull request #783 from asmsuechan/add-boostnoterc-for-config
Browse files Browse the repository at this point in the history
Add boostnoterc revival
  • Loading branch information
asmsuechan committed Aug 10, 2017
2 parents 9eaa90c + af91c40 commit 35938c0
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 4 deletions.
32 changes: 32 additions & 0 deletions .boostnoterc.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"editor": {
"fontFamily": "Monaco, Consolas",
"fontSize": "14",
"indentSize": "2",
"indentType": "space",
"keyMap": "vim",
"switchPreview": "BLUR",
"theme": "monokai"
},
"hotkey": {
"toggleFinder": "Cmd + Alt + S",
"toggleMain": "Cmd + Alt + L"
},
"isSideNavFolded": false,
"listStyle": "DEFAULT",
"listWidth": 174,
"navWidth": 200,
"preview": {
"codeBlockTheme": "dracula",
"fontFamily": "Lato",
"fontSize": "14",
"lineNumber": true,
},
"sortBy": "UPDATED_AT",
"ui": {
"defaultNote": "ALWAYS_ASK",
"disableDirectWrite": false,
"theme": "default"
},
"zoom": 1
}
22 changes: 18 additions & 4 deletions browser/main/lib/ConfigManager.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import _ from 'lodash'
import RcParser from 'browser/main/lib/RcParser'

const OSX = global.process.platform === 'darwin'
const win = global.process.platform === 'win32'
const electron = require('electron')
const { ipcRenderer } = electron
const consts = require('browser/lib/consts')
const path = require('path')
const fs = require('fs')

let isInitialized = false

Expand Down Expand Up @@ -60,11 +63,13 @@ function get () {
let config = window.localStorage.getItem('config')

try {
const boostnotercConfig = RcParser.parse()

config = Object.assign({}, DEFAULT_CONFIG, JSON.parse(config))
config.hotkey = Object.assign({}, DEFAULT_CONFIG.hotkey, config.hotkey)
config.ui = Object.assign({}, DEFAULT_CONFIG.ui, config.ui)
config.editor = Object.assign({}, DEFAULT_CONFIG.editor, config.editor)
config.preview = Object.assign({}, DEFAULT_CONFIG.preview, config.preview)

config = Object.assign({}, DEFAULT_CONFIG, boostnotercConfig)
config = assignConfigValues(config, boostnotercConfig, config)

if (!validate(config)) throw new Error('INVALID CONFIG')
} catch (err) {
console.warn('Boostnote resets the malformed configuration.')
Expand Down Expand Up @@ -126,6 +131,15 @@ function set (updates) {
})
}

function assignConfigValues (config, rcConfig, originalConfig) {
config = Object.assign({}, DEFAULT_CONFIG, rcConfig, originalConfig)
config.hotkey = Object.assign({}, DEFAULT_CONFIG.hotkey, rcConfig.hotkey, originalConfig.hotkey)
config.ui = Object.assign({}, DEFAULT_CONFIG.ui, rcConfig.ui, originalConfig.ui)
config.editor = Object.assign({}, DEFAULT_CONFIG.editor, rcConfig.editor, originalConfig.editor)
config.preview = Object.assign({}, DEFAULT_CONFIG.preview, rcConfig.preview, originalConfig.preview)
return config
}

export default {
get,
set,
Expand Down
15 changes: 15 additions & 0 deletions browser/main/lib/RcParser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import path from 'path'
import sander from 'sander'

function parse () {
const BOOSTNOTERC = '.boostnoterc'
const homePath = global.process.env.HOME || global.process.env.USERPROFILE
const boostnotercPath = path.join(homePath, BOOSTNOTERC)

if (!sander.existsSync(boostnotercPath)) return {}
return JSON.parse(sander.readFileSync(boostnotercPath).toString())
}

export default {
parse
}

0 comments on commit 35938c0

Please sign in to comment.