(content: string, defaultPath: string)
| 37 | * Used for uploaded submodules and folder import. |
| 38 | */ |
| 39 | export function parseModuleYaml(content: string, defaultPath: string): ModuleTemplate { |
| 40 | const parsed = yaml.load(content) as ParsedYaml | null; |
| 41 | if (!parsed || typeof parsed !== 'object') { |
| 42 | return { |
| 43 | name: defaultPath.replace(/^.*[/\\]/, '').replace(/\.(yaml|yml)$/i, '') || 'Module', |
| 44 | path: defaultPath, |
| 45 | description: 'Invalid or empty YAML', |
| 46 | goal: '', |
| 47 | defaultParams: {}, |
| 48 | workflow: [], |
| 49 | }; |
| 50 | } |
| 51 | |
| 52 | const name = |
| 53 | (typeof parsed.name === 'string' && parsed.name.trim()) || |
| 54 | defaultPath.replace(/^.*[/\\]/, '').replace(/\.(yaml|yml)$/i, '') || |
| 55 | 'Module'; |
| 56 | const goal = typeof parsed.goal === 'string' ? parsed.goal : ''; |
| 57 | const parameters = parsed.parameters && typeof parsed.parameters === 'object' |
| 58 | ? (Object.fromEntries( |
| 59 | Object.entries(parsed.parameters).map(([k, v]) => [k, v != null ? String(v) : '']) |
| 60 | ) as Record<string, string>) |
| 61 | : {}; |
| 62 | const workflow: WorkflowStep[] = Array.isArray(parsed.workflow) |
| 63 | ? parsed.workflow.map(stepToWorkflowStep).filter((s): s is WorkflowStep => s != null) |
| 64 | : []; |
| 65 | |
| 66 | return { |
| 67 | name, |
| 68 | path: defaultPath, |
| 69 | description: goal || undefined, |
| 70 | goal, |
| 71 | defaultParams: parameters, |
| 72 | workflow, |
| 73 | }; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Check if a parsed YAML looks like a main workflow (has workflow with multiple steps or is clearly a plan). |
no outgoing calls
no test coverage detected