core refactor for runtime support
This commit is contained in:
55
src/core/ConfigManager.js
Normal file
55
src/core/ConfigManager.js
Normal file
@@ -0,0 +1,55 @@
|
||||
const getDefaultConfig = () => {
|
||||
return {
|
||||
activeAdapter: 'supabase',
|
||||
theme: 'dark',
|
||||
}
|
||||
}
|
||||
|
||||
export const createConfigManager = (storage) => {
|
||||
let config = null
|
||||
|
||||
return {
|
||||
async loadConfig() {
|
||||
if (config) return config
|
||||
|
||||
const stored = await storage.load()
|
||||
config = stored || getDefaultConfig()
|
||||
|
||||
if (!stored) {
|
||||
await storage.save(config)
|
||||
}
|
||||
|
||||
return config
|
||||
},
|
||||
|
||||
getConfig() {
|
||||
return config
|
||||
},
|
||||
|
||||
async setConfig(newConfig) {
|
||||
config = newConfig
|
||||
await storage.save(newConfig)
|
||||
},
|
||||
|
||||
async refreshConfig() {
|
||||
config = await storage.load()
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
export const createConfigManagerClient = () => {
|
||||
return {
|
||||
async loadConfig() {
|
||||
return await window.api.configManagerCall('loadConfig')
|
||||
},
|
||||
async getConfig() {
|
||||
return await window.api.configManagerCall('getConfig')
|
||||
},
|
||||
async setConfig(newConfig) {
|
||||
return await window.api.configManagerCall('setConfig', newConfig)
|
||||
},
|
||||
async refreshConfig() {
|
||||
return await window.api.configManagerCall('refreshConfig')
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -4,22 +4,22 @@ export default class IpcAdapter {
|
||||
}
|
||||
|
||||
async init() {
|
||||
return await window.adapter.call('init')
|
||||
return await window.api.adapterCall('init')
|
||||
}
|
||||
|
||||
async getAll() {
|
||||
return await window.adapter.call('getAll')
|
||||
return await window.api.adapterCall('getAll')
|
||||
}
|
||||
|
||||
async create(note) {
|
||||
return await window.adapter.call('create', note)
|
||||
return await window.api.adapterCall('create', note)
|
||||
}
|
||||
|
||||
async update(note) {
|
||||
return await window.adapter.call('update', note)
|
||||
return await window.api.adapterCall('update', note)
|
||||
}
|
||||
|
||||
async delete(id) {
|
||||
return await window.adapter.call('delete', id)
|
||||
return await window.api.adapterCall('delete', id)
|
||||
}
|
||||
}
|
||||
|
||||
79
src/core/NodeStorage.js
Normal file
79
src/core/NodeStorage.js
Normal file
@@ -0,0 +1,79 @@
|
||||
import fs from 'fs/promises'
|
||||
import path from 'path'
|
||||
import { app } from 'electron'
|
||||
|
||||
const USER_DATA_STRING = '__DEFAULT_USER_DATA__'
|
||||
|
||||
export function createNodeStorage(defaultPlugin = null) {
|
||||
const configPath = path.join(app.getPath('userData'), 'config.json')
|
||||
|
||||
function resolveDefaults(obj) {
|
||||
if (Array.isArray(obj)) {
|
||||
return obj.map(resolveDefaults)
|
||||
} else if (obj && typeof obj === 'object') {
|
||||
const resolved = {}
|
||||
for (const [key, value] of Object.entries(obj)) {
|
||||
resolved[key] = resolveDefaults(value)
|
||||
}
|
||||
return resolved
|
||||
} else if (typeof obj === 'string' && obj.includes(USER_DATA_STRING)) {
|
||||
return obj.replace(USER_DATA_STRING, app.getPath('userData'))
|
||||
}
|
||||
return obj
|
||||
}
|
||||
|
||||
return {
|
||||
async load() {
|
||||
let parsed
|
||||
try {
|
||||
const raw = await fs.readFile(configPath, 'utf8')
|
||||
parsed = JSON.parse(raw)
|
||||
} catch {
|
||||
parsed = null
|
||||
}
|
||||
|
||||
if (!parsed || !parsed.activeAdapter) {
|
||||
const defaultConfig = {}
|
||||
if (defaultPlugin) {
|
||||
for (const field of defaultPlugin.configSchema) {
|
||||
defaultConfig[field.key] = field.default ?? null
|
||||
}
|
||||
}
|
||||
|
||||
parsed = {
|
||||
...(parsed ? parsed : {}),
|
||||
activeAdapter: defaultPlugin?.id || 'supabase',
|
||||
adapters: {},
|
||||
theme: 'dark',
|
||||
}
|
||||
if (defaultPlugin) {
|
||||
parsed.adapters[defaultPlugin.id] = defaultConfig
|
||||
}
|
||||
|
||||
await this.save(parsed)
|
||||
} else {
|
||||
parsed.adapters = resolveDefaults(parsed.adapters)
|
||||
}
|
||||
|
||||
return parsed
|
||||
},
|
||||
|
||||
async save(configObject) {
|
||||
const dir = path.dirname(configPath)
|
||||
await fs.mkdir(dir, { recursive: true })
|
||||
|
||||
const resolved = {
|
||||
...configObject,
|
||||
adapters: resolveDefaults(configObject.adapters),
|
||||
}
|
||||
|
||||
await fs.writeFile(
|
||||
configPath,
|
||||
JSON.stringify(resolved, null, 2),
|
||||
'utf8',
|
||||
)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
export default createNodeStorage
|
||||
@@ -1,16 +1,8 @@
|
||||
import PluginRegistry from './PluginRegistry.js'
|
||||
import IpcAdapter from './IpcAdapter.js'
|
||||
import IpcAdapter from './IpcAdapter'
|
||||
|
||||
let registry = null
|
||||
let activePluginId = null
|
||||
let adapter = null
|
||||
|
||||
export default function createPluginManager(environment, plugins = []) {
|
||||
registry = new PluginRegistry(environment)
|
||||
|
||||
for (const plugin of plugins) {
|
||||
registry.register(plugin)
|
||||
}
|
||||
export const createPluginManager = (registry) => {
|
||||
let activePluginId = null
|
||||
let adapter = null
|
||||
|
||||
return {
|
||||
listPlugins() {
|
||||
@@ -24,10 +16,6 @@ export default function createPluginManager(environment, plugins = []) {
|
||||
getAdapter(pluginId, adapterConfig = {}) {
|
||||
const plugin = registry.get(pluginId)
|
||||
|
||||
if (environment === 'electron') {
|
||||
return new IpcAdapter()
|
||||
}
|
||||
|
||||
if (!plugin) {
|
||||
throw new Error(`Plugin not found: ${pluginId}`)
|
||||
}
|
||||
@@ -41,6 +29,19 @@ export default function createPluginManager(environment, plugins = []) {
|
||||
return adapter
|
||||
},
|
||||
|
||||
async testPlugin(pluginId, adapterConfig = {}) {
|
||||
const plugin = registry.get(pluginId)
|
||||
|
||||
if (!plugin) {
|
||||
throw new Error(`Plugin not found: ${pluginId}`)
|
||||
}
|
||||
|
||||
const adapter = this.getAdapter(pluginId, adapterConfig)
|
||||
await adapter.init()
|
||||
|
||||
return await adapter.testConnection()
|
||||
},
|
||||
|
||||
getActiveAdapter() {
|
||||
return adapter
|
||||
},
|
||||
@@ -48,9 +49,30 @@ export default function createPluginManager(environment, plugins = []) {
|
||||
getActivePluginId() {
|
||||
return activePluginId
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
getEnvironment() {
|
||||
return environment
|
||||
// Client for calling manager through IPC
|
||||
export const createPluginManagerClient = () => {
|
||||
return {
|
||||
listPlugins() {
|
||||
return window.api.pluginManagerCall('listPlugins')
|
||||
},
|
||||
|
||||
getAdapter(pluginId, config) {
|
||||
return new IpcAdapter(pluginId, config)
|
||||
},
|
||||
|
||||
setActivePlugin(pluginId, adapterConfig) {
|
||||
return window.api.pluginManagerCall(
|
||||
'setActivePlugin',
|
||||
pluginId,
|
||||
adapterConfig,
|
||||
)
|
||||
},
|
||||
|
||||
async testPlugin(id, config) {
|
||||
return window.api.pluginManagerCall('testPlugin', { id, config })
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
export default class PluginRegistry {
|
||||
constructor(environment = 'web') {
|
||||
constructor() {
|
||||
this.plugins = new Map()
|
||||
this.environment = environment
|
||||
}
|
||||
|
||||
register(plugin) {
|
||||
@@ -9,11 +8,6 @@ export default class PluginRegistry {
|
||||
throw new Error('Plugin must have an id')
|
||||
}
|
||||
|
||||
const environments = plugin.environments || ['electron', 'web']
|
||||
if (!environments.includes(this.environment)) {
|
||||
return
|
||||
}
|
||||
|
||||
this.plugins.set(plugin.id, plugin)
|
||||
}
|
||||
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
const USER_DATA_STRING = '__DEFAULT_USER_DATA__'
|
||||
const DB_NAME = 'takerofnotes'
|
||||
const DB_VERSION = 1
|
||||
const STORE_NAME = 'config'
|
||||
@@ -53,7 +54,7 @@ async function saveToDB(data) {
|
||||
})
|
||||
}
|
||||
|
||||
function createIndexedDBStorage() {
|
||||
export function createWebStorage() {
|
||||
return {
|
||||
async load() {
|
||||
const stored = await getFromDB()
|
||||
@@ -65,84 +66,4 @@ function createIndexedDBStorage() {
|
||||
}
|
||||
}
|
||||
|
||||
function createIpcStorage() {
|
||||
return {
|
||||
async load() {
|
||||
return await window.api.getConfig()
|
||||
},
|
||||
async save(data) {
|
||||
await window.api.setConfig(data)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
function getDefaultConfig() {
|
||||
return {
|
||||
activeAdapter: 'supabase',
|
||||
theme: 'dark',
|
||||
}
|
||||
}
|
||||
|
||||
let config = null
|
||||
let configResolve = null
|
||||
const configPromise = new Promise((resolve) => {
|
||||
configResolve = resolve
|
||||
})
|
||||
|
||||
export function createConfigManager(environment, customStorage = null) {
|
||||
let storage
|
||||
|
||||
if (customStorage) {
|
||||
storage = customStorage
|
||||
} else if (environment === 'electron') {
|
||||
storage = createIpcStorage()
|
||||
} else {
|
||||
storage = createIndexedDBStorage()
|
||||
}
|
||||
|
||||
const loadConfig = async () => {
|
||||
if (config !== null) {
|
||||
return config
|
||||
}
|
||||
|
||||
const stored = await storage.load()
|
||||
config = stored || getDefaultConfig()
|
||||
if (!stored) {
|
||||
await storage.save(config)
|
||||
}
|
||||
|
||||
configResolve(config)
|
||||
return config
|
||||
}
|
||||
|
||||
const writeConfig = async (newConfig) => {
|
||||
await storage.save(newConfig)
|
||||
config = newConfig
|
||||
}
|
||||
|
||||
const getConfig = () => {
|
||||
if (config !== null) {
|
||||
return config
|
||||
}
|
||||
return configPromise
|
||||
}
|
||||
|
||||
const setConfig = async (newConfig) => {
|
||||
config = newConfig
|
||||
await writeConfig(newConfig)
|
||||
}
|
||||
|
||||
const refreshConfig = async () => {
|
||||
config = await storage.load()
|
||||
configResolve(config)
|
||||
}
|
||||
|
||||
return {
|
||||
loadConfig,
|
||||
getConfig,
|
||||
setConfig,
|
||||
refreshConfig,
|
||||
}
|
||||
}
|
||||
|
||||
export default createConfigManager
|
||||
export default createWebStorage
|
||||
@@ -1,5 +1,14 @@
|
||||
import createPluginManager from './PluginManager.js'
|
||||
import createConfigManager from './Config.js'
|
||||
import filesystemPlugin from '@takerofnotes/plugin-filesystem'
|
||||
import {
|
||||
createPluginManager,
|
||||
createPluginManagerClient,
|
||||
} from './PluginManager.js'
|
||||
import { createWebStorage } from './WebStorage.js'
|
||||
import PluginRegistry from './PluginRegistry.js'
|
||||
import {
|
||||
createConfigManager,
|
||||
createConfigManagerClient,
|
||||
} from './ConfigManager.js'
|
||||
import NotesAPI from './NotesAPI.js'
|
||||
|
||||
const generateEncryptionKey = () => {
|
||||
@@ -10,12 +19,46 @@ const generateEncryptionKey = () => {
|
||||
.join('')
|
||||
}
|
||||
|
||||
let coreInstance = null
|
||||
const initPluginManager = (runtime, plugins, config) => {
|
||||
if (runtime === 'electron-renderer') return createPluginManagerClient()
|
||||
|
||||
export function initializeCore(environment, plugins = []) {
|
||||
const pluginManager = createPluginManager(environment, plugins)
|
||||
const configManager = createConfigManager(environment)
|
||||
const registry = new PluginRegistry()
|
||||
|
||||
for (const plugin of plugins) {
|
||||
registry.register(plugin)
|
||||
}
|
||||
|
||||
const manager = createPluginManager(registry)
|
||||
manager.setActivePlugin(
|
||||
config.activeAdapter,
|
||||
config.adapters[config.activeAdapter],
|
||||
)
|
||||
|
||||
return manager
|
||||
}
|
||||
|
||||
const initConfigManager = async (runtime) => {
|
||||
if (runtime === 'electron-renderer') return createConfigManagerClient()
|
||||
|
||||
let storage
|
||||
if (runtime === 'electron-main') {
|
||||
const { createNodeStorage } = await import('./NodeStorage.js')
|
||||
storage = createNodeStorage(filesystemPlugin)
|
||||
} else if (runtime === 'web') {
|
||||
storage = createWebStorage()
|
||||
}
|
||||
|
||||
return createConfigManager(storage)
|
||||
}
|
||||
|
||||
export const initializeCore = async (runtime, { plugins }) => {
|
||||
const configManager = await initConfigManager(runtime)
|
||||
const config = await configManager.loadConfig()
|
||||
const pluginManager = initPluginManager(runtime, plugins, config)
|
||||
|
||||
// -------------------------
|
||||
// NotesAPI bootstrap
|
||||
// -------------------------
|
||||
let notesAPI = null
|
||||
let initPromise = null
|
||||
|
||||
@@ -30,12 +73,15 @@ export function initializeCore(environment, plugins = []) {
|
||||
|
||||
if (!encryptionKey) {
|
||||
encryptionKey = generateEncryptionKey()
|
||||
await configManager.setConfig({ ...config, encryptionKey })
|
||||
await configManager.setConfig({
|
||||
...config,
|
||||
encryptionKey,
|
||||
})
|
||||
}
|
||||
|
||||
const pluginId = config?.activeAdapter || 'filesystem'
|
||||
const adapterConfig =
|
||||
config.adapters?.[config.activeAdapter] || {}
|
||||
const adapterConfig = config?.adapters?.[pluginId] || {}
|
||||
|
||||
const adapter = pluginManager.getAdapter(
|
||||
pluginId,
|
||||
adapterConfig,
|
||||
@@ -51,27 +97,10 @@ export function initializeCore(environment, plugins = []) {
|
||||
return initPromise
|
||||
}
|
||||
|
||||
coreInstance = {
|
||||
environment,
|
||||
return {
|
||||
runtime,
|
||||
pluginManager,
|
||||
configManager,
|
||||
getNotesAPI,
|
||||
}
|
||||
|
||||
return coreInstance
|
||||
}
|
||||
|
||||
export function getCore() {
|
||||
if (!coreInstance) {
|
||||
throw new Error('Core not initialized. Call initializeCore() first.')
|
||||
}
|
||||
return coreInstance
|
||||
}
|
||||
|
||||
export { default as PluginRegistry } from './PluginRegistry.js'
|
||||
export { default as IpcAdapter } from './IpcAdapter.js'
|
||||
export { default as NotesAPI } from './NotesAPI.js'
|
||||
export {
|
||||
default as createConfigManager,
|
||||
createConfigManager as createConfig,
|
||||
} from './Config.js'
|
||||
|
||||
@@ -1,4 +0,0 @@
|
||||
export enum ENVIRONMENTS {
|
||||
ELECTRON = 'electron',
|
||||
WEB = 'web',
|
||||
}
|
||||
Reference in New Issue
Block a user