Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
quarto-dev
GitHub Repository: quarto-dev/quarto-cli
Path: blob/main/src/command/command-utils.ts
6442 views
1
/*
2
* command-utils.ts
3
*
4
* Copyright (C) 2020-2022 Posit Software, PBC
5
*/
6
7
import { initYamlIntelligenceResourcesFromFilesystem } from "../core/schema/utils.ts";
8
import { projectContext } from "../project/project-context.ts";
9
import { notebookContext } from "../render/notebook/notebook-context.ts";
10
import { resolveEngines } from "../execute/engine.ts";
11
import type { ProjectContext } from "../project/types.ts";
12
13
/**
14
* Create a minimal "zero-file" project context for loading bundled engine extensions
15
* when no actual project or file exists.
16
*
17
* This is needed for commands like `quarto check julia` that run outside any project
18
* but still need access to bundled engines. The context provides just enough structure
19
* to discover and register bundled engine extensions.
20
*
21
* @param dir - Directory to use as the base (defaults to current working directory)
22
* @returns A minimal ProjectContext with bundled engines loaded
23
*/
24
async function zeroFileProjectContext(dir?: string): Promise<ProjectContext> {
25
const { createExtensionContext } = await import(
26
"../extension/extension.ts"
27
);
28
const { resolveEngineExtensions } = await import(
29
"../project/project-context.ts"
30
);
31
32
const extensionContext = createExtensionContext();
33
const config = await resolveEngineExtensions(
34
extensionContext,
35
{ project: {} },
36
dir || Deno.cwd(),
37
);
38
39
// Return a minimal project context with the resolved engine config
40
return {
41
dir: dir || Deno.cwd(),
42
config,
43
} as ProjectContext;
44
}
45
46
/**
47
* Initialize project context and register external engines from project config.
48
*
49
* This consolidates the common pattern of:
50
* 1. Loading YAML intelligence resources
51
* 2. Creating project context
52
* 3. Registering external engines via reorderEngines()
53
*
54
* If no project is found, a zero-file context is created to load bundled engine
55
* extensions (like Julia), ensuring they're available for commands like `quarto check julia`.
56
*
57
* @param dir - Optional directory path (defaults to current working directory)
58
*/
59
export async function initializeProjectContextAndEngines(
60
dir?: string,
61
): Promise<void> {
62
// Initialize YAML intelligence resources (required for project context)
63
await initYamlIntelligenceResourcesFromFilesystem();
64
65
// Load project context if we're in a project directory, or create a zero-file
66
// context to load bundled engines when no project exists
67
const context = await projectContext(dir || Deno.cwd(), notebookContext()) ||
68
await zeroFileProjectContext(dir);
69
70
// Register external engines from project config
71
await resolveEngines(context);
72
}
73
74