All files resolve-plugin-files.ts

100% Statements 13/13
100% Branches 4/4
100% Functions 1/1
100% Lines 12/12

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                            5x 5x 5x 5x 5x 4x 5x 5x 1x 1x   4x 4x            
import { existsSync, readFileSync } from 'node:fs';
import { resolve, dirname } from 'node:path';
import type { OpRootNode } from '@contractkit/core';
import type { DiagnosticCollector } from '@contractkit/core';
 
/**
 * Resolves plugin file references in operation `plugins` blocks.
 *
 * For each operation that declares `plugins: { name: "path.yml" }`, reads the
 * referenced file (resolved relative to the operation's source `.ck` file) and
 * stores the content in `op.pluginFiles[name]`. Emits a warning and skips if
 * the file is not found.
 */
export function resolvePluginFiles(roots: OpRootNode[], rootDir: string, diag: DiagnosticCollector): void {
    for (const root of roots) {
        const contractDir = dirname(resolve(rootDir, root.file));
        for (const route of root.routes) {
            for (const op of route.operations) {
                if (!op.plugins) continue;
                for (const [name, value] of Object.entries(op.plugins)) {
                    const absPath = resolve(contractDir, value);
                    if (!existsSync(absPath)) {
                        diag.warn(root.file, op.loc.line, `plugins.${name}: file not found: ${value}`);
                        continue;
                    }
                    op.pluginFiles ??= {};
                    op.pluginFiles[name] = readFileSync(absPath, 'utf-8');
                }
            }
        }
    }
}