Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
quarto-dev
GitHub Repository: quarto-dev/quarto-cli
Path: blob/main/src/command/serve/cmd.ts
3584 views
1
/*
2
* cmd.ts
3
*
4
* Copyright (C) 2020-2022 Posit Software, PBC
5
*/
6
7
import { Command } from "cliffy/command/mod.ts";
8
9
import * as colors from "fmt/colors";
10
import { error } from "../../deno_ral/log.ts";
11
import { initYamlIntelligenceResourcesFromFilesystem } from "../../core/schema/utils.ts";
12
import { projectContext } from "../../project/project-context.ts";
13
14
import { serve } from "./serve.ts";
15
import { resolveHostAndPort } from "../../core/previewurl.ts";
16
import { renderFormats } from "../render/render-contexts.ts";
17
import { previewFormat } from "../preview/preview.ts";
18
import { withRenderServices } from "../render/render-services.ts";
19
import { notebookContext } from "../../render/notebook/notebook-context.ts";
20
import { RenderServices } from "../render/types.ts";
21
import { singleFileProjectContext } from "../../project/types/single-file/single-file.ts";
22
import { exitWithCleanup } from "../../core/cleanup.ts";
23
24
export const serveCommand = new Command()
25
.name("serve")
26
.arguments("[input:string]")
27
.option(
28
"--no-render",
29
"Do not render the document before serving.",
30
)
31
.option(
32
"-p, --port [port:number]",
33
"The TCP port that the application should listen on.",
34
)
35
.option(
36
"--host [host:string]",
37
"Hostname to bind to (defaults to 127.0.0.1)",
38
)
39
.option(
40
"--browser",
41
"Open a browser to preview the site.",
42
)
43
.description(
44
"Serve a Shiny interactive document.\n\nBy default, the document will be rendered first and then served. " +
45
"If you have previously rendered the document, pass --no-render to skip the rendering step.",
46
)
47
.example(
48
"Serve an interactive Shiny document",
49
"quarto serve dashboard.qmd",
50
)
51
.example(
52
"Serve a document without rendering",
53
"quarto serve dashboard.qmd --no-render",
54
)
55
// deno-lint-ignore no-explicit-any
56
.action(async (options: any, input?: string) => {
57
await initYamlIntelligenceResourcesFromFilesystem();
58
if (!input) {
59
error(
60
"No input passed to serve.\n" +
61
"If you are attempting to preview a website or book use the " +
62
colors.bold("quarto preview") + " command instead.",
63
);
64
exitWithCleanup(1);
65
throw new Error(); // we never reach this point but the Deno analyzer doesn't see it.
66
}
67
68
const { host, port } = await resolveHostAndPort(options);
69
70
const nbContext = notebookContext();
71
const context = (await projectContext(input, nbContext)) ||
72
(await singleFileProjectContext(input, nbContext));
73
const formats = await withRenderServices(
74
nbContext,
75
(services: RenderServices) =>
76
renderFormats(input, services, undefined, context),
77
);
78
const format = await previewFormat(input, undefined, formats, context);
79
80
const result = await serve({
81
input,
82
render: options.render,
83
format,
84
port,
85
host,
86
browser: options.browser,
87
projectDir: context?.dir,
88
tempDir: Deno.makeTempDirSync(),
89
});
90
91
if (!result.success) {
92
// error diagnostics already written to stderr
93
exitWithCleanup(result.code);
94
}
95
});
96
97