All files / client/src/hooks compile-command-builder.ts

100% Statements 7/7
75% Branches 6/8
100% Functions 4/4
100% Lines 7/7

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                                            71x 14x 14x     27x 13x         57x   24x            
import type { SourceProject } from "@shared/source-project";
 
export interface SketchTabInput { name: string; path?: string; content: string }
 
export interface CompileCommand {
  code: string;
  headers: Array<{ name: string; content: string }>;
  entryFile?: string;
}
 
/** Pure command builder shared by compile and compile-and-start flows. */
export function buildCompileCommand(
  project: SourceProject,
): CompileCommand;
export function buildCompileCommand(
  code: string,
  tabs: SketchTabInput[],
): CompileCommand;
export function buildCompileCommand(
  projectOrCode: SourceProject | string,
  tabs?: SketchTabInput[],
): CompileCommand {
  if (typeof projectOrCode !== "string") {
    const { entryFile, files } = projectOrCode;
    return {
      code: files[entryFile] ?? "",
      headers: Object.entries(files)
        .filter(([path]) => path !== entryFile)
        .map(([path, content]) => ({ name: path, content })),
      entryFile,
    };
  }
 
  return {
    code: projectOrCode,
    headers: (tabs ?? []).slice(1).map(({ name, path, content }) => ({
      name: path ?? name,
      content,
    })),
  };
}