All files / core/blong-kukum/primitives library.ts

100% Statements 250/250
97.82% Branches 45/46
100% Functions 11/11
100% Lines 250/250

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 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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 2511x 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 1x 1x 12x 12x 12x 11x 10x 10x 10x 9x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 8x 8x 8x 8x 8x 8x 8x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 14x 14x 14x 5x 4x 5x 12x 8x 8x 8x 8x 8x 8x 8x 8x 4x 4x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5x 5x 5x 5x 5x 5x 2x 3x 3x 2x 5x 5x 1x 1x 1x 1x 1x 1x 4x 4x 4x 3x 3x 3x 3x 3x 3x 3x 4x 4x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 1x 1x 1x 9x 9x 9x 9x 9x 9x 9x 6x 6x 6x 6x 7x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 1x 1x 1x  
import {
    capitalize,
    checkNames,
    type PrimitiveContext,
    type PrimitiveDescriptor,
    type PrimitiveFile,
    type PrimitiveHost,
} from '../engine.ts';
import {checkLayer} from './checks.ts';
import {groupOf, layerOf} from './shared.ts';
 
/** `library` — injected library functions the handler group shares. */
 
/**
 * Layers whose direct sub-folders are handler groups.
 *
 * A plain module in one of those folders is what the loader complains about, so
 * the audit looks exactly there. Custom layers can be named with `--layer`, but
 * the audit cannot know them, so it covers the well-known server ones.
 */
const HANDLER_LAYERS = [
    'orchestrator',
    'adapter',
    'gateway',
    'error',
    'test',
    'sim',
    'server/test',
];
 
/** The file-name contract `Watch` applies to a handler-group folder. */
const isGroupSource = (name: string): boolean =>
    /(?<!\.d)\.m?[tj]sx?$/i.test(name) &&
    !name.startsWith('.') &&
    !name.startsWith('~.') &&
    !/^layer\.(server|browser)\.[mc]?[tj]sx?$/i.test(name) &&
    !/^config\.[mc]?[tj]sx?$/i.test(name) &&
    // The group's own typed factory — a plain module the loader skips by name.
    !/^handlerGroup\.[mc]?[tj]sx?$/i.test(name) &&
    !/\.(test|play)\.[mc]?[tj]sx?$/i.test(name);
 
/** The factory a group keeps beside its handlers to type its `lib`. */
const HANDLER_GROUP_FILE = 'handlerGroup.ts';
 
/** True when a module default-exports something the loader can classify. */
const isDefaultExport = (source: string): boolean => /\bexport default\b/.test(source);
 
