Skip to content

Commit

Permalink
Resolve dependencies right away
Browse files Browse the repository at this point in the history
  • Loading branch information
robertherber committed Oct 16, 2023
1 parent 070c553 commit b81b43c
Show file tree
Hide file tree
Showing 38 changed files with 195 additions and 52 deletions.
12 changes: 12 additions & 0 deletions apps/supplement-stack/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# supplement-stack

## 0.0.8

### Patch Changes

- Updated dependencies
- [email protected]
- @zemble/core@0.0.8
- @zemble/graphql@0.0.12
- [email protected]
- [email protected]
- [email protected]

## 0.0.7

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion apps/supplement-stack/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "supplement-stack",
"version": "0.0.7",
"version": "0.0.8",
"description": "",
"main": "app.ts",
"private": true,
Expand Down
12 changes: 12 additions & 0 deletions apps/todo-app-with-auth-backend/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# todo-app-with-auth-backend

## 0.0.8

### Patch Changes

- Updated dependencies
- @zemble/core@0.0.8
- @zemble/graphql@0.0.12
- [email protected]
- [email protected]
- [email protected]
- [email protected]

## 0.0.7

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion apps/todo-app-with-auth-backend/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "todo-app-with-auth-backend",
"version": "0.0.7",
"version": "0.0.8",
"description": "",
"private": true,
"main": "app.ts",
Expand Down
7 changes: 7 additions & 0 deletions packages/apple-app-site-association/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# zemble-plugin-apple-app-site-association

## 0.0.6

### Patch Changes

- Updated dependencies
- @zemble/core@0.0.8

## 0.0.5

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/apple-app-site-association/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "zemble-plugin-apple-app-site-association",
"version": "0.0.5",
"version": "0.0.6",
"description": "",
"private": true,
"main": "plugin.ts",
Expand Down
9 changes: 9 additions & 0 deletions packages/auth-anonymous/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# zemble-plugin-auth-anonymous

## 0.0.8

### Patch Changes

- Updated dependencies
- @zemble/core@0.0.8
- @zemble/graphql@0.0.12
- [email protected]

