Skip to content

Commit

Permalink
plugins work in prod (hopefully)
Browse files Browse the repository at this point in the history
  • Loading branch information
TechStudent10 committed Jan 29, 2023
1 parent 6cd5020 commit 489dfee
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 56 deletions.
6 changes: 6 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,11 @@
"port": 9222,
"webRoot": "${workspaceRoot}/ui"
}
],
"compounds": [
{
"name": "Debug Main and Renderer",
"configurations": ["Debug Main Process", "Attach to Render Process"]
}
]
}
65 changes: 19 additions & 46 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ const LOGGING_DIR = app.isPackaged ?
path.join("logs")
const LOGGING_FILE_PATH = path.join(LOGGING_DIR, LOGGING_FILE_NAME)

const PLUGIN_PATH = app.isPackaged ? path.join(app.getPath("userData"), "plugins") : path.join(__dirname, "ui", "src", "plugins")

if (!fs.existsSync(LOGGING_DIR)) fs.mkdirSync(LOGGING_DIR)

const loggingStream = fs.createWriteStream(LOGGING_FILE_PATH)
Expand All @@ -31,28 +33,18 @@ function createWindow () {
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
contextIsolation: true,
sandbox: false
sandbox: false,
nodeIntegration: true
},
logo: path.join(__dirname, 'assets', 'logo.png')
})

win.loadFile(path.join(__dirname, 'ui', 'index.html'))

// if (!app.isPackaged) {
// win.webContents.openDevTools()
// }
}

let tmpDir

app.whenReady().then(() => {
// if (!app.isPackaged) {
// const { default: installExtension, REACT_DEVELOPER_TOOLS } = require("electron-devtools-installer")
// installExtension(REACT_DEVELOPER_TOOLS)
// .then((ex_name) => console.log(`Added "${ex_name}" Extension`))
// .catch((err) => console.log("An error occured when attempting to install extensions:", err))
// }

createWindow()

ipcMain.handle("settings:get", (event) => {
Expand All @@ -73,42 +65,23 @@ app.whenReady().then(() => {
loggingStream.write(`[${level.toUpperCase()}] ${message}` + "\n")
})

// When working with local scripts, use these functions
// ipcMain.handle("file:createTempScriptDir", (event, name, cb) => {
// try {
// tmpDir = fs.mkdtemp(path.join(tmpdir(), "proton", name))
// cb(tmpDir)
// } catch (e) {
// console.error(e)
// } finally {
// try {
// if (tmpDir) {
// fs.rm(tmpDir, { recursive: true })
// }
// } catch (e) {
// console.error(e)
// }
// }
// })

// ipcMain.handle("script:watchForChanges", (event, scriptDir, cb) => {
// fs.watch(scriptDir, (event, filename) => {
// console.log(`Change found in ${filename}`, e)
// if (filename) {
// cb(event, filename)
// } else {
// console.warn("No file found")
// }
// })
// })

ipcMain.handle("getPlugins", (event, pluginDir) => {
return fs.readdirSync(pluginDir)
ipcMain.handle("getDev", (event) => {
return app.isPackaged
})

ipcMain.handle("getUserDir", (event) => {
return app.getPath("userData")
})

ipcMain.handle("getPlugins", (event) => {
let result = fs.readdirSync(PLUGIN_PATH)
console.log(result)
return result
})

ipcMain.handle("getPlugin", (event, pluginDir, pluginName) => {
const plugin = require(path.join(pluginDir, pluginName))
const pluginConfig = require(path.join(pluginDir, pluginName, "proton.config.js"))
ipcMain.handle("getPlugin", (event, pluginName) => {
const plugin = require(path.join(PLUGIN_PATH, pluginName))
const pluginConfig = require(path.join(PLUGIN_PATH, pluginName, "proton.config.js"))
return JSON.stringify({plugin: plugin, pluginConfig: pluginConfig})
})

Expand Down
15 changes: 11 additions & 4 deletions preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,19 @@ const { default: ProtonPlugin } = require("@techstudent10/plugin")
const { contextBridge, ipcRenderer } = require("electron")

const path = require("path")
let PLUGIN_PATH;

ipcRenderer.invoke("getDev").then(isDev => {
ipcRenderer.invoke("getUserDir").then(userDir => {
PLUGIN_PATH = isDev ? path.join(userDir, "plugins") : path.join(__dirname, "ui", "src", "plugins")
})
})

contextBridge.exposeInMainWorld("electronAPI", {
getSettings: () => ipcRenderer.invoke("settings:get"),
getPlugins: (pluginPath) => ipcRenderer.invoke("getPlugins", pluginPath),
getPlugin: (pluginPath, pluginName) => {
const plugin = require(path.join(pluginPath, pluginName))
getPlugins: () => ipcRenderer.invoke("getPlugins"),
getPlugin: (pluginName) => {
const plugin = require(path.join(PLUGIN_PATH, pluginName))
// console.log(plugin)
const newPlugin = new ProtonPlugin(undefined)

Expand All @@ -19,7 +26,7 @@ contextBridge.exposeInMainWorld("electronAPI", {
newPlugin[name] = pluginObj[name]
})

const pluginConfig = require(path.join(pluginPath, pluginName, "proton.config.js"))
const pluginConfig = require(path.join(PLUGIN_PATH, pluginName, "proton.config.js"))
return {plugin: newPlugin, pluginConfig: pluginConfig}
},
logInfo: (message) => ipcRenderer.send("log", "info", message),
Expand Down
4 changes: 2 additions & 2 deletions renderer.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export interface IElectronAPI {
getSettings: () => {},
getPlugins: (pluginPath: string) => Promise,
getPlugin: (pluginPath: string, pluginName: string) => any,
getPlugins: () => Promise,
getPlugin: (pluginName: string) => any,
logInfo: (message: string) => void,
logWarn: (message: string) => void,
logErr: (message: string) => void,
Expand Down
4 changes: 2 additions & 2 deletions ui/src/libs/plugin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class PluginManager {
this.PLUGIN_PATH = options.path
}

window.electronAPI.getPlugins(this.PLUGIN_PATH).then((data: Array<string>) => {
window.electronAPI.getPlugins().then((data: Array<string>) => {
this.load(data)
})
}
Expand All @@ -30,7 +30,7 @@ class PluginManager {
pluginFiles.map(filename => {
// console.log("a", filename)
try {
const pluginObj = window.electronAPI.getPlugin(this.PLUGIN_PATH, filename)
const pluginObj = window.electronAPI.getPlugin(filename)
const plugin = pluginObj.plugin
const pluginConfig = pluginObj.pluginConfig
// console.log(plugin, "\n", pluginConfig)
Expand Down
1 change: 0 additions & 1 deletion ui/src/screens/editor/TextEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import * as monaco from "monaco-editor"
loader.config({ monaco })

import Tabs, { Tab } from './Tabs';
import 'react-responsive-tabs/styles.css';

import TextEditorFileList from "./TextEditorFileList";

Expand Down
1 change: 0 additions & 1 deletion ui/src/screens/editor/react-responsive-tabs.d.ts

This file was deleted.

0 comments on commit 489dfee

Please sign in to comment.