Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
quarto-dev
GitHub Repository: quarto-dev/quarto-cli
Path: blob/main/src/command/render/pandoc-dependencies-resources.ts
3583 views
1
/*
2
* pandoc-dependencies-resources.ts
3
*
4
* Copyright (C) 2020-2022 Posit Software, PBC
5
*/
6
7
import {
8
kFormatResources,
9
kResources,
10
kSupporting,
11
} from "../../config/constants.ts";
12
import { copyTo } from "../../core/copy.ts";
13
import { lines } from "../../core/text.ts";
14
15
import { basename, isAbsolute, join, relative } from "../../deno_ral/path.ts";
16
import {
17
appendDependencies,
18
FormatResourceDependency,
19
} from "./pandoc-dependencies.ts";
20
import { existsSync } from "../../deno_ral/fs.ts";
21
22
export interface Resource {
23
file: string;
24
}
25
26
// Populates the dependency file with format resources
27
// from typescript.
28
export async function writeFormatResources(
29
inputDir: string,
30
dependenciesFile: string,
31
formatResources: string | string[] | undefined,
32
) {
33
if (formatResources) {
34
const files = Array.isArray(formatResources)
35
? formatResources
36
: [formatResources];
37
38
const dependencies: FormatResourceDependency[] = files.map((file) => {
39
const absPath = join(inputDir, file);
40
if (!existsSync(absPath)) {
41
throw new Error(
42
`The referenced format resource '${file}' does not exist.`,
43
);
44
}
45
return {
46
type: kFormatResources,
47
content: { file: absPath },
48
};
49
});
50
await appendDependencies(dependenciesFile, dependencies);
51
}
52
}
53
54
// Processes the dependency file to copy
55
export async function processFormatResources(
56
inputDir: string,
57
dependenciesFile: string,
58
) {
59
// Read the dependency file
60
const resources: string[] = [];
61
const supporting: string[] = [];
62
const dependencyJsonStream = await Deno.readTextFile(dependenciesFile);
63
for (const jsonBlob of lines(dependencyJsonStream)) {
64
if (jsonBlob) {
65
// Read the dependency and process format resources
66
const dependency = JSON.parse(jsonBlob);
67
if (dependency.type === kFormatResources) {
68
// Copy the file to the input directory
69
const formatResource = dependency.content as Resource;
70
const targetPath = join(inputDir, basename(formatResource.file));
71
copyTo(
72
formatResource.file,
73
targetPath,
74
{
75
overwrite: true,
76
preserveTimestamps: true,
77
},
78
);
79
} else if (dependency.type === kResources) {
80
const resource = dependency.content as Resource;
81
resources.push(
82
isAbsolute(resource.file)
83
? relative(inputDir, resource.file)
84
: resource.file,
85
);
86
} else if (dependency.type === kSupporting) {
87
const supportingResource = dependency.content as Resource;
88
supporting.push(supportingResource.file);
89
}
90
}
91
}
92
return { supporting, resources };
93
}
94
95