All files / core/blong-kukum/orchestrator/kukum get.ts

67.16% Statements 45/67
60% Branches 3/5
100% Functions 1/1
67.16% Lines 45/67

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 681x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                                             1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
import {library} from '@feasibleone/blong';
import {planTemplate, withMarker} from '../../engine.ts';
import {
    baseResult,
    contextOf,
    descriptorFor,
    REALM_TEMPLATE_ROOT,
    type KukumLibContext,
    type OperationParams,
    type OperationResult,
} from '../../operation.ts';
 
/** `kukum.<primitive>.get` — the file set a primitive *would* produce, without writing. */
export default library(
    () =>
        async function get(
            this: KukumLibContext,
            id: string,
            params: OperationParams,
        ): Promise<OperationResult> {
            const host = this.platform;
            const root = host.resolve(params.target ?? '.');
            const descriptor = descriptorFor(id);
            const ctx = contextOf(id, params);
            const base = baseResult(id, 'get', ctx);
            // Mirrors `add`: for a realm the real answer is the template's file set, not
            // the minimal fallback descriptor.
            if (id === 'realm' && host.existsSync(host.join(REALM_TEMPLATE_ROOT, 'package.json'))) {
                const template = await planTemplate(host, {
                    templateRoot: REALM_TEMPLATE_ROOT,
                    root,
                    subject: ctx.subject,
                    object: ctx.object,
                    instructions: params.instructions ?? [],
                });
                return {
                    ...base,
                    files: template.map(change => ({
                        path: change.path,
                        content: '',
                        action: 'template',
                        handWritten: false,
                    })),
                    messages: [
                        `${descriptor.title} — scaffolded from the blong-kopi template`,
                        `Skill: ${descriptor.skill}`,
                        descriptor.summary,
                    ],
                };
            }
            const files = descriptor.files(ctx).map(file => ({
                path: file.path,
                content: withMarker(file.content),
                action: 'template',
                handWritten: false,
            }));
            return {
                ...base,
                files,
                messages: [
                    `${descriptor.title} (kinds: ${descriptor.kinds.join(', ')})`,
                    `Skill: ${descriptor.skill}`,
                    descriptor.summary,
                ],
            };
        },
);