Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
quarto-dev
GitHub Repository: quarto-dev/quarto-cli
Path: blob/main/src/command/render/cleanup.ts
6452 views
1
/*
2
* renderCleanup.ts
3
*
4
* Copyright (C) 2020-2022 Posit Software, PBC
5
*/
6
7
import { existsSync, safeRemoveDirSync } from "../../deno_ral/fs.ts";
8
import { dirname, extname, isAbsolute, join } from "../../deno_ral/path.ts";
9
10
import * as ld from "../../core/lodash.ts";
11
12
import {
13
normalizePath,
14
removeIfEmptyDir,
15
removeIfExists,
16
} from "../../core/path.ts";
17
import { figuresDir, inputFilesDir } from "../../core/render.ts";
18
19
import { Format } from "../../config/types.ts";
20
import { isHtmlFileOutput, isLatexOutput } from "../../config/format.ts";
21
import { kKeepMd, kKeepTex, kKeepTyp } from "../../config/constants.ts";
22
23
import { filesDirLibDir, filesDirMediabagDir } from "./render-paths.ts";
24
import { ProjectContext } from "../../project/types.ts";
25
26
export function renderCleanup(
27
input: string,
28
output: string,
29
format: Format,
30
project: ProjectContext,
31
supporting?: string[],
32
keepMd?: string,
33
) {
34
// compute figure format
35
const figureFormat = isLatexOutput(format.pandoc)
36
? extname(output).slice(1)
37
: format.pandoc.to;
38
39
// resolve output (could be either input relative or absolute)
40
if (!isAbsolute(output)) {
41
output = join(dirname(input), output);
42
}
43
44
// cleanup md if necessary
45
if (keepMd && !format.execute[kKeepMd] && keepMd !== output) {
46
removeIfExists(keepMd);
47
}
48
49
// if we aren't keeping the markdown or text and we are instructed to
50
// clean supporting files then do it
51
if (
52
!format.execute[kKeepMd] &&
53
!format.render[kKeepTex] &&
54
!format.render[kKeepTyp] &&
55
supporting
56
) {
57
// ammend supporting with lib dir (if it exists) for html formats
58
if (isHtmlFileOutput(format.pandoc)) {
59
const libDir = join(
60
dirname(input),
61
filesDirLibDir(input),
62
);
63
if (existsSync(libDir)) {
64
supporting.push(normalizePath(libDir));
65
}
66
// narrow supporting to figures dir for non-html formats
67
} else {
68
let filesDir = join(
69
dirname(input),
70
inputFilesDir(input),
71
);
72
if (existsSync(filesDir)) {
73
filesDir = normalizePath(filesDir);
74
}
75
supporting = supporting.map((supportingDir) => {
76
if (filesDir === supportingDir) {
77
return join(filesDir, figuresDir(figureFormat));
78
} else {
79
return supportingDir;
80
}
81
});
82
}
83
84
// ammend supporting with mediabag if it exists
85
// (we can always delete the mediabag because it is generated by filters)
86
const mediabagDir = join(dirname(input), filesDirMediabagDir(input));
87
if (existsSync(mediabagDir)) {
88
supporting.push(mediabagDir);
89
}
90
91
// clean supporting
92
ld.uniq(supporting).forEach((path: string) => {
93
if (existsSync(path)) {
94
safeRemoveDirSync(path, project.dir);
95
}
96
});
97
}
98
99
// remove empty files/lib dirs
100
const filesDir = join(dirname(input), inputFilesDir(input));
101
const figsDir = join(filesDir, figuresDir(figureFormat));
102
const libDir = join(dirname(input), filesDirLibDir(input));
103
const mediabagDir = join(dirname(input), filesDirMediabagDir(input));
104
105
removeIfEmptyDir(figsDir);
106
removeIfEmptyDir(libDir);
107
removeIfEmptyDir(mediabagDir);
108
removeIfEmptyDir(filesDir);
109
}
110
111