Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
quarto-dev
GitHub Repository: quarto-dev/quarto-cli
Path: blob/main/src/project/engine-project-context.ts
6458 views
1
/*
2
* engine-project-context.ts
3
*
4
* Copyright (C) 2023 Posit Software, PBC
5
*/
6
7
import { MappedString } from "../core/mapped-text.ts";
8
import { ExecutionEngineInstance } from "../execute/types.ts";
9
import { projectOutputDir } from "./project-shared.ts";
10
import {
11
EngineProjectContext,
12
FileInformation,
13
ProjectContext,
14
} from "./types.ts";
15
16
/**
17
* Creates an EngineProjectContext adapter from a ProjectContext
18
* This provides a restricted view of ProjectContext with additional utility methods
19
*
20
* @param context The source ProjectContext
21
* @returns An EngineProjectContext adapter
22
*/
23
export function engineProjectContext(
24
context: ProjectContext,
25
): EngineProjectContext {
26
const project: EngineProjectContext = {
27
// Core properties
28
dir: context.dir,
29
isSingleFile: context.isSingleFile,
30
config: context.config
31
? {
32
engines: context.config.engines as string[] | undefined,
33
project: context.config.project
34
? {
35
"output-dir": context.config.project["output-dir"],
36
}
37
: undefined,
38
}
39
: undefined,
40
fileInformationCache: context.fileInformationCache,
41
42
// Path utilities
43
getOutputDirectory: () => {
44
return projectOutputDir(context);
45
},
46
47
// Core methods
48
resolveFullMarkdownForFile: (
49
engine: ExecutionEngineInstance | undefined,
50
file: string,
51
markdown?: MappedString,
52
force?: boolean,
53
) => context.resolveFullMarkdownForFile(engine, file, markdown, force),
54
};
55
56
return project;
57
}
58
59
/**
60
* Type guard to check if an object implements the EngineProjectContext interface
61
*
62
* @param obj Object to check
63
* @returns Whether the object is an EngineProjectContext
64
*/
65
export function isEngineProjectContext(
66
obj: unknown,
67
): obj is EngineProjectContext {
68
if (!obj) return false;
69
70
const ctx = obj as Partial<EngineProjectContext>;
71
return (
72
typeof ctx.dir === "string" &&
73
typeof ctx.isSingleFile === "boolean" &&
74
typeof ctx.resolveFullMarkdownForFile === "function"
75
);
76
}
77
78