mirror of
https://github.com/sern-handler/cli
synced 2026-06-13 11:22:24 +00:00
extrapolate into plugin
This commit is contained in:
@@ -46,7 +46,35 @@ type BuildOptions = {
|
||||
*/
|
||||
env?: string;
|
||||
};
|
||||
const CommandHandlerPlugin = (commandPaths: Array<{name: string}>, eventsPaths: Array<{name: string}>) => {
|
||||
return {
|
||||
name: "commandhandler",
|
||||
setup(build) {
|
||||
build.onEnd(async result => {
|
||||
if(result.errors.length) return;
|
||||
const commandsImports = commandPaths.map((fname, i) =>
|
||||
`import m${i} from "./commands/${fname.name}"`);
|
||||
const eventImports = eventsPaths.map((fname, i) =>
|
||||
`import m${i} from "./events/${fname.name}"`);
|
||||
const commandMapTemplate =
|
||||
'export const __commands = new Map();\n ' +
|
||||
commandPaths.map((_, i) => `__commands.set(m${i}.meta.id, m${i});`).join("\n");
|
||||
const eventsMapTemplate =
|
||||
'export const __events = new Map();\n ' +
|
||||
eventsPaths.map((_, i) => `__events.set(m${i}.meta.id, m${i});`).join("\n");
|
||||
|
||||
const presence = await glob("./src/presence.{ts,js}")
|
||||
const startFile =
|
||||
commandsImports.join('\n') + '\n' +
|
||||
eventImports.join('\n') + '\n' +
|
||||
(presence.length && `import p0 from './presence.js'`) + "\n"
|
||||
commandMapTemplate + "\n" +
|
||||
eventsMapTemplate + "\n";
|
||||
await writeFile("./dist/handler.js", startFile);
|
||||
})
|
||||
}
|
||||
} as esbuild.Plugin
|
||||
}
|
||||
const resolveBuildConfig = (path: string|undefined, language: string) => {
|
||||
if(language === 'javascript') {
|
||||
return path ?? p.resolve('jsconfig.json')
|
||||
@@ -121,41 +149,20 @@ export async function build(options: Record<string, any>) {
|
||||
await mkdir(genDir, { recursive: true });
|
||||
}
|
||||
|
||||
const entryPoints = await glob(`./src/**/*{${validExtensions.join(',')}}`, {
|
||||
ignore: { childrenIgnored: p => p.isNamed('commands') },
|
||||
});
|
||||
const commandsPaths = await glob(`**/*`, {
|
||||
ignore: { ignored: p => p.isDirectory() },
|
||||
cwd: "./src/commands/"
|
||||
});
|
||||
const eventsPaths = await glob(`**/*`, {
|
||||
ignore: { ignored: p => p.isDirectory() },
|
||||
cwd: "./src/events/"
|
||||
});
|
||||
const eventNames = eventsPaths.map(p.parse);
|
||||
const commandNames = commandsPaths.map(p.parse);
|
||||
const commandsImports = commandNames.map((fname, i) =>
|
||||
`import m${i} from "./${p.join(`./commands/${fname.name}.js`).split(p.sep).join(p.posix.sep)}"`);
|
||||
const eventImports = eventNames.map((fname, i) =>
|
||||
`import m${i} from "./${p.join(`./events/${fname.name}.js`).split(p.sep).join(p.posix.sep)}"`);
|
||||
const commandMapTemplate =
|
||||
'export const __commands = new Map();\n ' +
|
||||
commandNames.map((_, i) => `__commands.set(m${i}.meta.id, m${i});`).join("\n");
|
||||
const eventsMapTemplate =
|
||||
'export const __events = new Map();\n ' +
|
||||
eventNames.map((_, i) => `__events.set(m${i}.meta.id, m${i});`).join("\n");
|
||||
|
||||
const presence = await glob("./src/presence.{ts,js}")
|
||||
const startFile =
|
||||
commandsImports.join('\n') + '\n' +
|
||||
eventImports.join('\n') + '\n' +
|
||||
(presence.length ? `import p0 from './presence.js'\n` : `\n`) +
|
||||
commandMapTemplate + "\n" +
|
||||
eventsMapTemplate + "\n";
|
||||
|
||||
console.log(entryPoints)
|
||||
console.log(commandsImports)
|
||||
console.log(commandMapTemplate)
|
||||
const entryPoints = await glob(`./src/**/*{${validExtensions.join(',')}}`);
|
||||
const [commandsPaths, eventsPaths] = await Promise.all([
|
||||
glob(`**/*`, {
|
||||
withFileTypes: true,
|
||||
ignore: { ignored: p => p.isDirectory() },
|
||||
cwd: "./src/commands/"
|
||||
}),
|
||||
glob(`**/*`, {
|
||||
withFileTypes: true,
|
||||
ignore: { ignored: p => p.isDirectory() },
|
||||
cwd: "./src/events/"
|
||||
})
|
||||
])
|
||||
|
||||
const defVersion = () => JSON.stringify(packageJson().version);
|
||||
const define = {
|
||||
...(buildConfig.define ?? {}),
|
||||
@@ -169,17 +176,11 @@ export async function build(options: Record<string, any>) {
|
||||
|
||||
//https://esbuild.github.io/content-types/#tsconfig-json
|
||||
await esbuild.build({
|
||||
entryPoints: [
|
||||
...entryPoints,
|
||||
...commandsPaths.map(path => p.join("./src/commands", path)),
|
||||
...eventsPaths.map(path => p.join("./src/events", path))
|
||||
],
|
||||
plugins: buildConfig.esbuildPlugins ?? [],
|
||||
entryPoints,
|
||||
plugins: [CommandHandlerPlugin(commandsPaths, eventsPaths)],
|
||||
...defaultEsbuild(buildConfig.format!, buildConfig.tsconfig),
|
||||
define,
|
||||
dropLabels: [buildConfig.mode === 'production' ? '__DEV__' : '__PROD__', ...buildConfig.dropLabels!],
|
||||
});
|
||||
|
||||
console.log(startFile)
|
||||
await writeFile("./dist/handler.js", startFile );
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user