/** True when the default export is a `library()`. */
const isLibrary = (source: string): boolean => /\bexport default\s+library\s*\(/.test(source);
 
/** The name the library file/function takes: `--name`, else the triple object. */
const nameOf = (ctx: PrimitiveContext): string => String(ctx.params.name ?? ctx.object ?? '');
 
/** The member functions of a `bindings` bundle, from `--members=a,b`. */
function membersOf(ctx: PrimitiveContext, fallback: string): string[] {
    const raw = ctx.params.members;
    const list = (Array.isArray(raw) ? raw : String(raw ?? '').split(','))
        .map(member => member.trim())
        .filter(Boolean);
    return list.length ? list : [`${fallback}Helper`];
}
 
/** A handler-group file on disk, as the audit and `find` see it. */
interface GroupFile {
    /** The layer folder, e.g. `adapter` or `server/test`. */
    layer: string;
    /** The group folder inside the layer, e.g. `db`. */
    group: string;
    /** Root-relative path, for `find`. */
    path: string;
    source: string;
}
 
/** Every source file that sits in a handler group of the realm/suite at `root`. */
async function groupFiles(host: PrimitiveHost, root: string): Promise<GroupFile[]> {
    const found: GroupFile[] = [];
    for (const layer of HANDLER_LAYERS) {
        const layerDir = host.join(root, layer);
        if (!host.existsSync(layerDir)) continue;
        for (const entry of await host.scan(layerDir)) {
            if (!entry.isDirectory() || entry.name.startsWith('.')) continue;
            const groupDir = host.join(layerDir, entry.name);
            for (const file of await host.scan(groupDir)) {
                if (!file.isFile() || !isGroupSource(file.name)) continue;
                const absolute = host.join(groupDir, file.name);
                found.push({
                    layer,
                    group: entry.name,
                    path: `${layer}/${entry.name}/${file.name}`,
                    source: String(host.readFileSync(absolute, {encoding: 'utf-8'})),
                });
            }
        }
    }
    return found;
}
 
/**
 * `library` — one or more injected library functions in one file.
 *
 * `function` is a single helper in its own file: the shape a group needs once or
 * twice. `bindings` puts a whole bundle of helpers that share a concern in ONE
 * file — the shape to reach for as soon as there are several, since one file per
 * helper stops being practical past two or three and each file is one more
 * `lib` merge with no reader benefit.
 */
const library: PrimitiveDescriptor = {
    id: 'library',
    title: 'Injected library function',
    skill: 'blong-handler',
    summary:
        'Functions shared inside one handler group. Kinds: function (one helper in one file) ' +
        'and bindings (a bundle of helpers sharing a concern, in one file).',
    kinds: ['function', 'bindings'],
    defaultKind: 'function',
    roots: [],
    check(ctx) {
        const name = nameOf(ctx);
        const problems = [
            ...checkNames(name, 'name'),
            ...checkLayer(ctx, ['orchestrator', 'adapter', 'gateway', 'error', 'test']),
        ];
        if (ctx.kind === 'bindings') {
            for (const member of membersOf(ctx, name)) {
                problems.push(...checkNames(member, 'member'));
            }
        }
        return problems;
    },
    /**
     * `files`, plus the one thing a new bundle needs beside it: the group's typed
     * factory. It is reported, never written — the bundle set it intersects is the
     * group's own knowledge.
     */
    compose({host, root, context}) {
        const files = this.files(context);
        const factory = `${layerOf(context, 'orchestrator')}/${groupOf(context)}/${HANDLER_GROUP_FILE}`;
        if (files.length && !host.existsSync(host.join(root, factory))) {
            files[0].notices = [
                ...(files[0].notices ?? []),
                `the group has no ${factory}: add one that exports ` +
                    '`handler = handlerGroup<…>()` over its library types, and its handlers ' +
                    'destructure `lib` with them',
            ];
        }
        return files;
    },
    /**
     * The library files an existing realm already holds.
     *
     * `find` cannot use `roots` here: a library lives one level deeper, in a
     * group folder whose name the descriptor cannot guess.
     */
    async discover(host, root) {
        return (await groupFiles(host, root))
            .filter(file => isLibrary(file.source))
            .map(file => file.path);
    },
    /**
     * The guardrail this primitive exists for: a plain module in a group folder
     * makes the loader log an error-level line on every run.
     */
    async audit({host, root}) {
        return (await groupFiles(host, root))
            .filter(file => !isDefaultExport(file.source))
            .map(
                file =>
                    `${file.path} is a plain module in the handler group ` +
                    `'${file.layer}/${file.group}': the loader logs an error for it on every ` +
                    'run. Make it an injected library function instead (kukum library add ' +
                    `--kind=bindings --layer=${file.layer} --group=${file.group} --name=NAME ` +
                    '--members=a,b), or move it out of the group folder.',
            );
    },
    files(ctx): PrimitiveFile[] {
        const name = nameOf(ctx);
        const Type = capitalize(name).replace(/Lib$/, '') + 'Lib';
        const layer = layerOf(ctx, 'orchestrator');
        const group = groupOf(ctx);
        const path = `${layer}/${group}/${name}.ts`;
 
        if (ctx.kind === 'bindings') {
            const members = membersOf(ctx, name);
            const declarations = members
                .map(
                    member => `function ${member}(input: unknown): unknown {
    return input;
}`,
                )
                .join('\n\n');
            const typeMembers = members
                .map(member => `    ${member}: typeof ${member};`)
                .join('\n');
            return [
                {
                    path,
                    content: `import {library} from '@feasibleone/blong';
 
/**
 * ${name} — helpers the ${group} handler group shares.
 *
 * One file holds every helper that belongs to this concern, because a file per
 * helper stops paying off for short functions. Handlers destructure the members
 * from \`lib\`, typed by the bundle type beside the factory: add the type to the
 * group's \`handlerGroup.ts\` (in this same folder), and every handler here is
 * typed.
 *
 *     export const handler = handlerGroup<${Type}>();   // once per group
 *     handler(({lib: {${members[0]}}}) => async function …)
 */
${declarations}
 
/** The bundle as a handler destructures it from \`lib\`. */
export type ${Type} = {
${typeMembers}
};
 
export default library((): ${Type} => ({${members.join(', ')}}));
`,
                },
            ];
        }
 
        return [
            {
                path,
                content: `import {library} from '@feasibleone/blong';
 
/**
 * ${name} — describe what this helper does.
 *
 * Shared with the ${group} group only: handlers destructure it from \`lib\`.
 * With one helper the group's \`handlerGroup.ts\` can intersect this module's
 * own shape; a second helper in the same concern belongs in ONE \`bindings\`
 * bundle (kukum library add --kind=bindings --name=${name} --members=a,b) instead
 * of a second file.
 */
export default library(
    () =>
        function ${name}(input: unknown): unknown {
            return input;
        },
);
`,
            },
        ];
    },
};
 
export default library;