Skip to content

Commit

Permalink
Run plugin discovery at startup if desired
Browse files Browse the repository at this point in the history
  • Loading branch information
ShishKabab committed Apr 11, 2020
1 parent 1140866 commit c476ef8
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 37 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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\"",
Expand Down
3 changes: 0 additions & 3 deletions ts/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +0,0 @@
import * as path from "path";

export const PROJECT_ROOT = path.resolve(path.join(__dirname, '..'))
37 changes: 31 additions & 6 deletions ts/main.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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() {
Expand All @@ -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'
Expand Down
35 changes: 8 additions & 27 deletions ts/plugins/discovery/main.ts
Original file line number Diff line number Diff line change
@@ -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: '<node_modules>/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>', NODE_MODULES_DIR)
export async function discoverInstalledPlugins(application: Application, options: {
pluginDirGlob?: string
nodeModulesPath: string
}) {
const pluginDirGlob = (
options.pluginDirGlob || '<node_modules>/storex-hub-plugin-*'
).replace('<node_modules>', 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, {
Expand Down Expand Up @@ -92,7 +77,3 @@ async function main() {
},
})
}

if (require.main === module) {
main()
}

0 comments on commit c476ef8

Please sign in to comment.