-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworkflows-migrator.ts
More file actions
127 lines (105 loc) · 4.13 KB
/
workflows-migrator.ts
File metadata and controls
127 lines (105 loc) · 4.13 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// kilocode_change - new file
import * as fs from "fs/promises"
import * as path from "path"
import os from "os"
import type { Config } from "../config/config"
import { Filesystem } from "../util/filesystem"
import { KilocodePaths } from "./paths"
export namespace WorkflowsMigrator {
const KILOCODE_WORKFLOWS_DIR = ".kilocode/workflows"
const GLOBAL_WORKFLOWS_DIR = path.join(os.homedir(), ".kilocode", "workflows")
export interface KilocodeWorkflow {
name: string
path: string
content: string
source: "global" | "project"
}
export interface MigrationResult {
commands: Record<string, Config.Command>
warnings: string[]
}
async function findWorkflowFiles(dir: string): Promise<string[]> {
const entries = await fs.readdir(dir, { withFileTypes: true }).catch(() => [])
return entries.filter((e) => e.isFile() && e.name.endsWith(".md")).map((e) => path.join(dir, e.name))
}
export function extractNameFromFilename(filename: string): string {
return path.basename(filename, ".md")
}
export function extractDescription(content: string): string | undefined {
const lines = content.split("\n")
let foundTitle = false
for (const line of lines) {
const trimmed = line.trim()
if (trimmed.startsWith("#")) {
foundTitle = true
continue
}
if (foundTitle && trimmed.length > 0) {
return trimmed.slice(0, 200)
}
}
return undefined
}
async function loadWorkflowsFromDir(dir: string, source: "global" | "project"): Promise<KilocodeWorkflow[]> {
if (!(await Filesystem.isDir(dir))) return []
const files = await findWorkflowFiles(dir)
const workflows: KilocodeWorkflow[] = []
for (const file of files) {
const content = await fs.readFile(file, "utf-8")
workflows.push({
name: extractNameFromFilename(file),
path: file,
content: content.trim(),
source,
})
}
return workflows
}
export async function discoverWorkflows(projectDir: string, skipGlobalPaths?: boolean): Promise<KilocodeWorkflow[]> {
const workflows: KilocodeWorkflow[] = []
if (!skipGlobalPaths) {
// 1. VSCode extension global storage (primary location for global workflows)
const vscodeWorkflowsDir = path.join(KilocodePaths.vscodeGlobalStorage(), "workflows")
workflows.push(...(await loadWorkflowsFromDir(vscodeWorkflowsDir, "global")))
// 2. Home directory ~/.kilocode/workflows (fallback/alternative location)
workflows.push(...(await loadWorkflowsFromDir(GLOBAL_WORKFLOWS_DIR, "global")))
}
// 3. Project workflows (.kilocode/workflows/)
const projectWorkflowsDir = path.join(projectDir, KILOCODE_WORKFLOWS_DIR)
workflows.push(...(await loadWorkflowsFromDir(projectWorkflowsDir, "project")))
return workflows
}
export function convertToCommand(workflow: KilocodeWorkflow): Config.Command {
return {
template: workflow.content,
description: extractDescription(workflow.content) ?? `Workflow: ${workflow.name}`,
}
}
export async function migrate(options: {
projectDir: string
/** Skip reading from global paths. Used for testing. */
skipGlobalPaths?: boolean
}): Promise<MigrationResult> {
const warnings: string[] = []
const commands: Record<string, Config.Command> = {}
const workflows = await discoverWorkflows(options.projectDir, options.skipGlobalPaths)
// Deduplicate by name (project takes precedence over global)
const workflowsByName = new Map<string, KilocodeWorkflow>()
// Add global first
for (const workflow of workflows.filter((w) => w.source === "global")) {
workflowsByName.set(workflow.name, workflow)
}
// Project overwrites global
for (const workflow of workflows.filter((w) => w.source === "project")) {
if (workflowsByName.has(workflow.name)) {
warnings.push(`Project workflow '${workflow.name}' overrides global workflow`)
}
workflowsByName.set(workflow.name, workflow)
}
// Convert to commands
for (const [name, workflow] of workflowsByName) {
commands[name] = convertToCommand(workflow)
}
return { commands, warnings }
}
}