-
Notifications
You must be signed in to change notification settings - Fork 347
/
Copy pathcmd.ts
432 lines (413 loc) · 14.6 KB
/
cmd.ts
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
/*
* cmd.ts
*
* Copyright (C) 2020-2022 Posit Software, PBC
*/
import { existsSync } from "fs/mod.ts";
import { dirname, extname, join, relative } from "path/mod.ts";
import * as colors from "fmt/colors.ts";
import { Command } from "cliffy/command/mod.ts";
import { kLocalhost } from "../../core/port-consts.ts";
import { waitForPort } from "../../core/port.ts";
import { fixupPandocArgs, parseRenderFlags } from "../render/flags.ts";
import {
handleRenderResult,
preview,
previewFormat,
setPreviewFormat,
} from "./preview.ts";
import {
kRenderDefault,
kRenderNone,
serveProject,
} from "../../project/serve/serve.ts";
import {
initState,
setInitializer,
} from "../../core/lib/yaml-validation/state.ts";
import { initYamlIntelligenceResourcesFromFilesystem } from "../../core/schema/utils.ts";
import { kProjectWatchInputs, ProjectContext } from "../../project/types.ts";
import { projectContext } from "../../project/project-context.ts";
import {
projectIsServeable,
projectPreviewServe,
} from "../../project/project-shared.ts";
import { isHtmlOutput } from "../../config/format.ts";
import { renderProject } from "../render/project.ts";
import { renderServices } from "../render/render-services.ts";
import { parseFormatString } from "../../core/pandoc/pandoc-formats.ts";
import { normalizePath } from "../../core/path.ts";
import { kCliffyImplicitCwd } from "../../config/constants.ts";
import { warning } from "log/mod.ts";
import { renderFormats } from "../render/render-contexts.ts";
import { Format } from "../../config/types.ts";
import { isServerShiny, isServerShinyPython } from "../../core/render.ts";
import { previewShiny } from "./preview-shiny.ts";
import { serve } from "../serve/serve.ts";
import { fileExecutionEngine } from "../../execute/engine.ts";
import { notebookContext } from "../../render/notebook/notebook-context.ts";
import { singleFileProjectContext } from "../../project/types/single-file/single-file.ts";
export const previewCommand = new Command()
.name("preview")
.stopEarly()
.option(
"--port [port:number]",
"Suggested port to listen on (defaults to random value between 3000 and 8000).\n" +
"If the port is not available then a random port between 3000 and 8000 will be selected.",
)
.option(
"--host [host:string]",
"Hostname to bind to (defaults to 127.0.0.1)",
)
.option(
"--render [format:string]",
"Render to the specified format(s) before previewing",
{
default: kRenderNone,
},
)
.option(
"--no-serve",
"Don't run a local preview web server (just monitor and re-render input files)",
)
.option(
"--no-navigate",
"Don't navigate the browser automatically when outputs are updated.",
)
.option(
"--no-browser",
"Don't open a browser to preview the site.",
)
.option(
"--no-watch-inputs",
"Do not re-render input files when they change.",
)
.option(
"--timeout",
"Time (in seconds) after which to exit if there are no active clients.",
)
.arguments("[file:string] [...args:string]")
.description(
"Render and preview a document or website project.\n\nAutomatically reloads the browser when " +
"input files or document resources (e.g. CSS) change.\n\n" +
"For website preview, the most recent execution results of computational documents are used to render\n" +
"the site (this is to optimize startup time). If you want to perform a full render prior to\n" +
'previewing pass the --render option with "all" or a comma-separated list of formats to render.\n\n' +
"For document preview, input file changes will result in a re-render (pass --no-watch to prevent).\n\n" +
"You can also include arbitrary command line arguments to be forwarded to " +
colors.bold("quarto render") + ".",
)
.example(
"Preview document",
"quarto preview doc.qmd",
)
.example(
"Preview document with render command line args",
"quarto preview doc.qmd --toc",
)
.example(
"Preview document (don't watch for input changes)",
"quarto preview doc.qmd --no-watch-inputs",
)
.example(
"Preview website with most recent execution results",
"quarto preview",
)
.example(
"Previewing website using a specific port",
"quarto preview --port 4444",
)
.example(
"Preview website (don't open a browser)",
"quarto preview --no-browser",
)
.example(
"Fully render all website/book formats then preview",
"quarto preview --render all",
)
.example(
"Fully render the html format then preview",
"quarto preview --render html",
)
// deno-lint-ignore no-explicit-any
.action(async (options: any, file?: string, ...args: string[]) => {
// one-time initialization of yaml validation modules
setInitializer(initYamlIntelligenceResourcesFromFilesystem);
await initState();
// if input is missing but there exists an args parameter which is a .qmd or .ipynb file,
// issue a warning.
if (!file || file === kCliffyImplicitCwd) {
file = Deno.cwd();
const firstArg = args.find((arg) =>
arg.endsWith(".qmd") || arg.endsWith(".ipynb")
);
if (firstArg) {
warning(
"`quarto preview` invoked with no input file specified (the parameter order matters).\nQuarto will preview the current directory by default.\n" +
`Did you mean to run \`quarto preview ${firstArg} ${
args.filter((arg) => arg !== firstArg).join(" ")
}\`?\n` +
"Use `quarto preview --help` for more information.",
);
}
}
file = file || Deno.cwd();
if (!existsSync(file)) {
throw new Error(`${file} not found`);
}
// show help if requested
if (args.length > 0 && args[0] === "--help") {
previewCommand.showHelp();
return;
}
// pull out our command line args
const portPos = args.indexOf("--port");
if (portPos !== -1) {
options.port = parseInt(args[portPos + 1]);
args.splice(portPos, 2);
}
const hostPos = args.indexOf("--host");
if (hostPos !== -1) {
options.host = String(args[hostPos + 1]);
args.splice(hostPos, 2);
}
const renderPos = args.indexOf("--render");
if (renderPos !== -1) {
options.render = String(args[renderPos + 1]);
args.splice(renderPos, 2);
}
const presentationPos = args.indexOf("--presentation");
if (presentationPos !== -1) {
options.presentation = true;
args.splice(presentationPos, 1);
} else {
options.presentation = false;
}
const browserPathPos = args.indexOf("--browser-path");
if (browserPathPos !== -1) {
options.browserPath = String(args[browserPathPos + 1]);
args.splice(browserPathPos, 2);
}
const noServePos = args.indexOf("--no-serve");
if (noServePos !== -1) {
options.noServe = true;
args.splice(noServePos, 1);
}
const noBrowsePos = args.indexOf("--no-browse");
if (noBrowsePos !== -1) {
options.browse = false;
args.splice(noBrowsePos, 1);
}
const noBrowserPos = args.indexOf("--no-browser");
if (noBrowserPos !== -1) {
options.browser = false;
args.splice(noBrowserPos, 1);
}
const navigatePos = args.indexOf("--navigate");
if (navigatePos !== -1) {
options.navigate = true;
args.splice(navigatePos, 1);
}
const noNavigatePos = args.indexOf("--no-navigate");
if (noNavigatePos !== -1) {
options.navigate = false;
args.splice(noNavigatePos, 1);
}
const watchInputsPos = args.indexOf("--watch-inputs");
if (watchInputsPos !== -1) {
options.watchInputs = true;
args.splice(watchInputsPos, 1);
}
const noWatchInputsPos = args.indexOf("--no-watch-inputs");
if (noWatchInputsPos !== -1) {
options.watchInputs = false;
args.splice(noWatchInputsPos, 1);
}
const timeoutPos = args.indexOf("--timeout");
if (timeoutPos !== -1) {
options.timeout = parseInt(args[timeoutPos + 1]);
args.splice(timeoutPos, 2);
}
// alias for --no-watch-inputs (used by older versions of quarto r package)
const noWatchPos = args.indexOf("--no-watch");
if (noWatchPos !== -1) {
options.watchInputs = false;
args.splice(noWatchPos, 1);
}
// alias for --no-watch-inputs (used by older versions of rstudio)
const noRenderPos = args.indexOf("--no-render");
if (noRenderPos !== -1) {
options.watchInputs = false;
args.splice(noRenderPos, 1);
}
if (options.port) {
// try to bind to requested port (error if its in use)
const port = parseInt(options.port);
if (await waitForPort({ port, hostname: kLocalhost })) {
options.port = port;
} else {
throw new Error(`Requested port ${options.port} is already in use.`);
}
}
// extract pandoc flag values we know/care about, then fixup args as
// necessary (remove our flags that pandoc doesn't know about)
const flags = await parseRenderFlags(args);
args = fixupPandocArgs(args, flags);
// if this is a single-file preview within a 'serveable' project
// without a specific render directive then render the file
// and convert the render to a project one
let touchPath: string | undefined;
let projectTarget: string | ProjectContext = file;
if (Deno.statSync(file).isFile) {
// get project and preview format
const nbContext = notebookContext();
const project = (await projectContext(dirname(file), nbContext)) ||
singleFileProjectContext(file, nbContext);
const formats = await (async () => {
const services = renderServices(nbContext);
try {
return await renderFormats(
file!,
services,
undefined,
project,
);
} finally {
services.cleanup();
}
})();
const format = await previewFormat(file, flags.to, formats, project);
// see if this is server: shiny document and if it is then forward to previewShiny
if (isHtmlOutput(parseFormatString(format).baseFormat)) {
const renderFormat = formats[format] as Format | undefined;
if (renderFormat && isServerShiny(renderFormat)) {
const engine = await fileExecutionEngine(file, flags, project);
setPreviewFormat(format, flags, args);
if (isServerShinyPython(renderFormat, engine?.name)) {
const result = await previewShiny({
input: file,
render: !!options.render,
port: typeof (options.port) === "string"
? parseInt(options.port)
: options.port,
host: options.host,
browser: options.browser,
projectDir: project?.dir,
tempDir: Deno.makeTempDirSync(),
format,
pandocArgs: args,
watchInputs: options.watchInputs!,
});
Deno.exit(result.code);
} else {
const result = await serve({
input: file,
render: !!options.render,
port: typeof (options.port) === "string"
? parseInt(options.port)
: options.port,
host: options.host,
format: format,
browser: options.browser,
projectDir: project?.dir,
tempDir: Deno.makeTempDirSync(),
});
Deno.exit(result.code);
}
}
}
if (project && projectIsServeable(project)) {
// special case: plain markdown file w/ an external previewer that is NOT
// in the project input list -- in this case allow things to proceed
// without a render
const filePath = normalizePath(file);
if (!project.files.input.includes(filePath)) {
if (extname(file) === ".md" && projectPreviewServe(project)) {
setPreviewFormat(format, flags, args);
touchPath = filePath;
options.browserPath = "";
file = project.dir;
projectTarget = project;
}
} else {
if (
isHtmlOutput(parseFormatString(format).baseFormat, true) ||
projectPreviewServe(project)
) {
setPreviewFormat(format, flags, args);
const services = renderServices(notebookContext());
try {
const renderResult = await renderProject(project, {
services,
progress: false,
useFreezer: false,
flags,
pandocArgs: args,
previewServer: true,
}, [file]);
if (renderResult.error) {
throw renderResult.error;
}
handleRenderResult(file, renderResult);
if (projectPreviewServe(project) && renderResult.baseDir) {
touchPath = join(
renderResult.baseDir,
renderResult.files[0].file,
);
}
} finally {
services.cleanup();
}
// re-write various targets to redirect to project preview
if (projectPreviewServe(project)) {
options.browserPath = "";
} else {
options.browserPath = relative(project.dir, file);
}
file = project.dir;
projectTarget = project;
}
}
}
}
// see if we are serving a project or a file
if (Deno.statSync(file).isDirectory) {
// project preview
const renderOptions = {
services: renderServices(notebookContext()),
flags,
};
await serveProject(projectTarget, renderOptions, args, {
port: options.port,
host: options.host,
browser: (options.browser === false || options.browse === false)
? false
: undefined,
[kProjectWatchInputs]: options.watchInputs,
timeout: options.timeout,
render: options.render,
touchPath,
browserPath: options.browserPath,
navigate: options.navigate,
}, options.noServe === true);
} else {
// single file preview
if (
options.render !== kRenderNone &&
options.render !== kRenderDefault &&
args.indexOf("--to") === -1
) {
args.push("--to", options.render);
}
await preview(relative(Deno.cwd(), file), flags, args, {
port: options.port,
host: options.host,
browser: (options.browser === false || options.browse === false)
? false
: undefined,
[kProjectWatchInputs]: options.watchInputs,
timeout: options.timeout,
presentation: options.presentation,
});
}
});