## 0.0.7

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/auth-anonymous/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "zemble-plugin-auth-anonymous",
"version": "0.0.7",
"version": "0.0.8",
"description": "",
"main": "plugin.ts",
"scripts": {
Expand Down
9 changes: 9 additions & 0 deletions packages/auth-api-token/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# zemble-plugin-auth-api-token

## 0.0.10

### Patch Changes

- Updated dependencies
- @zemble/core@0.0.8
- @zemble/graphql@0.0.12
- [email protected]

## 0.0.9

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/auth-api-token/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "zemble-plugin-auth-api-token",
"version": "0.0.9",
"version": "0.0.10",
"description": "",
"main": "plugin.ts",
"scripts": {
Expand Down
11 changes: 11 additions & 0 deletions packages/auth-otp/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# zemble-plugin-auth-otp

## 0.0.10

### Patch Changes

- Fix dependency resolution
- Updated dependencies
- @zemble/core@0.0.8
- @zemble/graphql@0.0.12
- [email protected]
- [email protected]

## 0.0.9

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/auth-otp/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "zemble-plugin-auth-otp",
"version": "0.0.9",
"version": "0.0.10",
"description": "",
"main": "plugin.ts",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion packages/auth-otp/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ const plugin = new Plugin<OtpAuthConfig, typeof defaultConfig>(__dirname, {
devConfig: {
handleAuthRequest: ({ email }, code, { logger }) => { logger.log(`handleAuthRequest for ${email}`, code) },
generateTokenContents,
from: { email: 'noreply@Zemble.com' },
from: { email: 'noreply@zemble.com' },
},
})

Expand Down
8 changes: 8 additions & 0 deletions packages/auth/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# zemble-plugin-auth

## 0.0.10

### Patch Changes

- Updated dependencies
- @zemble/core@0.0.8
- @zemble/graphql@0.0.12

## 0.0.9

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/auth/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "zemble-plugin-auth",
"version": "0.0.9",
"version": "0.0.10",
"description": "",
"main": "plugin.ts",
"bin": {
Expand Down
9 changes: 9 additions & 0 deletions packages/bull/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# zemble-plugin-bull

## 0.0.10

### Patch Changes

- Updated dependencies
- @zemble/core@0.0.8
- @zemble/graphql@0.0.12
- [email protected]

## 0.0.9

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/bull/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "zemble-plugin-bull",
"version": "0.0.9",
"version": "0.0.10",
"description": "",
"main": "plugin.ts",
"scripts": {
Expand Down
12 changes: 12 additions & 0 deletions packages/cms-users/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# zemble-plugin-cms-users

## 0.0.8

### Patch Changes

- Updated dependencies
- [email protected]
- @zemble/core@0.0.8
- @zemble/graphql@0.0.12
- [email protected]
- [email protected]
- [email protected]

## 0.0.7

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/cms-users/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "zemble-plugin-cms-users",
"version": "0.0.7",
"version": "0.0.8",
"description": "",
"main": "plugin.ts",
"private": true,
Expand Down
9 changes: 9 additions & 0 deletions packages/cms/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# zemble-plugin-cms

## 0.0.8

### Patch Changes

- Updated dependencies
- @zemble/core@0.0.8
- @zemble/graphql@0.0.12
- [email protected]

## 0.0.7

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/cms/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "zemble-plugin-cms",
"version": "0.0.7",
"version": "0.0.8",
"description": "",
"main": "plugin.ts",
"private": true,
Expand Down
6 changes: 6 additions & 0 deletions packages/core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @zemble/core

## 0.0.8

### Patch Changes

- Fix dependency resolution

## 0.0.7

### Patch Changes
Expand Down
36 changes: 14 additions & 22 deletions packages/core/Plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ import { createApp } from './server'
import mergeDeep from './utils/mergeDeep'
import { readPackageJson } from './utils/readPackageJson'

import type PluginWithMiddleware from './PluginWithMiddleware'
import type { ZembleApp } from './server'
import type { DependenciesResolver, Dependency, PluginOpts } from './types'
import type { Dependency, PluginOpts } from './types'

// initialize dotenv before any plugins are loaded/configured
configDotenv()
Expand All @@ -21,7 +20,7 @@ export class Plugin<

readonly #devConfig?: TConfig

readonly #dependencies: DependenciesResolver<Plugin<Zemble.GlobalConfig>>
readonly dependencies: readonly Plugin<Zemble.GlobalConfig>[]

readonly pluginPath: string

Expand All @@ -37,8 +36,16 @@ export class Plugin<
this.pluginPath = __dirname
this.#config = (opts?.defaultConfig ?? {}) as TResolvedConfig
this.#devConfig = opts?.devConfig
// @ts-expect-error not the most important
this.#dependencies = opts?.dependencies ?? []
const deps = opts?.dependencies ?? []

const allDeps = (typeof deps === 'function') ? deps(this) : deps

const filteredDeps = allDeps.filter(this.#filterDevDependencies.bind(this))

const resolvedDeps = filteredDeps
.map(({ plugin, config }) => plugin.configure(config))

this.dependencies = resolvedDeps

if (this.#isPluginDevMode) {
void this.#devApp().then((app) => app.start())
Expand Down Expand Up @@ -101,28 +108,13 @@ export class Plugin<
return this.#config
}

get dependencies() {
const allDeps = Promise.resolve((typeof this.#dependencies === 'function') ? this.#dependencies(this) : this.#dependencies)

return allDeps.then(async (allDeps) => {
const filteredDeps = allDeps.filter(this.#filterDevDependencies.bind(this))

const resolvedDeps: Promise<readonly Plugin[]> = Promise.all(filteredDeps
.map(({ plugin, config }) => plugin.configure(config))
.flatMap(async (dep) => [dep, ...await dep.dependencies]))
.then((deps) => deps.flat())

return resolvedDeps
})
}

async #devApp(): Promise<ZembleApp> {
const resolved = this.configure(this.#devConfig)
return createApp({
plugins: [
...await this.dependencies,
...this.dependencies,
resolved,
] as readonly (Plugin<Zemble.GlobalConfig> | PluginWithMiddleware<Zemble.GlobalConfig>)[],
],
})
}

Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zemble/core",
"version": "0.0.7",
"version": "0.0.8",
"description": "",
"main": "index.ts",
"scripts": {
Expand Down
8 changes: 3 additions & 5 deletions packages/core/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,15 @@ export const createApp = async ({ plugins: pluginsBeforeResolvingDeps }: Configu

app.use('*', cors())

const resolved = await Promise.all(
pluginsBeforeResolvingDeps.flatMap(async (plugin) => [...await plugin.dependencies, plugin]),
).then((plugins) => plugins.flat())
const resolved = pluginsBeforeResolvingDeps.flatMap((plugin) => [...plugin.dependencies, plugin])

const plugins = resolved.reduce((prev, plugin) => {
const existingPlugin = prev.find(({ pluginName }) => pluginName === plugin.pluginName)
if (existingPlugin) {
if (existingPlugin.config !== plugin.config) {
if (existingPlugin !== plugin) {
// in some situation we can end up with two instances of the same plugin (dependeing on package manager and
// other factors). In those cases we use the latest version of the plugin, but try to merge the config
const pluginToUse = existingPlugin.pluginVersion > plugin.pluginVersion
const pluginToUse = existingPlugin.pluginVersion >= plugin.pluginVersion
? existingPlugin.configure(plugin.config)
: plugin.configure(existingPlugin.config)

Expand Down
2 changes: 1 addition & 1 deletion packages/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export type Dependency<TConfig extends Zemble.GlobalConfig = Zemble.GlobalConfig
readonly devOnly?: boolean, // decides if we should warn if this plugin is not used in production
}

export type DependenciesResolver<TSelf> = readonly Dependency[] | Promise<readonly Dependency[]> | ((self: TSelf) => readonly Dependency[]) | ((self: TSelf) => Promise<readonly Dependency[]>)
export type DependenciesResolver<TSelf> = readonly Dependency[] | ((self: TSelf) => readonly Dependency[])

export type PluginOpts<TDefaultConfig extends Zemble.GlobalConfig, TSelf, TConfig extends Zemble.GlobalConfig> = {
/**
Expand Down
9 changes: 9 additions & 0 deletions packages/email-sendgrid/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# zemble-plugin-email-sendgrid

## 0.0.10

### Patch Changes

- Updated dependencies
- @zemble/core@0.0.8
- @zemble/graphql@0.0.12
- [email protected]

## 0.0.9

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/email-sendgrid/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "zemble-plugin-email-sendgrid",
"version": "0.0.9",
"version": "0.0.10",
"description": "",
"main": "plugin.ts",
"scripts": {
Expand Down
Loading

0 comments on commit b81b43c

Please sign in to comment.