Skip to content

Commit

Permalink
feat(loader): support entry.subgroup and entry.subtree
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed May 29, 2024
1 parent 9c95eb0 commit 619213a
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 17 deletions.
2 changes: 1 addition & 1 deletion packages/cordis/src/worker/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export function apply(ctx: Context, config: Config = {}) {
})

ctx.on('loader/entry', (type, entry) => {
if (entry.options.transparent) return
if (entry.options.group) return
ctx.logger('loader').info('%s plugin %c', type, entry.options.name)
})

Expand Down
8 changes: 5 additions & 3 deletions packages/loader/src/entry.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import { Context, ForkScope, Inject } from '@cordisjs/core'
import { Dict, isNullable } from 'cosmokit'
import { Loader } from './shared.ts'
import { Loader } from './loader.ts'
import { EntryGroup } from './group.ts'
import { EntryTree } from './tree.ts'

export namespace Entry {
export interface Options {
id: string
name: string
config?: any
group?: boolean | null
disabled?: boolean | null
intercept?: Dict | null
isolate?: Dict<true | string> | null
inject?: string[] | Inject | null
transparent?: boolean | null
when?: any
}
}
Expand Down Expand Up @@ -49,7 +50,8 @@ export class Entry {
public fork?: ForkScope
public suspend = false
public options!: Entry.Options
public children?: EntryGroup
public subgroup?: EntryGroup
public subtree?: EntryTree

constructor(public loader: Loader, public parent: EntryGroup) {}

Expand Down
8 changes: 3 additions & 5 deletions packages/loader/src/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ import { Context } from '@cordisjs/core'
import { dirname, extname, resolve } from 'node:path'
import { access, constants, readdir, readFile, stat, writeFile } from 'node:fs/promises'
import { fileURLToPath, pathToFileURL } from 'node:url'
import { remove } from 'cosmokit'
import * as yaml from 'js-yaml'
import { Entry } from './entry.ts'
import { EntryGroup } from './group.ts'
import { Loader } from './shared.ts'
import { remove } from 'cosmokit'
import { Loader } from './loader.ts'
import { EntryTree } from './tree.ts'

export class LoaderFile {
Expand Down Expand Up @@ -101,8 +100,7 @@ export class ImportTree extends EntryTree {
protected file!: LoaderFile

constructor(public ctx: Context) {
super()
this.root = new EntryGroup(ctx, this)
super(ctx)
ctx.on('ready', () => this.start())
ctx.on('dispose', () => this.stop())
}
Expand Down
9 changes: 5 additions & 4 deletions packages/loader/src/group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import { EntryTree } from './tree.ts'
export class EntryGroup {
public data: Entry.Options[] = []

constructor(public ctx: Context, public tree: EntryTree) {}
constructor(public ctx: Context, public tree: EntryTree) {
const entry = ctx.scope.entry
if (entry) entry.subgroup = this
}

async create(options: Omit<Entry.Options, 'id'>) {
const id = this.ctx.loader.ensureId(options)
Expand Down Expand Up @@ -61,9 +64,7 @@ export class Group extends EntryGroup {

// TODO support options
constructor(public ctx: Context) {
const entry = ctx.scope.entry!
super(ctx, entry.parent.tree)
entry.children = this
super(ctx, ctx.scope.entry!.parent.tree)
ctx.on('dispose', () => this.stop())
ctx.accept((config: Entry.Options[]) => {
this.update(config)
Expand Down
4 changes: 2 additions & 2 deletions packages/loader/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import Module from 'node:module'
import { pathToFileURL } from 'node:url'
import { readFile } from 'node:fs/promises'
import { Loader } from './shared.ts'
import { Loader } from './loader.ts'
import * as dotenv from 'dotenv'
import * as path from 'node:path'

export * from './internal.ts'
export * from './shared.ts'
export * from './loader.ts'

type ModuleLoad = (request: string, parent: Module, isMain: boolean) => any

Expand Down
File renamed without changes.
11 changes: 9 additions & 2 deletions packages/loader/src/tree.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import { Context } from '@cordisjs/core'
import { Dict, isNullable } from 'cosmokit'
import { Entry } from './entry.ts'
import { EntryGroup } from './group.ts'

export abstract class EntryTree {
public url!: string
public root!: EntryGroup
public root: EntryGroup
public entries: Dict<Entry> = Object.create(null)

constructor(public ctx: Context) {
this.root = new EntryGroup(ctx, this)
const entry = ctx.scope.entry
if (entry) entry.subtree = this
}

ensureId(options: Partial<Entry.Options>) {
if (!options.id) {
do {
Expand All @@ -32,7 +39,7 @@ export abstract class EntryTree {
}

resolveGroup(id: string | null) {
const group = id ? this.entries[id]?.children : this.root
const group = id ? this.entries[id]?.subgroup : this.root
if (!group) throw new Error(`entry ${id} not found`)
return group
}
Expand Down

0 comments on commit 619213a

Please sign in to comment.