-
-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathcompiler-plugin.ts
More file actions
60 lines (52 loc) · 1.79 KB
/
compiler-plugin.ts
File metadata and controls
60 lines (52 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import {
CompilerPlugin,
CompilerPluginRuntime,
getConfig,
MaybeFalsey,
PluginTransformParameters,
TransformedResult,
} from 'commandkit';
import { LocalBuilder } from './builder.js';
import { workflowTransformPlugin as workflowRollupPlugin } from '@workflow/rollup';
const USE_WORKFLOW_DIRECTIVE = 'use workflow';
const USE_STEP_DIRECTIVE = 'use step';
export interface WorkflowCompilerPluginOptions {}
const shouldTransform = (code: string): boolean => {
return (
code.includes(USE_WORKFLOW_DIRECTIVE) || code.includes(USE_STEP_DIRECTIVE)
);
};
export class WorkflowCompilerPlugin extends CompilerPlugin<WorkflowCompilerPluginOptions> {
public readonly name = 'WorkflowCompilerPlugin';
private builder: LocalBuilder | null = null;
private workflowRollupPlugin: ReturnType<typeof workflowRollupPlugin> | null =
null;
public async activate(ctx: CompilerPluginRuntime): Promise<void> {
this.builder = new LocalBuilder({
inputPaths: ['workflows', 'app/workflows'],
outDir: ctx.isDevMode ? '.commandkit' : getConfig().distDir,
});
this.workflowRollupPlugin = workflowRollupPlugin();
}
public async deactivate(): Promise<void> {
await this.builder?.build();
this.builder = null;
}
public async transform(
params: PluginTransformParameters,
): Promise<MaybeFalsey<TransformedResult>> {
if (!shouldTransform(params.code)) return;
if (typeof this.workflowRollupPlugin?.transform !== 'function') return;
// @ts-ignore mismatched types
const result = await this.workflowRollupPlugin.transform(
params.code,
params.id,
);
if (!result) return null;
if (typeof result === 'string') return { code: result };
return {
code: result.code,
map: typeof result.map === 'string' ? result.map : null,
};
}
}