Skip to content

Commit

Permalink
feat: migrate methods to arrow function
Browse files Browse the repository at this point in the history
  • Loading branch information
ogustavo-pereira committed Dec 28, 2022
1 parent 20483dd commit 4907c74
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 54 deletions.
5 changes: 3 additions & 2 deletions app/background/background.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import React from 'react'
import ReactDOM from 'react-dom'
import plugins from 'plugins'
import { on, send } from 'lib/rpc'
import { settings as pluginSettings, modulesDirectory } from 'lib/plugins'
import PluginsService from 'plugins'

global.React = React
global.ReactDOM = ReactDOM
Expand All @@ -11,7 +11,8 @@ on('initializePluginAsync', ({ name }) => {
console.group(`Initialize async plugin ${name}`)

try {
const plugin = PluginsService.getCorePlugins()[name] || window.require(`${modulesDirectory}/${name}`)
const plugin = plugins.getCorePlugins()[name] || window.require(`${modulesDirectory}/${name}`)
console.log({ plugin })
const { initializeAsync } = plugin

if (!initializeAsync) {
Expand Down
37 changes: 17 additions & 20 deletions app/background/index.html
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
<!doctype html>
<html>

<head>
<meta charset="utf-8">
<meta http-equiv="Content-Security-Policy" content="script-src * 'unsafe-inline' 'unsafe-eval';" />
<script>
global.isBackground = true;
(function () {
const script = document.createElement('script');
script.async = true;
script.src = (process.env.NODE_ENV)
? 'http://localhost:3000/dist/background.bundle.js'
: '../dist/background.bundle.js';
document.write(script.outerHTML);
}());
</script>
</head>

<body>
</body>

<head>
<meta charset="utf-8">
<meta http-equiv="Content-Security-Policy" content="script-src * 'unsafe-inline' 'unsafe-eval';" />
<script>
global.isBackground = true;
(function () {
const script = document.createElement('script');
script.async = true;
script.src = (process.env.NODE_ENV)
? 'http://localhost:3000/dist/background.bundle.js'
: '../dist/background.bundle.js';
document.write(script.outerHTML);
}());
</script>
</head>
<body>
</body>
</html>
20 changes: 9 additions & 11 deletions app/lib/initializePlugins.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
import { on } from 'lib/rpc'
import PluginsService from 'plugins'
import plugins from 'plugins'
import initPlugin from './initPlugin'

function listenToAsyncMessages() {
on('plugin.message', ({ name, data }) => {
const plugin = PluginsService.getAllPlugins()[name]
if (plugin.onMessage) plugin.onMessage(data)
})
}

/**
* Initialize all plugins and start listening for replies from plugin async initializers
*/
export default () => {
const initializePlugins = () => {
// Start listening for replies from plugin async initializers
listenToAsyncMessages()
on('plugin.message', ({ name, data }) => {
const plugin = plugins.getAllPlugins()[name]
if (plugin.onMessage) plugin.onMessage(data)
})

const allPlugins = PluginsService.getAllPlugins()
const allPlugins = plugins.getAllPlugins()

Object.keys(allPlugins).forEach((name) => initPlugin(allPlugins[name], name))
}

export default initializePlugins
5 changes: 3 additions & 2 deletions app/main/actions/search.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import PluginsService from 'plugins'
import plugins from 'plugins'
import config from 'lib/config'
import { shell, clipboard } from 'electron'

Expand All @@ -16,6 +16,8 @@ import {

import store from '../store'

const allPlugins = plugins.getAllPlugins()

const remote = process.type === 'browser'
? undefined
: require('@electron/remote')
Expand Down Expand Up @@ -43,7 +45,6 @@ const DEFAULT_SCOPE = {
*/
const eachPlugin = (term, display) => {
// TODO: order results by frequency?
const allPlugins = PluginsService.getAllPlugins()
Object.keys(allPlugins).forEach((name) => {
const plugin = allPlugins[name]
try {
Expand Down
13 changes: 11 additions & 2 deletions app/plugins/core/autocomplete/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,16 @@ import { search } from 'cerebro-tools'
import {
flow, filter, map, partialRight, values
} from 'lodash/fp'
import PluginsService from 'plugins'
import getExternalPlugins from 'plugins/externalPlugins'
import quit from 'plugins/core/quit'
import plugins from 'plugins/core/plugins'
import settings from 'plugins/core/settings'
import version from 'plugins/core/version'
import reload from 'plugins/core/reload'

const allPlugins = {
quit, plugins, settings, version, reload, ...getExternalPlugins()
}

const toString = (plugin) => plugin.keyword
const notMatch = (term) => (plugin) => (
Expand Down Expand Up @@ -32,6 +41,6 @@ const fn = ({ term, display, actions }) => flow(
filter(notMatch(term)),
map(pluginToResult(actions)),
display
)(PluginsService.getAllPlugins())
)(allPlugins)

export default { fn, name: 'Plugins autocomplete' }
4 changes: 2 additions & 2 deletions app/plugins/core/plugins/getInstalledPlugins.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { packageJsonPath } from 'lib/plugins'
import { readFile } from 'fs/promises'
import PluginsService from 'plugins'
import getExternalPlugins from 'plugins/externalPlugins'

const readPackageJson = async () => {
try {
Expand All @@ -20,7 +20,7 @@ const readPackageJson = async () => {
export default async () => {
const packageJson = await readPackageJson()
const result = {}
const externalPlugins = PluginsService.getExternalPlugins()
const externalPlugins = getExternalPlugins()

Object.keys(externalPlugins).forEach((pluginName) => {
result[pluginName] = { ...externalPlugins[pluginName], version: packageJson.dependencies[pluginName] || '0.0.0' }
Expand Down
8 changes: 4 additions & 4 deletions app/plugins/externalPlugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const getPluginName = (pluginPath) => {

const plugins = {}

function setupPluginsWatcher() {
const setupPluginsWatcher = () => {
if (global.isBackground) return

const pluginsWatcher = chokidar.watch(modulesDirectory, { depth: 1 })
Expand Down Expand Up @@ -108,6 +108,6 @@ function setupPluginsWatcher() {

setupPluginsWatcher()

export default function getExternalPlugins() {
return plugins
}
const getExternalPlugins = () => plugins

export default getExternalPlugins
18 changes: 7 additions & 11 deletions app/plugins/index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
import core from './core'
import getExternalPlugins from './externalPlugins'

class PluginsService {
getCorePlugins() {
return core
}
const getCorePlugins = () => core

getExternalPlugins() {
return getExternalPlugins()
}
const getAllPlugins = () => ({ ...core, ...getExternalPlugins() })

getAllPlugins() {
return { ...core, ...getExternalPlugins() }
}
const pluginsService = {
getExternalPlugins,
getCorePlugins,
getAllPlugins,
}

export default new PluginsService()
export default pluginsService

0 comments on commit 4907c74

Please sign in to comment.