Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
public-hoist-pattern[]=*commandkit*
1 change: 0 additions & 1 deletion apps/test-bot/src/workflows/greet/steps/greet.step.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Logger } from 'commandkit';
import { useClient } from 'commandkit/hooks';

export async function greetUser(userId: string, again = false) {
Expand Down
2 changes: 1 addition & 1 deletion examples/with-workflow/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"typescript": "^5.8.3"
},
"dependencies": {
"@commandkit/workflow": "^0",
"@commandkit/workflow": "latest",
"commandkit": "^1.2.0-rc.12",
"discord.js": "^14.24.0",
"workflow": "^4.0.1-beta.12"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { commandkit } from 'commandkit';
import { useClient } from 'commandkit/hooks';

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

const user = await commandkit.client.users.fetch(userId);
const client = useClient<true>();

const user = await client.users.fetch(userId);

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

Expand Down
15 changes: 11 additions & 4 deletions packages/commandkit/hooks.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,18 @@ const {
} = require('./dist/app/events/EventWorkerContext.js');

function useAnyEnvironment() {
const commandCtx = getContext();
if (commandCtx) return commandCtx;
try {
const commandCtx = getContext();
if (commandCtx) return commandCtx;
} catch {}

try {
const eventWorkerCtx = getEventWorkerContext();
if (eventWorkerCtx) return eventWorkerCtx;
} catch {}

const eventWorkerCtx = getEventWorkerContext();
if (eventWorkerCtx) return eventWorkerCtx;
const maybeCommandKit = getCommandKit();
if (maybeCommandKit) return { commandkit: maybeCommandKit };

throw new Error(
'No environment found. This hook must be used within a CommandKit environment.',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ export class CommandKitPluginRuntime {
*/
public async preload(plugin: RuntimePlugin) {
for (const entrypoint of plugin.preload) {
await import(toFileURL(`${getCurrentDirectory()}/${entrypoint}`));
const path = entrypoint.startsWith('module:')
? entrypoint.slice(7)
: toFileURL(`${getCurrentDirectory()}/${entrypoint}`);
await import(path);
}
}

Expand Down
10 changes: 10 additions & 0 deletions packages/workflow/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@
"files": [
"dist"
],
"exports": {
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.js"
},
"./handler": {
"types": "./dist/public-handler.d.ts",
"import": "./dist/public-handler.js"
}
},
"scripts": {
"check-types": "tsc --noEmit",
"build": "tsc"
Expand Down
45 changes: 35 additions & 10 deletions packages/workflow/src/builder.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { BaseBuilder, createBaseBuilderConfig } from '@workflow/builders';
import { Logger } from 'commandkit';
import { mkdir, readFile, writeFile } from 'node:fs/promises';
import { join } from 'node:path';
import { pathToFileURL } from 'node:url';

export interface LocalBuilderOptions {
/**
Expand All @@ -16,13 +18,13 @@ export interface LocalBuilderOptions {
export class LocalBuilder extends BaseBuilder {
#outDir: string;
public constructor(private options: LocalBuilderOptions) {
const outDir = join(options.outDir, '.compiled-workflows');
const outDir = join(process.cwd(), options.outDir, '.compiled-workflows');
super({
...createBaseBuilderConfig({
workingDir: process.cwd(),
watch: false,
dirs: options.inputPaths.map((path) =>
join(process.cwd(), options.outDir, path),
join(process.cwd(), 'src', path),
),
}),
buildTarget: 'next', // Placeholder, not actually used
Expand All @@ -34,35 +36,58 @@ export class LocalBuilder extends BaseBuilder {
const inputFiles = await this.getInputFiles();
await mkdir(this.#outDir, { recursive: true });

const workflowPath = join(this.#outDir, 'workflows.js');
const stepsPath = join(this.#outDir, 'steps.js');
const webhookPath = join(this.#outDir, 'webhook.js');

await this.createWorkflowsBundle({
outfile: join(this.#outDir, 'workflows.js'),
outfile: workflowPath,
bundleFinalOutput: false,
format: 'esm',
inputFiles,
});

await this.createStepsBundle({
outfile: join(this.#outDir, 'steps.js'),
outfile: stepsPath,
externalizeNonSteps: true,
format: 'esm',
inputFiles,
});

await this.createWebhookBundle({
outfile: join(this.#outDir, 'webhook.js'),
outfile: webhookPath,
bundle: false,
});

await this.generateHandler();
await this.generateHandler({
workflow: workflowPath,
steps: stepsPath,
webhook: webhookPath,
});
}

public getHandlerPath(): string {
return join(this.#outDir, 'handler.js');
return join(import.meta.dirname, 'public-handler.js');
}

private async generateHandler(): Promise<void> {
const handlerPath = this.getHandlerPath();
private async generateHandler({
workflow,
steps,
webhook,
}: {
workflow: string;
steps: string;
webhook: string;
}): Promise<void> {
const handlerPath = join(import.meta.dirname, 'handler.js');
const source = await readFile(handlerPath, 'utf-8');
await writeFile(handlerPath, source);
await writeFile(
this.getHandlerPath(),
source
.replace('{{workflowPath}}', pathToFileURL(workflow).toString())
.replace('{{stepsPath}}', pathToFileURL(steps).toString())
.replace('{{webhookPath}}', pathToFileURL(webhook).toString()),
);
Logger.debug`Generated workflow handler at ${handlerPath}`;
}
}
6 changes: 3 additions & 3 deletions packages/workflow/src/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
// @ts-nocheck
import { Hono } from 'hono';
import { serve } from '@hono/node-server';
import { POST as WebhookPOST } from './webhook.mjs';
import { POST as StepPOST } from './steps.mjs';
import { POST as FlowPOST } from './workflows.mjs';
import { POST as WebhookPOST } from '{{webhookPath}}';
import { POST as StepPOST } from '{{stepsPath}}';
import { POST as FlowPOST } from '{{workflowPath}}';

const app = new Hono();

Expand Down
2 changes: 1 addition & 1 deletion packages/workflow/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ export class WorkflowPlugin extends RuntimePlugin<WorkflowPluginOptions> {

public constructor(options: WorkflowPluginOptions) {
super(options);
this.preload.add('.compiled-workflows/handler.js');
this.preload.add('module:@commandkit/workflow/handler');
}
}
Empty file.