From c476ef8611104805161fcea3f06287419848da5e Mon Sep 17 00:00:00 2001 From: Vincent den Boer Date: Sat, 11 Apr 2020 15:21:02 +0200 Subject: [PATCH] Run plugin discovery at startup if desired --- package.json | 1 - ts/constants.ts | 3 --- ts/main.ts | 37 ++++++++++++++++++++++++++++++------ ts/plugins/discovery/main.ts | 35 ++++++++-------------------------- 4 files changed, 39 insertions(+), 37 deletions(-) diff --git a/package.json b/package.json index 25c3394..adaf7c1 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,6 @@ "main": "lib/main.js", "scripts": { "start": "ts-node ts/main.ts", - "discover-plugins": "ts-node ts/plugins/discovery/main.ts", "prepare": "tsc", "prepare:watch": "npm run prepare -- -w", "test": "mocha --require ts-node/register \"ts/**/*.test.ts\"", diff --git a/ts/constants.ts b/ts/constants.ts index 1b63889..e69de29 100644 --- a/ts/constants.ts +++ b/ts/constants.ts @@ -1,3 +0,0 @@ -import * as path from "path"; - -export const PROJECT_ROOT = path.resolve(path.join(__dirname, '..')) diff --git a/ts/main.ts b/ts/main.ts index c55117d..f3ba390 100644 --- a/ts/main.ts +++ b/ts/main.ts @@ -1,3 +1,4 @@ +import { join } from 'path'; import cryptoRandomString from 'crypto-random-string' import { StorageBackend } from "@worldbrain/storex"; import { DexieStorageBackend } from '@worldbrain/storex-backend-dexie' @@ -7,16 +8,13 @@ import { Application, ApplicationOptions } from "./application"; import { BcryptAccessTokenManager } from "./access-tokens"; import { createHttpServer } from "./server"; import { PluginManager } from './plugins/manager'; +import { discoverInstalledPlugins } from './plugins/discovery/main'; export async function main() { const application = await setupApplication() + await maybeRunPluginDiscovery(application) await startServer(application) - - const storage = await application.storage - const pluginManager = new PluginManager({ - pluginManagementStorage: storage.systemModules.plugins, - }) - await pluginManager.setup(() => application.api()) + await loadPlugins(application) } export async function setupApplication() { @@ -27,6 +25,33 @@ export async function setupApplication() { return application } +async function maybeRunPluginDiscovery(application: Application) { + if (!process.env.STOREX_HUB_DISCOVER_PLUGINS) { + return + } + + const patternOrTrue = process.env.STOREX_HUB_DISCOVER_PLUGINS + if (patternOrTrue.toLowerCase() === 'false') { + return + } + + await discoverInstalledPlugins(application, { + nodeModulesPath: join(process.cwd(), 'node_modules'), + pluginDirGlob: patternOrTrue.toLowerCase() !== 'true' ? patternOrTrue : undefined + }) + + // TODO: Don't know why yet, but added plugins do not immediately get stored + await new Promise(resolve => setTimeout(resolve, 1000)) +} + +async function loadPlugins(application: Application) { + const storage = await application.storage + const pluginManager = new PluginManager({ + pluginManagementStorage: storage.systemModules.plugins, + }) + await pluginManager.setup(() => application.api()) +} + export async function startServer(application: Application) { const server = await createHttpServer(application, { secretKey: 'very secret key' diff --git a/ts/plugins/discovery/main.ts b/ts/plugins/discovery/main.ts index 73b4e6e..ca81df9 100644 --- a/ts/plugins/discovery/main.ts +++ b/ts/plugins/discovery/main.ts @@ -1,42 +1,27 @@ import path from 'path' import { existsSync, readFileSync } from 'fs' -import yargs from 'yargs' import fastGlob from 'fast-glob' import createPrompt from 'prompt-sync' -import { PROJECT_ROOT } from '../../constants' -import { setupApplication } from '../../main' import { discoverPlugins } from '.' import { PluginInfo } from '../types' +import { Application } from '../../application' const prompt = createPrompt() -interface Args { - pluginGlobPattern: string -} - -const NODE_MODULES_DIR = path.join(PROJECT_ROOT, 'node_modules') - -function parseArgs(): Args { - return yargs - .option('plugin-glob-pattern', { - description: 'Where to look for plugins', - default: '/storex-hub-plugin-*', - type: 'string', - }) - .argv as any as Args -} - function validatePluginInfo(untrusted: any): PluginInfo | null { return untrusted } -async function main() { - const args = parseArgs() - const pluginDirGlob = args.pluginGlobPattern.replace('', NODE_MODULES_DIR) +export async function discoverInstalledPlugins(application: Application, options: { + pluginDirGlob?: string + nodeModulesPath: string +}) { + const pluginDirGlob = ( + options.pluginDirGlob || '/storex-hub-plugin-*' + ).replace('', options.nodeModulesPath) const pluginDirs = (await fastGlob([pluginDirGlob], { onlyDirectories: true })).map(dir => path.resolve(dir)) - const application = await setupApplication() const storage = await application.storage await discoverPlugins(pluginDirs, { @@ -92,7 +77,3 @@ async function main() { }, }) } - -if (require.main === module) { - main() -} \ No newline at end of file