Skip to content

Commit

Permalink
✨ Dynamically generate import entries
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashu11-A committed Oct 22, 2024
1 parent 66fc9cd commit e8dca8c
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 9 deletions.
2 changes: 1 addition & 1 deletion plugins/tickets/build/crons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ if (!PKG_MODE) {
const crons = await glob(['crons/**/*.{ts,js}'], { cwd: path })
const sortedCrons = crons.sort(customSort)

let content = await readFile(registerPath, { encoding: 'utf-8' })
let content = await readFile(registerPath, { encoding: 'utf-8' }) ?? ''
content += '\n// Crons\n'

for (const pather of sortedCrons) {
Expand Down
6 changes: 2 additions & 4 deletions plugins/tickets/build/discord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,12 @@ if (!PKG_MODE) {
], { cwd: dir })).map((path) => join('discord', path))
const registerPath = join(dir, '../register.ts')

let content = await readFile(registerPath, { encoding: 'utf-8' })
let content = await readFile(registerPath, { encoding: 'utf-8' }) ?? ''
content += '\n// Discord\n'

for (const filePath of paths) {
content += `import './${filePath}'\n`
}

await writeFile(registerPath, content, { encoding: 'utf-8' })
}

import '../src/register'
}
14 changes: 10 additions & 4 deletions plugins/tickets/build/entries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import { PKG_MODE } from 'utils'
if (!PKG_MODE) {
const plugins = []
const content: Record<string, string> = {}

const path = join(dirname(fileURLToPath(import.meta.url)), '../src')
const registerPath = join(path, 'register.ts')
const format = (plugin: string) => join(path, plugin)

plugins.push(
Expand All @@ -24,10 +25,15 @@ if (!PKG_MODE) {
}

await writeFile('entries.json', JSON.stringify(content, null, 2))
}

let registers = await readFile(registerPath, { encoding: 'utf-8' }) ?? ''
registers += `// Entries
import { Plugins } from 'socket-client'
import '../build/entries'
import * as entries from '../entries.json'
Plugins.setPlugins(entries)
Plugins.setPlugins(entries)
`

await writeFile(registerPath, registers, { encoding: 'utf-8' })
}
1 change: 1 addition & 0 deletions plugins/tickets/src/app.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import './index'
import './register'
import 'reflect-metadata'

import { Crons } from 'discord'
Expand Down
6 changes: 6 additions & 0 deletions plugins/tickets/src/register.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
// Entries

import { Plugins } from 'socket-client'
import * as entries from '../entries.json'

Plugins.setPlugins(entries)

// Crons
import './discord/crons/test.ts'
Expand Down

0 comments on commit e8dca8c

Please sign in to comment.