Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions packages/include/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ export class Include extends EntryTree {
private content?: string
private data?: EntryOptions[]
private writeTask?: NodeJS.Timeout
private applying = false
private refreshTail: Promise<void> = Promise.resolve()

constructor(ctx: Context, public config: Include.Config) {
super(ctx)
Expand Down Expand Up @@ -117,6 +119,7 @@ export class Include extends EntryTree {
const { id, insert, name, ...overrides } = patch

if (insert) {
let list: EntryOptions[]
if (id) {
const target = entryMap.get(id)
if (!target) {
Expand All @@ -128,9 +131,16 @@ export class Include extends EntryTree {
continue
}
if (!Array.isArray(target.config)) target.config = []
target.config.push(...insert)
list = target.config
} else {
data.push(...insert)
list = data
}
for (const item of insert) {
this.ensureId(item)
const entry = { ...item }
if (entryMap.has(entry.id) || list.some(existing => existing.name === entry.name)) continue
list.push(entry)
entryMap.set(entry.id, entry)
}
continue
}
Expand Down Expand Up @@ -185,8 +195,21 @@ export class Include extends EntryTree {
}

async refresh() {
const run = this.refreshTail.then(() => this.applyRefresh())
this.refreshTail = run.catch(() => {})
return run
}

private async applyRefresh() {
if (!await this.read()) return
this.root.update(this.data!)
clearTimeout(this.writeTask)
this.writeTask = undefined
this.applying = true
try {
await this.root.update(this.applyPatches([...this.data!]))
} finally {
this.applying = false
}
}

private async _writeFile(config: EntryOptions[]) {
Expand All @@ -211,6 +234,7 @@ export class Include extends EntryTree {
}

write() {
if (this.applying) return
this.context.emit('loader/config-update')
return this.writeFile(this.root.data)
}
Expand Down
114 changes: 114 additions & 0 deletions packages/include/tests/patch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { Context, Fiber } from 'cordis'
import Loader from '@cordisjs/plugin-loader'
import LoggerConsole from '@cordisjs/plugin-logger-console'
import { expect, describe, it, afterEach } from 'vitest'
import { readFile, unlink, writeFile } from 'node:fs/promises'
import Include from '../src/index.ts'

function waitFor(condFn: () => any, timeout = 5000, interval = 100): Promise<void> {
return new Promise<void>((resolve, reject) => {
Expand Down Expand Up @@ -240,4 +242,116 @@ describe('Include patches', () => {
// Name matches, so patch should apply and inner should be disabled
expect(ctx.bail('test/get-value')).to.be.undefined
}, 10000)

it('should keep patches after refresh', async () => {
const runtime = new URL('./fixtures/refresh-runtime.yml', import.meta.url)
await writeFile(runtime, await readFile(new URL('./fixtures/base.yml', import.meta.url)))
try {
ctx = new Context()
await ctx.plugin(LoggerConsole)
fiber = await ctx.plugin(Loader, {
baseUrl: import.meta.url,
})
await ctx.loader.create({
name: '@cordisjs/plugin-include',
config: {
path: './fixtures/refresh-runtime.yml',
patches: [
{ id: 'inner', disabled: true },
],
},
})
await ctx.loader.await()
expect(ctx.bail('test/get-value')).to.be.undefined

await writeFile(runtime, `${await readFile(runtime, 'utf8')}\n# refreshed\n`)
const entry = [...ctx.loader.entries()].find(item => item.options.name === '@cordisjs/plugin-include')
await (entry!.subtree as Include).refresh()
await ctx.loader.await()
expect(ctx.bail('test/get-value')).to.be.undefined
} finally {
await unlink(runtime).catch(() => {})
}
}, 10000)

it('should not duplicate insert patches after a baked refresh', async () => {
const runtime = new URL('./fixtures/refresh-insert.yml', import.meta.url)
await writeFile(runtime, await readFile(new URL('./fixtures/base.yml', import.meta.url)))
try {
ctx = new Context()
await ctx.plugin(LoggerConsole)
fiber = await ctx.plugin(Loader, {
baseUrl: import.meta.url,
})
const id = await ctx.loader.create({
name: '@cordisjs/plugin-include',
config: {
path: './fixtures/refresh-insert.yml',
patches: [
{ insert: [{ name: './extra-plugin' }] },
],
},
})
await ctx.loader.await()
const include = ctx.loader.resolve(id).subtree as Include
const extras = () => include.root.data.filter(item => String(item.name).includes('extra-plugin'))
expect(extras()).to.have.length(1)
expect([...ctx.loader.entries()].filter(item => String(item.options.name).includes('extra-plugin'))).to.have.length(1)

include.write()
await new Promise(r => setTimeout(r, 100))
const baked = await readFile(runtime, 'utf8')
expect(baked).to.match(/extra-plugin/)

await writeFile(runtime, `${baked}\n# refreshed\n`)
await include.refresh()
await ctx.loader.await()
expect(extras()).to.have.length(1)
expect([...ctx.loader.entries()].filter(item => String(item.options.name).includes('extra-plugin'))).to.have.length(1)
expect(ctx.bail('test/get-extra')).to.equal('extra')
} finally {
await unlink(runtime).catch(() => {})
}
}, 10000)

it('should not bake insert patches on a comment-only refresh', async () => {
const runtime = new URL('./fixtures/refresh-comment.yml', import.meta.url)
const original = await readFile(new URL('./fixtures/base.yml', import.meta.url), 'utf8')
await writeFile(runtime, original)
try {
ctx = new Context()
await ctx.plugin(LoggerConsole)
fiber = await ctx.plugin(Loader, {
baseUrl: import.meta.url,
})
const id = await ctx.loader.create({
name: '@cordisjs/plugin-include',
config: {
path: './fixtures/refresh-comment.yml',
patches: [
{ insert: [{ name: './extra-plugin' }] },
],
},
})
await ctx.loader.await()
const include = ctx.loader.resolve(id).subtree as Include
const extras = () => include.root.data.filter(item => String(item.name).includes('extra-plugin'))
expect(await readFile(runtime, 'utf8')).to.equal(original)
expect(extras()).to.have.length(1)
const extraId = extras()[0].id

await writeFile(runtime, `${original}\n# refreshed\n`)
await include.refresh()
await ctx.loader.await()
await new Promise(r => setTimeout(r, 100))
const after = await readFile(runtime, 'utf8')
expect(after).to.equal(`${original}\n# refreshed\n`)
expect(after).to.not.match(/extra-plugin/)
expect(extras()).to.have.length(1)
expect(extras()[0].id).to.equal(extraId)
expect(ctx.bail('test/get-extra')).to.equal('extra')
} finally {
await unlink(runtime).catch(() => {})
}
}, 10000)
})