Skip to content

Commit 1ac231d

Browse files
committed
refactor: rewrite caching
1 parent e6c423b commit 1ac231d

File tree

11 files changed

+441
-323
lines changed

11 files changed

+441
-323
lines changed

apps/test-bot/src/commands/misc/help.ts

+20-2
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,32 @@ export const data: CommandData = {
55
description: 'This is a help command.',
66
};
77

8-
export async function run({ interaction }: SlashCommandProps) {
8+
function $botVersion(): string {
9+
'use macro';
10+
const { join } = require('node:path');
11+
const path = join(process.cwd(), 'package.json');
12+
return require(path).version;
13+
}
14+
15+
export async function run({ interaction, handler }: SlashCommandProps) {
916
await interaction.deferReply();
1017

18+
const botVersion = $botVersion();
19+
20+
const commands = handler.commands
21+
.map((c, i) => {
22+
return `${i + 1}. **\`/${c.data.name}\`**`;
23+
})
24+
.join('\n');
25+
1126
return interaction.editReply({
1227
embeds: [
1328
{
1429
title: 'Help',
15-
description: `This is a help command.`,
30+
description: commands,
31+
footer: {
32+
text: `Bot Version: ${botVersion}`,
33+
},
1634
color: 0x7289da,
1735
timestamp: new Date().toISOString(),
1836
},

apps/test-bot/src/commands/misc/xp.ts

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import {
33
CommandData,
44
unstable_cacheTag as cacheTag,
55
} from 'commandkit';
6-
import { setTimeout } from 'node:timers/promises';
76
import { database } from '../../database/store';
87

98
export const data: CommandData = {

packages/commandkit/bin/build.mjs

+2-3
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ import {
1111
write,
1212
} from './common.mjs';
1313
import ora from 'ora';
14-
import { esbuildPluginUseMacro } from 'use-macro';
15-
import { cacheDirectivePlugin } from './esbuild-plugins/use-cache.mjs';
14+
import { commandkitPlugin } from './esbuild-plugins/plugin.mjs';
1615

1716
export async function bootstrapProductionBuild(config) {
1817
const {
@@ -48,7 +47,7 @@ export async function bootstrapProductionBuild(config) {
4847
watch: false,
4948
cjsInterop: true,
5049
entry: [src, '!dist', '!.commandkit', `!${outDir}`],
51-
esbuildPlugins: [cacheDirectivePlugin()],
50+
esbuildPlugins: [commandkitPlugin()],
5251
});
5352

5453
await injectShims(outDir, main, antiCrash, polyfillRequire);

packages/commandkit/bin/development.mjs

+6-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { parseEnv } from './parse-env.mjs';
1313
import child_process from 'node:child_process';
1414
import ora from 'ora';
1515
import { injectShims } from './build.mjs';
16-
import { cacheDirectivePlugin } from './esbuild-plugins/use-cache.mjs';
16+
import { commandkitPlugin } from './esbuild-plugins/plugin.mjs';
1717

1818
const RESTARTING_MSG_PATTERN = /^Restarting '|".+'|"\n?$/;
1919
const FAILED_RUNNING_PATTERN = /^Failed running '.+'|"\n?$/;
@@ -74,7 +74,11 @@ export async function bootstrapDevelopmentServer(opts) {
7474
async onSuccess() {
7575
return await injectShims('.commandkit', main, false, requirePolyfill);
7676
},
77-
esbuildPlugins: [cacheDirectivePlugin()],
77+
esbuildPlugins: [
78+
commandkitPlugin({
79+
'use-macro': false,
80+
}),
81+
],
7882
});
7983

8084
status.succeed(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import { MacroTransformer } from 'use-macro';
2+
import { cacheDirectivePlugin } from './use-cache.mjs';
3+
import { readFile } from 'node:fs/promises';
4+
5+
const defaultConfig = {
6+
'use-macro': true,
7+
'use-cache': true,
8+
};
9+
10+
/**
11+
* @typedef {Object} CommandKitEsbuildPluginConfig
12+
* @property {boolean} [use-macro]
13+
* @property {boolean} [use-cache]
14+
*/
15+
16+
/**
17+
* @param {CommandKitEsbuildPluginConfig} [config]
18+
*/
19+
export const commandkitPlugin = (config) => {
20+
config = Object.assign({}, defaultConfig, config);
21+
22+
const plugins = [
23+
{
24+
name: 'use-macro',
25+
plugin: async (content, args) => {
26+
const transformer = new MacroTransformer();
27+
const { contents } = await transformer.transform(content, args.path);
28+
return contents;
29+
},
30+
},
31+
{
32+
name: 'use-cache',
33+
plugin: async (content, args) => {
34+
const { contents } = await cacheDirectivePlugin(content, args);
35+
return contents;
36+
},
37+
},
38+
].filter((p) => {
39+
return !!config[p.name];
40+
});
41+
42+
return {
43+
name: 'commandkit-transformer-plugin',
44+
setup(build) {
45+
if (!plugins.length) return;
46+
47+
const fileFilter = /\.(c|m)?(t|j)sx?$/;
48+
49+
build.onLoad({ filter: fileFilter }, async (args) => {
50+
const source = await readFile(args.path, 'utf8');
51+
const loader = args.path.split('.').pop();
52+
53+
let contents = source;
54+
55+
for (const _plugin of plugins) {
56+
const { plugin, name } = _plugin;
57+
try {
58+
contents = await plugin(contents, args);
59+
} catch (e) {
60+
const err = new Error(`Plugin ${name} failed with ${e}`);
61+
err.stack = e.stack;
62+
63+
throw err;
64+
}
65+
}
66+
67+
return {
68+
contents,
69+
loader,
70+
};
71+
});
72+
},
73+
};
74+
};

0 commit comments

Comments
 (0)