Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
quarto-dev
GitHub Repository: quarto-dev/quarto-cli
Path: blob/main/src/command/serve/serve.ts
3584 views
1
/*
2
* run.ts
3
*
4
* Copyright (C) 2020-2022 Posit Software, PBC
5
*/
6
7
import { processSuccessResult } from "../../core/process.ts";
8
import { ProcessResult } from "../../core/process-types.ts";
9
10
import { fileExecutionEngine } from "../../execute/engine.ts";
11
import { RunOptions } from "../../execute/types.ts";
12
13
import { render } from "../render/render-shared.ts";
14
import { renderServices } from "../render/render-services.ts";
15
import {
16
previewURL,
17
printBrowsePreviewMessage,
18
resolveHostAndPort,
19
} from "../../core/previewurl.ts";
20
import { isRStudio, isServerSession } from "../../core/platform.ts";
21
import { openUrl } from "../../core/shell.ts";
22
import { notebookContext } from "../../render/notebook/notebook-context.ts";
23
import { info } from "../../deno_ral/log.ts";
24
import { projectContext } from "../../project/project-context.ts";
25
import { singleFileProjectContext } from "../../project/types/single-file/single-file.ts";
26
27
export async function renderForServe(
28
file: string,
29
format?: string,
30
) {
31
const services = renderServices(notebookContext());
32
try {
33
const result = await render(file, {
34
services,
35
flags: {
36
to: format,
37
execute: true,
38
},
39
previewServer: true,
40
});
41
return result;
42
} finally {
43
services.cleanup();
44
}
45
}
46
47
export async function serve(options: RunOptions): Promise<ProcessResult> {
48
const { host, port } = await resolveHostAndPort(options);
49
const nbContext = notebookContext();
50
const project = (await projectContext(options.input, nbContext)) ||
51
(await singleFileProjectContext(options.input, nbContext));
52
53
const engine = await fileExecutionEngine(options.input, undefined, project);
54
if (engine?.run) {
55
// render if requested
56
if (options.render) {
57
const result = await renderForServe(options.input, options.format);
58
if (result.error) {
59
throw result.error;
60
}
61
}
62
63
// print message and open browser when ready
64
const onReady = async () => {
65
if (isRStudio()) {
66
// Preserve expected RStudio semantics for shiny doc output
67
// (VSCode extension for Quarto handles either text output)
68
// https://github.com/quarto-dev/quarto-cli/issues/8186
69
const url = previewURL(host, port, "");
70
info(`Listening on ${url}`);
71
} else {
72
printBrowsePreviewMessage(host, port, "");
73
}
74
75
if (options.browser && !isServerSession()) {
76
await openUrl(previewURL(host, port, ""));
77
}
78
};
79
80
// run using engine
81
await engine.run({
82
...options,
83
input: options.input,
84
host,
85
port,
86
onReady,
87
});
88
return processSuccessResult();
89
}
90
91
return Promise.reject(
92
new Error("Unable to run computations for input file"),
93
);
94
}
95
96