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