Skip to content

Commit 619213a

Browse files
committed
feat(loader): support entry.subgroup and entry.subtree
1 parent 9c95eb0 commit 619213a

File tree

7 files changed

+25
-17
lines changed

7 files changed

+25
-17
lines changed

packages/cordis/src/worker/logger.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export function apply(ctx: Context, config: Config = {}) {
3535
})
3636

3737
ctx.on('loader/entry', (type, entry) => {
38-
if (entry.options.transparent) return
38+
if (entry.options.group) return
3939
ctx.logger('loader').info('%s plugin %c', type, entry.options.name)
4040
})
4141

packages/loader/src/entry.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
import { Context, ForkScope, Inject } from '@cordisjs/core'
22
import { Dict, isNullable } from 'cosmokit'
3-
import { Loader } from './shared.ts'
3+
import { Loader } from './loader.ts'
44
import { EntryGroup } from './group.ts'
5+
import { EntryTree } from './tree.ts'
56

67
export namespace Entry {
78
export interface Options {
89
id: string
910
name: string
1011
config?: any
12+
group?: boolean | null
1113
disabled?: boolean | null
1214
intercept?: Dict | null
1315
isolate?: Dict<true | string> | null
1416
inject?: string[] | Inject | null
15-
transparent?: boolean | null
1617
when?: any
1718
}
1819
}
@@ -49,7 +50,8 @@ export class Entry {
4950
public fork?: ForkScope
5051
public suspend = false
5152
public options!: Entry.Options
52-
public children?: EntryGroup
53+
public subgroup?: EntryGroup
54+
public subtree?: EntryTree
5355

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

packages/loader/src/file.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@ import { Context } from '@cordisjs/core'
22
import { dirname, extname, resolve } from 'node:path'
33
import { access, constants, readdir, readFile, stat, writeFile } from 'node:fs/promises'
44
import { fileURLToPath, pathToFileURL } from 'node:url'
5+
import { remove } from 'cosmokit'
56
import * as yaml from 'js-yaml'
67
import { Entry } from './entry.ts'
7-
import { EntryGroup } from './group.ts'
8-
import { Loader } from './shared.ts'
9-
import { remove } from 'cosmokit'
8+
import { Loader } from './loader.ts'
109
import { EntryTree } from './tree.ts'
1110

1211
export class LoaderFile {
@@ -101,8 +100,7 @@ export class ImportTree extends EntryTree {
101100
protected file!: LoaderFile
102101

103102
constructor(public ctx: Context) {
104-
super()
105-
this.root = new EntryGroup(ctx, this)
103+
super(ctx)
106104
ctx.on('ready', () => this.start())
107105
ctx.on('dispose', () => this.stop())
108106
}

packages/loader/src/group.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ import { EntryTree } from './tree.ts'
55
export class EntryGroup {
66
public data: Entry.Options[] = []
77

8-
constructor(public ctx: Context, public tree: EntryTree) {}
8+
constructor(public ctx: Context, public tree: EntryTree) {
9+
const entry = ctx.scope.entry
10+
if (entry) entry.subgroup = this
11+
}
912

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

6265
// TODO support options
6366
constructor(public ctx: Context) {
64-
const entry = ctx.scope.entry!
65-
super(ctx, entry.parent.tree)
66-
entry.children = this
67+
super(ctx, ctx.scope.entry!.parent.tree)
6768
ctx.on('dispose', () => this.stop())
6869
ctx.accept((config: Entry.Options[]) => {
6970
this.update(config)

packages/loader/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import Module from 'node:module'
22
import { pathToFileURL } from 'node:url'
33
import { readFile } from 'node:fs/promises'
4-
import { Loader } from './shared.ts'
4+
import { Loader } from './loader.ts'
55
import * as dotenv from 'dotenv'
66
import * as path from 'node:path'
77

88
export * from './internal.ts'
9-
export * from './shared.ts'
9+
export * from './loader.ts'
1010

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

File renamed without changes.

packages/loader/src/tree.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
1+
import { Context } from '@cordisjs/core'
12
import { Dict, isNullable } from 'cosmokit'
23
import { Entry } from './entry.ts'
34
import { EntryGroup } from './group.ts'
45

56
export abstract class EntryTree {
67
public url!: string
7-
public root!: EntryGroup
8+
public root: EntryGroup
89
public entries: Dict<Entry> = Object.create(null)
910

11+
constructor(public ctx: Context) {
12+
this.root = new EntryGroup(ctx, this)
13+
const entry = ctx.scope.entry
14+
if (entry) entry.subtree = this
15+
}
16+
1017
ensureId(options: Partial<Entry.Options>) {
1118
if (!options.id) {
1219
do {
@@ -32,7 +39,7 @@ export abstract class EntryTree {
3239
}
3340

3441
resolveGroup(id: string | null) {
35-
const group = id ? this.entries[id]?.children : this.root
42+
const group = id ? this.entries[id]?.subgroup : this.root
3643
if (!group) throw new Error(`entry ${id} not found`)
3744
return group
3845
}

0 commit comments

Comments
 (0)