Skip to content

Commit 341fc3a

Browse files
committed
feat: cli templates api
1 parent ce2a57d commit 341fc3a

File tree

33 files changed

+484
-352
lines changed

33 files changed

+484
-352
lines changed

apps/test-bot/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@
1010
"node": "node"
1111
},
1212
"dependencies": {
13-
"commandkit": "workspace:*",
1413
"@commandkit/cache": "workspace:*",
1514
"@commandkit/devtools": "workspace:*",
1615
"@commandkit/i18n": "workspace:*",
1716
"@commandkit/legacy": "workspace:*",
17+
"commandkit": "workspace:*",
1818
"discord.js": "^14.19.1",
1919
"dotenv": "^16.4.7"
2020
},
2121
"devDependencies": {
2222
"tsx": "^4.7.0"
2323
}
24-
}
24+
}

apps/test-bot/src/app/commands/(general)/help.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { CommandData, ChatInputCommand } from 'commandkit';
2-
import { redEmbedColor } from '../../../feature-flags/red-embed-color';
3-
import { Colors, MessageFlags } from 'discord.js';
2+
import { redEmbedColor } from '@/feature-flags/red-embed-color';
3+
import { Colors } from 'discord.js';
44

55
export const command: CommandData = {
66
name: 'help',

apps/test-bot/tsconfig.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@
1414
"alwaysStrict": false,
1515
"checkJs": false,
1616
"strict": true,
17-
"strictNullChecks": true
17+
"strictNullChecks": true,
18+
"baseUrl": ".",
19+
"paths": {
20+
"@/*": ["./src/*"]
21+
}
1822
},
1923
"include": ["src", "commandkit.config.ts", "commandkit-env.d.ts"]
2024
}

apps/website/docs/api-reference/classes/CompilerPluginRuntime.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ new CompilerPluginRuntime(plugins)
3131

3232

3333
- [Source](https://github.com/underctrl-io/commandkit/blob/e7997746676c2b18be64230c918cb998973d18b5/packages/commandkit/src/plugins/plugin-runtime/CompilerPluginRuntime.ts#L148)
34-
### public toEsbuildPlugin(): \{
34+
### public toJSON(): \{
3535
name | : [string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) | ;
3636
setup | : (
3737
build: Setup

apps/website/docs/guide/01-getting-started/04-commandkit-config.mdx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,17 @@ export default defineConfig({
4141
});
4242
```
4343

44-
### ESBuild Plugins
44+
### Rolldown Plugins
4545

46-
Customize your build process with esbuild plugins:
46+
Customize your build process with rolldown plugins:
4747

4848
```ts title="commandkit.config.ts"
4949
import { defineConfig } from 'commandkit';
50-
import { someEsbuildPlugin } from 'some-esbuild-plugin';
50+
import { someRolldownPlugin } from 'some-rolldown-plugin';
5151

5252
export default defineConfig({
53-
esbuildPlugins: [
54-
someEsbuildPlugin({
53+
rolldownPlugins: [
54+
someRolldownPlugin({
5555
// Plugin specific options
5656
}),
5757
],
@@ -120,11 +120,11 @@ Here's a comprehensive configuration example:
120120
```ts title="commandkit.config.ts"
121121
import { defineConfig } from 'commandkit';
122122
import { somePlugin } from '@commandkit/some-plugin';
123-
import { someEsbuildPlugin } from 'some-esbuild-plugin';
123+
import { someRolldownPlugin } from 'some-rolldown-plugin';
124124

125125
export default defineConfig({
126126
plugins: [somePlugin()],
127-
esbuildPlugins: [someEsbuildPlugin()],
127+
rolldownPlugins: [someRolldownPlugin()],
128128
compilerOptions: {
129129
macro: {
130130
development: true,

apps/website/docs/guide/06-plugins/02-creating-a-compiler-plugin.mdx

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@ CommandKit Compiler Plugins are a powerful way to extend the functionality of yo
77

88
## Creating a Compiler Plugin
99

10-
### Using esbuild plugins
10+
### Using rolldown plugins
1111

12-
You can also use esbuild plugins as compiler plugins. This is useful if you want to use esbuild plugins to transform your source code.
12+
You can also use rolldown plugins as compiler plugins. This is useful if you want to use rolldown plugins to transform your source code.
1313

1414
```ts title="commandkit.config.ts"
1515
import { defineConfig } from 'commandkit';
16-
import { someEsbuildPlugin } from 'some-esbuild-plugin';
16+
import { someRolldownPlugin } from 'some-rolldown-plugin';
1717

1818
export default defineConfig({
19-
esbuildPlugins: [someEsbuildPlugin()],
19+
rolldownPlugins: [someRolldownPlugin()],
2020
});
2121
```
2222

@@ -76,3 +76,43 @@ export class MyPlugin extends CompilerPlugin {
7676
}
7777
}
7878
```
79+
80+
## Extending commandkit templates
81+
82+
CommandKit templates are a way to autogenerate files and other contents through `commandkit create` command. CommandKit by default comes with `command` and `event` templates. Compiler plugins can register custom templates to be used by the `commandkit create` command.
83+
84+
```ts title="my-plugin.ts"
85+
import { CompilerPlugin, CompilerPluginRuntime } from 'commandkit';
86+
87+
export class MyPlugin extends CompilerPlugin {
88+
public readonly name = 'my-plugin';
89+
90+
public async activate(ctx: CompilerPluginRuntime): Promise<void> {
91+
// Registers `commandkit create event <name> <path>` template
92+
ctx.registerTemplate('event', async (args: string[]) => {
93+
const [name, path] = args;
94+
95+
const template = `
96+
export default async function on${name[0].toUpperCase() + name.slice(1)}() {
97+
console.log('${name} event fired!');
98+
};
99+
`.trim();
100+
101+
await writeFile(join(path, 'event.ts'), template);
102+
});
103+
}
104+
105+
public async deactivate(ctx: CompilerPluginRuntime): Promise<void> {
106+
// remove the template from the registry when the plugin is deactivated
107+
ctx.unregisterTemplate('event');
108+
}
109+
}
110+
```
111+
112+
:::warning
113+
The `registerTemplate` method must be called inside the `activate` method, and the `unregisterTemplate` method must be called inside the `deactivate` method.
114+
:::
115+
116+
:::info
117+
Plugins may override the default templates by registering their own templates with the same name.
118+
:::

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,4 @@
5959
"vite@>=6.2.0 <6.2.6": ">=6.2.6"
6060
}
6161
}
62-
}
62+
}

packages/commandkit/cache.cjs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const {
2+
cacheTag,
3+
cacheLife,
4+
invalidate,
5+
revalidate,
6+
} = require('@commandkit/cache');
7+
8+
module.exports = {
9+
cacheTag,
10+
cacheLife,
11+
invalidate,
12+
revalidate,
13+
};

packages/commandkit/cache.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { cacheTag, cacheLife, invalidate, revalidate } from '@commandkit/cache';

packages/commandkit/config.cjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const { defineConfig } = require('./dist/config/config.js');
22

33
module.exports = {
4-
defineConfig,
4+
defineConfig,
55
};

0 commit comments

Comments
 (0)