Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add electron-store to manage settings #609

Merged
merged 5 commits into from
Oct 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions __mocks__/electron-store.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Store {
get() {
return {}
}

set() {}
}

module.exports = Store
2 changes: 1 addition & 1 deletion app/background/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="Content-Security-Policy" content="script-src * 'unsafe-inline';"/>
<meta http-equiv="Content-Security-Policy" content="script-src * 'unsafe-inline' 'unsafe-eval';"/>
<script>
(function() {
const script = document.createElement('script');
Expand Down
95 changes: 23 additions & 72 deletions app/lib/config.js
Original file line number Diff line number Diff line change
@@ -1,75 +1,34 @@
import { app, ipcRenderer } from 'electron'
import fs from 'fs'
import path from 'path'
import { memoize } from 'cerebro-tools'
import { ipcRenderer } from 'electron'
import Store from 'electron-store'
import themes from './themes'

const remote = process.type === 'browser'
? undefined
: require('@electron/remote')

const electronApp = remote ? remote.app : app

// initiate portable mode
// set data directory to ./userdata
process.argv.forEach((arg) => {
if (arg.toLowerCase() === '-p' || arg.toLowerCase() === '--portable') {
electronApp.setPath('userData', path.join(process.cwd(), 'userdata'))
}
})

const CONFIG_FILE = path.join(electronApp.getPath('userData'), 'config.json')

const defaultSettings = memoize(() => {
const locale = electronApp.getLocale() || 'en-US'
const [lang, country] = locale.split('-')
return {
locale,
lang,
country,
theme: themes[0].value,
hotkey: 'Control+Space',
showInTray: true,
firstStart: true,
developerMode: false,
cleanOnHide: true,
hideOnBlur: true,
skipDonateDialog: false,
lastShownDonateDialog: null,
plugins: {},
isMigratedPlugins: false,
crashreportingEnabled: true,
openAtLogin: true
}
})

const readConfig = () => {
try {
return JSON.parse(fs.readFileSync(CONFIG_FILE).toString())
} catch (err) {
const config = defaultSettings()

if (err.code !== 'ENOENT') {
console.error('Error reading config file', err)
return config
}

if (process.env.NODE_ENV === 'test') return config

fs.writeFileSync(CONFIG_FILE, JSON.stringify(config, null, 2))
return defaultSettings()
}
const schema = {
locale: { type: 'string', default: 'en-US' },
lang: { type: 'string', default: 'en' },
country: { type: 'string', default: 'US' },
theme: { type: 'string', default: themes[0].value },
hotkey: { type: 'string', default: 'Control+Space' },
showInTray: { type: 'boolean', default: true },
firstStart: { type: 'boolean', default: true },
developerMode: { type: 'boolean', default: false },
cleanOnHide: { type: 'boolean', default: true },
hideOnBlur: { type: 'boolean', default: true },
skipDonateDialog: { type: 'boolean', default: false },
lastShownDonateDialog: { type: 'number', default: 0 },
plugins: { type: 'object', default: {} },
isMigratedPlugins: { type: 'boolean', default: false },
crashreportingEnabled: { type: 'boolean', default: true },
openAtLogin: { type: 'boolean', default: true }
}

const store = new Store({ schema })

/**
* Get a value from global configuration
* @param {String} key
* @return {Any}
*/
const get = (key) => {
const config = readConfig()
return config[key]
}
const get = (key) => store.get(key)

/**
* Write a value to global config. It immedately rewrites global config
Expand All @@ -79,15 +38,7 @@ const get = (key) => {
* @param {Any} value
*/
const set = (key, value) => {
const config = {
// If after app update some keys were added to config
// we use default values for that keys
...defaultSettings(),
...readConfig()
}
config[key] = value
fs.writeFileSync(CONFIG_FILE, JSON.stringify(config, null, 2))

store.set(key, value)
if (ipcRenderer) {
console.log('notify main process', key, value)
// Notify main process about settings changes
Expand Down
3 changes: 1 addition & 2 deletions app/lib/plugins/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import getAppDataPath from 'appdata-path'
import path from 'path'
import fs from 'fs'
import npm from './npm'
Expand All @@ -20,7 +19,7 @@ const EMPTY_PACKAGE_JSON = JSON.stringify({
dependencies: {}
}, null, 2)

export const pluginsPath = path.join(getAppDataPath('Cerebro'), 'plugins')
export const pluginsPath = path.join(process.env.CEREBRO_DATA_PATH, 'plugins')
export const modulesDirectory = path.join(pluginsPath, 'node_modules')
export const packageJsonPath = path.join(pluginsPath, 'package.json')

Expand Down
18 changes: 17 additions & 1 deletion app/main.development.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
app, ipcMain, crashReporter, screen
} from 'electron'
import path from 'path'
import {
WINDOW_WIDTH,
INPUT_HEIGHT,
Expand Down Expand Up @@ -44,9 +45,24 @@ if (process.env.NODE_ENV !== 'development') {
}
}

app.whenReady().then(() => {
const setupEnvVariables = () => {
process.env.CEREBRO_VERSION = app.getVersion()

const isPortableMode = process.argv.some((arg) => arg.toLowerCase() === '-p' || arg.toLowerCase() === '--portable')
// initiate portable mode
// set data directory to ./userdata
if (isPortableMode) {
const userDataPath = path.join(process.cwd(), 'userdata')
app.setPath('userData', userDataPath)
process.env.CEREBRO_DATA_PATH = userDataPath
} else {
process.env.CEREBRO_DATA_PATH = app.getPath('userData')
}
}

app.whenReady().then(() => {
setupEnvVariables()

mainWindow = createMainWindow({
isDev,
src: `file://${__dirname}/main/index.html`, // Main window html
Expand Down
2 changes: 1 addition & 1 deletion app/main/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="Content-Security-Policy" content="script-src * 'unsafe-inline';"/>
<meta http-equiv="Content-Security-Policy" content="script-src * 'unsafe-inline' 'unsafe-eval';"/>
<title>Cerebro</title>
<link rel='stylesheet' id='cerebro-theme'>
<script>
Expand Down
1 change: 1 addition & 0 deletions docs/plugins/plugin-structure.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,4 @@ Take a look at [React Select](https://github.com/JedWatson/react-select) for mor
The following variables are available in the `process.env` object:

* `CEREBRO_VERSION` – Version of Cerebro
* `CEREBRO_DATA_PATH` – Path to Cerebro data directory
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"description": "Cerebro is an open-source launcher to improve your productivity and efficiency",
"main": "main.js",
"scripts": {
"test": "cross-env NODE_ENV=test jest",
"test": "cross-env NODE_ENV=test CEREBRO_DATA_PATH=userdata jest",
"test-watch": "jest -- --watch",
"lint": "eslint app/background app/lib app/main app/plugins *.js",
"hot-server": "node -r @babel/register server.js",
Expand Down Expand Up @@ -91,9 +91,9 @@
"dependencies": {
"@cerebroapp/cerebro-ui": "2.0.0-alpha.2",
"@electron/remote": "2.0.8",
"appdata-path": "1.0.0",
"auto-launch": "5.0.5",
"cerebro-tools": "0.1.8",
"electron-store": "8.1.0",
"escape-string-regexp": "5.0.0",
"lodash": "4.17.21",
"normalize.css": "8.0.1",
Expand Down
74 changes: 67 additions & 7 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2773,7 +2773,7 @@ ajv@^6.10.0, ajv@^6.12.0, ajv@^6.12.4, ajv@^6.12.5:
json-schema-traverse "^0.4.1"
uri-js "^4.2.2"

ajv@^8.0.0, ajv@^8.11.0, ajv@^8.8.0:
ajv@^8.0.0, ajv@^8.11.0, ajv@^8.6.3, ajv@^8.8.0:
version "8.11.0"
resolved "https://registry.yarnpkg.com/ajv/-/ajv-8.11.0.tgz#977e91dd96ca669f54a11e23e378e33b884a565f"
integrity sha512-wGgprdCvMalC0BztXvitD2hC04YffAvtsUn93JbGXYLAtCUO4xd17mCCZQxUOItiBwZvJScWo8NIvQMQ71rdpg==
Expand Down Expand Up @@ -2886,11 +2886,6 @@ [email protected]:
tar "^6.1.11"
temp-file "^3.4.0"

[email protected]:
version "1.0.0"
resolved "https://registry.yarnpkg.com/appdata-path/-/appdata-path-1.0.0.tgz#c4022d0b6727d1ddc1dd7ecec143d4352f3eefad"
integrity sha512-ZbH3ezXfnT/YE3NdqduIt4lBV+H0ybvA2Qx3K76gIjQvh8gROpDFdDLpx6B1QJtW7zxisCbpTlCLhKqoR8cDBw==

applescript@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/applescript/-/applescript-1.0.0.tgz#bb87af568cad034a4e48c4bdaf6067a3a2701317"
Expand Down Expand Up @@ -3050,6 +3045,11 @@ at-least-node@^1.0.0:
resolved "https://registry.yarnpkg.com/at-least-node/-/at-least-node-1.0.0.tgz#602cd4b46e844ad4effc92a8011a3c46e0238dc2"
integrity sha512-+q/t7Ekv1EDY2l6Gda6LLiX14rU9TV20Wa3ofeQmwPFZbOMo9DXrLbOjFaaclkXKWidIaopwAObQDqwWtGUjqg==

atomically@^1.7.0:
version "1.7.0"
resolved "https://registry.yarnpkg.com/atomically/-/atomically-1.7.0.tgz#c07a0458432ea6dbc9a3506fffa424b48bccaafe"
integrity sha512-Xcz9l0z7y9yQ9rdDaxlmaI4uJHf/T8g9hOEzJcsEqX2SjCj4J20uK7+ldkDHMbpJDK76wF7xEIgxc/vSlsfw5w==

[email protected]:
version "5.0.5"
resolved "https://registry.yarnpkg.com/auto-launch/-/auto-launch-5.0.5.tgz#d14bd002b1ef642f85e991a6195ff5300c8ad3c0"
Expand Down Expand Up @@ -3799,6 +3799,22 @@ [email protected]:
resolved "https://registry.yarnpkg.com/concat-map/-/concat-map-0.0.1.tgz#d8a96bd77fd68df7793a73036a3ba0d5405d477b"
integrity sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=

conf@^10.2.0:
version "10.2.0"
resolved "https://registry.yarnpkg.com/conf/-/conf-10.2.0.tgz#838e757be963f1a2386dfe048a98f8f69f7b55d6"
integrity sha512-8fLl9F04EJqjSqH+QjITQfJF8BrOVaYr1jewVgSRAEWePfxT0sku4w2hrGQ60BC/TNLGQ2pgxNlTbWQmMPFvXg==
dependencies:
ajv "^8.6.3"
ajv-formats "^2.1.1"
atomically "^1.7.0"
debounce-fn "^4.0.0"
dot-prop "^6.0.1"
env-paths "^2.2.1"
json-schema-typed "^7.0.3"
onetime "^5.1.2"
pkg-up "^3.1.0"
semver "^7.3.5"

config-chain@^1.1.11:
version "1.1.12"
resolved "https://registry.yarnpkg.com/config-chain/-/config-chain-1.1.12.tgz#0fde8d091200eb5e808caf25fe618c02f48e4efa"
Expand Down Expand Up @@ -4092,6 +4108,13 @@ data-urls@^2.0.0:
whatwg-mimetype "^2.3.0"
whatwg-url "^8.0.0"

debounce-fn@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/debounce-fn/-/debounce-fn-4.0.0.tgz#ed76d206d8a50e60de0dd66d494d82835ffe61c7"
integrity sha512-8pYCQiL9Xdcg0UPSD3d+0KMlOjp+KGU5EPwYddgzQ7DATsg4fuUDjQtsYLmWjnk2obnNHgV3vE2Y4jejSOJVBQ==
dependencies:
mimic-fn "^3.0.0"

[email protected], debug@^2.6.8, debug@^2.6.9:
version "2.6.9"
resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.9.tgz#5d128515df134ff327e90a4c93f4e077a536341f"
Expand Down Expand Up @@ -4365,6 +4388,13 @@ dot-prop@^5.1.0, dot-prop@^5.2.0:
dependencies:
is-obj "^2.0.0"

dot-prop@^6.0.1:
version "6.0.1"
resolved "https://registry.yarnpkg.com/dot-prop/-/dot-prop-6.0.1.tgz#fc26b3cf142b9e59b74dbd39ed66ce620c681083"
integrity sha512-tE7ztYzXHIeyvc7N+hR3oi7FIbf/NIjVP9hmAt3yMXzrQ072/fpjGLx2GxNxGxUl5V73MEqYzioOMoVhGMJ5cA==
dependencies:
is-obj "^2.0.0"

dotenv-expand@^5.1.0:
version "5.1.0"
resolved "https://registry.yarnpkg.com/dotenv-expand/-/dotenv-expand-5.1.0.tgz#3fbaf020bfd794884072ea26b1e9791d45a629f0"
Expand Down Expand Up @@ -4435,6 +4465,14 @@ [email protected]:
lazy-val "^1.0.5"
mime "^2.5.2"

[email protected]:
version "8.1.0"
resolved "https://registry.yarnpkg.com/electron-store/-/electron-store-8.1.0.tgz#46a398f2bd9aa83c4a9daaae28380e2b3b9c7597"
integrity sha512-2clHg/juMjOH0GT9cQ6qtmIvK183B39ZXR0bUoPwKwYHJsEF3quqyDzMFUAu+0OP8ijmN2CbPRAelhNbWUbzwA==
dependencies:
conf "^10.2.0"
type-fest "^2.17.0"

electron-to-chromium@^1.3.896:
version "1.3.899"
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.3.899.tgz#4d7d040e73def3d5f5bd6b8a21049025dce6fce0"
Expand Down Expand Up @@ -4504,7 +4542,7 @@ enhanced-resolve@^5.10.0:
graceful-fs "^4.2.4"
tapable "^2.2.0"

env-paths@^2.2.0:
env-paths@^2.2.0, env-paths@^2.2.1:
version "2.2.1"
resolved "https://registry.yarnpkg.com/env-paths/-/env-paths-2.2.1.tgz#420399d416ce1fbe9bc0a07c62fa68d67fd0f8f2"
integrity sha512-+h1lkLKhZMTYjog1VEpJNG7NZJWcuc2DDk/qsqSTRRCOXiLjeQ1d1/udrUGhqMxUgAlwKNZ0cf2uqan5GLuS2A==
Expand Down Expand Up @@ -6970,6 +7008,11 @@ json-schema-traverse@^1.0.0:
resolved "https://registry.yarnpkg.com/json-schema-traverse/-/json-schema-traverse-1.0.0.tgz#ae7bcb3656ab77a73ba5c49bf654f38e6b6860e2"
integrity sha512-NM8/P9n3XjXhIZn1lLhkFaACTOURQXjWhV4BA/RnOv8xvgqtqpAX9IO4mRQxSx1Rlo4tqzeqb0sOlruaOy3dug==

json-schema-typed@^7.0.3:
version "7.0.3"
resolved "https://registry.yarnpkg.com/json-schema-typed/-/json-schema-typed-7.0.3.tgz#23ff481b8b4eebcd2ca123b4fa0409e66469a2d9"
integrity sha512-7DE8mpG+/fVw+dTpjbxnx47TaMnDfOI1jwft9g1VybltZCduyRQPJPvc+zzKY9WPHxhPWczyFuYa6I8Mw4iU5A==

json-stable-stringify-without-jsonify@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/json-stable-stringify-without-jsonify/-/json-stable-stringify-without-jsonify-1.0.1.tgz#9db7b59496ad3f3cfef30a75142d2d930ad72651"
Expand Down Expand Up @@ -7746,6 +7789,11 @@ mimic-fn@^2.1.0:
resolved "https://registry.yarnpkg.com/mimic-fn/-/mimic-fn-2.1.0.tgz#7ed2c2ccccaf84d3ffcb7a69b57711fc2083401b"
integrity sha512-OqbOk5oEQeAZ8WXWydlu9HJjz9WVdEIvamMCcXmuqUYjTknH/sqsWvhQ3vgwKFRR1HpjvNBKQ37nbJgYzGqGcg==

mimic-fn@^3.0.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/mimic-fn/-/mimic-fn-3.1.0.tgz#65755145bbf3e36954b949c16450427451d5ca74"
integrity sha512-Ysbi9uYW9hFyfrThdDEQuykN4Ey6BuwPD2kpI5ES/nFTDn/98yxYNLZJcgUAKPT/mcrLLKaGzJR9YVxJrIdASQ==

mimic-response@^1.0.0, mimic-response@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/mimic-response/-/mimic-response-1.0.1.tgz#4923538878eef42063cb8a3e3b0798781487ab1b"
Expand Down Expand Up @@ -8358,6 +8406,13 @@ pkg-dir@^4.1.0, pkg-dir@^4.2.0:
dependencies:
find-up "^4.0.0"

pkg-up@^3.1.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/pkg-up/-/pkg-up-3.1.0.tgz#100ec235cc150e4fd42519412596a28512a0def5"
integrity sha512-nDywThFk1i4BQK4twPQ6TA4RT8bDY96yeuCVBWL3ePARCiEKDRSrNGbFIgUJpLp+XeIR65v8ra7WuJOFUBtkMA==
dependencies:
find-up "^3.0.0"

plist@^3.0.1:
version "3.0.2"
resolved "https://registry.yarnpkg.com/plist/-/plist-3.0.2.tgz#74bbf011124b90421c22d15779cee60060ba95bc"
Expand Down Expand Up @@ -9970,6 +10025,11 @@ type-fest@^0.8.1:
resolved "https://registry.yarnpkg.com/type-fest/-/type-fest-0.8.1.tgz#09e249ebde851d3b1e48d27c105444667f17b83d"
integrity sha512-4dbzIzqvjtgiM5rw1k5rEHtBANKmdudhGyBEajN01fEyhaAIhsoKNy6y7+IN93IfpFtwY9iqi7kD+xwKhQsNJA==

type-fest@^2.17.0:
version "2.19.0"
resolved "https://registry.yarnpkg.com/type-fest/-/type-fest-2.19.0.tgz#88068015bb33036a598b952e55e9311a60fd3a9b"
integrity sha512-RAH822pAdBgcNMAfWnCBU3CFZcfZ/i1eZjwFU/dsLKumyuuP3niueg2UAukXYF0E2AAoc82ZSSf9J0WQBinzHA==

type-is@~1.6.18:
version "1.6.18"
resolved "https://registry.yarnpkg.com/type-is/-/type-is-1.6.18.tgz#4e552cd05df09467dcbc4ef739de89f2cf37c131"
Expand Down