Skip to content

Commit

Permalink
refactor: rewrite caching
Browse files Browse the repository at this point in the history
  • Loading branch information
twlite committed Jan 14, 2025
1 parent e6c423b commit 1ac231d
Show file tree
Hide file tree
Showing 11 changed files with 441 additions and 323 deletions.
22 changes: 20 additions & 2 deletions apps/test-bot/src/commands/misc/help.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,32 @@ export const data: CommandData = {
description: 'This is a help command.',
};

export async function run({ interaction }: SlashCommandProps) {
function $botVersion(): string {
'use macro';
const { join } = require('node:path');
const path = join(process.cwd(), 'package.json');
return require(path).version;
}

export async function run({ interaction, handler }: SlashCommandProps) {
await interaction.deferReply();

const botVersion = $botVersion();

const commands = handler.commands
.map((c, i) => {
return `${i + 1}. **\`/${c.data.name}\`**`;
})
.join('\n');

return interaction.editReply({
embeds: [
{
title: 'Help',
description: `This is a help command.`,
description: commands,
footer: {
text: `Bot Version: ${botVersion}`,
},
color: 0x7289da,
timestamp: new Date().toISOString(),
},
Expand Down
1 change: 0 additions & 1 deletion apps/test-bot/src/commands/misc/xp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
CommandData,
unstable_cacheTag as cacheTag,
} from 'commandkit';
import { setTimeout } from 'node:timers/promises';
import { database } from '../../database/store';

export const data: CommandData = {
Expand Down
5 changes: 2 additions & 3 deletions packages/commandkit/bin/build.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ import {
write,
} from './common.mjs';
import ora from 'ora';
import { esbuildPluginUseMacro } from 'use-macro';
import { cacheDirectivePlugin } from './esbuild-plugins/use-cache.mjs';
import { commandkitPlugin } from './esbuild-plugins/plugin.mjs';

export async function bootstrapProductionBuild(config) {
const {
Expand Down Expand Up @@ -48,7 +47,7 @@ export async function bootstrapProductionBuild(config) {
watch: false,
cjsInterop: true,
entry: [src, '!dist', '!.commandkit', `!${outDir}`],
esbuildPlugins: [cacheDirectivePlugin()],
esbuildPlugins: [commandkitPlugin()],
});

await injectShims(outDir, main, antiCrash, polyfillRequire);
Expand Down
8 changes: 6 additions & 2 deletions packages/commandkit/bin/development.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { parseEnv } from './parse-env.mjs';
import child_process from 'node:child_process';
import ora from 'ora';
import { injectShims } from './build.mjs';
import { cacheDirectivePlugin } from './esbuild-plugins/use-cache.mjs';
import { commandkitPlugin } from './esbuild-plugins/plugin.mjs';

const RESTARTING_MSG_PATTERN = /^Restarting '|".+'|"\n?$/;
const FAILED_RUNNING_PATTERN = /^Failed running '.+'|"\n?$/;
Expand Down Expand Up @@ -74,7 +74,11 @@ export async function bootstrapDevelopmentServer(opts) {
async onSuccess() {
return await injectShims('.commandkit', main, false, requirePolyfill);
},
esbuildPlugins: [cacheDirectivePlugin()],
esbuildPlugins: [
commandkitPlugin({
'use-macro': false,
}),
],
});

status.succeed(
Expand Down
74 changes: 74 additions & 0 deletions packages/commandkit/bin/esbuild-plugins/plugin.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { MacroTransformer } from 'use-macro';
import { cacheDirectivePlugin } from './use-cache.mjs';
import { readFile } from 'node:fs/promises';

const defaultConfig = {
'use-macro': true,
'use-cache': true,
};

/**
* @typedef {Object} CommandKitEsbuildPluginConfig
* @property {boolean} [use-macro]
* @property {boolean} [use-cache]
*/

/**
* @param {CommandKitEsbuildPluginConfig} [config]
*/
export const commandkitPlugin = (config) => {
config = Object.assign({}, defaultConfig, config);

const plugins = [
{
name: 'use-macro',
plugin: async (content, args) => {
const transformer = new MacroTransformer();
const { contents } = await transformer.transform(content, args.path);
return contents;
},
},
{
name: 'use-cache',
plugin: async (content, args) => {
const { contents } = await cacheDirectivePlugin(content, args);
return contents;
},
},
].filter((p) => {
return !!config[p.name];
});

return {
name: 'commandkit-transformer-plugin',
setup(build) {
if (!plugins.length) return;

const fileFilter = /\.(c|m)?(t|j)sx?$/;

build.onLoad({ filter: fileFilter }, async (args) => {
const source = await readFile(args.path, 'utf8');
const loader = args.path.split('.').pop();

let contents = source;

for (const _plugin of plugins) {
const { plugin, name } = _plugin;
try {
contents = await plugin(contents, args);
} catch (e) {
const err = new Error(`Plugin ${name} failed with ${e}`);
err.stack = e.stack;

throw err;
}
}

return {
contents,
loader,
};
});
},
};
};
Loading

0 comments on commit 1ac231d

Please sign in to comment.