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

100% Statements 87/87
81.25% Branches 13/16
100% Functions 1/1
100% Lines 87/87

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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 881x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 71x 69x 69x 69x 69x 69x 69x 69x 69x 69x 69x 69x 69x 69x 69x 69x 69x 3x 3x 3x 3x 3x 3x 3x 66x 66x 66x 66x 66x 66x 69x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
import {library} from '@feasibleone/blong';
import {apply, plan, planTemplate} from '../../engine.ts';
import {
    baseResult,
    collectWarnings,
    contextOf,
    descriptorFor,
    diagnose,
    REALM_TEMPLATE_ROOT,
    toResultFile,
    type KukumLibContext,
    type OperationParams,
    type OperationResult,
} from '../../operation.ts';
 
/**
 * `kukum.<primitive>.add` — scaffold a primitive.
 *
 * One file serves every primitive: the file name is the part of the
 * endpoint path that differs — the predicate — and the primitive id is an
 * argument. The platform is read off `this` because that is where the runtime
 * put it, never captured in the factory.
 */
export default library(
    () =>
        async function add(
            this: KukumLibContext,
            id: string,
            params: OperationParams,
        ): Promise<OperationResult> {
            const host = this.platform;
            const descriptor = descriptorFor(id);
            const root = host.resolve(params.target ?? '.');
            const ctx = contextOf(id, params);
            const base = baseResult(id, 'add', ctx);
            const messages = [...(descriptor.check?.(ctx) ?? [])];
            if (messages.length) throw new Error(messages.join('; '));
            // A realm is the one primitive whose value is the whole canonical tree, so
            // prefer the real template over the minimal descriptor set.
            const useTemplate =
                id === 'realm' && host.existsSync(host.join(REALM_TEMPLATE_ROOT, 'package.json'));
            const changes = useTemplate
                ? await planTemplate(host, {
                      templateRoot: REALM_TEMPLATE_ROOT,
                      root,
                      subject: ctx.subject,
                      object: ctx.object,
                      instructions: params.instructions ?? [],
                  })
                : plan(host, descriptor, {
                      root,
                      context: ctx,
                      instructions: params.instructions ?? [],
                      mode: params.mode ?? 'auto',
                  });
            if (!params.dryRun) {
                const {written, skipped} = apply(host, changes, {force: params.force});
                base.written = written;
                base.skipped = skipped;
                const diagnostics = await diagnose(host, root, written);
                return {
                    ...base,
                    files: changes.map(toResultFile),
                    messages: [...messages, ...changes.flatMap(change => change.notices ?? [])],
                    diagnostics,
                    warnings: collectWarnings(id, changes, {
                        applied: true,
                        force: params.force,
                    }).concat(
                        skipped.map(
                            path =>
                                `${path} was left alone: it is hand-written (pass force to overwrite)`,
                        ),
                    ),
                };
            }
            return {
                ...base,
                files: changes.map(toResultFile),
                messages,
                warnings: collectWarnings(id, changes, {
                    applied: false,
                    force: params.force,
                }),
            };
        },
);