All files / src/core/introspection manifest.ts

88.23% Statements 15/17
50% Branches 3/6
71.42% Functions 5/7
88.23% Lines 15/17

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                                                  3x 3x 3x 2x                                     1x 1x 1x       1x 1x 1x               1x 1x 1x 1x 1x    
import * as path from 'path';
import * as fs from 'fs';
import { scanApiDirectory } from '../file-system/scan-api-dir.js';
import type { ParsedRoute } from '../routing/route-parser.js';
import type { ParsedSocket } from '../routing/socket-parser.js';
 
export interface VitekManifest {
  routes: Array<{
    method: string;
    pattern: string;
    params: string[];
    file: string;
  }>;
  middlewares: Array<{
    basePattern: string;
    path: string;
  }>;
  sockets: Array<{
    pattern: string;
    params: string[];
    file: string;
  }>;
}
 
export function getManifest(root: string, apiDir: string): VitekManifest {
  const fullApiDir = path.isAbsolute(apiDir) ? apiDir : path.resolve(root, apiDir);
  const scanResult = scanApiDirectory(fullApiDir);
  return {
    routes: scanResult.routes.map((r) => ({
      method: r.method,
      pattern: r.pattern,
      params: r.params,
      file: path.relative(root, r.file).replace(/\\/g, '/'),
    })),
    middlewares: scanResult.middlewares.map((m) => ({
      basePattern: m.basePattern,
      path: path.relative(root, m.path).replace(/\\/g, '/'),
    })),
    sockets: scanResult.sockets.map((s) => ({
      pattern: s.pattern,
      params: s.params,
      file: path.relative(root, s.file).replace(/\\/g, '/'),
    })),
  };
}
 
export function getRoutes(root: string, apiDir: string): ParsedRoute[] {
  const fullApiDir = path.isAbsolute(apiDir) ? apiDir : path.resolve(root, apiDir);
  const scanResult = scanApiDirectory(fullApiDir);
  return scanResult.routes;
}
 
export function getSockets(root: string, apiDir: string): ParsedSocket[] {
  const fullApiDir = path.isAbsolute(apiDir) ? apiDir : path.resolve(root, apiDir);
  const scanResult = scanApiDirectory(fullApiDir);
  return scanResult.sockets;
}
 
export function writeManifest(
  root: string,
  apiDir: string,
  outDir: string
): string {
  const manifest = getManifest(root, apiDir);
  const manifestPath = path.join(outDir, 'vitek-manifest.json');
  fs.mkdirSync(outDir, { recursive: true });
  fs.writeFileSync(manifestPath, JSON.stringify(manifest, null, 2), 'utf-8');
  return manifestPath;
}