Skip to content

Commit 64496ec

Browse files
committed
fix: workflow plugin
1 parent 5bc3a02 commit 64496ec

File tree

11 files changed

+70
-23
lines changed

11 files changed

+70
-23
lines changed

.npmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
public-hoist-pattern[]=*commandkit*

apps/test-bot/src/workflows/greet/steps/greet.step.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { Logger } from 'commandkit';
21
import { useClient } from 'commandkit/hooks';
32

43
export async function greetUser(userId: string, again = false) {

examples/with-workflow/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"typescript": "^5.8.3"
1616
},
1717
"dependencies": {
18-
"@commandkit/workflow": "^0",
18+
"@commandkit/workflow": "latest",
1919
"commandkit": "^1.2.0-rc.12",
2020
"discord.js": "^14.24.0",
2121
"workflow": "^4.0.1-beta.12"

examples/with-workflow/src/workflows/greet/steps/greet.step.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
import { commandkit } from 'commandkit';
1+
import { useClient } from 'commandkit/hooks';
22

33
export async function greetUser(userId: string, again = false) {
44
'use step';
55

6-
const user = await commandkit.client.users.fetch(userId);
6+
const client = useClient<true>();
7+
8+
const user = await client.users.fetch(userId);
79

810
const message = again ? 'Hello again!' : 'Hello!';
911

packages/commandkit/hooks.cjs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,18 @@ const {
99
} = require('./dist/app/events/EventWorkerContext.js');
1010

1111
function useAnyEnvironment() {
12-
const commandCtx = getContext();
13-
if (commandCtx) return commandCtx;
12+
try {
13+
const commandCtx = getContext();
14+
if (commandCtx) return commandCtx;
15+
} catch {}
16+
17+
try {
18+
const eventWorkerCtx = getEventWorkerContext();
19+
if (eventWorkerCtx) return eventWorkerCtx;
20+
} catch {}
1421

15-
const eventWorkerCtx = getEventWorkerContext();
16-
if (eventWorkerCtx) return eventWorkerCtx;
22+
const maybeCommandKit = getCommandKit();
23+
if (maybeCommandKit) return { commandkit: maybeCommandKit };
1724

1825
throw new Error(
1926
'No environment found. This hook must be used within a CommandKit environment.',

packages/commandkit/src/plugins/plugin-runtime/CommandKitPluginRuntime.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,10 @@ export class CommandKitPluginRuntime {
5555
*/
5656
public async preload(plugin: RuntimePlugin) {
5757
for (const entrypoint of plugin.preload) {
58-
await import(toFileURL(`${getCurrentDirectory()}/${entrypoint}`));
58+
const path = entrypoint.startsWith('module:')
59+
? entrypoint.slice(7)
60+
: toFileURL(`${getCurrentDirectory()}/${entrypoint}`);
61+
await import(path);
5962
}
6063
}
6164

packages/workflow/package.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@
88
"files": [
99
"dist"
1010
],
11+
"exports": {
12+
".": {
13+
"types": "./dist/index.d.ts",
14+
"import": "./dist/index.js"
15+
},
16+
"./handler": {
17+
"types": "./dist/public-handler.d.ts",
18+
"import": "./dist/public-handler.js"
19+
}
20+
},
1121
"scripts": {
1222
"check-types": "tsc --noEmit",
1323
"build": "tsc"

packages/workflow/src/builder.ts

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { BaseBuilder, createBaseBuilderConfig } from '@workflow/builders';
2+
import { Logger } from 'commandkit';
23
import { mkdir, readFile, writeFile } from 'node:fs/promises';
34
import { join } from 'node:path';
5+
import { pathToFileURL } from 'node:url';
46

57
export interface LocalBuilderOptions {
68
/**
@@ -16,13 +18,13 @@ export interface LocalBuilderOptions {
1618
export class LocalBuilder extends BaseBuilder {
1719
#outDir: string;
1820
public constructor(private options: LocalBuilderOptions) {
19-
const outDir = join(options.outDir, '.compiled-workflows');
21+
const outDir = join(process.cwd(), options.outDir, '.compiled-workflows');
2022
super({
2123
...createBaseBuilderConfig({
2224
workingDir: process.cwd(),
2325
watch: false,
2426
dirs: options.inputPaths.map((path) =>
25-
join(process.cwd(), options.outDir, path),
27+
join(process.cwd(), 'src', path),
2628
),
2729
}),
2830
buildTarget: 'next', // Placeholder, not actually used
@@ -34,35 +36,58 @@ export class LocalBuilder extends BaseBuilder {
3436
const inputFiles = await this.getInputFiles();
3537
await mkdir(this.#outDir, { recursive: true });
3638

39+
const workflowPath = join(this.#outDir, 'workflows.js');
40+
const stepsPath = join(this.#outDir, 'steps.js');
41+
const webhookPath = join(this.#outDir, 'webhook.js');
42+
3743
await this.createWorkflowsBundle({
38-
outfile: join(this.#outDir, 'workflows.js'),
44+
outfile: workflowPath,
3945
bundleFinalOutput: false,
4046
format: 'esm',
4147
inputFiles,
4248
});
4349

4450
await this.createStepsBundle({
45-
outfile: join(this.#outDir, 'steps.js'),
51+
outfile: stepsPath,
4652
externalizeNonSteps: true,
4753
format: 'esm',
4854
inputFiles,
4955
});
5056

5157
await this.createWebhookBundle({
52-
outfile: join(this.#outDir, 'webhook.js'),
58+
outfile: webhookPath,
5359
bundle: false,
5460
});
5561

56-
await this.generateHandler();
62+
await this.generateHandler({
63+
workflow: workflowPath,
64+
steps: stepsPath,
65+
webhook: webhookPath,
66+
});
5767
}
5868

5969
public getHandlerPath(): string {
60-
return join(this.#outDir, 'handler.js');
70+
return join(import.meta.dirname, 'public-handler.js');
6171
}
6272

63-
private async generateHandler(): Promise<void> {
64-
const handlerPath = this.getHandlerPath();
73+
private async generateHandler({
74+
workflow,
75+
steps,
76+
webhook,
77+
}: {
78+
workflow: string;
79+
steps: string;
80+
webhook: string;
81+
}): Promise<void> {
82+
const handlerPath = join(import.meta.dirname, 'handler.js');
6583
const source = await readFile(handlerPath, 'utf-8');
66-
await writeFile(handlerPath, source);
84+
await writeFile(
85+
this.getHandlerPath(),
86+
source
87+
.replace('{{workflowPath}}', pathToFileURL(workflow).toString())
88+
.replace('{{stepsPath}}', pathToFileURL(steps).toString())
89+
.replace('{{webhookPath}}', pathToFileURL(webhook).toString()),
90+
);
91+
Logger.debug`Generated workflow handler at ${handlerPath}`;
6792
}
6893
}

packages/workflow/src/handler.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
// @ts-nocheck
33
import { Hono } from 'hono';
44
import { serve } from '@hono/node-server';
5-
import { POST as WebhookPOST } from './webhook.mjs';
6-
import { POST as StepPOST } from './steps.mjs';
7-
import { POST as FlowPOST } from './workflows.mjs';
5+
import { POST as WebhookPOST } from '{{webhookPath}}';
6+
import { POST as StepPOST } from '{{stepsPath}}';
7+
import { POST as FlowPOST } from '{{workflowPath}}';
88

99
const app = new Hono();
1010

packages/workflow/src/plugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ export class WorkflowPlugin extends RuntimePlugin<WorkflowPluginOptions> {
77

88
public constructor(options: WorkflowPluginOptions) {
99
super(options);
10-
this.preload.add('.compiled-workflows/handler.js');
10+
this.preload.add('module:@commandkit/workflow/handler');
1111
}
1212
}

0 commit comments

Comments
 (0)